/* Title: SerialCom.c Date Created: 6/9/2009 Last Modified: 6/9/2009 Target: Atmel ATmega168 Environment: AVR-GCC Note: the makefile is expecting a '168 with a 16 MHz crystal. Adapted from the Arduino sketch "Serial Call and Response," by Tom Igoe. // This program sends an ASCII A (byte of value 65) on startup // and repeats that until it gets some data in. // Then it waits for a byte in the serial port, and // sends three (faked) sensor values whenever it gets a byte in. Written by Windell Oskay, http://www.evilmadscientist.com/ Copyright 2009 Windell H. Oskay Distributed under the terms of the GNU General Public License, please see below. Additional license terms may be available; please contact us for more information. More information about this project is at http://www.evilmadscientist.com/article.php/peggy */ #include //#include #define F_CPU 16000000 // 16 MHz oscillator. #define BaudRate 9600 #define MYUBRR (F_CPU / 16 / BaudRate ) - 1 void delayLong() { unsigned int delayvar; delayvar = 0; while (delayvar <= 65500U) { asm("nop"); delayvar++; } } unsigned char serialCheckRxComplete(void) { return( UCSR0A & _BV(RXC0)) ; // nonzero if serial data is available to read. } unsigned char serialCheckTxReady(void) { return( UCSR0A & _BV(UDRE0) ) ; // nonzero if transmit register is ready to receive new data. } unsigned char serialRead(void) { while (serialCheckRxComplete() == 0) // While data is NOT available to read {;;} return UDR0; } void serialWrite(unsigned char DataOut) { while (serialCheckTxReady() == 0) // while NOT ready to transmit {;;} UDR0 = DataOut; } void establishContact() { while (serialCheckRxComplete() == 0) { serialWrite('H'); // serialWrite(65U); delayLong(); delayLong(); delayLong(); delayLong(); delayLong(); delayLong(); delayLong(); } } int main (void) { //Interrupts are not needed in this program. You can optionally disable interrupts. //asm("cli"); // DISABLE global interrupts. DDRD = _BV(1); DDRB = _BV(0) | _BV(1) | _BV(3) | _BV(5); //Serial Initialization /*Set baud rate */ UBRR0H = (unsigned char)(MYUBRR>>8); UBRR0L = (unsigned char) MYUBRR; /* Enable receiver and transmitter */ UCSR0B = (1<