Sending "Hello World!" from the USART
Last modified by Microchip on 2023/11/09 09:02
Contents
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
void USART0_Transmit(char* StringPtr){
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
}
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
char String[]="Hello world!!";
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
USART_putstring(String);
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/io.h>
#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);
}
}
#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 should 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 the 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.