#include #include #include // is necessary to use the interrput // handling macro INTERRUPT() #include // is necessary to use // sei() and cli() #include #include "I2C_routines.h" /*-------------------------------------- For this program to work, you must read the factory osccal value from the device! this value must then be written into EEPROM memory at address that matches value defined as EEPROM_OSCCAL_ADDRESS --------------------------------------*/ #define EEPROM_OSCCAL_ADDRESS 0x000 uint8_t tx_busy(void); void tx_byte(char); void InitHardware(void); int main(void) { InitHardware(); i2cInit(); DDRE = 0xFF; // puts pointer at address 0000 start(); sendByte(0b10100000); if( recieveAck() == 0) { PORTE = 0x10; } sendByte(0); if( recieveAck() == 0) { PORTE = 0x20; } sendByte(0); if( recieveAck() == 0) { PORTE = 0x30; } stop(); while(1) { } return 0; } /* ------------------------------------------------------------ */ /*** InitHardware ** ** Synopsis: InitiHardware() ** ** Parameters: none ** ** Return Value: none ** ** Errors: none ** ** Description: calibrates the internal osc for reliable ** serial transmission and reception, properly sets up the ** serial hardware for transmission and reception ** enables the recieve interrupt. ** */ /* ------------------------------------------------------------ */ void InitHardware(void) { // disable interrupts cli(); /*Reads the oscal value stored in eeprom and calibrates the internal oscillator */ OSCCAL = eeprom_read_byte(EEPROM_OSCCAL_ADDRESS); /* set up hardware serial port 1 mode: asynchronous buad rate: 2400 parity: none data bits: 8 stop bits: 1 read the data sheet for more information about these registers */ UCSR1A = 0; UCSR1C = 0; UCSR1C = ( 1 << UCSZ11 | 1 << UCSZ10 ); UBRR1H = 0; UBRR1L = 191; // enable serial TX and RX anb RX interrupt UCSR1B = 0; UCSR1B = ( 1 << RXCIE1 | 1 << TXEN1 | 1 << RXEN1 ); // enable interrupts sei(); return; } /* ------------------------------------------------------------ */ /*** tx_byte ** ** Synopsis: tx_byte(char) ** ** Parameters: a byte value to be sent via the serial port ** ** Return Value: none ** ** Errors: none ** ** Description: transmits 1 byte serially, returns when ** the transmission is complete ** */ /* ------------------------------------------------------------ */ void tx_byte(char Character) { // load the tx buffer UDR1 = Character; // wait for the transmission to complete while( tx_busy == 0 ) { } // clear the tx complete flag UCSR1A |= ( 1 << TXC1 ); return; } /* ------------------------------------------------------------ */ /*** tx_busy ** ** Synopsis: done = tx_busy() ** ** Parameters: none ** ** Return Value: zero if the transmission is not complete, ** a NON-ZERO value if the transmission is complete ** ** Errors: none ** ** Description: checks the status of the TX1 bit in the ** UCSR1A register ** */ /* ------------------------------------------------------------ */ uint8_t tx_busy(void) { volatile uint8_t status; // get the status of the tx status = UCSR1A; // mask out unnesecary bits status = ( status & ( 1 << TXC1 ) ); return status; } /* ------------------------------------------------------------ */ /*** ** ** Synopsis: usart serial recieve interrupt handler ** ** Parameters: none ** ** Return Value: none ** ** Errors: none ** ** Description: when the hardware USART succesfully recieves ** a byte, it immediatly sends that byte out via the serial TX ** */ /* ------------------------------------------------------------ */ SIGNAL(SIG_UART1_RECV) { tx_byte(UDR1); start(); sendByte(0b10100001); if( recieveAck() == 0) { PORTE = 0x40; } tx_byte(recieveByte()); sendNack(); stop(); }