示例#1
0
文件: main.c 项目: sk1p/TV-B-Gone-kit
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();
}
示例#2
0
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();
}
示例#3
0
文件: main.c 项目: InSAnUA/Baldwisdom
int main(void) {
  uint8_t i, j;
  uint16_t ontime, offtime;

  
  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



  // 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

  for (i=0; i<1; i++) {   // repeat the code twice
      //To keep Watchdog from resetting in middle of code.
    wdt_reset();
    quickflashLED(); // visible indication that a code is being output
    uint8_t freq = pgm_read_byte(&NikonCode);
    // set OCR for Timer1 and Timer0 to output this POWER code's carrier frequency
    OCR0A = OCR0B = freq; 
    
    // 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
    j = 0;  // index into codeElements of this POWER code
    do {
      // read the onTime and offTime from the program memory
      ontime = pgm_read_word(&NikonCode+(j*4)+1);
      offtime = pgm_read_word(&NikonCode+(j*4)+3);

      xmitCodeElement(ontime, offtime, 1);  // transmit this codeElement (ontime and offtime)
      j++;
    } while ( offtime != 0 );  // offTime = 0 signifies last codeElement for a POWER code

    PORTB |= _BV(IRLED);           // turn off IR LED

    // delay 250 milliseconds before transmitting next POWER code
    delay_ten_us(25000);
  }
    // 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 
  quickflashLED4x();

  // Shut down everything and put the CPU to sleep
  TCCR1 = 0;                      // turn off frequency generator (should be off already)
  TCCR0B = 0;
  PORTB |= _BV(LED); // turn on the button pullup, turn off visible LED
  PORTB |= _BV(IRLED);          // turn off IR LED
  delay_ten_us(1000);             // wait 10 millisec second

  MCUCR = _BV(SM1) |  _BV(SE);    // power down mode,  SE=1 (bit 5) -- enables Sleep Modes
  sleep_cpu();                    // put CPU inteo Power Down Sleep Mode
}