|
|
USART (Universal asynchronous receiver/transmitter) are commonly used in conjunction with communication standards such as EIA RS 232. It is the most common method for interfacing an 8-bit microcontrollers to PC via the serial port.
But the logic levels of microcontroller and serial port differ.
For TTL
logic 1: 5V
logic 0: 0V
For RS 232
logic 1: -3 to -15V
logic 0: 3 to 15V
Thus to interface a microcontroller to PC we need a level converter. MAX232 IC is the most widely used single channel TTL-RS232 level converter.
For the theory and working of USART in atmega32 you can refer to the datasheet here.
I will discuss on how to practically implement USART communication on linux.
First up is initialization of USART i.e, enabling transmitter reciever , setting up the baudrate or using USART interrupts.
USART module as three control and status registers UCSRA ,UCSRB and UCSRC.
UCSRA has 6 bits indicating the status of transmission and 2 control bits for controlling speed and communication type.
Bit 0 – MPCM: Multi-processor Communication Mode
This bit enables the Multi-processor Communication mode. When the MPCM bit is written to
one, all the incoming frames received by the USART receiver that do not contain address infor-
mation will be ignored. The transmitter is unaffected by the MPCM setting.
Bit 1 – U2X: Double the USART Transmission Speed
This bit only has effect for the asynchronous operation. Write this bit to zero when using synchronous operation.
Writing this bit to one will reduce the divisor of the baud rate divider from 16 to 8 effectively dou-
bling the transfer rate for asynchronous communication.
In our case
UCSRA = 0x00;
Hence neither double speed nor multiprocessor communication mode is enabled.
For UCSRB,
For enabling reciever and transmitter RXEN and TXEN bits have to be set which gives the he equivalent 0x18.
Hence UCSRB = 0x18
UCSRC=0x86 .This results in selecting asynchronous operation,Parity disabled,1 stop bit, 8-bit character size and falling clock edge. URSEL is to be set in order to write to UCSRC.
USART Baud Rate Register is a 16bit register comprising of two 8 bit registers UBRRL and UBRRH.
URSEL bit has to be cleared in order to access UBRR registers.The bit 11:0 is a 12 bit register used to set the baud rate according to the following table
In linux unfortunately the printf and scanf statements are not associated with the standard io streams. Thus we have to associate file bufffers which acts as an alias for handling these streams.For the documentation of you can refer here.
But to make your work easy i have made a header file for handling the i/o streams.
1. Copy the usart.h in include directory of avr gcc installation. In my case avr is at /usr/avr/ and the usart.h file goes to /usr/avr/include/ directory.
2. #include "usart.h" - This statement is to be written in the header inclusion section
3. Getting the usart stream
FILE usart0_str = FDEV_SETUP_STREAM(USARTSendByte, USARTReceiveByte, _FDEV_SETUP_RW);
4. Linking it to the standard i/o stream
stdin=&usart0_str;
stdout=&usart0_str;
Clubbing it together
To print the float value we have to change the compiler parameters and hence the make file would look like
Setting up minicom
1. Change the settings
2. Set up serial port
3 . Save the settings
Bingo !! The final output!
No comments:
Post a Comment