I'm trying to get the atmega328 to communicate over serial port with my pc. I'm using hyperterminal to echo back the keyboard character entries. I'm using the max3232 chip to do the communicated with the computer. When I try to compile it gives and error and says that the variables in this function are undeclared, but they aren't variables but data register assignments. if I replace the 'n' char in the register assignments to a number it will compile and program but no info is displayed in hyperterminal. What should the 'n' be set too? My RX is on pin 2 and TX is on pin 3, if that helps. it is being programmed cuz I've done the blinky light programs. Any help is greatly appreciated.
unsigned char USARTReadChar( void )
{
//Wait untill a data is available
while(!(UCSRnA & (1<<RXCn)))
{
//Do nothing
}
//Now USART has got data from host
//and is available is buffer
return UDRn;
}
THE ENTIRE SOURCE CODE
#define FOSC 1843200 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
//This function is used to initialize the USART
//at a given UBRR value
void USARTInit(unsigned int ubrr)
{
//Set Baud rate
UBRR0H = (ubrr>>8);
UBRR0L = ubrr;
//Enable The receiver and transmitter
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
// Set fram format: 8data 2stopBit
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
//This function is used to read the available data
//from USART. This function will wait untill data is
//available.
unsigned char USARTReadChar( void )
{
//Wait untill a data is available
while(!(UCSR0A & (1<<RXC0)))
{
//Do nothing
}
//Now USART has got data from host
//and is available is buffer
return UDR0;
}
//This fuction writes the given "data" to
//the USART which then transmit it via TX line
void USARTWriteChar(unsigned char data)
{
//Wait untill the transmitter is ready
while(!(UCSR0A & (1<<UDRE0)))
{
//Do nothing
PORTD ^= 1 << PINB2;
}
//Now write the data to USART buffer
UDR0 = data;
}
int main(void)
{
DDRB |= 1 << PINB2;
//Varriable Declaration
char data;
USARTInit(MYUBRR);
//Loop forever
while(1)
{
//Read data
data = USARTReadChar();
/* Now send the same data but but surround it in
square bracket. For example if user sent 'a' our
system will echo back '[a]'.
*/
USARTWriteChar('[');
USARTWriteChar(data);
USARTWriteChar(']');
}
}