Analog value transmission use USART module of PIC Microcontroller and Mikro C
In this tutorial, we will learn how Analog value transmission using the USART module of PIC Microcontroller. The A/D conversion of the analog input signal result is a corresponding 10-bit digital number. The size of this 10-bit digital number is two Bytes. That's why we have two Byte digital number must be sent. You can watch the following video or read the written tutorial below.
USART Module :
Now let's take a closer look at the USART module. The Universal Synchronous Asynchronous Receiver Transmitter ( USART ) module is one of the two serial I/O modules.
USART is also known as a Serial Communication Interface or SCI. The USART can be configured as a full duplex asynchronous system that can communicate with peripheral devices such as CRT terminals and personal computer, or it can be configured as a half duplex synchronous system that can communicate with peripheral device such as A/D or D/A integrated circuits, serial EEPROMs etc.
The USART can be configured in the following modes:
- Asynchronous ( Full duplex )
- Synchronous - Master ( Half duplex )
- Synchronous - Slave ( Half duplex )
USART Asynchronous mode:
In this mode, the USART uses standard non-return-to-zero (NRZ) format (one START bit, eight or nine data bits, and one STOP bit). The most common data format is 8-bits. An on-chip, dedicated, 8-bit baud rate generator can be used to derive standard baud rate frequencies from the oscillator. The USART transmits and receives the LSb first. The transmitter and receiver are functionally independent, but use the same data format and baud rate.
Circuit schematic:
Here's the circuit schematic. The circuit schematic is very quick sample. It has two parts. One is Receiver and other is Sender. The LCD display is connected to the PORTB of the Receiver. Here the TX pin of the Sender is connected to the RX pin at the Receiver. The 5K variable is provide analog value to the microcontroller.
Sender Mikro C code:
unsigned int adc_value;
char lower_byte;
char higher_byte;
void main() {
UART1_Init(9600); // Initialize the UART library
delay_ms(100); // Staiblized the UART module
while(1){
adc_value = ADC_Read(0); // Read Analog value from RA0 pin
lower_byte = adc_value; // Read lower byte
higher_byte = adc_value >> 8; // Read higher byte
UART1_Write(lower_byte); // Send lower_byte
delay_ms(50); // 50ms delay
UART1_Write(higher_byte); // Send higher byte
delay_ms(500);
}
}
Code Explain:
Here's the Mikro C code for Sender. The code is very quick simple. In this code we have included UART and ADC library.
In define section, we have to define some variables. we have defined unsigned integer variable "adc_value" which contain the 10-bit digital value. Next, we have defined two character variable.
In main section, we have to need initialized the USART module at 9600 Kilo bits per second ( Kbps). Here's the 100 millisecond ( ms ) delay which stabilize the USART module.
In the main loop, we will read analog value from analog channel 0 by ADC_Read( 0 ) function. Here, we will copy the 8-bit lower byte and 8-bit higher byte from the adc_value register. Using the UART1_Write ( ) function, I will send lower bytes and higher bytes at 50ms intervals.
Receiver Mikro C Code:
// Lcd module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End Lcd module connections
char output[20];
char lower_byte;
char higher_byte;
unsigned int adc_value;
void main() {
UART1_Init(9600); // Initialize the USART module at 9600Kbps
delay_ms(100); // wait for usart module staiblized
Lcd_Init(); // Initialize the LCD module
Lcd_Cmd(_LCD_CLEAR); // Clear the LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off the LCD display
Lcd_Out(1,1,"Developed By");
Lcd_Out(2,1,"MINA TECHNOLOGY");
delay_ms(2000);
LCD_Cmd(_LCD_CLEAR);
while(1){
if(UART1_Data_Ready()){
lower_byte = UART1_Read(); // Read lower_byte
delay_ms(70); // wait for higher_byte
if(UART1_Data_Ready() ){
higher_byte = UART1_Read(); // Read higher_byte
}
adc_value = (higher_byte<<8) | lower_byte; // Read higer // and lower bytes intToStr(adc_value,output); // Convert int to strisng
Lcd_Out(1,1,"Analog Value:");
Lcd_Out(2,3,output);
}
}
}
Code Explain:
Here's the Mikro C code for Receiver. In this code we have included UART, LCD and Conversion library.
In define section, we have defined LCD module connections . The LCD display module is connected to PORTB. We need a character array whose length will be 20. Next, we have declared some variable.
In main section, we have to need initialized the USART module at 9600 Kilo bits per second ( Kbps). Here's the 100 millisecond ( ms ) delay which stabilize the USART module. Next, we have initialized the LCD module. We have written some command for LCD module.
In the main loop section, we have to need if statement which will check the receive data. If data is received then the statement will be true and statement execute. We will receive the lower byte value using the UART1_Read ( ) function. The 70 millisecond which wait for higher byte. Here, for data communication we have used time base data communication method. The time base data communication method is to send two or more bytes at regular intervals. Which the Receiver understands is either a high byte or a low byte. We have again used the if statement to accept higher bytes. Next we will copy the value of lower byte and higher byte to adc_value variable. We will convert the integer value to a string using the intToStr ( ) function. Next we will show the string on the LCD display.
Click the download button for source code:
No comments