View previous topic :: View next topic |
Author |
Message |
atmega64
Joined: 23 Feb 2005 Posts: 298 Location: ITALY
|
Posted: Thu Nov 26, 2020 4:34 pm Post subject: Timer does not start immediately |
|
|
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 |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Thu Nov 26, 2020 4:51 pm Post subject: |
|
|
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 |
|
|
atmega64
Joined: 23 Feb 2005 Posts: 298 Location: ITALY
|
Posted: Thu Nov 26, 2020 5:04 pm Post subject: |
|
|
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 |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Nov 26, 2020 7:30 pm Post subject: |
|
|
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
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 |
|
|
atmega64
Joined: 23 Feb 2005 Posts: 298 Location: ITALY
|
Posted: Fri Nov 27, 2020 10:40 am Post subject: |
|
|
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 |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sat Nov 28, 2020 7:41 am Post subject: |
|
|
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 |
|
Back to top |
|
|
atmega64
Joined: 23 Feb 2005 Posts: 298 Location: ITALY
|
Posted: Mon Nov 30, 2020 8:10 am Post subject: |
|
|
Yes, OK...
but with this system, now the timer response is acceptable ...
could it still be improved? |
|
Back to top |
|
|
|