I have a teensy 2.0 board and am trying to use it to control this LCD through serial TTL.
I got it to work fine with my Arduino uno and some sample Arduino code, but I want to do it with the teensy and the C language.
The teensy uses the ATmega32u4 and here is the datasheet for that chip.
In the datasheet, starting on page 186, is the section on USART. On page 192 is some sample C language code on how to initialize the USART. I am confused on a couple of lines and could use some help.
void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRHn = (unsigned char)(baud>>8);
UBRRLn = (unsigned char)baud;
/*rest of code here*/
}
First, I am not clear on what the baud rate number should be. My LCD is calling for 9600 baud with 1 start bit, 1 stop bit, no parity bit, and 8 bits of data. On page 189 of the ATmega32u4's datasheet I did the calculation on what the baud rate should be which in the case of the teensy and LCD combination should be 103 because the chip frequency is 16mhz and the baud rate is 9600. This also matches the number in the chart on page 213 of the ATmega32u4's datasheet. So, is this the correct number that would be passed into the USART_Init function for the variable baud?
I am also confused on the two lines that set the baud rate. I understand that, as per page 209 of the ATmega32u4's datasheet the baud rate registers are broken into a low and high register but only bits 8:10 on the high register are used making it a combined 12-bit register. So, going back to the code, I don't understand some things:
- Why is it being typecast to an unsigned char because I am assuming that UBRRHn should be in binary notation.
- I also do not understand the (baud>>8) part because if you fill in 103 for baud then you get (103>>8) and that ain't right. Can someone explain this line for me? Thanks.
UBRRHn = (unsigned char)(baud>>8);<br/>
UBRRLn = (unsigned char)baud;
- Should baud be the 12 bit binary version of 103 which is 00001100111? If so, then how would the code be written?
Also, I found some sample code(http://www.pjrc.com/teensy/uart.html) on the teensy website that sets up the UART and it sets up the baud rate like this:
void uart_init(uint32_t baud)
{
cli();
UBRR1 = (F_CPU / 4 / baud - 1) / 2;
/*rest of code here*/
}
If I plug in the numbers it would look like this:
UBRR1 = (16000000 / 4 / 9600 - 1) / 2; /* Which would equal 208 */
- The number 208 is different than 103 so I don't understand where they are getting the equation to calculate it.
- UBRRn is only mentioned in ATmega32u4's datasheet on pages 188 and 189. It looks like that holds the value of both UBRRHn and UBRRLn. The above code seems to be trying to set both at the same time but as an int. I am so confused, doesn't that need to be in binary notation?
Well, I am looking forward to getting over this hump and moving on to making my LCD dance. :)