Estoy utilizando el microcontrolador AVR atmega32. El código de abajo se supone que para cambiar el LED conectado a PORTD
. La frecuencia del oscilador conectado al microcontrolador es 16MHz .
Así que calculé el número de desbordamientos debe TCNTO
encuentro para retrasar el LED 1 segundo, utilizando: T=1FCPU=62.5∗10−9s Por lo tanto, el número de desbordamientos es : 62.5∗10−9∗256∗n=1s Así que n=62500 Cuando simulé ese código, el LED parpadeaba cada segundo, pero cuando lo apliqué, el LED parpadeaba cada 4 segundos.
¿Estoy utilizando mal la fórmula?
¿Cómo especificar la hora que quiero con precisión para las aplicaciones que necesitan exactitud en el tiempo?
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
int main()
{
uint32_t timeOverFlowCount=0;
DDRD=0xff; //configure PORTD as output
/* timer0 */
TCNT0=0x00;
TCCR0=1<<CS00;
while(1){
while((TIFR & 1<<TOV0)==0); //wait until TCNT0 over flow (reach 255)
TCNT0=0x00; //begin from 0 again
TIFR &= 1<<TOV0; //Write 1 to the D0 (TOV0) flag to clear it
timeOverFlowCount++; // increment one to know how many it reachs 255
if(timeOverFlowCount==62500){//if it counts 255 five times, toggle the LED
PORTD ^=(0x01<<PD4); //toggke the led
timeOverFlowCount=0; //set counter to 0
}
}
}
```