Sending "Hello World!" from the USART
In This Video
- Provide relevant community developed training materials for a useful USART_putstring() function.
- Modify this to call our USART_Transmit() function.
- Use delays for testing purposes.
- Put additional functionality into USART_irq.c and the function prototype to USART_irq.h, so main remains clean.
Procedure
Add Functionality to the USART Program
Add the following function to our program just below the #include statements.
while(*StringPtr != 0x00){ //Check if there is still more chars to send from the null char
USART_send(*StringPtr); //Send one char at a time
StringPtr++;} //Increment the pointer to read the next char
}
Assign a Value to String[]
Add the following line to our main.c above the previous function.
Call the Function From main.c
Add this line to the main.c file to send the String to the USART. Place it just below the sie() line.
Compile and Run the Program
Open up your terminal program and if you are fast enough, you'll see the following:
Make it Easier to See in Our Terminal Program
Add a delay function and move the USART_putstring(String) line into our main loop. We will need to include the delay header file as well. Here is the new main.c file contents with these changes.
#include <avr/interrupt.h> //ADDED
#include "USART_irq.h"
#include <util/delay.h>
char String[]="Hello world!!";
void USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){ //Check if there is still more chars to send from the null char
USART0_Transmit(*StringPtr); //Send one char at a time
StringPtr++;} //Increment the pointer to read the next char
}
int main(void)
{
/* Set the baudrate to 9600 bps using 8MHz internal RC oscillator */
USART0_Init(MYUBRR); //CHANGED to PASS MYUBRR from calculation
sei();
USART_putstring(String);
for( ; ; ) {
/* Echo the received character */
USART0_Transmit(USART0_Receive());
USART_putstring(String);
_delay_ms(1000);
}
}
Compile and Run the Program
You will see the following in your terminal program. Every time you click Enter, the terminal program will show what what is being sent from the UART. Depending on how you set up the terminal program, you might see "Hello world!!" coming in every second as in the video.
Clean up Main Again
Move our new function into the UART_irq.c file. Put the function definition into the USART_irq.h file. Now retest the program to make sure the functionality is still there.