首页>
技术资讯>
详情

18B20通讯程序-PIC|

2017-01-10 来源:CloudBest 阅读量: 0
关键词:

    这是我在16F877,18F1320,18F1220上通过的18B20程序,18B20主要是延时问题,这个解决了,什么都可以通过。
   
    C程序
   
    #include <pic18f1220.h>
   
    #define uch unsigned char
   
    #define unint unsigned int
   
    #define DQ RB3 //定义18B20数据端口
   
    #define DQ_DIR TRISB3 //定义18B20D口方向寄存器
   
    #define W1_INPUT 1
   
    #define W1_OUTPUT 0
   
    #define FALSE 0
   
    #define TRUE !FALSE
   
    #define DQ_HIGH() DQ_DIR = W1_INPUT
   
    #define DQ_LOW() DQ = 0; DQ_DIR = W1_OUTPUT
   
    void delay(unint x)
   
    {
   
    unint d;
   
    d=x;
   
    while(--d)
   
    {;}
   
    }
   
    bit reset(void) //初始化18B20
   
    {
   
    static bit presence; //定义一个应答信号
   
    DQ_LOW();
   
    delay(70); //置总线为低电平并保持至少480us
   
    DQ_HIGH(); //等电阻拉高总线并保持15-60us
   
    delay(5);
   
    presence=DQ; //接受应答信号
   
    delay(20); //延时60-240us
   
    return(presence); //返回应答信号
   
    }
   
    //*************** 读一位函数******************//
   
    bit read_bit(void)
   
    {
   
    static bit i;
   
    DQ_LOW();
   
    DQ_LOW();
   
    DQ_HIGH();
   
    asm("nop");
   
    asm("nop");
   
    asm("nop");
   
    i=DQ;
   
    delay(3);
   
    return(i);
   
    }[nextpage]
    
    //*********************写一位函数****************//
   
    void write_bit(uch bitval)
   
    {
   
    DQ_LOW();
   
    delay(1);
   
    if (bitval==1)
   
    {
   
    DQ_HIGH();
   
    }
   
    delay(3);
   
    DQ_HIGH();
   
    }
   
    //************** 从18B20中读一个字节**************//
   
    uch read_byte(void)
   
    {
   
    uch i;
   
    uch j;
   
    uch value=0;
   
    for (i=0;i<8;i++)
   
    {
   
    j=read_bit(); //调读位函数
   
    if (j) //如果是 1 置1
   
    {
   
    value|=(0x01《i); //先读低位,再读高位
   
    asm("nop");
   
    asm("nop");
   
    asm("nop");
   
    }
   
    } //否则置 0
   
    return(value);
   
    }
   
    //*********************向18B20中 写一个字节**************//
   
&nb

热门推荐 查看更多