Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Timer does not start immediately

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
atmega64

Bascom Member



Joined: 23 Feb 2005
Posts: 298
Location: ITALY

italy.gif
PostPosted: Thu Nov 26, 2020 4:34 pm    Post subject: Timer does not start immediately Reply with quote

Hi,
with the following code,

when I switch from a low frequency to a high frequency (for example Cpwmfreq = 50)

the timer does not start immediately but after a few seconds ... why?

Code:
$regfile = "m1284def.dat"
$crystal = 16000000
$hwstack = 48
$swstack = 40
$framesize = 48
$baud = 19200

Dim Cpwmfreq , Dutycicle As Word
Dim Cpwmfreqsng As Single

Config Single = Scientific , Digits = 1

'......

   Cpwmfreqsng = Cpwmfreq / 10

   Cpwmfreqsng = Cpwmfreqsng - 0.5

   Cpwmfreqsng = Cpwmfreqsng * 3120

   Cpwmfreq = Round(cpwmfreqsng)

   Cpwmfreq = 17160 - Cpwmfreq

   Enable Timer1

   Tccr1a = &B10000010
   Tccr1b = &B00011101
   Icr1 = Cpwmfreq                                       '3120

   Cpwmfreq = Cpwmfreq / 100
   Dutycicle = Cpwmfreq * Dutycicle

   Pwm1a = Dutycicle

'.........

 


(BASCOM-AVR version : 2.0.8.3 )
Back to top
View user's profile Visit poster's website
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Thu Nov 26, 2020 4:51 pm    Post subject: Reply with quote

Code:
Cpwmfreqsng = Cpwmfreq / 10

Cpwmfreq is in this stage 0.
Dividing zero by 10 will give nothing.

Code:
Cpwmfreqsng = Cpwmfreqsng * 3120

Here is Cpwmfreqsng -1560.

Code:
Cpwmfreq = Round(cpwmfreqsng)

Rounding makes the -1560 , 63976 positive.

Why are you starting with all variable empty?

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
atmega64

Bascom Member



Joined: 23 Feb 2005
Posts: 298
Location: ITALY

italy.gif
PostPosted: Thu Nov 26, 2020 5:04 pm    Post subject: Reply with quote

sorry but the input code is missing ...

obviously Cpwmfreq is never zero and never negative

the value of Cpwmfreq ranges from 5 to 50
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Thu Nov 26, 2020 7:30 pm    Post subject: Reply with quote

If your timer is for example at 4000 and you set 30 then compare match dont fire till Timer reach its end which is 65535 >4s @1024 prescale.
So maybe try Timer1 = 0 so after 30 compare will arrive quicly Wink

If you "Enable Timer1" then you enable interrupt from it. So you can always update registers in the interrupt routine cause Timer have always 0 then.
Back to top
View user's profile Visit poster's website
atmega64

Bascom Member



Joined: 23 Feb 2005
Posts: 298
Location: ITALY

italy.gif
PostPosted: Fri Nov 27, 2020 10:40 am    Post subject: Reply with quote

hi,

well, I added
Code:
load timer1, Cpwmfreq


seems to work well now ...

what do you think about it?

updated code:
Code:
$regfile = "m1284def.dat"
$crystal = 16000000
$hwstack = 48
$swstack = 40
$framesize = 48
$baud = 19200

Dim Cpwmfreq , Dutycicle As Word
Dim Cpwmfreqsng As Single

Config Single = Scientific , Digits = 1

'......

   Cpwmfreqsng = Cpwmfreq / 10

   Cpwmfreqsng = Cpwmfreqsng - 0.5

   Cpwmfreqsng = Cpwmfreqsng * 3120

   Cpwmfreq = Round(cpwmfreqsng)

   Cpwmfreq = 17160 - Cpwmfreq

   Enable Timer1

   load timer1, Cpwmfreq   '<<<<<<

   Tccr1a = &B10000010
   Tccr1b = &B00011101
   Icr1 = Cpwmfreq                                       '3120

   Cpwmfreq = Cpwmfreq / 100
   Dutycicle = Cpwmfreq * Dutycicle

   Pwm1a = Dutycicle

'.........
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sat Nov 28, 2020 7:41 am    Post subject: Reply with quote

Load Timer means 65535 - Cpwmfreq
So first Compare will fire Cpwmfreq * 2 I think because first Timer must count ex. 30 into 0 and then next 30 to Compare. Think twice Very Happy
Back to top
View user's profile Visit poster's website
atmega64

Bascom Member



Joined: 23 Feb 2005
Posts: 298
Location: ITALY

italy.gif
PostPosted: Mon Nov 30, 2020 8:10 am    Post subject: Reply with quote

Yes, OK...

but with this system, now the timer response is acceptable ...

could it still be improved?
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum