#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P1^1;
uint temp;
float f_temp;
uchar flag ;
sbit sce = P2^0; //片选
sbit res = P2^1; //复位,0复位
sbit dc = P2^2; //1写数据,0写指令
sbit sdin = P2^3; //数据
sbit sclk = P2^4; //时钟
unsigned char code hanzi[]=
{
0x88,0x84,0xFE,0xAB,0xAA,0xFF,0xAA,0xAA,0xAA,0xAA,0x80,0x00,0x04,0x04,0x02,0x01,
0x00,0x07,0x00,0x01,0x02,0x04,0x04,0x00,//集0
0x0C,0x14,0x14,0x14,0x15,0xF6,0x14,0x14,0x14,0x14,0x0C,0x00,0x00,0x00,0x00,0x04,
0x04,0x07,0x00,0x00,0x00,0x00,0x00,0x00,//宁1
0xFC,0x00,0xFF,0x00,0xF9,0x09,0x09,0xFF,0x09,0x09,0xF9,0x00,0x04,0x02,0x01,0x00,
0x01,0x00,0x00,0x07,0x00,0x01,0x01,0x00,//师2
0x22,0x4A,0x92,0x67,0x02,0xF2,0x12,0x97,0x92,0xF2,0x02,0x00,0x01,0x07,0x00,0x00,
0x00,0x07,0x04,0x04,0x04,0x04,0x07,0x00,//范3
0x9C,0x84,0x95,0x96,0x95,0xD6,0xB4,0x94,0x87,0x94,0x8C,0x00,0x00,0x00,0x00,0x04,
0x04,0x07,0x00,0x00,0x00,0x00,0x00,0x00,//学4
0xFF,0x81,0x8D,0xF3,0x26,0x2A,0xEA,0x2B,0xEA,0x2A,0x26,0x00,0x07,0x00,0x00,0x04,
0x04,0x02,0x01,0x00,0x07,0x04,0x07,0x00,//院5
0x00,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x23,0x22,0x00,0x00,0x04,0x04,0x04,0x04,
0x04,0x07,0x04,0x04,0x04,0x04,0x04,0x00,//王6
0x08,0x30,0xC0,0x00,0xFF,0x00,0x00,0xFF,0x40,0x20,0x18,0x00,0x04,0x04,0x04,0x04,
0x07,0x04,0x04,0x07,0x04,0x04,0x04,0x00,//业7
0x00,0xFF,0x49,0xFF,0x10,0x4E,0x48,0xFF,0x48,0x48,0x48,0x00,0x06,0x01,0x04,0x07,
0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00,//胜8
0x18,0xD6,0x54,0xFF,0x54,0x56,0xD4,0x00,0xFC,0x00,0xFF,0x00,0x00,0x03,0x00,0x07,
0x00,0x02,0x03,0x00,0x04,0x04,0x07,0x00,//制9
0x10,0xF1,0x02,0x10,0xD8,0x56,0x54,0x5F,0x54,0xD4,0x10,0x00,0x06,0x01,0x02,0x02,
0x05,0x05,0x05,0x05,0x05,0x05,0x04,0x00,//造10
};
unsigned char code shuzi[]={
0xF8,0x04,0x04,0x04,0xF8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,//0
0x00,0x08,0xFC,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,//1
0x18,0x84,0x44,0x44,0x38,0x00,0x03,0x02,0x02,0x02,0x02,0x00,//2
0x88,0x04,0x24,0x24,0xD8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,//3
0xC0,0xB0,0x88,0xFC,0x80,0x00,0x00,0x00,0x00,0x03,0x00,0x00,//4
0x3C,0x14,0x14,0x14,0xE4,0x00,0x01,0x02,0x02,0x02,0x01,0x00,//5
0xF8,0x24,0x24,0x24,0xC8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,//6
0x04,0x04,0x04,0xE4,0x1C,0x00,0x00,0x00,0x03,0x00,0x00,0x00,//7
0xD8,0x24,0x24,0x24,0xD8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,//8
0x38,0x44,0x44,0x44,0xF8,0x00,0x01,0x02,0x02,0x02,0x01,0x00,//9
0x00,0x00,0x04,0x0A,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'
0xF8,0x04,0x04,0x04,0x98,0x00,0x01,0x02,0x02,0x02,0x01,0x00,//C
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,//.
0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,//:
0x04,0x04,0xFC,0x04,0x04,0x00,0x00,0x00,0x03,0x00,0x00,0x00,//T
0x20,0x20,0xFC,0x20,0x20,0x00,0x00,0x00,0x01,0x00,0x00,0x00,//空格,+
0x20,0x20,0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//-
};
void delayms(uint z)
{
uint x,y;
for(y=z;y>0;y--)
for(x=1400;x>0;x--);
}
void LCD_write_byte(unsigned char dt, unsigned char command)
{
unsigned char i;
sce=0;
dc=command;
for(i=0;i<8;i++)
{
if(dt&0x80)
sdin=1;
else
sdin=0;
dt=dt<<1;
sclk=0;
sclk=1;
}
dc=1;
sce=1;
sdin=1;
}
void LCD_init(void)
{
res=0;
delayms(1);
res=1;
LCD_write_byte(0x21,0);//初始化Lcd,功能设定使用扩充指令
LCD_write_byte(0xd0,0);//设定液晶偏置电压
LCD_write_byte(0x20,0);//使用基本指令
LCD_write_byte(0x0C,0);//设定显示模式,正常显示
}
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_byte(0x40 | Y, 0);// column
LCD_write_byte(0x80 | X, 0);// row
}
void LCD_clear(void)
{
unsigned char t;
unsigned char k;
LCD_set_XY(0,0);
for(t=0;t<6;t++)
{
for(k=0;k<84;k++)
{
LCD_write_byte(0x00,1);
}
}
}
void LCD_write_shu(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页 dd:字符
{
unsigned char i;
LCD_set_XY(row*6, page);// 列,页
for(i=0; i<6;i++)
{
LCD_write_byte(shuzi[c*12+i],1);
}
LCD_set_XY(row*6, page+1);// 列,页
for(i=6; i<12;i++)
{
LCD_write_byte(shuzi[c*12+i],1);
}
}
/*---------------------------------------------
LCD_write_hanzi: 显示12(宽)*16(高)点阵列汉字等半角类
输入参数:c:显示的字符;
编写日期:20080918
-----------------------------------------------*/
void LCD_write_hanzi(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页 dd:字符
{
unsigned char i;
LCD_set_XY(row*6, page);// 列,页
for(i=0; i<12;i++)
{
LCD_write_byte(hanzi[c*24+i],1);
}
LCD_set_XY(row*6, page+1);// 列,页
for(i=12; i<24;i++)
{
LCD_write_byte(hanzi[c*24+i],1);
}
}
/*************************************************************************************/
void delay(unsigned int i)//延时函数
{
while(i--);
}
/***************************************************************************************/
/***************************************************************************************/
//18b20初始化函数
uchar Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(10);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(5);
return (x);
}
/******************************************************************************************/
//读一个字节
unsigned char ReadOne(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay(5);
}
return(dat);
}
/************************************************************************************************/
//写一个字节
void WriteOne(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
delay(5);
}
/*********************************************************************************************************/
void tempchange(void)
{
uchar a,b;
Init_DS18B20();
WriteOne(0xcc);
WriteOne(0x44);
delay(200);
Init_DS18B20();
WriteOne(0xcc);
WriteOne(0xbe);
a=ReadOne();
b=ReadOne();
if((b&0xF0)>0)
{
flag=1;
}else
{
flag=0;
}
if(flag){ //如果为负温度取反加1
b=~b;
a=~a+1;
} //判断是否为负温度
temp=b;
temp<<=8;
temp=temp|a;
f_temp=temp*0.0625;
temp=f_temp*10+0.5;
}
void dis_temp()
{
uchar i;
//LCD_write_shu(4,2,f);
i=temp%1000/100;
LCD_write_shu(5,2,i);
i=temp%100/10;
LCD_write_shu(6,2,i);
i=temp%10;
LCD_write_shu(8,2,i);
}
void main()
{
unsigned char k,i;
res=0;
i++;
for(k=0;k<250;k++);
res=1;
LCD_init(); //初始化LCD模块
LCD_clear(); //清屏幕
LCD_write_hanzi(1,0,0); //J
LCD_write_hanzi(3,0,1); //N
LCD_write_hanzi(5,0,2); //S
LCD_write_hanzi(7,0,3); //F
LCD_write_hanzi(9,0,4); //X
LCD_write_hanzi(11,0,5); //Y
LCD_write_shu(3,2,14);
LCD_write_shu(7,2,12);
LCD_write_shu(9,2,10);
LCD_write_shu(10,2,11);
LCD_write_hanzi(1,4,6); //w
LCD_write_hanzi(3,4,7); //y
LCD_write_hanzi(5,4,8); //S
LCD_write_hanzi(9,4,9); //制
LCD_write_hanzi(11,4,10); //造
while(1)
{
tempchange();
dis_temp();
if(flag==1)
{LCD_write_shu(4,2,16);}
else
{LCD_write_shu(4,2,15);}
}
} |