- 1、本文档共10页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
MPLAB XC8写串口收发及中断实例
MPLAB XC8写串口收发及中断实例
/*=====================
Configuration setting
Abbreviations and struction defination
Firmware revision :V01
Issue date:2014-07-09
=======================*/
#includexc.h
#includeusart.h
#includeboard_xc8.h
#include math.h
#include stdio.h
#pragma config WDT = OFF,WDTPS = 32,OSC = XT,OSCS = OFF,PWRT = OFF,BOR=OFF,BORV=42
#pragma config CCP2MUX = OFF,STVR = OFF,LVP = OFF,CP0 = OFF
#pragma config CP1 = ON,CP2 = ON,CP3 = ON,CPB = ON,CPD = ON,WRT0 = ON
#pragma config WRT1 = ON,WRT2 = ON,WRT3 = ON,WRTB = ON,WRTC = ON
#pragma config WRTD = ON,EBTR0 = ON,EBTR1 = ON,EBTR2 = ON,EBTR3 = ON,EBTRB = ON
unsigned int T2count=0,T1count=0;
unsigned char dis[5]={0,0,0,0,0};
unsigned char pakge[9]={0xff,0x0d,0,0,0,0,0,0,0},pakge1[9]={0xff,0x0e,0,0,0,0,0,0,0};
uchar usart_buf[10]={0x00},rx2_buf[8]={0x00}; //usart buffer
uchar usart_temp3[10]={0x00},usart_temp4[10]={0x00},usart_temp7[10]={0x00}; //Temporarily store uasrt data to avoid data lose
//uchar usart_sum=0x00; //verified by ckeck sum
uchar usart_cnt=0,rx2_cnt=0,distancecount=0;//used to control and to count the number of received data
uchar usart_ready_flag=FALSE; //wh
char tim1flag;
unsigned int LED1count,tempval;
void interrupt high_isr (void)
{
if(INTCONbits.INT0IF){//.....//用户自定义的功能
INTCONbits.INT0IF=0;
}
if(PIR3bits.RC2IF){
rx2_buf[rx2_cnt]=RCREG2;
if(rx2_cnt==0)
{
if(rx2_buf[0]==0xFF)//收到有效的起始标志0xff,开始接收
rx2_cnt++;
}
else if(rx2_cnt==1)
{
if(rx2_buf[1]==0xdc)//判断第1位是有效的0xdc
rx2_cnt++;
else
rx2_cnt=0; //prepare for next step receive
}
else if(rx2_cnt==7){rx2_cnt=0;
dis[0]=rx2_buf[2];//接收上位机发来的初始设置
dis[1]=rx2_buf[3];
dis[2]=rx2_buf[4];
dis[3]=rx2_buf[5];
dis[4]=rx2_buf[6];
PORTLED3=~PORTLED3;
}
else rx2_cnt++;//只做0和1位的指令验证,
PIR3bits.RC2IF=0;
}
}
void interrupt low_priority low_isr (void)
{
if(PIR1bits.TMR2IF==1)
PIR1bits.TMR2IF=0;
if(INTCONbits.TMR0IF==1){
TMR0L=0xcf
文档评论(0)