#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^;
uint wendu;
sbit sce = P1^; //片选
sbit res = P1^; //复位,0复位
sbit dc = P1^; //1写数据,0写指令
sbit sdin = P1^; //数据
sbit sclk = P1^; //时钟
/*------------------------------------shuzihanzi-----------------------------------*/
unsigned char code shuzi[]={
//
0xF8,0x04,0x04,0x04,0xF8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,
//
0x00,0x08,0xFC,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
//
0x18,0x84,0x44,0x44,0x38,0x00,0x03,0x02,0x02,0x02,0x02,0x00,
//
0x88,0x04,0x24,0x24,0xD8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,
//
0xC0,0xB0,0x88,0xFC,0x80,0x00,0x00,0x00,0x00,0x03,0x00,0x00,
//
0x3C,0x14,0x14,0x14,0xE4,0x00,0x01,0x02,0x02,0x02,0x01,0x00,
//
0xF8,0x24,0x24,0x24,0xC8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,
//
0x04,0x04,0x04,0xE4,0x1C,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
//
0xD8,0x24,0x24,0x24,0xD8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,
//
0x38,0x44,0x44,0x44,0xF8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,
//'
0x00,0x00,0x04,0x0A,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//C
0xF8,0x04,0x04,0x04,0x98,0x00,0x01,0x02,0x02,0x02,0x01,0x00,
//.
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
//:
0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
//T
0x04,0x04,0xFC,0x04,0x04,0x00,0x00,0x00,0x03,0x00,0x00,0x00
};
unsigned char code hanzi[]={
0x22,0x44,0x00,0xC0,0x5F,0xD5,0x55,0xD5,0x5F,0xC0,0x00,0x00,0x04,
0x02,0x09,0x0F,0x08,0x0F,0x08,0x0F,0x08,0x0F,0x08,0x00,/*"温",0*/
0x00,0xFE,0x0A,0x8A,0xBE,0xAA,0xAB,0xAA,0xBE,0x8A,0x0A,0x00,0x08,
0x07,0x00,0x08,0x09,0x0A,0x04,0x04,0x0A,0x09,0x08,0x00,/*"度",1*/
0x11,0x22,0x80,0x3E,0x2A,0xEA,0x2A,0xEA,0x2A,0x3E,0x80,0x00,0x04,
0x02,0x08,0x09,0x08,0x0F,0x08,0x0F,0x08,0x09,0x08,0x00,/*"湿",2*/
0x00,0xFE,0x0A,0x8A,0xBE,0xAA,0xAB,0xAA,0xBE,0x8A,0x0A,0x00,0x08,
0x07,0x00,0x08,0x09,0x0A,0x04,0x04,0x0A,0x09,0x08,0x00,/*"度",3*/
0x20,0x22,0x24,0xE8,0x20,0x3F,0x20,0xE8,0x24,0x22,0x20,0x00,0x08,
0x04,0x02,0x01,0x00,0x00,0x00,0x07,0x08,0x08,0x0E,0x00,/*"光",4*/
0xF2,0x92,0x9E,0x00,0xE0,0x2F,0x29,0xF9,0x29,0x2F,0xE0,0x00,0x08,
0x08,0x07,0x00,0x09,0x09,0x09,0x0F,0x09,0x0D,0x09,0x00,/*"强",5*/
};
/*--------------------------5110驱动------------------------------*/
/**********************延时函数************************/
void delayus(uint z) //微妙级延时
{
uint y;
for(y=z;y>;y--);
}
void delayms(uint z) //毫秒级延时
{
uint x,y;
for(y=z;y>;y--)
for(x=;x>;x--);
}
/********************写字节**************************/
void LCD_write_byte(unsigned char dt, unsigned char command) //写字节
{
unsigned char i;
sce=; //5110片选有效,允许输入数据
dc=command; //0写指令,1写数据
for(i=;i<;i++) //传送8bit数据
{
if(dt&0x80)
sdin=;
else
sdin=;
dt=dt<<;
sclk=;
sclk=;
}
dc=;
sce=; //禁止5110
sdin=;
}
/*******************初始化********************************************/
void LCD_init(void) //初始化(command=0,写指令)
{
res=;
delayms();
res=;
LCD_write_byte(0x21,);//初始化Lcd,功能设定使用扩充指令
LCD_write_byte(0xa0,);//设定液晶偏置电压(5v),5110接5V时偏置电压改为(0xa7,0),接3.3V时改为(0xc0,0)
LCD_write_byte(0x20,);//使用基本指令
LCD_write_byte(0x0C,);//设定显示模式,正常显示
}
/*********************设置初始坐标************************************/
void LCD_set_XY(unsigned char X, unsigned char Y) //设置LCD坐标,X:0-83 Y:0-5
{
LCD_write_byte(0x40 | Y, );// column //列
LCD_write_byte(0x80 | X, );// row //行
}
/**********************清屏函数***************************************/
void LCD_clear(void) //LCD清屏函数
{
unsigned char t;
unsigned char k;
LCD_set_XY(,); //设置RAM起始地址
for(t=;t<;t++)
{
for(k=;k<;k++)
{
LCD_write_byte(0x00,); //写数据
}
}
}
/****************************输入数字**************************************/
void LCD_write_shu(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页 dd:字符,page范围为0-5,
{
unsigned char i;
LCD_set_XY(row*, page);// 列,页
for(i=; i<;i++)
{
LCD_write_byte(shuzi[c*+i],);
}
LCD_set_XY(row*, page+);// 列,页
for(i=; i<;i++)
{
LCD_write_byte(shuzi[c*+i],);
}
}
/******************************输入汉字*************************************/
void LCD_write_zi(unsigned char row, unsigned char page,unsigned char c)
{
unsigned char i;
LCD_set_XY(row*, page);// 列,页
for(i=; i<;i++)
{
LCD_write_byte(hanzi[c*+i],);
}
LCD_set_XY(row*, page+);// 列,页
for(i=; i<;i++)
{
LCD_write_byte(hanzi[c*+i],);
}
}
/*-----------------------------------------18b20驱动--------------------------------*/
/*************精确延时函数**************** */
// 数据表如下
/*
********************************************************************************************************************
延时时间 a的值 b的值 c的值 延时误差(us)
10us 1 1 1 -0.5
20us 1 1 8 0
30us 1 1 15 +0.5
40us 2 1 9 0
50us 1 1 28 0
60us 1 1 35 +0.5
70us 1 1 42 +1
80us 1 1 48 0
90us 1 1 55 +0.5
100us 1 1 61 -0.5
200us 1 1 128 0
300us 3 1 63 +1.5
400us 2 1 129 0
500us 5 1 63 +0.5
600us 6 1 63 0
700us 7 1 63 -0.5
800us 1 3 175 +0.5
900us 9 1 63 -1.5
1ms 1 3 219 -1.5
2ms 2 3 220 +3
3ms 3 3 220 +3
Xms X 3 220 +3
(X的范围为2到255)
*/
void Delay(unsigned char a1,b1,c1)
{
unsigned char a,b,c;
for(a=;a
{
DQ = ; //将总线拉低,要在1us之后释放总线
//单片机要在此下降沿后的15us内读数据才会有效。
_nop_(); //至少维持了1us,表示读时序开始
dat >>= ; //让从总线上读到的位数据,依次从高位移动到低位。
DQ = ; //释放总线,此后DS18B20会控制总线,把数据传输到总线上
Delay(,,); //延时10us,此处参照推荐的读时序图,尽量把控制器采样时间放到读时序后的15us内的最后部分
if(DQ) //控制器进行采样
{
dat |= 0x80; //若总线为1,即DQ为1,那就把dat的最高位置1;若为0,则不进行处理,保持为0
}
Delay(,,); //20us //此延时不能少,确保读时序的长度60us。
}
return (dat);
}
void Write_One_Byte(uchar dat)
{
uchar i = ;
for(i=;i>;i--)
{
DQ = ; //拉低总线
_nop_(); //至少维持了1us,表示写时序(包括写0时序或写1时序)开始
DQ = dat&0x01; //从字节的最低位开始传输
//指令dat的最低位赋予给总线,必须在拉低总线后的15us内,
//因为15us后DS18B20会对总线采样。
Delay(,,); //必须让写时序持续至少60us
DQ = ; //写完后,必须释放总线,
dat >>= ;
Delay(,,);
}
}
void Get_Tmp_dispaly() //获取温度get the wenduerature
{
uchar A1,A2,A3;
float tt;
uchar a,b;
Init_Ds18b20(); //初始化
Write_One_Byte(0xcc); //忽略ROM指令
Write_One_Byte(0x44); //温度转换指令
Init_Ds18b20(); //初始化
Write_One_Byte(0xcc); //忽略ROM指令
Write_One_Byte(0xbe); //读暂存器指令
a = Read_One_Byte(); //读取到的第一个字节为温度LSB
b = Read_One_Byte(); //读取到的第一个字节为温度MSB
wendu = b; //先把高八位有效数据赋于wendu
wendu <<= ; //把以上8位数据从wendu低八位移到高八位
wendu = wendu|a; //两字节合成一个整型变量
tt = wendu*0.0625; //得到真实十进制温度值
//因为DS18B20可以精确到0.0625度
//所以读回数据的最低位代表的是0.0625度
wendu = tt*+0.5; //放大十倍
//这样做的目的将小数点后第一位也转换为可显示数字
//同时进行一个四舍五入操作。
A1 = wendu/; //百位
A2 = wendu%/; //十位
A3 = wendu%; //个位
LCD_write_shu(,,A1); //得到十位数
LCD_write_shu(,,A2 ); //得到个位数
LCD_write_shu(,,A3); //得到个位数
}
void main()
{
unsigned char k,i;
res=;
for(k=;k<;k++);
res=;
LCD_init(); //初始化LCD模块
LCD_clear(); //清屏幕
LCD_write_shu(,,); //点
LCD_write_shu(,,);//ling
LCD_write_shu(,,); //C
//LCD_write_shu(5,0,13);
// LCD_write_zi(1,0,0); //温
//LCD_write_zi(3,0,1); //度
LCD_write_zi(,,); //wen
LCD_write_zi(,,);//度
//LCD_write_shu(5,2,13);
//LCD_write_zi(1,4,4); //guang
//LCD_write_zi(3,4,5); //qiang
LCD_write_shu(,,); //强冒
while()
{
Get_Tmp_dispaly();
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章