2 再谈PIC单片机发送
原理是接收的逆过程
例程如下(用来发6221;分频比为256)
- ;///////////////////////////////////////////////////////////////////////////////////////////////
- READDIGT:
- MOVF SENDLOOP,0
- ADDWF PC,1
- GOTO SENDC4
- GOTO SENDC3
- GOTO SENDC2
- GOTO SENDC1
- GOTO SENDC0
- ;///////////////////////////////////
- SENDC0:
- MOVF C4COUNT,0;;读要发的数据(假设要发5个字)
- RETURN
- SENDC1:
- MOVF C3COUNT,0
- RETURN
- SENDC2:
- MOVF C2COUNT,0
- RETURN
- SENDC3:
- MOVF C1COUNT,0
- RETURN
- SENDC4:
- MOVF C0COUNT,0
- RETURN
- ;///////////////////////////////////
- SENDBIT:
- CLRF TIME
- BCF PORTB,1
- SENDBIT1:
- CLRWDT
- MOVLW .35
- SUBWF TIME,0
- BTFSS STATUS,2
- GOTO SENDBIT1
- CLRF TIME
- BSF PORTB,1
- SENDBIT2:
- CLRWDT
- MOVLW .18
- SUBWF TIME,0
- BTFSS STATUS,2
- GOTO SENDBIT2
- CLRF SENDLOOP ;码头数据发送
- //////////////////////////////////////////////
- SENDBIT3:
- CLRWDT
- BCF INTCON,7
- CALL READDIGT
- MOVWF SENDCOUNT
- BSF INTCON,7
- CALL SENDDIGT
- INCF SENDLOOP,1
- MOVLW .5
- SUBWF SENDLOOP,0
- BTFSS STATUS,2
- GOTO SENDBIT3
- CLRF TIME
- BCF PORTB,1
- SENDDIGT5:
- CLRWDT
- MOVLW .2;;加发一个结束位
- SUBWF TIME,0
- BTFSS STATUS,2
- GOTO SENDDIGT5
- BSF PORTB,1
- BSF STARTBIT
- RETURN
- ;/////////////////////////////////////////////////////////////////////////////////////////
- SENDDIGT:;;实现0和1的发送
- MOVLW .8
- MOVWF LOOPSENDCOUNT
- SENDDIGTGO:
- BTFSS SENDCOUNT,7
- GOTO ZERSEND
- CLRF TIME
- SENDDIGT1:
- CLRWDT
- BCF PORTB,1
- MOVLW .2
- SUBWF TIME,0
- BTFSS STATUS,2
- GOTO SENDDIGT1
- CLRF TIME
- SENDDIGT2:
- CLRWDT
- BSF PORTB,1
- MOVLW .6
- SUBWF TIME,0
- BTFSS STATUS,2
- GOTO SENDDIGT2
- GOTO SENDOVER
- ZERSEND:
- CLRF TIME
- SENDDIGT3:
- CLRWDT
- BCF PORTB,1
- MOVLW .2
- SUBWF TIME,0
- BTFSS STATUS,2
- GOTO SENDDIGT3
- CLRF TIME
- SENDDIGT4:
- CLRWDT
- BSF PORTB,1
- MOVLW .2
- SUBWF TIME,0
- BTFSS STATUS,2
- GOTO SENDDIGT4
- SENDOVER:
- RLF SENDCOUNT,1
- DECFSZ LOOPSENDCOUNT
- GOTO SENDDIGTGO
- RETURN
相应的C代码如下:(C5口是发送口)
- #include
- #include
- #include
- unsigned char dispcount[5];//要发送的码值
- unsigned char i,k,data,rfbit,zbit,rfgobit;
- #pragma interrupt_level 1
- interrupt adint(void)
- {
- if(TMR1IF==1)
- {
- TMR1IF=0;
- TMR1H=0b11111100;
- TMR1L=0b00010111;
- rfbit=rfbit+1;//发送指针加一
- send( );
- }
- }
- //////////////////////////////////////////
- send( )
- {
- if(rfgobit==0)
- {
- switch(rfbit)
- {
- case 1 :
- RC5=0;
- break;
- case 6 :
- RC5=1;
- break;
- case 7 :
- RC5=0;
- rfgobit=1;//制发送完标制
- rfbit=0;
- break;
- default :
- break;
- }
- }
- if(rfgobit==1)
- {
- zbit=dispcount[i]&0b00000001;
- switch(rfbit)
- {
- case 1:
- RC5=1;
- break;
- case 2 :
- if(zbit==0)
- {
- RC5=0;
- rfbit=0;
- rf( );
- }
- break;
- case 3 :
- if(zbit==1)
- {
- RC5=0;
- rfbit=0;
- rf( );
- }
- break;
- default :
- break;
- }
- }
- }
- //////////////////////////////////////////////
- rf( )
- {
- k=k+1;
- if(k==8)
- {
- i=i+1;
- k=0;
- if(i==6)
- {
- i=1;
- rfgobit=0;
- TMR1H=0;
- TMR1L=0;
- RC5=1;
- dispcount[1]=0xaa;
- dispcount[2]=0xbb;
- dispcount[3]=0xcc;
- dispcount[4]=0xdd;
- dispcount[5]=0xee;
- }
- return;
- }
- dispcount[i]=dispcount[i]>>1;
- }
- //////////////////////////////////////
- main( )
- {
- di( );
- TRISC=0b11011111;
- TRISA=0b111111;
- TRISB=0b11111111;
- i=1;
- k=0;
- TMR1H=0b11111100;
- TMR1L=0b00010111;
- T1CON=1;
- TMR1IE=1;
- RC5=1;
- PEIE=1;
- ei( );
- dispcount[1]=0xaa;
- dispcount[2]=0xbb;
- dispcount[3]=0xcc;
- dispcount[4]=0xdd;
- dispcount[5]=0xee;
- while( 1 )
- {
- ;
- }
- }
这两个程序只是一个演示例程
在实际运用中可变通的把这种发送与接受用于两机之间的通讯(好处是移植性好,抗干扰好且一发一收只要两根线或一根线)另外如要产生载波(38KHZ)的话可用其输出口去调制38KHZ发生源(比如555电路,PWM)来得到,或干脆用软件来实现(不过难度较大;不如用带38KHZ的单片机来做)