- 9
- 0
- 约4.73千字
- 约 10页
- 2017-12-19 发布于河南
- 举报
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=0xc
原创力文档

文档评论(0)