/********************************************* Laser Communication System Receiver File Authors: Keith Carter, Michael Muccio Cornell University, Electrical and Computer Engineering ECE 476, Spring 2003 This program is used to program a Mega32 MCU to be the receiver for a serial voice data laser communication system. It receives serial data and blasts received bytes out PORTB to a DAC. Chip type : ATmega32 Program type : Application Clock frequency : 16.000000 MHz Memory model : Small Internal SRAM size : 2048 External SRAM size : 0 Data Stack size : 512 *********************************************/ #include #include #include //set baud to 27.7 kbps #define baudnum 36 #define halfbaudnum 18 //global variables char bitcnt; // bit counter char Rxbyte; // received data char sb; // number of stop bits (1, 2, ...) char k; // debugging counter //the data receive subroutine void get(void); //Receive Byte void get(void) { Rxbyte = 0x00; //clear receive byte bitcnt=9+sb; //initialize bit counter k=0; //initialize debug counter //while loop waits for start bit while(PIND.0 != 0) { k=k+1;// hang out } //delay to middle of start bit delay_us(halfbaudnum); //while loop samples at middle of bits //stops at bitcnt of 2 to avoid shifting //in stop bit while(bitcnt>2) { delay_us(baudnum); //1 bit delay Rxbyte=Rxbyte>>1; //shift receive byte right 1 //sample bit //move value to MSB of receive byte if (PIND.0) { Rxbyte=Rxbyte | 0b10000000; } else { Rxbyte=Rxbyte & 0b01111111; } bitcnt--; //decrement bitcnt, 1 bit received } //blast received byte out PORTB PORTB = Rxbyte; } void main(void) { DDRD=0b00000010; //direction for UART pins DDRB=0xff; //direction for output port sb=1; // set to 1 stop bit //infinite loop constantly calls get() to receive bytes while (1) { //receive bytes get(); //delay 1 bit to get past stop bit delay_us(baudnum); } }