当前位置:文档之家› MPLAB XC8写的串口收发和中断实例

MPLAB XC8写的串口收发和中断实例

/*=====================


Configuration setting
Abbreviations and struction defination
Firmware revision :V01
Issue date:2014-07-09
=======================*/

#include
#include
#include"board_xc8.h"

#include
#include

#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;//30ms
TMR0H=0x8a;
T1count++;
tim1flag=1;
if(T1count>50){T1count=0;//1500ms
PORTLED1=~PORTLED1;
}

INTCONbits.TMR0IF=0;
}

if (PIR1bits.RC1IF==1){

//PIR1bits.RC1IF==0;
usart_buf[usart_cnt]=RCREG1;
if(usart_cnt==0)
{
if(usart_buf[0]==0xF4)
usart_cnt++;
}
else if(usart_cnt==1)
{
if(usart_buf[1]==0x06)
usart_cnt++;
else
usart_cnt=0; //prepare for next step receive
}
else if(usart_cnt==9)
{

if(usart_buf[2]==0xaa){//接收的数据暂存
usart_temp3[3]=usart_buf[3];
usart_temp3[4]=usart_buf[4];
usart_temp3[5]=usart_buf[5];
usart_temp3[6]=usart_buf[6];
usart_temp3[7]=usart_buf[7];
usart_temp3[7]=0;
}
if(usart_buf[2]==0xbb)

{
usart_temp4[3]=usart_buf[3];
usart_temp4[4]=usart_buf[4];

tempval=usart_buf[3];
tempval=tempval<<8|usart_temp[4];//overflow!
//Nop();

}
if(usart_buf[2]==0xcc)
for(i=3;i<10;i++)usart_temp7[i]=usart_buf[i];


usart_cnt=0;
}
else usart_cnt++;
PIR1bits.RC1IF==0;
}


}




void delayms(void)
{
TMR1IE=0;
TMR1IF=0;
TMR1L=0xcf;
TMR1H=0x8a;
T1CON=0B00000101;
while(!TMR1IF);
T1CONbits.TMR1ON=0;
}

void sys_init(void)
{


RCON=0x80;//0x80;IPEN=1 interrupt priority enble,
IPR1bits.RCIP=0;//
IPR1bits.TMR2IP=0;
IPR3bits.RC2IP=1;//1 0610 rx2 ok;


INTCON2=0x00;//PORTB pull up enble,INT0 F


PIE3bits.RC2IE=1;//
DIRECTIONLED1=0;
DIRECTIONLED2=0;
DIRECTIONLED3=0;
TRISB=0X03;


PORTLED1=1;
PORTLED2=1;
PORTLED3=1;
INTCON=0b11010000;//INT0 IE=1
TMR0L=0xcf;//30ms
TMR0H=0x8a;
T0CON=0b00001000;
T0CONbits.TMR0ON=0;
}

void USART_init(void)
{
TRISCbits.RC7=1;//
TRISCbits.RC6=0;

TRISGbits.RG1=0;//
TRISGbits.RG2=1;

SPBRG1=12;//baud rate 19200 at BRGH=1
TXSTA1=0x04;//0B0000 0100 set SYNC=0 BRGH=1(high speed),0b00100100
PIE1bits.RC1IE=1;
RCSTA1=0B10010000;//0x90
//enable usart receive interrupt

SPBRG2=25;//9600
RCSTA2=0B10010000;//0x90
TXSTA2=0b00100110;
}
void main(void)
{char c;


sys_init();
USART_init();


while(1){

pakge[2]=dis[0]<<4|dis[1];//数据打包为上传准备
pakge[3]=dis[2]<<4|dis[3];
pakge[4]=dis[4]<<4&0xf0;

pakge[5]=usart_temp3[3];


pakge[6]=usart_temp3[4];
pakge[7]=usart_temp3[5];
pakge[8]=usart_temp3[6];

RCSTA1bits.CREN1=0;//关rx1

TXSTA2bits.TXEN2=1;//开tx2上传数据
for(c=0;c<9;c++){
TXREG2=pakge[c];

while(TXSTA2bits.TRMT==FALSE);
PIR3bits.TX2IF=0;
}
pakge1[5]=0;
tempval/=1000;
pakge1[6]= tempval;
for(c=0;c<9;c++){
TXREG2=pakge1[c];

while(TXSTA2bits.TRMT==FALSE);
PIR3bits.TX2IF=0;
}
PIR3bits.TX2IF=0;
TXSTA2bits.TXEN2=0;//上传结束,关tx2
RCSTA1bits.CREN1=1;//1开rx1
PORTLED2=~PORTLED2;


delayms();//30ms
//Nop();
}
}




相关主题
文本预览
相关文档 最新文档