For any AVR experts out there,
I'm trying to build a function to allow a bit more capability than the standard serial.begin function of the Arduino IDE. I was wondering if this looks like I'm on the right track
#include <avr/io.h>
bool USARTbegin( int port, int baudRate, int dataBits, char parity, int stopBits )
{
if(port == 0)
{
int clockCPU;
int prescale;
//basic setup
UCSR0A = 0x20; //B00100000;
UCSR0B = 0x0C; //B0001100
UCSR0C = 0x06; //B0000110 default
//change bits per settings
//baud rate setting
prescale = ((( clockCPU / ( baudRate * 16 UL))) - 1)
UBRR0H = (prescale >> 8); //load upper 8 bits to high byte
UBRR0L = (prescale); //load lower 8bits to low byte
//message databit setting
if(dataBits == 9)
{
UCSR0B = (1<<UCSZ02);
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
}
else if(dataBits == 8)
{
UCSR0B = (0<<UCSZ02);
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
}
else if(dataBits == 7)
{
UCSR0B = (0<<UCSZ02);
UCSR0C = (1<<UCSZ01) | (0<<UCSZ00);
}
else if(dataBits == 6)
{
UCSR0B = (0<<UCSZ02);
UCSR0C = (0<<UCSZ01) | (1<<UCSZ00);
}
else if(dataBits == 5)
{
UCSR0B = (0<<UCSZ02);
UCSR0C = (0<<UCSZ01) | (0<<UCSZ00);
}
else
{
return false;
}//end of data bit
//parity setting
if(parity == o || parity == O)
{
UCSR0C = (1<<UPM01) | (1<<UPM00);
}
else if(parity == e || parity == E)
{
UCSR0C = (1<<UPM01) | (0<<UPM00);
}
else if(parity == n || parity == N)
{
UCSR0C = (0<<UPM01) | (0<<UPM00);
}
else
{
return false;
}//end of parity
//stop bits
if(stopBits == 1)
{
UCSR0C = (0<<USBS0);
}
else if(stopBits == 2)
{
UCSR0C = (1<<USBS0);
}
else
{
return false;
}//end of stop bits
}//end of port number
return true;
}
I realize that reading 9 bits of data requires pulling 1 bit from one of the UCSR's then reading UBR. Right now I don't ever forsee using 9 bit data length.
One thing I cannot figure out from the mega datashett, how can you tell how many bytes are currently in the recieve buffer?
thanks