用PIC低档单片机模拟串口通讯.
2016-06-02 来源:eefocus
答案是肯定的,但是,通常用于发送数据,接收数据相对就麻烦一些.
串口通讯的原理我就不讲了,各位可以去查询相关的书籍,以下是我做的用于IPOD控制的串口通讯程序.是用普通的PIC12F508单片机实现的.由于我比较穷,没有买IPOD,所以程序是否能正常运作并没有实践过,在此声明.此程序只做抛砖引玉之用.
;IPOD REMOTE CONTROL PORGRAM
; vcc +----V-----+ groud
; PLAY/PAUSE | GP0|TxD--->IPOD
; VOL+ | GP1|FORWARD>>
; VOL- | GP2|REVERSE<<
; +----------+
; 4M InterOTC 1us/Instruction
; 9600bps 104us/bit so 104 cycles/bit only for G2 Spec
; Rate is 19200 baud (52 cycles /bit @ 4M) ||| for G3 Spec
; 8N1 DATA TRANS FORMAT 0 <--------> 1 (Startbit:0 + 1byte data+Stop bit:1)
; Author: Deng Chunlin.by 2006-3-23. mailto: denjackson@163.com
;*
list p=12f508 ; list directive to define processor
#include
__CONFIG _CP_ON & _WDT_OFF & _MCLRE_OFF & _IntRC_OSC
;*
;define varibal and macro
;
GPI0 EQU 6
TIMER0 EQU 1
STATUS EQU 3
OSCCAL EQU 5
w EQU H'0000'
f EQU H'0001'
Z EQU H'0002'
C EQU H'0000'
Tx EQU 0
Fw EQU 1
Rw EQU 2
Vd EQU 3
Vp EQU 4
Pa EQU 5
Txbyte EQU 7H ;Trans data register.
bitcnt EQU 8H ;Trans bit counts
temp EQU 9H ; delay temp varibal
temp2 EQU 10H ; delay temp varial2
Txipod1 EQU 11H ; command temp1
Txipod2 EQU 12H ; command temp2
;
ORG 00H
NOP
movwf OSCCAL ;Enter Inter RC OSC Fix tax.
MOVLW 07H ;1:256 set to Timer0
OPTION
MOVLW B'00111110' ;GP0 OUT,Other ports In
TRIS 6
; bcf GPIO,Tx
goto main
;SUB Program Area*
;*delay sub program*
;* delay sub program.
; Input register:temp
; cycles=3*x+2+2+2=3x+6.
;
UART_delay
decfsz temp,f ;1/2 cycle
goto UART_delay ;2 cycles
retlw 0 ;2 cycles
;ipod putchar*
;* ipod putchar
;* This subroutine transmits the byte stored in the 'TXbyte' register
;*xxxx Rate is 9600 baud (=104 cycles/bit @ 4MHz) !!! from g2 spec!!
;* Rate is 19200 baud (52 cycles /bit @ 4M) ||| for G3 Spec
;* input: TXbyte
;* registers: bitcnt, TXbyte, temp
;* stack: one level (UART_delay)
;
iputchar
movlw D'10'
movwf bitcnt
iputchar0
comf Txbyte,f
setc ;Set C=1
iputchar1 ; --- ---
skpc ; if C=1 then jump else c=0 then jump to iputchar2. 2 1
goto iputchar2 ; 2
bcf GPIO,Tx ; Send a '0' 1
goto iputchar3 ; 2
iputchar2
bsf GPIO,Tx ; Send a '1' 1
nop ; 1
iputchar3
movlw D'14' ; 1 1
movwf temp ; 1 1
;---------------------------------------------------------------------------------
il decfsz temp,f ;1/2
goto il ;2
; 42 42
;---------------------------------------------------------------------------------
rrf Txbyte,f ; >> right move a bit. f(0) to c. 1 1
decfsz bitcnt,f ; 10 bits all are be sended? 1 1
goto iputchar1 ;not, go on to send left bits. 2 2
retlw 0
;*ipod_packet*
;* G3 iPod output routine - ipod_packet
;*
;* Format: output normally low
;* | output high, 500 uSec
;* | output message @ 8N1, 9600 baud, lsb first
;* | output high 4 extra bits
;* loop| low for ~11.16 mSec
;*
;* VOL+ 0x55 0x03 0x02 0x00 0x02 0xE9
;* VOL- 0x55 0x03 0x02 0x00 0x04 0xF7
;* SKIP>> 0x55 0x03 0x02 0x00 0x10 0xEB
;* <
;* (button release) 0x55 0x03 0x02 0x00 0x00 0xFB
;* Txipod1 Txipod2
;* input: TXipod1-2 has these bytes --------^^ ^^
;* stack: uses 2 stack locations (iputchar > UARTdelay)
;
ipod_packet
movlw 0 ;Stop bit.(inverted from 1 to 0)
movwf Txbyte
movlw D'10' ;send 10 stop bits(500us)??
movwf bitcnt ;1 start bit +8 data bits+1 stop bit=10 bits
call iputchar2 ;alternate entry doesn't have start bit.
movlw 055H
movwf Txbyte
call iputchar
movlw 03H
movwf Txbyte
call iputchar
movlw 02H
movwf Txbyte
call iputchar
movlw 0
movwf Txbyte
call iputchar
movf Txipod1,w
movwf Txbyte
call iputchar
movf Txipod2,w
movwf Txbyte
movlw D'14' ;1 startbit+8 data bits+1 stopbit+4 extra stop bits.
movwf bitcnt
call iputchar0
bcf GPIO,Tx ;bring back down to 0
movlw D'14' ;
movwf temp2 ;
movlw 0 ; 0-1,will be overflow to 255.
movwf temp ; (774+3)*14~=11ms
lp1 ; delay about 11ms?
call UART_delay ; 256*3+6=774 clock.
decfsz temp2,f ;
goto lp1 ;
retlw 0
;*
delay
clrf TIMER0
d1s MOVLW D'255' ;
SUBWF TIMER0,w ;TIMER0
BTFSS STATUS,Z ;
GOTO d1s
retlw 0
;main loop
;
;
;
main
call delay
btfss GPIO,Fw
goto fwd
nop
nop
btfss GPIO,Rw
goto rev
nop
nop
btfss GPIO,Vd
goto vdo
nop
nop
btfss GPIO,Vp
goto vpl
nop
nop
btfss GPIO,Pa
goto pypa
nop
goto main
fwd
movlw 010H
movwf Txipod1
movlw 0EBH
movwf Txipod2
call ipod_packet
btfss GPIO,Fw
goto fwd
goto rel ;goto release....
rev
movlw 08H
movwf Txipod1
movlw 0F3H
movwf Txipod2
call ipod_packet
btfss GPIO,Rw
goto rev
goto rel ;goto release....
vdo
movlw 04H
movwf Txipod1
movlw 0F7H
movwf Txipod2
call ipod_packet
btfss GPIO,Vd
goto vdo
goto rel ;goto release....
vpl
movlw 02H
movwf Txipod1
movlw 0E9H
movwf Txipod2
call ipod_packet
btfss GPIO,Vp
goto vpl
goto rel ;goto release....
pypa
movlw 01H
movwf Txipod1
movlw 0FAH
movwf Txipod2
call ipod_packet
btfss GPIO,Pa
goto pypa
goto rel ;goto release....
rel
movlw 0
movwf Txipod1
movlw 0FBH
movwf Txipod2
call ipod_packet
goto main
end