单片机——ds18b20——代码

#include 
   
   
    
      
#include "../delay/delay.h"  
#include "lcd.h"  
  
/*==================================================== 
                 判断写入命令/数据函数 
=====================================================*/  
void lcd_write(unsigned char byte, unsigned char flag)  
{  
    if(flag)  
    {  
        RS = 1;        //选择数据寄存器  
    }  
    else  
    {  
        RS = 0;        //选择指令寄存器  
    }  
      
    RW = 0;            //写  
    E = 1;             //选中LCD  
    LCDPORT = byte;  
    delay_us(5);       //稳定  
    E = 0;             //失能LCD  
}  
  
/*==================================================== 
                    LCD初始化函数 
=====================================================*/  
void lcd_init()  
{  
    delay_ms(15);  
    lcd_write(0x38,LCD_WRITE_COM);    //显示8位数据接口,两行显示,5*7点阵字符  
    delay_ms(5);  
    lcd_write(0x38,LCD_WRITE_COM);  
    delay_ms(5);  
    lcd_write(0x38,LCD_WRITE_COM);  
    delay_ms(5);  
    lcd_write(0x38,LCD_WRITE_COM);  
    delay_ms(5);  
    lcd_write(0x08,LCD_WRITE_COM);    //关闭显示,关闭光标的闪烁  
    delay_ms(5);  
    lcd_write(0x01,LCD_WRITE_COM);    //清屏  
    delay_ms(5);  
    lcd_write(0x06,LCD_WRITE_COM);    //设置指针方式,画面不平移  
    delay_ms(5);  
    lcd_write(0x0c,LCD_WRITE_COM);    //打开显示  
    delay_ms(5);  
}  
  
/*==================================================================================== 
                                 写入字符函数 
函数原型:void lcd_dis_byte(unsigned char x, unsigned char y, unsigned char byte); 
说明:x为行(1——2),y为列(1——8)。 
=====================================================================================*/  
void lcd_dis_byte(unsigned char x, unsigned char y, unsigned char byte)  
{  
    unsigned char add;  
      
    if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))  
    {  
        return ;  
    }  
      
    add = 0x80 + (x - 1) * 0x40 + (y - 1);  
      
    lcd_write(add,LCD_WRITE_COM);          
    lcd_write(byte,LCD_WRITE_DATA);  
}  
  
/*==================================================================================== 
                                  写入字符串函数 
函数原型:void lcd_dis_str(unsigned char x, unsigned char y, unsigned char *disdata); 
说明:x为行(1——2),y为列(1——8)。 
=====================================================================================*/  
void lcd_dis_str(unsigned char x, unsigned char y, unsigned charchar *disdata)  
{  
    unsigned char add;  
      
    if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))  
    {  
        return ;  
    }  
      
    add = 0x80 + (x - 1) * 0x40 + (y - 1);  
    lcd_write(add,LCD_WRITE_COM);  
      
    while(*disdata != '\0')  
    {         
        lcd_write(*disdata,LCD_WRITE_DATA);  
        disdata++;  
    }  
}  
  
#if 0  
/*=========================================================================== 
                                时间显示函数 
============================================================================*/  
void lcd_dis_time()  
{  
    lcd_write(0x80 + 4,LCD_WRITE_COM);  
    lcd_write((hour / 10) + 0x30,LCD_WRITE_DATA);  
    lcd_write((hour % 10) + 0x30,LCD_WRITE_DATA);  
    lcd_write((min / 10) + 0x30,LCD_WRITE_DATA);  
    lcd_write((min % 10) + 0x30,LCD_WRITE_DATA);  
    lcd_write((sec / 10) + 0x30,LCD_WRITE_DATA);  
    lcd_write((sec % 10) + 0x30,LCD_WRITE_DATA);  
}  
#endif  

ds18b20.c

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
#include 
    
    
     
       
#include 
     
     
      
        
#include 
      
      
       
         
#include "./lcd/lcd.h"  
#include "./delay/delay.h"  
  
sbit ds = P1^0;  
  
bit ack = 0;  
  
/*================================================================= 
                            复位DS18B20 
==================================================================*/  
void ds_reset()  
{  
    ds = 1;  
    ds = 0;  
    delay_us(200);  
    delay_us(100);        //480-960us,800us  
    ds = 1;               //free ds  
    delay_us(30);  
    if(0 == ds)  
    {  
        ack = 1;  
    }  
    else  
    {  
        ack = 0;  
    }  
      
    delay_us(200);  
    delay_us(100);  
}  
  
/*================================================================= 
                       向DS18B20中写入一个字节 
==================================================================*/  
void ds_send_byte(unsigned char byte)  
{  
    unsigned char i;  
      
    for(i = 0; i < 8; i++)  
    {  
        ds = 0;  
          
        _nop_();  
        _nop_();  
          
        ds = byte & 0x01;  
        byte >>= 1;  
          
        delay_us(30);  
          
        ds = 1;  
    }  
      
    delay_us(30);  
}  
  
bit ds_read_bit()  
{  
    bit temp;  
      
    ds = 1;  
    ds = 0;  
      
    _nop_();  
    _nop_();  
      
    ds = 1;  
    temp = ds;  
    delay_us(30);  
      
    return temp;  
}  
  
unsigned char ds_read_byte()  
{  
    unsigned char i;  
    unsigned char j;  
    unsigned char k;  
      
    for(i = 0; i < 8; i++)  
    {  
        j = ds_read_bit();  
        k = (j << 7) | (k >> 1);  
    }  
      
    return k;  
}  
  
void main()  
{  
    unsigned char a;  
    unsigned char disbuf[20];  
  
    unsigned int temp;    
    unsigned int b;   
      
    float temperature;  
      
    lcd_init();  
      
    while(1)  
    {  
        ds_reset();  
        ds_send_byte(0xcc);  
        ds_send_byte(0x44);  
          
        ds_reset();  
        ds_send_byte(0xcc);  
        ds_send_byte(0xbe);  
          
        a = ds_read_byte();  
        b = ds_read_byte();  
      
        temp = (b << 8) | a;  
        temperature = (float)temp * 0.0625;  
  
        sprintf(disbuf,"Temp is %7.3f",temperature);  
        lcd_dis_str(1,1,disbuf);          
    }  
}  


      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值