// Code to grab the time from // an ESP8266 chip (represented as an integer) // using serial communications // and receive on an attiny44 // // get_char, put_char and elements from the preamble taken from code by Neil Gershenfeld // // // // Rahul Naidoo // 2016 // HTM[a]A #include #include #include #include #define output(directions,pin) (directions |= pin) // set port direction for output #define input(directions,pin) (directions &= (~pin)) // set port direction for input #define set(port,pin) (port |= pin) // set port pin #define clear(port,pin) (port &= (~pin)) // clear port pin #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define bit_delay_time 100 // bit delay for 9600 with overhead #define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay #define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay #define pause_delay() _delay_ms(10) // pause between sending ints #define char_delay() _delay_ms(10) // char delay char cambridgetime(char num){ if (num == 'A') { return(19); } else if (num == 'B') { return(20); } else if (num == 'C') { return(21); } else if (num == 'D') { return(22); } else if (num == 'E') { return(23); } else if (num == 'F') { return(0); } else if (num == 'G') { return(1); } else if (num == 'H') { return(2); } else if (num == 'I') { return(3); } else if (num == 'J') { return(4); } else if (num == 'K') { return(5); } else if (num == 'L') { return(6); } else if (num == 'M') { return(7); } else if (num == 'N') { return(8); } else if (num == 'O') { return(9); } else if (num == 'P') { return(10); } else if (num == 'Q') { return(11); } else if (num == 'R') { return(12); } else if (num == 'S') { return(13); } else if (num == 'T') { return(14); } else if (num == 'U') { return(15); } else if (num == 'V') { return(16); } else if (num == 'W') { return(17); } else if (num == 'X') { return(18); } else { return(28); } } void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) { // // read character into rxbyte on pins pin // assumes line driver (inverts bits) // *rxbyte = 0; while (pin_test(*pins,pin)) // // wait for start bit // ; // // delay to middle of first data bit // half_bit_delay(); bit_delay(); // // unrolled loop to read data bits // if pin_test(*pins,pin) *rxbyte |= (1 << 0); else *rxbyte |= (0 << 0); bit_delay(); if pin_test(*pins,pin) *rxbyte |= (1 << 1); else *rxbyte |= (0 << 1); bit_delay(); if pin_test(*pins,pin) *rxbyte |= (1 << 2); else *rxbyte |= (0 << 2); bit_delay(); if pin_test(*pins,pin) *rxbyte |= (1 << 3); else *rxbyte |= (0 << 3); bit_delay(); if pin_test(*pins,pin) *rxbyte |= (1 << 4); else *rxbyte |= (0 << 4); bit_delay(); if pin_test(*pins,pin) *rxbyte |= (1 << 5); else *rxbyte |= (0 << 5); bit_delay(); if pin_test(*pins,pin) *rxbyte |= (1 << 6); else *rxbyte |= (0 << 6); bit_delay(); if pin_test(*pins,pin) *rxbyte |= (1 << 7); else *rxbyte |= (0 << 7); // // wait for stop bit // bit_delay(); half_bit_delay(); } void put_char(volatile unsigned char *port, unsigned char pin, char txchar) { // // send character in txchar on port pin // assumes line driver (inverts bits) // // start bit // clear(*port,pin); bit_delay(); // // unrolled loop to write data bits // if bit_test(txchar,0) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,1) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,2) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,3) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,4) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,5) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,6) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,7) set(*port,pin); else clear(*port,pin); bit_delay(); // // stop bit // set(*port,pin); bit_delay(); // // char delay // bit_delay(); } int main(void) { static char Timeint; CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // DDRA = 0b00001001; set(PORTA, (1 << PA3)); // set the input pin //input(0b00001001, (1 << PA2)); input(DDRA, (1 << PA3)); set(PORTA, (1 << PA1)); //set the output pin //output(0b00001001, (1 << PA0)); output(DDRA, (1 << PA1)); while (1) { if ((PINA & 0b00001000) == 0 ) { get_char(&PINA, (1 << PA3), &Timeint); char newchar = cambridgetime(Timeint); // convert the time put_char(&PORTA, (1 << PA1), 1); char_delay(); put_char(&PORTA, (1 << PA1), 2); char_delay(); put_char(&PORTA, (1 << PA1), 3); char_delay(); put_char(&PORTA, (1 << PA1), 4); char_delay(); put_char(&PORTA, (1 << PA1), newchar); pause_delay(); put_char(&PORTA, (1 << PA1), newchar); pause_delay(); put_char(&PORTA, (1 << PA1), newchar); pause_delay(); } } return(0); }