自在自线亚洲а∨天堂在线-中文字幕一区视频播放-扒开双腿猛进入喷水高潮叫声-欧美日本亚洲一区二区-老熟妇高潮偷拍一区二区-国产精品高清一区二区不卡-午夜色福利视频一区二区三区-亚洲国产成人精品福利在线观看-亚洲欧美成人一区二区在线电影

機(jī)電之家資源網(wǎng)
單片機(jī)首頁(yè)|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開(kāi)發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開(kāi)發(fā)
培訓(xùn)信息
贊助商
用AVR單片機(jī)模擬的串口程序
用AVR單片機(jī)模擬的串口程序
 更新時(shí)間:2008-7-26 20:32:20  點(diǎn)擊數(shù):4
【字體: 字體顏色

 在一些應(yīng)用中 , 經(jīng)常要用到雙串口,但是一般單片機(jī)只提供一個(gè)串口,其實(shí)完全可以用普通I/O口模擬一個(gè)串口。以下的程序是我編寫(xiě)的模擬串口程序,程序中使用了單片機(jī)的定時(shí)器0,和INT0中斷。數(shù)據(jù)的發(fā)送和接收由中斷程序自動(dòng)進(jìn)行。程序已經(jīng)過(guò)AVR仿真器仿真和實(shí)際燒片使用,證明可靠。有一點(diǎn)需要說(shuō)明的是,此模擬的串口為半雙工方式。
    主程序中,單片機(jī)將標(biāo)準(zhǔn)串口設(shè)置為115200bps,將模擬串口設(shè)置為19200bps。單片機(jī)將標(biāo)準(zhǔn)串口收到的數(shù)據(jù)從模擬串口發(fā)送出去,將模擬串口接收到的數(shù)據(jù)從標(biāo)準(zhǔn)串口發(fā)送回來(lái)。

;**************************************************************************************************
;*    title:        half duplex uart simulaton program
;*    version:        1.0
;*    program time:    2001/11/05
;*    target:        AT90S8515
;*    design:        zsmbj@beijing
;**************************************************************************************************
.include "c:\program files\atmel\avr studio\appnotes\8515def.inc"
;BPS=19200
;F=11059200

.equ    N=72

.equ    txd0    =3        ;uart0 txd
.equ    rxd0    =2        ;uart0 rxd
;****************************************************************
    .equ    stack=0x0ff
;****************************************************************
;bit define
    .equ    rdr=0
    .equ    fe0=1
    .equ    td=6
    .equ    busy=7
;register define
    .def    temp=r16
    .def    sbuf0=r17
    .def    status=r18
    .def    bit_cnt=r19
;**************************************************************************************************
        .org    0x00
        rjmp    reset
        .org    0x01
        rjmp    int00
        .org    0x07
        rjmp    timer0_int
;**********************************************************
.cseg
;**********************************************************
;****initial
;**********************************************************
        .org    0x0010
;reset at90s8515
reset:
        ldi    temp,0b00001000
        out    ddrb,temp

        ldi    temp,high(stack)        ;stack
        out    sph,temp
        ldi    temp,low(stack)
        out    spl,temp

        ldi    temp,5                ;baud 115200bps at 11.0592M fosc
        out    ubrr,temp
        ldi    temp,0b00011000            ;enable rx and tx
        out    ucr,temp
;timer0 set
        ldi    temp,0x02            ;ck/8 0.72338us
        out    tccr0,temp

        ldi    temp,0x0a            ;disable outside sram,int0 fall edge make a interrupt
        out    mcucr,temp
        ldi    temp,0x40
        out    gimsk,temp            ;enable int0 and int1 interrupt

        ldi    temp,0
        MOV    status,temp
        sbi    portb,txd0            ;txd0 bit=1

        sei                    ;globle interrupt enable
        rjmp    main            
;******************************************    
timer0_int:
        push    temp
        in    temp,sreg
        push    temp
        
        ldi    temp,(256-N)
        out    TCNT0,temp
        inc    bit_cnt

        sbrs    status,td
        rjmp    timer0_receive
;>>>>>>>>>>>>>>>>>>>>>>>>>>
;send data 8 data bit and 1 stop bit
timer0_send:
        sbrc    bit_cnt,3            ;if bit_cnt=8 then stop bit
        rjmp    timer0_send_stop
timer0_send_data:
        sbrc    sbuf0,0                ;txd=0
        sbi    portb,txd0
        sbrs    sbuf0,0                ;txd=1
        cbi    portb,txd0
        lsr    sbuf0
        rjmp    timer0_end
timer0_send_stop:
        sbi    portb,txd0            ;stop bit=1
        sbrc    bit_cnt,0
        rjmp    timer0_complete            ;if bit_cnt=9 then complete
;;;;;;;;;;;;;;;;;;;
        in    temp,gifr
        sbr    temp,(1<<intf0)
        out    gifr,temp            ;clr int0 flag
        
        in    temp,gimsk
        sbr    temp,(1<<int0)
        out    gimsk,temp            ;enable gimsk/int0

        rjmp    timer0_end
;>>>>>>>>>>>>>>>>>>>>>>>>>>
;receive start 1bit data 8 bit stop 1bit
timer0_receive:
        cpi    bit_cnt,1            ;if bit_cnt=1 then start bit
        breq    timer0_receive_start
        cpi    bit_cnt,10            ;if bit_cnt=10 then stop bit
        breq    timer0_receive_stop
        
        rjmp    timer0_receive_data
timer0_receive_start:
        sbis    pind,rxd0
        rjmp    timer0_end
        
        cbr    status,(1<<rdr)            ;start bit wrong then rdr=0 exit
        rjmp    timer0_complete
timer0_receive_data:
        sec
        sbis    pind,rxd0            ;get rxd0 data
        clc
        ror    sbuf0
        rjmp    timer0_end
timer0_receive_stop:
        cbr    status,(1<<fe0)            ;if stop bit=0 then fe0=0
        sbis    pind,rxd0
        rjmp    timer0_complete
        sbr    status,(1<<fe0)
        sbr    status,(1<<rdr)            ;rdr=1
;>>>>>>>>>>>>>>>>>>>>>>>>>>
timer0_complete:        
        in    temp,timsk
        cbr    temp,(1<<toie0)
        out    timsk,temp            ;disable timsk/toie0
;;;;;;;;;;;;;;;;;;;
        in    temp,gifr
        sbr    temp,(1<<intf0)
        out    gifr,temp            ;clr int0 flag
        
        in    temp,gimsk
        sbr    temp,(1<<int0)
        out    gimsk,temp            ;enable gimsk/int0

        cbr    status,(1<<busy)|(1<<td)    ;busy=0,td=0
timer0_end:
        pop    temp
        out    sreg,temp
        pop    temp

        reti        
;******************************************    
int00:
        push    temp
        in    temp,sreg
        push    temp

        ldi    temp,(256-N/2)            ;skip 0.5bit
        out    TCNT0,temp

        ldi    status,(1<<busy)        ;busy=1,rdr=0,td=0,fe0=0
        clr    bit_cnt
        
        in    temp,tifr
        sbr    temp,(1<<tov0)
        out    tifr,temp            ;clr tifr/tov0

        in    temp,timsk
        sbr    temp,(1<<toie0)
        out    timsk,temp            ;enable timsk/toie0

        in    temp,gimsk
        cbr    temp,(1<<int0)
        out    gimsk,temp            ;disable gimsk/int0

        pop    temp
        out    sreg,temp
        pop    temp
        reti
;**********************************************************rxd0_data:
txd0_data:
        ldi    status,(1<<busy)|(1<<td)    ;busy=1,td=1,rdr=0

        push    temp
        in    temp,gimsk
        cbr    temp,(1<<int0)
        out    gimsk,temp            ;disable gimsk/int0
        pop    temp
        
        ser    bit_cnt                ;bit_cnt=0xff
        MOV    sbuf0,temp            ;send data
        
        ldi    temp,(256-N)
        out    TCNT0,temp            ;wait 1 bit timer0 interrupt

        in    temp,tifr
        sbr    temp,(1<<tov0)
        out    tifr,temp            ;clr tifr/tov0

        in    temp,timsk
        sbr    temp,(1<<toie0)
        out    timsk,temp            ;enable timsk/toie0

        cbi    portb,txd0            ;uart start        

        ret
;******************************************    
rxd0_data:
        sbrs    status,fe0            ;if fe0=0 then exit
        rjmp    rxd0_data_end
        cbr    status,(1<<rdr)            ;rdr=0
        MOV    temp,sbuf0
rxd0_data_end:
        ret
;******************************************    

;uart received a byts from uart  and then return it from uart0:
;uart received a byts from uart0 and then return it from uart :
main:
    sbic    usr,rxc
    rjmp    send_115200

    sbrs    status,rdr
    rjmp    uart_end
send_19200:    
    rcall    rxd0_data            ;get uart data from 19200bps uart0

wait2:    sbis    usr,udrie
    rjmp    wait2
    out    udr,temp            ;send data to 115200bps uart
    rjmp    uart_end

send_115200:
    in    temp,udr            ;get uart data from 115200bps uart
    sbic    usr,fe
    rjmp    uart_end            ;if fe err then end

wait3:    sbrc    status,td            ;wait send flag
    rjmp    wait3
    rcall    txd0_data            ;send data to 19200bps uart0
uart_end:
    rjmp    main
;**********************************************************
    .exit
;**********************************************************

 

  • 上一篇: CRC16校驗(yàn)的程序
  • 下一篇: 在AVR Studio里使用AVR-GCC
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁(yè)  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國(guó)行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營(yíng)許可證:浙B2-20080178-1