PIC16F877A 串口使用 屏幕回显
扫描二维码
随时随地手机看文章
实现串口输出,以及将接收到的信息发送回。
利用PC机串口测试,正常。
kit3、MPLAB X IDE Version 1.85;target PIC16F877A最小系统。
#include
#define uchar unsigned char
#define uint unsigned int
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//main.c
void uart_init(void);
void uart_send_byte(char uart_data);
char uart_receive_byte(void);
void delay(void);
/***********************************************/
char uart_current_data;
char uart_last_data;
char evm_info[]="Welcom to use PIC16F877-EVM-BOARD-V1.0";
/*********************************************************************/
void main(void)
{
unsigned char i,j;
TRISB = 0x00+(0x01<<5); //初始化端口B,RB5为输入口,其余为输出口
TRISC = 0x80; // bit7 rx input bit6 tx output初始化端口C的方向控制寄存器(
//对应的比特为0是输出口,比特为1输入口)
TRISD = 0x00; //初始化D口
TRISE = 0x00; //初始化E口
ADCON1= 0x06; //端口都为数字口
TRISA = 0x00; //设置A口均为输出口
OPTION_REG &= (~(0x01<<7)); //
PORTA = 0xff; //初始化端口寄存器
PORTB = 0xff&(~(0x01<<5));
PORTC = 0xff;
PORTD = 0xff;
PORTE = 0xff;
uart_init(); //初始化PIC16F877串行通讯接口
for(i=0;i<38;i++)
{
uart_send_byte(evm_info[i]);
}
uart_send_byte(0x0d); //将回车键第一字符给PC
uart_send_byte(0x0a); //将回车键第二字符给PC
for(;;) //程序主循环
{
uart_current_data=uart_receive_byte(); //接收一个字符,没有收到字符,返回0x00
if( uart_current_data!=0x00 && uart_current_data!=0x0d &&
uart_current_data!=0x0a)
{
uart_send_byte(uart_current_data); //将字符发回PC
uart_last_data=uart_current_data; //暂存当前的字符
}
else if(uart_current_data==0x0d) //回车键字符判断?
{
uart_last_data=uart_current_data; //暂存回车键字符第一字符
}
else if(uart_current_data==0x0a && uart_last_data==0x0d)//收到了正
//确的回车键?
{
uart_send_byte(0x0d); //将回车键第一字符给PC
uart_send_byte(0x0a); //将回车键第二字符给PC
uart_last_data=0x00;
}
}
}
/************************************************/
void delay(void)
{
unsigned int i;
for(i=0;i<500;i++);
}
void interrupt isr_service(void) //中断服务程序
{
if(T0IE && T0IF)
{
T0IF = 0; //在此加入TMR0中断服务
}
if (TMR1IE && TMR1IF) //判TMR1中断
{
TMR1IF=0; //在此加入TMR1中断服务
}
} //中断结束并返回
/************************************************/
void uart_init(void)
{
SPBRG=0x4d; //设置通讯波特率为9600/秒
SPEN=1; //1 = Serial port enabled (configures RC7/RX/DT and
// RC6/TX/CK pins as serial port pins)
RX9=0; //0 = Selects 8-bit reception
SYNC=0; //Asynchronous mode
BRGH=1; //1 = High speed
TX9=0; //0 = Selects 8-bit transmission
TXEN=1; //1 = Transmit enabled
CREN=1;
}
/***********************************************/
void uart_send_byte(char uart_data)
{
TXREG=uart_data;
while(1)
{ //等待发送完毕
if(TRMT==1) break ; // 使用if(TXIF == 1) break 时,会导致发送数据的丢失
}
}
/***********************************************/
char uart_receive_byte(void)
{
char result=0;
if(RCIF==1) //判是否收到了数据?
{
result=RCREG; //读取接收到的数据
}
return(result);
}
/***********************************************/