打印
[国产单片机]

求一个24C02的读写程序,急!急!急!

[复制链接]
1377|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
huasanshao0206|  楼主 | 2013-12-29 07:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 huasanshao0206 于 2013-12-29 09:03 编辑

如题,求一个经过验证的读写程序,最好是一个读函数,一个写函数,不要把开始、结束、ACK等单独写,因为用的单片机嵌套深度不够。用的MCU是EM78P156NM,外部4M晶振。以前用的I2C程序不能用了,照着手册上的时序改了2天还是不对,太伤自尊了:'(。话说EM78P156用2CLOCK和4CLOCK哪个好些啊?谢谢您的回复,不胜感激!

相关帖子

沙发
ayb_ice| | 2013-12-30 14:51 | 只看该作者
再改两天就可以了

使用特权

评论回复
板凳
airwill| | 2013-12-30 15:37 | 只看该作者
网上搜一下, 会有很多程序供你参考的

使用特权

评论回复
地板
hopewise| | 2013-12-30 15:54 | 只看该作者
;**************************************
;**************24C02的写操作***********
ROM_WRITE PROC
       clr   sda_c                   ;设输出模式
       clr   scl_c
  
       set   sda                     ;起始信号
       set   scl
       call  DELAY_8us
       clr   sda
       call  DELAY_8us
       clr   scl
       call  DELAY_8us
       set   sda                     ;器件地址1010  
       call  DELAY_8us
       set   scl
       call  DELAY_8us
   
       clr   scl
       clr   sda                     ;0
       set   scl
       call  DELAY_8us
  
       clr   scl
       set   sda                     ;1
       call  DELAY_8us
       set   scl
       call  DELAY_8us
  
       clr   scl     
       clr   sda                     ;0
       set   scl
       call  DELAY_8us
  
       clr   scl
       clr   sda                     ;a2=0
       set   scl
       call  DELAY_8us
  
       clr   scl
       set   scl
       call  DELAY_8us

       clr   scl
       set   scl
       call  DELAY_8us
  
       clr   scl
       clr   sda
       set   scl                     ;R/W=0,设为写模式
       call  DELAY_8us
  
       clr   scl
       set   sda_c                   ;设sda为输入模式
       call  DELAY_8us
       set   scl
       call  DELAY_8us

wait_ack:
       sz    sda                     ;等待应答信号
       jmp   wait_ack

       clr   scl                     ;写地址
       clr   sda_c
       mov   a,08h
       mov   data_8,a

random_write:
      clr   sda
      sz    word_address.7
      set   sda
      call  DELAY_8us
      set   scl
      call  DELAY_8us
  
      clr   scl
      rl    word_address
      sdz   data_8
      jmp   random_write
      set   sda_c
      call  DELAY_8us
      set   scl
      call  DELAY_8us

fdev:
      sz    sda                       ;等待应答信号
      jmp   fdev

      clr   scl                       ;写数据
      clr   sda_c
      mov   a,08h
      mov   data_8,a

data_in:
      clr   sda
      sz    write_in.7
      set   sda
      call  DELAY_8us
      set   scl
      call  DELAY_8us
  
      clr   scl
      rl    write_in
      sdz   data_8
      jmp   data_in

      set   sda_c
      set   scl

fdev2:
      sz    sda                       ;等待应答信号
      jmp   fdev2

      clr   scl                     
      clr   sda_c

      clr   sda
      set   scl
      set   sda                        ;停止信号

      mov   a,30h                      ;延时
      mov   delay_5,a
      mov   a,10h
      mov   delay,a
delay2:
     sdz   delay_5
     jmp   delay2
     sdz   delay
     jmp   delay2
     ret
ROM_WRITE ENDP
;********************************
;**************读24C02操作*******
ROM_READ PROC
     clr   read_out
     clr   sda_c
     clr   scl_c
  
     set   sda
     call  DELAY_8us
     set   scl
     call  DELAY_8us
     clr   sda                         ;起始信号
  
     clr   scl
     set   sda                         ;1
     call  DELAY_8us
     set   scl
     call  DELAY_8us
  
     clr   scl
     clr   sda                         ;0
     set   scl
     call  DELAY_8us
  
     clr   scl
     set   sda                         ;1
     call  DELAY_8us
     set   scl
     call  DELAY_8us
  
     clr   scl     
     clr   sda                         ;0
     set   scl
     call  DELAY_8us
   
     clr   scl
     clr   sda                         ;a2=0
     set   scl
     call  DELAY_8us
  
     clr   scl
     set   scl
     call  DELAY_8us  
                        
     clr   scl
     set   scl  
     call  DELAY_8us      
                  
     clr   scl
     clr   sda
     set   scl                         ;写模式
     call  DELAY_8us
  
     clr   scl
     set   sda_c
     set   scl
     call  DELAY_8us
wait:
     sz    sda                         ;等待应答信号
     jmp   wait

     clr   scl                         ;写读操作的地址
     clr   sda_c
     mov   a,08h
     mov   data_8,a
     
read_address_in:
     clr   sda
     sz    word_address.7
     set   sda
     call  DELAY_8us
     set   scl
     call  DELAY_8us
  
     clr   scl
     rl    word_address
     sdz   data_8
     jmp   read_address_in

     set   sda_c
     set   scl
     call  DELAY_8us
FDEV1:
     sz    sda                          ;等待应答信号
     jmp   fdev1
     clr   scl
     clr   sda_c

read_data:
     set   sda
     call  DELAY_8us
     set   scl
     call  DELAY_8us
     clr   sda                          ;起始信号
     clr   scl
  
     set   sda                          ;1
     call  DELAY_8us
     set   scl
     call  DELAY_8us
  
     clr   scl
     clr   sda                          ;0
     set   scl
     call  DELAY_8us
   
     clr   scl
     set   sda                          ;1
     call  DELAY_8us
     set   scl
     call  DELAY_8us
  
     clr   scl      
     clr   sda                          ;0
     set   scl
     call  DELAY_8us
  
     clr   scl
     clr   sda                          ;a2=0
     set   scl
     call  DELAY_8us
  
     clr   scl
     clr   sda
     set   scl  
     call  DELAY_8us   
                     
     clr   scl
     clr   sda
     set   scl  
     call  DELAY_8us
                        
     clr   scl
     set   sda                          ;读模式
     call  DELAY_8us
     set   scl
     call  DELAY_8us
  
     clr   scl
     set   sda_c
     set   SCL  
     call  DELAY_8us

w_ack:
     sz    sda                          ;等待应答信号
     jmp   w_ack

     clr   scl
     set   sda_c
            
     mov   a,08h                    
     mov   data_8,a

random_out:
     set   scl
     clr   read_out.7
     sz    sda
     set   read_out.7
     clr   scl
     rl    read_out
     sdz   data_8
     jmp   random_out

     clr   sda_c
     set   sda
     set   scl
     clr   scl

     clr   sda                            ;停止信号
     set   scl
     set   sda
     ret
ROM_READ ENDP
;**********************************
;***************延时8us秒**********
DELAY_8us:                             
      mov    a, 1
      mov    D_TEMP,a
D8US:
      sdz    D_TEMP
      jmp   D8US
      ret
;**********************************

使用特权

评论回复
5
hopewise| | 2013-12-30 15:56 | 只看该作者
合泰MCU/4MHZ,这是我以前一直在用的.

使用特权

评论回复
6
旭日东起| | 2013-12-31 16:08 | 只看该作者
嵌套深度,4层都没有吗? 你可以把你以前的改一下就行了

使用特权

评论回复
7
lpzailushang| | 2014-1-1 20:31 | 只看该作者
EMC156仿真的时候确实有点问题,嵌套不够,容易乱跳

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

7

主题

36

帖子

0

粉丝