#define F_CPU 1000000 #include #include #include #include #include #include "../helpers.h" #include "../lcd.h" static volatile uint8_t address = 0; static volatile uint16_t cnt = 0; static volatile uint32_t eepromaddress = 0; static volatile uint8_t signal[60]; // signal that is recorded the first time static volatile uint8_t firstsignal[60]; // all following signals that is compared with the first signal static volatile uint8_t firstsignallength; // length of the first signal // how many keys do you want to record uint8_t numberKeys = 20; void timer_init() { // timer TCCR0 |= (1 << CS00); TIMSK |= (1 << OCIE0); // interrupt for changing port GICR |= (1 << INT1); MCUCR |= (1 << ISC10); MCUCR |= (1 << ISC11); } void eeprom_init() { // position in eeprom eepromaddress = 0; } void var_init() { cnt = 0; // timebase address = 0; // position in array } ISR(TIMER0_COMP_vect) { // count up a counter (timebase) // but only if a signal has appeared on the port one time if (address > 0) { cnt ++; } } ISR(INT1_vect) { // we got a signal change on the port, so save the timebase into array // and increase position in array signal[address] = cnt; address ++; } void recordSignal() { firstsignallength = address; // save the signal to the array "firstsignal" for (uint8_t i = 0; i < address; i++) { firstsignal[i] = signal[i]; } } uint8_t matchSignal() { // check if firstsignal equals signal // ignore a difference of +-1 for (uint8_t i = 0; i < firstsignallength; i++) { if (firstsignal[i] != signal[i] && (firstsignal[i] + 1) != signal[i] && firstsignal[i] != (signal[i] + 1)) { return 0; } } return 1; } void finishSignal() { // write length of signal eeprom_write_byte(eepromaddress, firstsignallength); eepromaddress ++; // write all parts of the signal for (uint8_t i = 0; i < firstsignallength; i++) { eeprom_write_byte(eepromaddress, firstsignal[i]); eepromaddress ++; } } int main() { // deactivate all interrupts cli(); // configure timer and interrupts timer_init(); // configure variables var_init(); // configure eeprom eeprom_init(); // configure LCD lcd_init(); // loop all keys, that you want to record for (uint8_t key = 0; key < numberKeys; key++) { for (int16_t loop = 0; loop < 3; loop++) { // show the user the number what key on the remote control is expected lcd_uint16(key); // reset all variables var_init(); // activate interrupts sei(); while(1) { if (address > 0) { // if address is > 0 then we have received a signal _delay_ms(100); // block any other signals cli(); // filter for valid signals if (address > 5) { if (loop == 0) { // the first signal should be recorded as it is recordSignal(); } else { // all following signals should match the first one if (matchSignal() == 0) { // if not, start again including recording the first signal loop = -1; } } //##################################### } else { // no valid signal lcd_str("----"); // reset variables but do not leave the loop var_init(); // activate interrupts sei(); continue; } // leave loop to get the next signal break; } } } // write signal to eeprom finishSignal(); } }