Author Topic: ATMega324 timer1 keeps running  (Read 3720 times)

0 Members and 1 Guest are viewing this topic.

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 887
Re: ATMega324 timer1 keeps running
« Reply #25 on: August 12, 2023, 01:41:52 am »
Quote
It is still running too fast
The code link in post #17 shows how you can set the fuses, post #19 mentions the CKDIV8 fuse and also that you can set the prescaler at runtime regardless of what the CKDIV8 fuse happens to be.

The timer clock will be a known speed- you know the source clock, the cpu clock prescaler either via CKDIV8 fuse (/1 or /8) or set on your own via CLKPR, and the timer prescaler (/64 it appears you want). Your OCR1A calculation also has to be correct, which by your original post will be 2x faster than it should be (an extra /2 included because you were reading a toggle freq formula).

Without much work, you can create a simple test by toggling an led with the help of the timer so it will be easy to tell if the timer is running as expected. Not much fun chasing unknown clock speeds, so may as well get desired==actual before you get too deep into code that has magic values inserted to 'make it work'.

A simple modification of previously linked code-
https://godbolt.org/z/b5o8Eq678
which should blink an led at 1Hz- it either does or does not. If not, something is wrong. Create your own test as you see fit to get the timer running as expected.
 

Offline admiralkTopic starter

  • Regular Contributor
  • *
  • Posts: 178
  • Country: us
Re: ATMega324 timer1 keeps running
« Reply #26 on: August 12, 2023, 02:56:53 am »
Quote
(an extra /2 included because you were reading a toggle freq formula)

That kind of struck a bell. I am not sure which way is right, but taking out the *2 made it run too slow; about 1.5 sec compared to about 2/3 sec. Maybe I am just counting wrong, but again, it does not really matter. The motors, in the real project, are an open loop drive. I will need to find the real time by trial and error. Someone mentioned setting a higher prescaler would give me more resolution. That is one of the experiments I need to try.

At this point, I forget why I picked a 64 prescaler. It is certainly not written in stone. My main objective was to get the timer to stop in the interrupt so that the states would all run at the same time, regardless of when the interrupt was triggered. Originally, the BACK state could last for any fraction of the timer. It could have been the whole time, if the timer was just starting. Or, it could have been a blink of an eye, if the timer was almost done. Now that that has been accomplished, I can work on how long each state needs to run at a given motor speed. It is much easier to change that than the motor speed. And more sensible.

I do appreciate your input just the same.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf