View previous topic :: View next topic |
Author |
Message |
CBailey
Joined: 07 May 2015 Posts: 102
|
Posted: Sun Apr 23, 2023 6:43 pm Post subject: Timer1 .5Hz-1.5Hz without Compare registers |
|
|
I'm using Compare1a and Compare1B. I'm trying to get Timer1 to have a .5Hz-1.5Hz output, but I haven't figured out how to adjust it. Is it possible? Thanks!
Code: |
StartTimer:
' Here we configure the timers
Compare1A = 300
Compare1B = 1200
Config Timer1 = Timer , Prescale = 256 , Clear_timer = 0
On Compare1a StartPulse
On Compare1b EndPulse
Enable Compare1a
Enable Compare1b
Enable Interrupts
Start Timer1
|
(BASCOM-AVR version : 2.0.8.5 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Apr 23, 2023 6:59 pm Post subject: |
|
|
What is a uC clock speed?
About CTC mode. CompareB cannot be greater than CompareA because on CompareA the timer is cleared. So you can steel use a CompareB until it is smaller or equal to CompareA. _________________ Check B-Flash -my MCS bootloader app for Android |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 102
|
Posted: Sun Apr 23, 2023 7:02 pm Post subject: |
|
|
Thanks for the reply! Clock is 16MHz.
Is there a way to keep Compare1A or Compare1B from resetting the clock? I thought that only happened if configured. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Apr 23, 2023 7:09 pm Post subject: |
|
|
When 16MHz then with prescaler 1024 you can even achieve 0.239Hz not "only" 1,5Hz minimum
Better describe what you want to achieve. Is two outputs needed or one? It is not clear for me. _________________ Check B-Flash -my MCS bootloader app for Android |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 102
|
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Apr 23, 2023 7:31 pm Post subject: |
|
|
I still not get it maybe because it`s sunday but you can use Compare registers in "Normal" timer mode for generating interrupts where you can do some pin toggling or whatever. This is not recomended method.
So you can calculate how jitter should be, set Compareb for example to fire interrupt where you set the pin and set Comparea for next interrupt for pin reset. Then start the timer and in final interrupt stop it.
There should be better solution i think but I still dont understand the idea. _________________ Check B-Flash -my MCS bootloader app for Android |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 102
|
Posted: Sun Apr 23, 2023 7:48 pm Post subject: |
|
|
Thanks again for the help! I'm not sure I'm understanding either.
I think what I need is Compare1C. Start the timer, When Compare1A triggers, I need to set one or two pins high. When Compare1B triggers, I set the one or two pins low. When Compare1C triggers, it resets the timer. Is there something besides the two compare registers that would let me adjust the timer? I need to adjust the time the pulse starts, the time the pulse stops, and the overall time. I also need to control two outputs, so I'll need to use an ISR on the compare trigger. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Apr 23, 2023 7:58 pm Post subject: |
|
|
If only one pin and also hardware connected to the timer Compareb will be sufficient then this will be easy task.
Simply math for 0.5Hz = 2000ms this can be done with Prescale 1024 and value of 31249. Then you need a +25ms extra right? So 2025 will be 31639.
31639 - 31249 = 390 So you set a CompareA to 31639 and CombareB to 390.
By hardware you can config CompareB = Clear_up for set and this will be cleared on CompareA.
But if you need more pins then you need those interrupts indeed.
On some uC you have CompareC that can be used for second pin. _________________ Check B-Flash -my MCS bootloader app for Android |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 102
|
Posted: Sun Apr 23, 2023 8:21 pm Post subject: |
|
|
Would it be possible for one timer to trigger another timer? |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Apr 23, 2023 8:24 pm Post subject: |
|
|
Why not but for normal Atmega only in ISR I think. You can even reconfigure the timer multiple times for different purposes. There is more options like Event`s in new chips.
You may notice that this 0,5Hz + 25ms is not 0.5Hz anymore _________________ Check B-Flash -my MCS bootloader app for Android |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Apr 23, 2023 8:28 pm Post subject: |
|
|
Why do you start a new thread for your old question?
Quote: | Basically, I need a pulse that is generally in sync with the main clock (0.5Hz-1.5Hz), but will start sooner or later. |
For varying the period to be within the requested range you need CTC mode.
If you don't like the brute force method as suggested earlier in the link, you can calculate and vary the period by CTC.
Say the main clock should be 1Hz and viewing it over a scope of 5 periods, then 0.9, 0.8, 1.2, 1.0, 1.1 seconds will achieve a medium of 1 Hz main clock with jitter.
You need to successively calculate and keep track of the jitter the way that accumulated over all periods, the main clock is kept at the desired range. |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 102
|
Posted: Sun Apr 23, 2023 9:13 pm Post subject: |
|
|
Ok, so I switched Compare1A and Compare1B. However, if I use CTC (Clear_Timer = 1) my output is always on, indicating EndPulse never runs. From reading documentation, I assumed I should use Clear_Timer = 1, but couldn't get it to work that way. With zero, the LED connected to the pin that's toggled by the ISR blinks about every second. Is there something else I need for CTC mode?
I'm trying to avoid too many crazy calculations that might throw off timing. If my period is 1Hz, I'd like 1Hz, not 1.1Hz However, I'm open to anything as now I don't have a clue how to do it. Maybe I misunderstood the original answer. Did you mean run an ISR every 1ms?
Code: |
Compare1B = 300
Compare1A = 1200
Config Timer1 = Timer , Prescale = 256 , Clear_timer = 1
On Compare1B StartPulse
On Compare1A EndPulse
Enable Compare1a
Enable Compare1b
Enable Interrupts
Start Timer1
|
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Apr 23, 2023 10:28 pm Post subject: |
|
|
CBailey wrote: | Is there something else I need for CTC mode? |
A reasonable TOP value maybe? TOP of 1200, prescaler 256 at clock of 16MHz equals to 52Hz, the led will look dim, flickering if moved.
Quote: | I'm trying to avoid too many crazy calculations that might throw off timing. |
Calculations is regular business for microcontrollers.
If you however, the maker of the clock (metaphorically spoken), are not able to calculate how many gear wheels you need and how many gear teethes each wheel must have, you won't be able to create a clock.
Quote: | If my period is 1Hz, I'd like 1Hz, not 1.1Hz |
1.1Hz can be seen as 1Hz period with minus 90ms jitter.
Quote: | Did you mean run an ISR every 1ms? |
My words:
Quote: | timerinterrupt at 1ms |
sound this way.
But as said, this is brute force.
Controlling TOP and this way the period the way the main clock is kept over all periods is much smarter.
The one time costs are to employ some of that grey matter |
|
Back to top |
|
|
|