How to use Serial Monitor and PIC microcontroller
In this tutorial we will learn how to read analog value and that show the serial monitor. As the serial monitor we used PICkit 2 UART tool window. In this project we are used PIC16F877A microcontroller and PICkit 2. The PICkit 2 control the UART communication and establish connection between microcontroller and personal computer.
You can watch the following video or read the written tutorial below.
For explaining how to working serial monitor we created two examples, the first example we will read analog value using microcontroller and that will see the value in the serial monitor.
This is the first example. Here we notice that the analog value serially write in the serial monitor.
The
Now we will see the second example.
This is the second example . Here we are noticed the digital voltage in the serial monitor.
Let's look at the UART tool features of PICkit2.
The PICkit 2 Programmer application versions 2.40 and later include the UART Tool feature. This feature allows PICkit 2 to be used as a serial Universal Asynchronous Receiver and Transmitter ( UART ) terminal interface for communicating with a PIC microcontroller.
Potential uses include:
- Displaying debug text output from the microcontroller
- Logging microcontroller data to a text file
- Developing and debugging a microcontroller UART interface
- Interfacing with and sending commands to the microcontroller during development
The tool supports full duplex asynchronous serial communications from 150 to 38400 baud, including custom non-standard baud rates. The PICkit 2 connects via the ICSP connector directly to microcontroller TX and RX pins at logic levels. No transceiver is needed.
Now we will see the pin out of PICkit 2. It has 6 pins. The array indicates the first pin. The first pin right to left is the Vpp or MCLR pin which supply the programming voltage and reset target microcontroller. The second pin is the VDD which power supply the target microcontroller. The third pin is GND. Next is the RX pin which receive the uart data. Next the fifth pin is TX pin which send command the target microcontroller . Last pin 6 is not use.
PICkit 2 pinout |
Now, we will see the circuit schematic. the circuit schematic is very easy. Here we are connected the RX pin of PICkit 2 with TX pin of Microcontroller and vice versa TX pin with RX pin .
Circuit Schematics |
Here are the two code for UART communication and below is the description of them.
unsigned int i=0;
char text[10];
void main() {
Adc_Init() ; // Initialize the Adc module
UART1_Init(9600); // Initialize the USART module at 9600kps boud rate
while(1){
i = ADC_Read(0); // Read the analog value
IntToStr(i, text);
UART1_Write_Text("Analog Value:");
UART1_Write_Text(text);
delay_ms(100);
UART1_Write_Text(" ");
delay_ms(500);
}
}
Mickro C code explain:
Here is the simple code. We write the code in Mikro C pro for pic compiler. The mikroC PRO for PIC is a powerful, feature-rich development tool for PIC microcontrollers.
Here's the simple code. Frist we included ADC , Conversion, C_String and UART library by check these boxs. In declaration section of the program we declared a unsigned integer i which store the analog value. We declared a character array for show the data in the serial monitor.
In the void main section of program we have initialized ADC module and then we have nitialize UART module at 9600 kbps. Refer to the device data sheet for baud rates allowed for specific Fosc.
In the loop section we called the ADC_Read(0) function to read the analog value form analog channel 0 . The analog channel 0 that means the RA0 pin of microcontroller is analog input. The A/D conversion of analog signal result in a corresponding 10 bit digital number . Next we are transmited this analog value by UART1_Write_Text() function.
Let's looked the practical example of this code.
Second Example code:
unsigned int i=0;
float volt = 0;
char text[10];
void main() {
Adc_Init() ; // Initialize the Adc module
UART1_Init(9600); // Initialize the USART module at 9600kps boud rate
while(1){
i = ADC_Read(0); // Read the analog value
volt = ( i * 5.00 ) / 1023;
FloatToStr(volt, text);
UART1_Write_Text("Voltage:");
UART1_Write_Text(text);
delay_ms(100);
UART1_Write_Text(" ");
delay_ms(500);
}
}
Here's the second example code. We've made some changes to the previous code.
In define section of the program we declared a float variable which store the float voltage. Here we have done some mathematical calculations which converts analog values to digital voltage. Next we converted the float voltage to string and then we send the serial monitor.
Again, let's looked the practical example of this code.
This is the second example . Here we are noticed the digital voltage in the serial monitor. if the voltage less than 0 volt then the voltage shows Euler's number.
Click the download button for source code.
No comments