How to Program USART Receiver in PIC16F877A
The following code demonstrate, how to write a program to initialize serial port at 9600 baud-rate and receive character from serial port of computer. The data is displayed on virtual terminal. This example shows only code demonstration, for real hardware you must have to use level converter IC (MUX232) for proper data receive on computer hyper-terminal.
The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.
Code in mikroC
// direction signal
sbit LED_dir at TRISB.B1;
// bit labels portb
sbit LED at PORTB.B1;
void main(void)
{
char intro_str[] = “Ilmaan Technology | eteched.com\n\r”;
char info_str[] = “Please type from keyboad in transmitter window\n\r”;
char receive = ‘\0’;
// set direction as output
LED_dir = 0;
// init LED
LED = 0;
// initialize hardware UART1
// establish communication at 9600 bps
UART1_Init(9600);
// transmit intro string
UART1_Write_Text(intro_str);
// transmit info string
UART1_Write_Text(info_str);
while(1)
{
LED = ~LED;
// If data is ready, read it:
if (UART1_Data_Ready() == 1) {
receive = UART1_Read();
UART1_Write(receive);
}
}
}
sbit LED_dir at TRISB.B1;
// bit labels portb
sbit LED at PORTB.B1;
void main(void)
{
char intro_str[] = “Ilmaan Technology | eteched.com\n\r”;
char info_str[] = “Please type from keyboad in transmitter window\n\r”;
char receive = ‘\0’;
// set direction as output
LED_dir = 0;
// init LED
LED = 0;
// initialize hardware UART1
// establish communication at 9600 bps
UART1_Init(9600);
// transmit intro string
UART1_Write_Text(intro_str);
// transmit info string
UART1_Write_Text(info_str);
while(1)
{
LED = ~LED;
// If data is ready, read it:
if (UART1_Data_Ready() == 1) {
receive = UART1_Read();
UART1_Write(receive);
}
}
}
Download Files
For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.
Comments
Post a Comment