以下程序全部放在125US中断里面
以下是发送端程序
#include "USER_PROGRAM.H" #define TX_IO _pa1 typedef struct { unsigned char b0 : 1; unsigned char b1 : 1; unsigned char b2 : 1; unsigned char b3 : 1; unsigned char b4 : 1; unsigned char b5 : 1; unsigned char b6 : 1; unsigned char b7 : 1; } _bits; _bits var_0; #define Read_Once var_0.b0 #define Send_One_Bit_Over var_0.b1 #define Start_Send var_0.b2 #define FLAG_10MS var_0.b3 #define KEY_PRESS var_0.b4 #define KEY_PRESS1 var_0.b5 #define KEY_PRESS2 var_0.b6 /*******************发送通讯变量*****************************/ unsigned char Tx_Step,Tx_Dat_Step; unsigned char Start_Count; //bit Read_Once,Send_One_Bit_Over; unsigned char Tx_Low_Count,Tx_High_Count,Send_Count; //bit Start_Send; unsigned char Tx_Data1; /*******************发送通讯变量*****************************/ //bit FLAG_10MS; unsigned char KEY_DATA_TEMP; //bit KEY_PRESS,KEY_PRESS1,KEY_PRESS2; void IO_INIT() { TX_IO=1; _pac=0x00; } void CLR_BIT() { Read_Once =0; Send_One_Bit_Over =0; Start_Send =0; FLAG_10MS =0; KEY_PRESS =0; KEY_PRESS1 =0; KEY_PRESS2 =0; } void TIMER_INIT() { _tmr=(256-125); //(256-125); _tmrc=0b10000100;//Ftmr=Fsys/16=1 MHz _tf=0; _te=1; _ton=1; _emi=1; } void DEAL_SCAN_KEY() { GET_KEY_BITMAP(); if(DATA_BUF[0]!=0) { if(!KEY_PRESS1) { KEY_PRESS1=1; KEY_PRESS=1; KEY_PRESS2=1; } } else { KEY_PRESS1=0; } if(DATA_BUF[0]&0x01) { KEY_DATA_TEMP|=0x01; } else { KEY_DATA_TEMP&=0xFE; } if(DATA_BUF[0]&0x02) { KEY_DATA_TEMP|=0x02; } else { KEY_DATA_TEMP&=0xFD; } if(DATA_BUF[0]&0x04) { KEY_DATA_TEMP|=0x04; } else { KEY_DATA_TEMP&=0xFB; } if(DATA_BUF[0]&0x08) { KEY_DATA_TEMP|=0x08; } else { KEY_DATA_TEMP&=0xF7; } if(DATA_BUF[0]&0x10) { KEY_DATA_TEMP|=0x10; } else { KEY_DATA_TEMP&=0xEF; } if(DATA_BUF[0]&0x20) { KEY_DATA_TEMP|=0x20; } else { KEY_DATA_TEMP&=0xDF; } } void TIMER_UP_DATA() { static unsigned char _500MS_COUNT; if(KEY_PRESS) { KEY_PRESS=0; _500MS_COUNT=50; } _500MS_COUNT++; if(_500MS_COUNT>=51) { if(!Start_Send) { _500MS_COUNT=0; Tx_Data1=KEY_DATA_TEMP; Start_Send=1; } } } void USER_PROGRAM_INITIAL() { CLR_BIT(); IO_INIT(); TIMER_INIT(); } void USER_PROGRAM() { if(FLAG_10MS) { FLAG_10MS=0; DEAL_SCAN_KEY(); TIMER_UP_DATA(); } } #pragma vector TIMER_ISR @ 0x0C void TIMER_ISR(void) { static unsigned char _1MS_COUNT; _tf=0; _1MS_COUNT++; if(_1MS_COUNT>=8) { _1MS_COUNT=0; FLAG_10MS=1; } if (Start_Send) //开始发送 { switch (Tx_Step) { case 0: ///////////////////////////蜂鸣器数据处理////////////////////////////// TX_IO=0; //拉低IO口 Start_Count++; if (Start_Count>=32) //72*125us 9ms { Start_Count=0; Tx_Step=1; } break; case 1: TX_IO=1; //拉高IO口 Start_Count++; if (Start_Count>=36) //36*125us 4.5ms { Start_Count=0; Tx_Step=2; } break; case 2: //开始发送数据 if (Tx_Data1&0x80) //先发高位 //如果为1 { if (Send_One_Bit_Over==0) { if (Tx_Low_Count<5) //先拉低0.56ms { Tx_Low_Count++; TX_IO=0; } if (Tx_Low_Count==5) { Tx_High_Count++; TX_IO=1; if (Tx_High_Count>=12) { Tx_Low_Count=0; Tx_High_Count=0; Send_One_Bit_Over=1; } } } } else //是0 { if (Send_One_Bit_Over==0) { if (Tx_Low_Count<5) //先拉低0.56ms { Tx_Low_Count++; TX_IO=0; } if (Tx_Low_Count==5) { Tx_High_Count++; TX_IO=1; if (Tx_High_Count>3) { Tx_Low_Count=0; Tx_High_Count=0; Send_One_Bit_Over=1; } } } } if (Send_One_Bit_Over) { Send_One_Bit_Over=0; Tx_Data1<<=1; Send_Count++; if (Send_Count>8) //发送完成了 { Send_Count=0; //清标志位 Read_Once=0; Start_Send=0; Tx_Step=0; TX_IO=1; } } break; default: break; } } }
以下是接收端程序
//////////接收变量 #define Rx_Read_Once var_0.b0 #define Rx_Read_Now var_0.b1 #define Rx_Change var_0.b2 #define Rx_Dat_Bit var_0.b3 #define Rx_Dat_Bit1 var_0.b4 #define Rx_Flag var_0.b5 unsigned char Rx_Step,Rx_Start_Count,Rx_Start_Count1,RX_Dat_Low_Count,RX_Dat_High_Count,RX_Count; unsigned char RX_Dat1,RX_Dat; //////////接收变量 /****************************用扫描的方式代替中断*********************************************/ if (Rx_Read_Once==0) //读一次现在的IO口状态 { Rx_Read_Once=1; Rx_Read_Now=GPIO_GetBit(GPIOC, PIN4); } if (Rx_Read_Now!=GPIO_GetBit(GPIOC, PIN4)) //IO口电平发生了变化 { Rx_Read_Once=0; Rx_Change=1; } switch (Rx_Step) { case 0: if (GPIO_GetBit(GPIOC, PIN4)==0) { Rx_Step=1; } Rx_Start_Count=0; break; case 1: if (GPIO_GetBit(GPIOC, PIN4)==0) { Rx_Start_Count++; } if (GPIO_GetBit(GPIOC, PIN4)==1) { if (Rx_Start_Count>=28&&Rx_Start_Count<=34) //9ms 9000/125=72 { Rx_Start_Count=0; Rx_Step=2; } else { Rx_Start_Count=0; Rx_Step=0; } } break; case 2: if (GPIO_GetBit(GPIOC, PIN4)==1) { Rx_Start_Count1++; } if (GPIO_GetBit(GPIOC, PIN4)==0) { if (Rx_Start_Count1>=30&&Rx_Start_Count1<=40) //4.5ms 4500/125=36 { Rx_Step=3; //UART0_SendByte(Rx_Start_Count1); Rx_Start_Count1=0; Rx_Change=0; Rx_Read_Once=0; } else { Rx_Start_Count1=0; Rx_Step=0; } } break; case 3: if (Rx_Dat_Bit==0) //数一下低电平的时间 { RX_Dat_Low_Count++; if (Rx_Change==1) //IO口由低变成高了 { Rx_Change=0; Rx_Read_Once=0; Rx_Dat_Bit=1; } } if (Rx_Dat_Bit==1) //数完低电平 数高电平 { RX_Dat_High_Count++; if (Rx_Change==1) //IO口由高变成低 { Rx_Change=0; Rx_Read_Once=0; Rx_Dat_Bit=0; Rx_Dat_Bit1=1; } } if (Rx_Dat_Bit1) { Rx_Dat_Bit1=0; RX_Dat1<<=1; if (RX_Dat_Low_Count>=3&&RX_Dat_High_Count>=11) //收到拉低0.5MS 拉高1.5 { RX_Dat=(RX_Dat1 |= 0x01); //收到1 } if (RX_Dat_Low_Count>=3&&RX_Dat_High_Count<=6&&RX_Dat_High_Count>=3) //收到拉低0.5MS 拉高0.5MS { RX_Dat=RX_Dat1; //收到0 //0 } RX_Dat_Low_Count=0; RX_Dat_High_Count=0; RX_Count++; if (RX_Count>=8) { RX_Count=0; RX_Dat=RX_Dat1; Rx_Step=0; Rx_Flag=1; RX_Dat1=0; } } break; default: break; } /*************************4P通讯接收END***************************************/