📄 ir.c
字号:
#include "ir.h" // infrared utility header file#include "timer.h" // timer utility header file#include "sfr62p.h" // SFR definition of M16C/62P#define IFR_BAUD 16 // the times to control sending datastatic char cSending = 0; // flag for the character is sendingstatic char cSendBits = 0; // bit countstatic char CanRec = 0; // flag for a character is receivedstatic char cSendTimerCount = 0; // count of times of SendTimer() executedstatic char cRevTimerCount = 0; // count of times of RecTimer() executedstatic char cReving = 0; // flag for the character is receivingstatic char cStartBit = 0; // flag for the start bit is receivedstatic char cRevBits = 0; // count of bits receivedstatic unsigned char ucRecBuf = 0; // receive bufferstatic unsigned char ucSendBuf = 0; // send buffer/*""FUNC COMMENT""************************************************************** ID : ---* Function name : void InitIR(void)* Function : initialize infrared input and output* Paramenter : none* Return : none* Function used : InitTA0() : Initialize TA0* : InitTA3() : Initialize TA3* Notice : none* History : ---*""FUNC COMMENT END""*********************************************************/void InitIR(void){ pd7_6 = 1; // set P7_6 (infrared output) as output p7_6 = 0; // initialize P7_6 pd9_0 = 0; // set P9_0 (infrared input)as input InitTA0(); // initialize TA0 InitTA3(); // initialize TA3}/*""FUNC COMMENT""************************************************************** ID : ---* Function name : void Busy(void)* Function : to show the period of sending* Paramenter : none* Return : char : the flag for the character is sending* Function used : none* Notice : none* History : ---*""FUNC COMMENT END""*********************************************************/char Busy(void){ return cSending; // return the flag for the character is sending}/*""FUNC COMMENT""************************************************************** ID : ---* Function name : void CanReceive(void)* Function : to show if a character is received* Paramenter : none* Return : char : the flag for a character is received* Function used : none* Notice : none* History : ---*""FUNC COMMENT END""*********************************************************/char CanReceive(void){ return CanRec; // return the flag for a character is received}/*""FUNC COMMENT""************************************************************** ID : ---* Function name : void CanReceive(void)* Function : send a character* Paramenter : unsigned char cSend : the character to be sent* Return : char : process result* Function used : none* Notice : none* History : ---*""FUNC COMMENT END""*********************************************************/char Send(unsigned char cSend){ if(cSending) // if there is a character sending, return return 0; asm("FCLR I"); // disable interrupt cSendTimerCount = 0; // clear sending times ucSendBuf = cSend; // set send buffer cSending = 1; // set sending flag cSendBits = 0; // clear bit sent asm("FSET I"); // enable interrupt return 1;}/*""FUNC COMMENT""************************************************************** ID : ---* Function name : void SendTimer(void)* Function : when the function is executed 16 times, 1 bit is sent* Paramenter : none* Return : none* Function used : none* Notice : none* History : ---*""FUNC COMMENT END""*********************************************************/void SendTimer(void){ if(!cSending) // if there is not character sending, return return; if(cSendBits == 0) // send the start bit { ta3s = 1; // start TA3 // if the function is executed 16 times, the bit is sent. Sending 1 bit need 1 / 1200 (s), // the function is executed every 1 / 19200 (s) if(cSendTimerCount >= IFR_BAUD) { cSendTimerCount = 0; // clear count of times of SendTimer() executed cSendBits ++; // send the next bit } } else if(cSendBits == 9) // send the stop bit { ta3s = 0; // disable TA3 if(cSendTimerCount >= IFR_BAUD*2) // the stop bit is sent twice { cSendTimerCount = 0; // clear count of times of SendTimer() executed cSendBits = 0; // clear the bit count cSending = 0; // clear the sending flag } } else // send the data bit { if(cSendTimerCount == 1) // the first time to send data bit { if((ucSendBuf >> (cSendBits-1)) & 0x01) // check the data bit { ta3s = 0; // if 1, not send } else { ta3s = 1; // if 0, send } } if(cSendTimerCount >= IFR_BAUD) // a bit is sent { cSendTimerCount = 0; // clear count of times of SendTimer() executed cSendBits ++; // clear bit count } } cSendTimerCount ++; // increase count of times of SendTimer() executed}/*""FUNC COMMENT""************************************************************** ID : ---* Function name : void Receive(void)* Function : receive a character* Paramenter : none* Return : unsigned char : the data received* Function used : none* Notice : none* History : ---*""FUNC COMMENT END""*********************************************************/unsigned char Receive(void){ CanRec = 0; // clear the flag for a character is received return ucRecBuf; // return the character received}/*""FUNC COMMENT""************************************************************** ID : ---* Function name : void RecTimer(void)* Function : when the function is executed 16 times, 1 bit is received* Paramenter : none* Return : none* Function used : none* Notice : none* History : ---*""FUNC COMMENT END""*********************************************************/void RecTimer(void){ static revdata = 0; // temperary data for receiving data if(!cStartBit) // the start bit is not received { if(cReving==0 && !p9_0) // some signal is received { cReving = 1; // set the flag for the character is receiving cRevTimerCount = 0; // clear the count of times of RecTimer() executed } if(cReving==1 && cRevTimerCount>=IFR_BAUD/2) // sample the data at the center of receiving time { if(!p9_0) // signal is received { cStartBit = 1; // the start bit is received cRevBits = 0; // set bit received as 0 revdata = 0; // clear the temperary data for receiving data cRevTimerCount = 0; // clear the count of times of RecTimer() executed } else // it is noise { cReving =0; // clear the flag for the character is receiving cRevTimerCount = 0; // clear the count of times of RecTimer() executed } } } else // the start bit is received { if (cRevTimerCount >= IFR_BAUD) // the time to sample data { unsigned char rev = p9_0; if(cRevBits < 8) // it is data bit { revdata += (rev << cRevBits); // save data bit cRevBits ++; // to receive the next bit } else // it is stop bit { if (p9_0) // the data is valid { CanRec = 1; // set the flag for a character is received ucRecBuf = revdata; // save the data received } cReving = 0; // clear the flag for the character is receiving cStartBit = 0; // clear the flag for start bit cRevBits = 0; // clear the count of bits received } cRevTimerCount = 0; // clear the count of times of RecTimer() executed } } cRevTimerCount ++; // increase the count of times of RecTimer() executed}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -