//pin3-> timer 2 #define TIMER_PWM_PIN 3 // main Arduino clock = 16000000Hz //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// inline void TimerEnablePWM() { TCCR2A |= _BV(COM2B1); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// inline void TimerDisablePWM() { TCCR2A &= ~(_BV(COM2B1)); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void mark(int time) // pulse parameters in usec { // Sends an IR mark for the specified number of microseconds. // The mark output is modulated at the PWM frequency. TimerEnablePWM(); // Enable pin 3 PWM output if (time > 0) delayMicroseconds(time); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void space(int time) { // Sends an IR space for the specified number of microseconds. // A space is no output, so the PWM output is disabled. TimerDisablePWM(); // Disable pin 3 PWM output if (time > 0) delayMicroseconds(time); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// inline void sendNECBYTE(unsigned char data) { for (int i = 0; i < 8; i++) { if (data & 1) { mark(560); space(1690); } else { mark(560); space(560); } data >>= 1; } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void sendNECFrame(unsigned int adr, unsigned char cmd) { mark(9000); space(4500); sendNECBYTE(adr >> 8); sendNECBYTE(adr & 0xff); sendNECBYTE(cmd); sendNECBYTE(~cmd); mark(560); space(560); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void sendNECFrameRepeat() { mark(9000); space(2250); mark(560); space(560); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup(void) { Serial.begin(9600); //configuration signal à 38Khz // COM2A = 00: disconnect OC2A // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted // WGM2 = 111: FAST PWM, TOP=OCRA, // CS2 = 010: prescaling 1/8 //periode= (16.000.000/8) /38.000= 52.6 OCR2A = 51; //periode -1 OCR2B = 25; //TON // mode =7 et prédiviseur à 8 TCCR2A = _BV(WGM21) | _BV(WGM20) ; TCCR2B = _BV(CS21) | _BV(WGM22) ; pinMode(TIMER_PWM_PIN, OUTPUT); digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low //pour le 1° exercice, vérifier que l'on a bien du 38Khz sur la pin 3 //commutation de la broche OC2B à chaque compare match entre le timer et le contenu de OC2R2B // TCCR2A |= _BV(COM2B1); // while(1); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop(void) { unsigned int adr = 0x1234; static unsigned char cmd = 0x56; sendNECFrame(adr, cmd); cmd++; delay(20); //le délai est obligatoire pour que le récepteur puisse raccrocher sur la trame suivante sendNECFrameRepeat(); delay(100); //le délai est obligatoire pour que le récepteur puisse raccrocher sur la trame suivante }