This is basic example on how to send and receive one character over AVR UART peripheral. It is easy to understand and if you have not worked with UART on microcontroller before, it is good starting point. In article AVR serial port UART register usage we have explained details of register usage. Here we will give one example in order to see complete project and test this on your microcontroller.
When writing firmware for microcontroller often there is need to have constants or constant arrays. For example, to have fixed value to compare with measurement from ADC. If this value changes during program execution then it must be defined as variable and placed in RAM or EEPROM, but if it is fixed then it is better to place it in FLASH memory. Main reason why it is better to place it in FLASH is because it is biggest memory space in microcontroller and often we do not have enough RAM for project. Also, when writing predefined strings for sending to UART as messages for user, it is best to declare these strings as constants.
When communicating with other devices, we often need to parse incoming data stream to recognize predefined format of commands, for example, when using GPRS modem or GPS receiver. There are many examples when data stream parsing is a must have! In previous articles we have seen how to define strings (these will be predefined commands) in FLASH memory space and in that way save RAM memory for variables.
Now we will show how to detect when useful information have been received and extract received data.
When writing a program for microcontroller (also often called firmware) it is very useful to have communication with microcontroller. In this way you can give command and get feedback from microcontroller about current state and readings. This can be done by several integrated peripherals that are used for communication. One is especially convenient because every PC has serial port and that peripheral is UART (Universal Asynchronous Receiver Transmitter). It is asynchronous communication and basic configuration that is most often used needs only two pins. These are TX and RX.