微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 用PIC低档单片机模拟串口通讯.

用PIC低档单片机模拟串口通讯.

时间:11-19 来源:互联网 点击:
串口通讯一般需要单片机带串口通讯接口,而此类单片机一般较贵,可否用普通单片机做串口通讯?

答案是肯定的,但是,通常用于发送数据,接收数据相对就麻烦一些.

串口通讯的原理我就不讲了,各位可以去查询相关的书籍,以下是我做的用于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 ; processor specific variable definitions
__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 H0000
f EQU H0001
Z EQU H0002
C EQU H0000
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 B00111110 ;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 D10
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 D14 ; 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
;* ;* PLAY/PAUSE 0x55 0x03 0x02 0x00 0x01 0xFA
;* (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 D10 ;send 10 stop bits(500us)??
movwf bitcnt ;1 start bit +8 data bits+1 stop bit=10 bits
call iputchar2 ;alternate entry doesnt 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 D14 ;1 startbit+8 data bits+1 stopbit+4 extra stop bits.
movwf bitcnt
call iputchar0

bcf GPIO,Tx ;bring back down to 0

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top