#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
#include "1602.h"
#include "delay.h"
sbit ds = P0^0;
sbit LED = P0^1;
bit ack = 0;
void ds18b20_init(void)
{
ds = 0;
DelayUs2x(200);
DelayUs2x(200);
ds = 1;
DelayUs2x(30);
if(ds == 0)
ack = 1;
else
ack = 0;
DelayUs2x(200);
}
void ds18b20_send_byte(unsigned char byte)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
ds = 1;
ds = 0;
_nop_();
_nop_();
ds = byte & 0x01;
byte >>= 1;
DelayUs2x(30);
}
DelayUs2x(30);
ds = 1;
}
bit ds18b20_read_bit()
{
bit temp;
ds = 1;
ds = 0;
_nop_();
_nop_();
ds = 1;
temp = ds;
DelayUs2x(30);
return temp;
}
unsigned char ds18b20_read_byte()
{
unsigned char i,j,k = 0;
for(i = 0; i < 8; i++)
{
j = ds18b20_read_bit();
k = (j << 7) | (k >> 1);
}
return k;
}
void main(void)
{
//ds18b20_init();
//ds18b20_send_byte(0xcc);
unsigned char a;
unsigned int temp,b;
float temp_f;
unsigned char buf[16];
LCD_Init();
LCD_Clear();
while(1)
{
ds18b20_init();
ds18b20_send_byte(0xcc);
ds18b20_send_byte(0x44);
DelayMs(20);
ds18b20_init();
ds18b20_send_byte(0xcc);
ds18b20_send_byte(0xbe);
a = ds18b20_read_byte();
b = ds18b20_read_byte();
temp = (b << 8) + a;
temp_f = (float)temp * 0.0625;
sprintf(buf,"Temp: %6.3f ",temp_f);
LCD_Write_String(0,0,buf);
LCD_Write_Char(12,0,0xdf);
LCD_Write_Char(13,0,0x43);
}
/*{
if(ack == 1)
LED = 0;
}*/
}
/*-----------------------------------------------
名称:LCD1602
论坛:www.doflye.net
编写:shifang
日期:2009.5
修改:无
内容:
引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
------------------------------------------------*/
#include "1602.h"
#include "delay.h"
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
/*------------------------------------------------
判忙函数
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
/*------------------------------------------------
写入命令函数
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
// while(LCD_Check_Busy()); //忙则等待
DelayMs(5);
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
//while(LCD_Check_Busy()); //忙则等待
DelayMs(5);
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x); //表示第一行
}
else
{
LCD_Write_Com(0xC0 + x); //表示第二行
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
写入字符函数
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
初始化函数
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*显示模式设置*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
DelayMs(5);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
#include "delay.h"
void DelayUs2x(unsigned char t) //2t+7
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
DelayUs2x(200);
DelayUs2x(254);
}
}
void DelayS(unsigned char t)
{
unsigned char i;
for(i = 0; i < t; i++)
{
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
}
}