#include <avr/interrupt.h>
struct Horloge {
volatile unsigned long millisecondes;
volatile unsigned char secondes;
volatile unsigned char minutes;
};
Horloge chrono = {0, 0, 0};
void init_T2()
{
TCNT2 = 0;//initialisation a 0
// TCCR2A = 0b10;
TCCR2A |= (1 << WGM21); // placer en mode CTC
// t_cycle_ctc = 1ms , fcpu = 16Mhz , prescaler = 64
//OCR2A = (t_cycle_ctc * fcpu)/prescaler - 1
OCR2A = 249;//prediviseur = 64
// TIMSK2 |= (1 << OCIE2A);
TIMSK2 = 0b10;
// TCCR2B |= (1 << CS22);
TCCR2B = 0b100;
sei();
}
void desactive_T0()
{
TIMSK0 = 0;
TCCR0B = 0;
}
ISR(TIMER2_COMPA_vect)
{
chrono.millisecondes++;
if (chrono.millisecondes >= 1000)
{
chrono.millisecondes = 0;
chrono.secondes++;
if (chrono.secondes >= 60)
{
chrono.secondes = 0;
chrono.minutes++;
}
}
}
void affiche_chrono()
{
Serial.print("Minutes: ");
Serial.print(chrono.minutes);
Serial.print(", Secondes: ");
Serial.print(chrono.secondes);
Serial.print(", Millisecondes: ");
Serial.println(chrono.millisecondes);
}
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
init_T2();
}
void clignotement_led()
{
static unsigned long previousMillis = 0;
static boolean ledState = LOW;
const long interval = 250; // Un quart de seconde (250 millisecondes)
unsigned long currentMillis = chrono.millisecondes;
if (currentMillis - previousMillis >= interval) {
// Inversion de l'état de la LED
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
if(ledState == HIGH)
{
PORTD |= (1 << 2);
}else{
PORTD &= ~(1 << 2);
}
previousMillis = currentMillis;
}
}
void loop() {
clignotement_led();
}