Learn To Write Code For 8051, Arduino, AVR, dsPIC, PIC, STM32 ARM Microcontroller, etc.
Coding Embedded Controller With C/C++.
Printed Circuit Board (PCB) Project For Electronics Hobbyists.
In the previous post I have shown about a few details of the USART module the ATMega32 device with some working examples. Here I put some more USART examples with some AVR C programming techniques - making a condition with the received characters, and using the ANSI C string functions library.
Conditioning the receiving characters
Within this simple example, The user sends some characters to the MCU USART module via the host PC USART terminal. The MCU keeps track of the characters and store them in an array at a size of 15 characters, or any size up to the programmer.
Whenever the user send the ENTER key ( ASCII value of 0x0D) the MCU terminates the the next character storing and send back all the previous characters back to the host PC terminal.
/*
* uart_example_1.c
*
* Created: 9/30/2020 10:21:10 AM
* Author : aki-technical
*/
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#define uartReady UCSRA&(1<<RXC)
void uartInit(unsigned long baud){
unsigned int UBRR;
/*Baud rate calculator*/
UBRR=(F_CPU/(16*baud))-1;
UBRRH=(unsigned char)(UBRR>>8);
UBRRL=(unsigned char)UBRR;
/*Enable the transmitter and receiver*/
UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXEN);
/*asynchronous mode, 8-bit, 1-stop bit*/
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}
void uartTransmit(unsigned char data){
/*Stay here until the buffer is empty*/
while(!(UCSRA&(1<<UDRE)));
/*Put the data into the buffer*/
UDR=data;
}
char uartReceive(){
/*Wait until the buffer is full*/
while(!(UCSRA&(1<<RXC)));
/*Get the data ready to use*/
return UDR;
}
void sendText(char *txt){
while(*txt) uartTransmit(*txt++);
}
int main(void)
{
char tmp,i=0;
char txt[15];
DDRC=0xFF;
uartInit(9600);
sendText("ATMEGA32 AVR UART Example 1\n\r");
while (1)
{
/*If There is a character in the buffer*/
if (uartReady)
{
tmp=uartReceive();
PORTC=tmp;
txt[i]=tmp;
i++;
}
/*If the ENTER Key Is Presented*/
if (tmp==0x0D)
{
/*Send text back to the terminal*/
sendText(txt);
/*Clear the character storage*/
tmp='\0';
/*Clear all the texts*/
while(i>0){
txt[i]='\0';
i--;
}
}
}
}
The overall source codes of the program is a little different from the previous post, except the a few block of C code that make the conditioning the received characters.
schematic diagram
I set the clock to 16 MHz to work with my own prototyping board. The output LED bar graph at PORTC indicates the present of data received.
A sample of this program
Click here to download the zip file of this working example.
Using the ANSI C string functions library
This example I use the string compare function from the ANSI C string library function. There are a lot of string processing functions in this library. However I pick up only one's that only required in this example.
The string compare - strcmp()
int strcmp(char *string1,char *string2);
where,
- string1 is the first string to be compared
- string2 is the second string to be compared
This function returns:
less than zero if string1 is less than string2
greater than zero if string1 is greater than string2
zero if these two string are equal.
Now let move the overall processes of this example. Once the user sends the command the MCU reads the character stored in the USART buffer, and make it a string. Whenever the user enter a new line or ENTER, the MCU start process the string comparison. When the comparison is matched the MCU toggles its corresponding output pin.
Let see the source code.
/*
* uart_example_2.c
*
* Created: 9/30/2020 5:34:08 PM
* Author : aki-technical
*/
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <string.h>
#define uartReady UCSRA&(1<<RXC)
void uartInit(unsigned long baud){
unsigned int UBRR;
/*Baud rate calculator*/
UBRR=(F_CPU/(16*baud))-1;
UBRRH=(unsigned char)(UBRR>>8);
UBRRL=(unsigned char)UBRR;
/*Enable the transmitter and receiver*/
UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXEN);
/*asynchronous mode, 8-bit, 1-stop bit*/
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}
void uartTransmit(unsigned char data){
/*Stay here until the buffer is empty*/
while(!(UCSRA&(1<<UDRE)));
/*Put the data into the buffer*/
UDR=data;
}
unsigned char uartReceive(void){
/*Wait until the buffer is full*/
while(!(UCSRA&(1<<RXC)));
/*Get the data ready to use*/
return UDR;
}
void sendText(char *txt){
while(*txt) uartTransmit(*txt++);
}
char tmp,i=0;
char txt[15];
/*Some string constants to compare with USART command*/
No comments:
Post a Comment