I hope someone can help this frustrated newbie to correct my simple UART application.
I have an LCD that communicates at 2400 Baud to accept commands and characters serially. I have written (and borrowed) the code below that is supposed to set up the UART in the 32A and then allow the sending of data (I am not interested in receiving data right now).
The data sheet for the LCD indicates that a character 254 or FE is the prefix to a command being sent and that 1 is the command to clear the screen. (when the 254 is NOT sent then what is sent is a character to show on the LCD). I am communicating at 8,n,1 2400 baud with an external 16MHz crystal with fuses correctly set. I am using Atmel Studio with usbtiny.
I wonder if someone could point out my errors in the program since nothing happens on the LCD when this is executed. Many thanks.
#define F_CPU 16000000UL // set the CPU clock
#define BAUD 2400UL // define baud
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // set baudrate value for UBRR
// function to initialize UART
void uart_init (void)
UBRRL=(unsigned char)BAUDRATE; //set baud rate
UCSRB|=(1<<TXEN); ///|(1<<RXEN); //enable receiver and transmitter
UCSRC|=(1<<URSEL); //1 to write to UCSRC
UCSRC|=(1<<UCSZ0)|(1<<UCSZ1); //8 bit words
UCSRC&= ~(1<<UPM1)|~(1<<UPM0); //No Parity THESE THREE STEPS PROBABLY NOT NEEDED
UCSRC&=~(1<<UMSEL); //Asynch mode
UCSRC&=~(1<<USBS); //= = 1 stop bit
// function to send data
void uart_transmit (signed int x) //transmit a 16 bit word
while (!( UCSRA & (1<<UDRE))); // wait while register is free
UDR = x; // load data in the register
// main function: entry point of program
int main (void)
uart_init(); // initialize UART
uart_transmit (0b00010001); //The second 1 should show on the cleared screen