|
发表于 2018-7-7 11:08:34
|
显示全部楼层
本帖最后由 3DA502 于 2018-7-7 11:19 编辑
我写比lz的更简单
货真价实不到20行
- void tm0() interrupt 1 using 1
- {
- static WORD hwWaiteCnt;
- static WORD hwTicCnt;
- static WORD hwTicStampPosedge ;
- WORD hwTicStampNegedge;
- WORD hwTicInc;
-
- static BYTE streamBuf;
-
-
- if (--TCNT == 0)
- {
- TCNT = 3; //reset send baudrate counter
- if (TING) //judge whether sending
- {
- if (TBIT == 0) {
- PinTXB = 0; //send start bit
- TDAT = TBUF; //load data from TBUF to TDAT
- TBIT = 9; //initial send bit number (8 data bits + 1 stop bit)
- } else {
- TDAT >>= 1; //shift data to CY
- if (--TBIT == 0) {
- PinTXB = 1;
- TING = 0; //stop send
- TEND = 1; //set send completed flag
- } else {
- PinTXB = CY; //write CY to TX port
- }
- }
- }
- }
-
-
- if(STATE_BLAND == state.stateNum){ /* stop test and waite a time*/
- if(hwWaiteCnt > 0){
- hwWaiteCnt -= 1;
- return;
- }else{
- hwWaiteCnt = BLAND_TICK;
- state.stateNum = STATE_WAITE4_SYNC ;
- }
- }
-
- streamBuf <<= 1;
- streamBuf |= PinRF; /* bit stream buff*/
-
- hwTicCnt += 1; /* tick increase */
-
- if(STATE_WAITE4_SYNC == state.stateNum){
- if(streamBuf == 0){
- state.syncLowCnt += 1; /* if low , keep count*/
- if(state.syncLowCnt > SYNC_LENGTH){ /* if get sync signal*/
-
- state.rcvBufIdex = 0; /* reset the buffer index*/
-
- state.stateNum = STATE_SAMPLE_START;
- rcvID.rcvIDlong =0;
- }
- }else{
- state.syncLowCnt = 0; /* reset , if any pulse */
- }
- }
-
- if(STATE_SAMPLE_START == state.stateNum){
- if( streamBuf == 0x01 ){ /* rising edge*/
- hwTicStampPosedge = hwTicCnt;/* sample ticks1*/
- }
- if( streamBuf == 0xfe ){ /* rising edge*/
- hwTicStampNegedge = hwTicCnt;/* sample ticks2*/
- // if(hwTicStampNegedge > hwTicStampPosedge){
- hwTicInc = hwTicStampNegedge - hwTicStampPosedge; /* ignoi a little bug*/
- //}
- if(hwTicInc > 0xb0){
- state.rcvBuf[state.rcvBufIdex] = '1';
- }else{
- state.rcvBuf[state.rcvBufIdex] = '0';
- }
-
- if(hwTicInc > 0xb0){
- rcvID.rcvIDlong += 1;
- }
- rcvID.rcvIDlong <<= 1;
-
- state.rcvBufIdex += 1; /* index ++*/
- if(state.rcvBufIdex >= 23){
- state.rcvBufIdex = 0;
- state.stateNum = STATE_SEND;
- }
- }
- }
- }
-
复制代码
|
|