⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 ibutton的单片机读写小程序,简单实用,主要针对DS1991
💻 TXT
字号:
原创:方便在C51里调用的IBUTTON读写程序 
xiangmingjun 发表于 2005-4-23 15:28:19 

实用程序,直接存为.ASM文件,然后在KEIL中和其他的C程序一起编译就行了,调用是标准的C函数,奉献给大家,其后附得有一个调用例程。(晶振为11.0592)


$NOMOD51

NAME    IBU

B    DATA    0F0H
ACC    DATA    0E0H

;Ibutton引脚定义为P3.2
DATA_BIT    BIT        0B0H.2
;函数声明
;--Ibutton复位
?PR?IbTouchReset?IBU     SEGMENT CODE 
;--Ibutton数据读出
?PR?IbDataIn?IBU         SEGMENT CODE 
;--Ibutton数据写入
?PR?_IbTouchByte?IBU     SEGMENT CODE 
?DT?_IbTouchByte?IBU     SEGMENT DATA OVERLAYABLE 
;--写或读一位
?PR?IbTouchBit?IBU       SEGMENT CODE 
;--Ibutton CRC数据校验
?PR?IbDataCrc?IBU        SEGMENT CODE 

;全局变量定义
?DT?IBU                  SEGMENT DATA 
PUBLIC    IbuttonCrc    

;函数声明
PUBLIC    IbDataCrc
PUBLIC    IbTouchBit
PUBLIC    _IbTouchByte
PUBLIC    IbDataIn
PUBLIC    IbTouchReset

;局部变量定义
RSEG  ?DT?_IbTouchByte?IBU
?_IbTouchByte?BYTE:
IbuttonCrc?242:   DS   1

RSEG  ?DT?IBU
IbuttonCrc:   DS   1


;----------------------------------------------------------------
    RSEG  ?PR?IbTouchReset?IBU
IbTouchReset:
          PUSH      B                     ;       Save the B register.
          PUSH      ACC                   ;       Save the accumulator.
          MOV       A,        #4          ;       Load outer loop variable.
          CLR       DATA_BIT              ;       Start the reset pulse.
          MOV       B,        #221        ;   2.  Set time interval.
          DJNZ      B,        $           ; 442.  Wait with Data low.
          SETB      DATA_BIT              ;   1.  Release Data line.
          MOV       B,        #6          ;   2.  Set time interval.
          CLR       C                     ;   1.  Clear presence flag.
WAITLOW:
          JB        DATA_BIT, WH          ;       Exit loop if line high.
          DJNZ      B,        WAITLOW     ;       Hang around for 3360
          DJNZ      ACC,      WAITLOW     ;          us if line is low.
          SJMP      SHORT                 ;       Line could not go high.
WH:
          MOV       B,        #111        ;       Delay for presence detect.
HL:
          ORL       C,        /DATA_BIT   ; 222.  Catch presence pulse.
          DJNZ      B,        HL          ; 222.  Wait with Data high.
SHORT:
          POP       ACC                   ;       Restore accumulator.
          POP       B                     ;       Restore B register.
          RET                             ;       Return.
; ---------------------------------------------------------------
; uchar IbDataIn(void)
    RSEG  ?PR?IbDataIn?IBU
    USING    0
IbDataIn:    
          MOV       R7,        #0FFH       ;       Initialize for input.
          CALL        _IbTouchByte
          MOV       R7,           A
          RET                             ;       Return to caller.
;----------------------------------------------------------------
; void IbTouchByte(uchar InputData)

    RSEG  ?PR?_IbTouchByte?IBU
_IbTouchByte:
          MOV        A,          R7
          PUSH      B                     ;       Save the B register.
          MOV       B,        #8          ;       Setup for 8 bits.
BIT_LOOP:
          RRC       A                     ;   1.  Get bit in carry.
          CALL      IbTouchBit            ;   2.  Send bit.
          DJNZ      B,        BIT_LOOP    ;   2.  Get next bit.
          RRC       A                     ;       Get final bit in ACC.
          POP       B                     ;       Restore B register.
          RET     
;---------------------------------------------------------------
; void IbTouchBit(void)
    RSEG  ?PR?IbTouchBit?IBU
IbTouchBit:
          CLR       DATA_BIT              ;   1.  Start the time slot.
          NOP                             ;   1.  Delay to make sure
          NOP                             ;   1.    that the Touch Memory
          NOP                             ;   1.      sees a low for at
          NOP                             ;   1.        least 1 microsecond.
          MOV       DATA_BIT, C           ;   2.  Send out the data bit.
          NOP                             ;   1.  Delay to give the
          NOP                             ;   1.    data returned from
          NOP                             ;   1.      the Touch Memory
          NOP                             ;   1.        time to settle
          NOP                             ;   1.          before reading
          NOP                             ;   1.            the bit.
          MOV       C,        DATA_BIT    ;   1.  Sample input data bit.
          PUSH      B                     ;   2.  Save B register.
          MOV       B,        #12H        ;   2.  Delay until the end
          DJNZ      B,        $           ;   36.   of the time slot.
          POP       B                     ;       Restore B register.
          SETB      DATA_BIT              ;       Terminate time slot.
          RET  
;---------------------------------------------------------------- 
; void IbDataCrc(void)
    RSEG  ?PR?IbDataCrc?IBU
IbDataCrc:
          PUSH      ACC                   ; Save the Accumulator.
          PUSH      B                     ; Save the B register.
          PUSH      ACC                   ; Save bits to be shifted.
          MOV       B,        #8          ; Set to shift eight bits.
CRC_LOOP:
          XRL       A,        IbuttonCrc  ; Calculate DQIN xor CRCT0.
          RRC       A                     ; Move it to the carry.
          MOV       A,        IbuttonCrc  ; Get the last CRC value.
          JNC       ZERO                  ; Skip if DQIN xor CRCT0 = 0.
          XRL       A,        #18H        ; Update the CRC value.
ZERO:
          RRC       A                     ; Position the new CRC.
          MOV       IbuttonCrc,     A     ; Store the new CRC.
          POP       ACC                   ; Get the remaining bits.
          RR        A                     ; Position next bit in LSB.
          PUSH      ACC                   ; Save the remaining bits.
          DJNZ      B,        CRC_LOOP    ; Repeat for eight bits.
          POP       ACC                   ; Clean up the stack.
          POP       B                     ; Restore the B register.
          POP       ACC                   ; Restore the Accumulator.
          RET                             ; Return.
; END OF IbDataCrc
;---------------------------------------------------------------
    END




//调用例程
#i nclude    "define.h"

extern void IbTouchReset();
extern uchar IbDataIn();
extern void IbTouchByte(uchar InputData);
extern void IbTouchBit();
extern void IbDataCrc();

extern uchar IbuttonCrc;
extern uchar IbuttonData[8];
extern bit bHaveNewData;

//外部0中断
void Ex0_InSevice() interrupt 0
{
    uchar i,IbTemp[8],IbCrc=0;    
    EX0=0;    
    IbuttonPin=1;
    IbTouchReset();       
    IbTouchByte(0x33);
    IbuttonCrc=0;
    for(i=0;i<7;i++)
    {
        IbTemp[i]=IbDataIn();
        IbCrc|=IbTemp[i];
        IbDataCrc();
    }
    IbTemp[7]=IbDataIn();
    if(IbuttonCrc==IbTemp[7] && IbCrc!=0)
    {
        IbuttonPin=1;
        bHaveNewData=1;
        for(i=0;i<8;i++)
        {
            IbuttonData[i]=IbTemp[i];
        }
        //读入正确
    }
    else
    {
        bHaveNewData=0;
        //读入错误
    }
    IE0=0;        //清外部中断0标志
    EX0=1;
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -