Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

/*

Serial.C

Written by Jeffrey J. Richardson Aug. 17, 2002

Simple program that first initializes the serial port for 9600, N, 8, 1.
Second, the program initializes PortC for output to the LEDs.
Third, the program enters an endless loop that...
Uses the standard library function to get a serial character
Display the character on the LEDs
Echo the character back to the sending device

Uses the MegaAVR-DEVelopment Board available from PRLLC


with an ATmega163 controller operating at 6MHz.

*/

#include<mega163.h> Constants used to calculate the


#include<stdio.h> UART configuration registers.
#define XTAL 6000000L
#define BAUD 9600

main()
{
unsigned char ch; // 8 bit variable to store the serial character
unsigned int UBR; // 16 bit variable to hold serial port calculations

UBR = XTAL / 16 / BAUD - 1; // calculate the load values


UBRR = (unsigned char)(UBR & 0xFF); // load the lower 8 bits
UBRRHI = (unsigned char)(UBR >> 8); // load the upper bits
UCSRB = 0x18; // enable transmit and receive

DDRC = 0xFF; // setup PortC for output


PORTC = 0x00;

while(1) // do forever...
{
ch = getchar(); // get a character from the serial port
PORTC = ~ch; // display the character on the LEDs
putchar(ch); // send the character back to the terminal
}
}

You might also like