新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 用PIC低檔單片機模擬串口通訊.

        用PIC低檔單片機模擬串口通訊.

        作者: 時間:2016-11-19 來源:網絡 收藏
        串口通訊一般需要單片機帶串口通訊接口,而此類單片機一般較貴,可否用普通單片機做串口通訊?

        答案是肯定的,但是,通常用于發送數據,接收數據相對就麻煩一些.

        本文引用地址:http://www.104case.com/article/201611/318299.htm

        串口通訊的原理我就不講了,各位可以去查詢相關的書籍,以下是我做的用于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

        movlw D14 ;
        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 D255 ;
        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



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 鄂托克旗| 江永县| 老河口市| 若尔盖县| 澄江县| 赫章县| 诏安县| 习水县| 西乌珠穆沁旗| 新河县| 克什克腾旗| 麻城市| 利川市| 佛冈县| 德阳市| 会东县| 白朗县| 玉林市| 双牌县| 兴国县| 肃南| 昌江| 镇远县| 休宁县| 海晏县| 遂平县| 乡宁县| 陆河县| 金寨县| 永善县| 阿鲁科尔沁旗| 东丰县| 白玉县| 容城县| 宿松县| 洪湖市| 韶山市| 乡城县| 张家界市| 兰州市| 南皮县|