bokee.net

电子/电气工程师博客

正文 更多文章

基于C8051F020的DS3231驱动程序

                            基于C8051F020DS3231驱动程序

                                   徐丽红           王佰营

                                   wbymcs51.blog.bokee.net

;=================================================================

;             function:  read_ds3231   ;时钟读程序wbymcs51.blog.bokee.net

;             input:             -------------

;             output:           second,minute,hour(秒,分,时)       

;             usage:            -------------

;=================================================================

read_ds3231:   ;时钟读程序

              jb            sm_busy,        $

              setb bHrLw

              mov        sd_addr,  #00h              ;ds3231eprom地址

              setb sm_busy

              setb aa

              mov        wri_add,  #0d0h            ;器件地址+

              setb sta

              jb            sm_busy,        $

 

              mov        buff,                     #second

              setb sm_busy

              setb aa

              mov        byte_number,  #03h       ;读数据个数

              mov        wri_add,  #0d1h            ;器件地址+

              setb sta

              jb            sm_busy,        $

              ret

 

;=================================================================

;             function:  write_ds3231  ;时钟写程序wbymcs51.blog.bokee.net

;             input:             second,minute,hour(秒,分,时)

;             output:           -------------           

;             usage:            -------------

;=================================================================

write_ds3231: ;时钟写程序

              clr          bHrLw

              mov        sd_addr,  #00h

              mov        byte_number,#03h

              jb            sm_busy, $

           setb sm_busy

           setb aa

        mov        wri_add,  #0d0h

           setb sta

        jb            sm_busy, $

              ret

 

;====================================================================

;             function:  Int_smbus      I2C总线中断处理程序wbymcs51.blog.bokee.net

;             input:             -----------------

;             output:           -----------------

;             usage:            -----------------

;====================================================================

Int_smbus:     ;I2C总线中断处理程序

 

              push       psw

              push       acc

              push       dph

              push       dpl

 

              mov        psw,       #GROUP2

              setb si

              mov        a,            smb0sta

              anl          a,            #7fh

              cjne a,            #00h,      isb_08            ;总线错误

              setb sto

              ljmp smb_end

 

isb_08:    cjne a,            #08h,      isb_10            ;发送起始数据

              ;mov              smb0dat,        #0d0h

              mov        smb0dat,        wri_add

              clr          sta

              ljmp smb_end

 

isb_10:    cjne a,            #10h,      isb_18            ;重复发送起始位

              ;mov              smb0dat,        #0d1h

              mov        smb0dat,        wri_add;read_add

              clr          sta

              ljmp smb_end

 

isb_18:    cjne a,            #18h,      isb_20            ;发送器件地址+W后收到ACK

              mov        smb0dat,        sd_addr

              ljmp smb_end

        

isb_20:    cjne a,            #20h,      isb_28            ;发送eprom地址+W后收到NACK

              setb sto

              setb sta

              ;clr         si

              ljmp smb_end

 

isb_28:    cjne a,            #28h,      isb_30            ;发送数据后收到ACK

              jnb          bHrLw,   next1

              setb sto

              clr          sm_busy

              sjmp       smb_end

 

next1:      mov        a,                          byte_number

              cjne a,            #00h,      isb_not0

              setb sto

              clr          sm_busy

              sjmp       smb_end

 

isb_not0:

              mov        r0,          buff

              mov        smb0dat, @r0

              inc          buff

              dec         byte_number

              ;=====================

       ;      mov        a,            p4_led

       ;      xrl          a,            #01h

       ;      mov        p4_led,    a

              ;=====================

              sjmp       smb_end

 

isb_30:    cjne a,            #30h,      isb_38            ;发送数据后收到NACK

              setb sto

              setb sta

              sjmp       smb_end

 

isb_38:    cjne a,            #38h,      isb_40            ;竞争失败

              setb sto

              setb sta

              sjmp       smb_end

 

isb_40:    cjne a,            #40h,      isb_48            ;发送从地址+R后收到ACK

              mov        a,                          byte_number

              cjne a,            #01h,      isb_not1

isb_small1:clr         aa

              sjmp       isb_next

isb_not1:

              jc            isb_small1

              setb aa

isb_next:cjne   a,            #00h,      isb_n0

              setb sto

              clr          sm_busy

isb_n0:    sjmp       smb_end

 

isb_48:    cjne a,            #48h,      isb_50            ;发送从地址+R后收到NACK

              setb sta

              sjmp       smb_end

 

isb_50:    cjne a,            #50h,      isb_58            ;接收数据成功,主机发送ACK

              mov        a,                          byte_number

              cjne a,            #01h,      isb_not1a

              clr          aa

isb_not1a:

 

isb_n0a:jc              small0

              mov        r0,                 buff

              mov        @r0,              smb0dat

              inc          buff

              dec         byte_number

small0:    sjmp       smb_end

 

isb_58:    cjne a,            #58h,      isb_jmp          ;发送数据字节后已发送NACK

              ;mov              rec_byte, smb0dat

              mov        r0,                 buff

              mov        @r0,              smb0dat

 

              setb sto

              clr          sm_busy

              sjmp       smb_end

 

isb_jmp:setb    sto

              clr          sm_busy

             

smb_end:

              clr          si

              pop         dpl

              pop         dph

              pop         acc

              pop         psw

              reti

分享到:

上一篇:C8051F040单片机系统初始化程

下一篇:TMS320LF2406调试入门

评论 (2条) 发表评论

  • tanya (游客) : 非常佩服用汇编写

    2010-01-05 20:10

  • hpdell (游客) : ds3231 有没有C51程序啊!!!

    2009-09-17 14:50

发表评论
验证码