//Abril,2024
//JMFA
// Programa que configura el Timer0
// MCU AtMega328p
#include <avr/io.h>
#define F_CPU 16000000UL
#define MILISEGUNDOS01 1000
#define MILISEGUNDOS02 3000
#define CICLO_TIMER0_MS 16.38
void configura(void);
int main(void)
{ //Inicia main()
//Configuramos a D13 (PB5) de arduino como salida
DDRB = DDRB|0x01<<5;
PORTB = PORTB|0x01<<5; //led=on
//Configurando el Timer0 en Modo Normal
//WGM02=0, WGM01=0, WGM00=0
TCCR0A = TCCR0A&(~(0x01<<0)); //WGM00=0
TCCR0A = TCCR0A&(~(0x01<<1)); //WGM01=0
TCCR0B = TCCR0B&(~(0x01<<3)); //WGM03=0
//Configurando el factor de preescala del reloj
//CS02=1, CS01=0, CS00=0. Control Scale (100),escala /1024
//CORRESPONDE A 16.38 MILISEGUNOS POR CICLO TIMER0
TCCR0B=TCCR0B|(0x01<<0); //CS00=1
TCCR0B=TCCR0B&(~(0x01<<1)); //CS01=0
TCCR0B=TCCR0B|(0x01<<2); //CS02=1
int retardos1 = 0;
int retardos2 = 0;
int NumCiclos1=MILISEGUNDOS01/CICLO_TIMER0_MS;
int NumCiclos2=MILISEGUNDOS02/CICLO_TIMER0_MS;
while (1)
{ //INICIO while
//Acumula retardos, prueba TOV0==1, método POLLING
if (TIFR0 & 0x01<<0 == 0x01) {
TIFR0 = TIFR0 &(~(0x01<<0)); //Pone en 0 la bandera TOV
retardos1 = ++retardos1;
retardos2 = ++retardos2;
//Si alcanza el numero de retardos esperados
}
if (retardos1==NumCiclos1) {//Ciclos del Timer 0 a esperar
PORTB = PORTB|(0x01<<5); // LED 13 ENCENDIDO
retardos1 = 0; //regresa retardos a 0
}
if (retardos2==NumCiclos2) {//Ciclos del Timer 0 a esperar
PORTB = PORTB&(~(0x01<<5)); // LED 13 APAGADO
retardos2 = 0; //regresa retardos a 0
}
}
}//Termina main()