void setup() { Serial.begin(9600); TCCR2A = 0; TCCR2B = 0; digitalWrite(LED, LOW); digitalWrite(IRLED, LOW); pinMode(LED, OUTPUT); pinMode(IRLED, OUTPUT); pinMode(REGIONSWITCH, INPUT); pinMode(TRIGGER, INPUT); digitalWrite(REGIONSWITCH, HIGH); //Pull-up digitalWrite(TRIGGER, HIGH); delay_ten_us(5000); // Let everything settle for a bit // determine region if (digitalRead(REGIONSWITCH)) { region = US; // US DEBUGP(putstring_nl("US")); } else { region = EU; DEBUGP(putstring_nl("EU")); } // Tell the user what region we're in - 3 is US 4 is EU quickflashLEDx(3+region); // Indicate how big our database is DEBUGP(putstring("\n\rNA Codesize: "); putnum_ud(num_NAcodes); );
int main(void) { uint8_t i; TCCR1 = 0; // Turn off PWM/freq gen, should be off already TCCR0A = 0; TCCR0B = 0; i = MCUSR; // Save reset reason MCUSR = 0; // clear watchdog flag WDTCR = _BV(WDCE) | _BV(WDE); // enable WDT disable WDTCR = 0; // disable WDT while we setup DDRB = _BV(LED) | _BV(IRLED); // set the visible and IR LED pins to outputs PORTB = _BV(LED) | // visible LED is off when pin is high _BV(IRLED) | // IR LED is off when pin is high _BV(REGIONSWITCH); // Turn on pullup on region switch pin // check the reset flags if (i & _BV(BORF)) { // Brownout // Flash out an error and go to sleep flashslowLEDx(2); tvbgone_sleep(); } delay_ten_us(5000); // Let everything settle for a bit // Starting execution loop delay_ten_us(25000); // turn on watchdog timer immediately, this protects against // a 'stuck' system by resetting it wdt_enable(WDTO_8S); // 1 second long timeout wdt_reset(); // set OCR for Timer1 to output this POWER code's carrier frequency // XXX we need to set the carrier frequency to 32700 Hz, I think OCR0A = freq_to_timerval(32700); // XXX change 733 to 536 to change mode? // XXX emit burst... xmitCodeElement(45, 733, 1); // XXX emit second burst xmitCodeElement(45, 733, 1); // We are done, no need for a watchdog timer anymore wdt_disable(); // flash the visible LED on PB0 4 times to indicate that we're done delay_ten_us(65500); // wait maxtime delay_ten_us(65500); // wait maxtime quickflashLEDx(4); tvbgone_sleep(); }
int main(void) { uint16_t ontime, offtime; uint8_t i,j, Loop; uint8_t region = US; // by default our code is US Loop = 0; // by default we are not going to loop TCCR1 = 0; // Turn off PWM/freq gen, should be off already TCCR0A = 0; TCCR0B = 0; i = MCUSR; // Save reset reason MCUSR = 0; // clear watchdog flag WDTCR = _BV(WDCE) | _BV(WDE); // enable WDT disable WDTCR = 0; // disable WDT while we setup DDRB = _BV(LED) | _BV(IRLED); // set the visible and IR LED pins to outputs PORTB = _BV(LED) | // visible LED is off when pin is high _BV(IRLED) | // IR LED is off when pin is high _BV(REGIONSWITCH); // Turn on pullup on region switch pin // check the reset flags if (i & _BV(BORF)) { // Brownout // Flash out an error and go to sleep flashslowLEDx(2); tvbgone_sleep(); } delay_ten_us(5000); // Let everything settle for a bit // determine region if (PINB & _BV(REGIONSWITCH)) { region = US; // US } else { region = EU; } // Tell the user what region we're in - 3 is US 4 is EU quickflashLEDx(3+region); // Starting execution loop delay_ten_us(25000); // turn on watchdog timer immediately, this protects against // a 'stuck' system by resetting it wdt_enable(WDTO_8S); // 1 second long timeout do { //Execute the code at least once. If Loop is on, execute forever. // We may have different number of codes in either database if (region == US) { j = num_NAcodes; } else { j = num_EUcodes; } // for every POWER code in our collection for(i=0 ; i < j; i++) { //To keep Watchdog from resetting in middle of code. wdt_reset(); // point to next POWER code, from the right database if (region == US) { code_ptr = (PGM_P)pgm_read_word(NApowerCodes+i); } else { code_ptr = (PGM_P)pgm_read_word(EUpowerCodes+i); } // Read the carrier frequency from the first byte of code structure const uint8_t freq = pgm_read_byte(code_ptr++); // set OCR for Timer1 to output this POWER code's carrier frequency OCR0A = freq; // Get the number of pairs, the second byte from the code struct const uint8_t numpairs = pgm_read_byte(code_ptr++); // Get the number of bits we use to index into the timer table // This is the third byte of the structure const uint8_t bitcompression = pgm_read_byte(code_ptr++); // Get pointer (address in memory) to pulse-times table // The address is 16-bits (2 byte, 1 word) const PGM_P time_ptr = (PGM_P)pgm_read_word(code_ptr); code_ptr+=2; // Transmit all codeElements for this POWER code // (a codeElement is an onTime and an offTime) // transmitting onTime means pulsing the IR emitters at the carrier // frequency for the length of time specified in onTime // transmitting offTime means no output from the IR emitters for the // length of time specified in offTime // For EACH pair in this code.... for (uint8_t k=0; k<numpairs; k++) { uint8_t ti; // Read the next 'n' bits as indicated by the compression variable // The multiply by 4 because there are 2 timing numbers per pair // and each timing number is one word long, so 4 bytes total! ti = (read_bits(bitcompression)) * 4; // read the onTime and offTime from the program memory ontime = pgm_read_word(time_ptr+ti); // read word 1 - ontime offtime = pgm_read_word(time_ptr+ti+2); // read word 2 - offtime // transmit this codeElement (ontime and offtime) xmitCodeElement(ontime, offtime, (freq!=0)); } //Flush remaining bits, so that next code starts //with a fresh set of 8 bits. bitsleft_r=0; // delay 250 milliseconds before transmitting next POWER code delay_ten_us(25000); // visible indication that a code has been output. quickflashLED(); } } while (Loop == 1); // We are done, no need for a watchdog timer anymore wdt_disable(); // flash the visible LED on PB0 4 times to indicate that we're done delay_ten_us(65500); // wait maxtime delay_ten_us(65500); // wait maxtime quickflashLEDx(4); tvbgone_sleep(); }