How to Program USART Transmitter in PIC16F877A
The following code demonstrate, how to write a program to initialize serial port at 9600 baud-rate and send continuous a constant string to 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.5.6.1” 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 _data[] = “Ilmaan Technology | eteched.com\n\r”;
int x;
// set direction as output
LED_dir = 0;
// init LED
LED = 0;
// initialize hardware UART1
// establish communication at 9600 bps
UART1_Init(9600);
while(1)
{
LED = ~LED;
// if the previous data has been shifted out, send next data:
if (UART1_Tx_Idle() == 1)
{
UART1_Write(_data[x++]);
// check _data[] index, when last char
// is transmitted, then reset ‘x’ value
if(x > strlen(_data))
x=0;
}
}
}
sbit LED_dir at TRISB.B1;
// bit labels portb
sbit LED at PORTB.B1;
void main(void)
{
char _data[] = “Ilmaan Technology | eteched.com\n\r”;
int x;
// set direction as output
LED_dir = 0;
// init LED
LED = 0;
// initialize hardware UART1
// establish communication at 9600 bps
UART1_Init(9600);
while(1)
{
LED = ~LED;
// if the previous data has been shifted out, send next data:
if (UART1_Tx_Idle() == 1)
{
UART1_Write(_data[x++]);
// check _data[] index, when last char
// is transmitted, then reset ‘x’ value
if(x > strlen(_data))
x=0;
}
}
}
Download Files
For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.
Comments
Post a Comment