How to count time in C for ATMega8 with clock 16MHz -
How to count the time in interruption and set as a global variable? My microcontroller is Atmega8, F_CPU is 16MHz, programming language is C. I know some things, the line given below is the beginning of the clock.
TCCR1B | = (1 & lt; cs10); And let's line that the value of the cycles divided by the global variable "a" "xxx", but what value should be assigned to xxx?
a = TCnt1 / xxx; PS1: I have to count the last turn of my propeller in the inspired LED display and this value should be given in the middle of the next time to display the image correctly. PS2: To delay, I use the function: _delay_ms ().
ago _delay_ms is a good way to give the right time is not.
I give the following suggestions: Use timer 1 to measure the frequency of your rotor. When you consider your static interrupt (consider a per-turn), write the current timer value in a variable, calculate the difference of the cycle cycle on each one of the following (think correctly overflow).
Now you have the number of revolution cycles. Now I will set a second timer in a way so that the counter top value (CTC mode) is equal to your measured cycle number divided by your angle resolution of your display. Ho. By overloading the overflow, you can launch new data in the LEDs.
Example:
Pre-condition / assumptions:
- F_CPU (and the actual clock) 16 MHz
- Resolution of display: Speed of rotor: 137hz
- Timer 1 presceller = 4 (otherwise it overflows multiple times per revolution)
With this value you should get a 29197 timer-tie difference which translates 116788.3 CPU-cycle per revolution. You have 583.94 per section circle divided by 200 lines. This number is too big for this 8-bit counter, using Prescale 2 gives 291.97 - is too big. So you have to use the price (next 4) of the front (hopefully) 4 to get 145.98. It is round 146 and -1 because 0 is also counted. So I suggest using this counter as the top value of 145. This idea should be implemented functionally by acquiring cycle value and increasing the presceller until it fits in your 8bit.
The entire program will look like the following:
Unstable uint16_t oldTimer = 0; Unstable uint16_t newTimer = 0; Volatile uint8_t flag = 0; // bit1 = push new data, bit 2 = new revolution measure ISR (Postian Interpate) {oldTimer = newTimer; NewTimer = TCNT1; Flag | = 2; } ISR (Timer 2 overflow) {latchDataToOutput (); Flag | = 1; } Int main () {while (1) {if (flag & 2) {calcNewTimer2Timing (); Flag & amp; = ~ 2; } If (flag and 1) {OutputDetectTitist (); Flag & amp; = ~ 1; }}}
Comments
Post a Comment