📄 kaiguan.txt
字号:
#include <c8051f020.h>
#include <intrins.h>
sbit DQ=P0^0;
sbit LED=P0^1;
sfr LCDDATA=0xf8;
sbit LCDBUSY=P3^4; //LCD忙标志位
sbit LCDREQ=P3^3; //LCD请求标志位
sbit LCDRES=P3^5; //LCD复位线
unsigned char tempL=0; //设为全局变量
unsigned char tempH=0;
float temperature;//温度值保存在temperature里
void Device_init()
{ SFRPAGE=0x0F;
P3=0x10;/*P3^4为读*/
P3MDOUT = 0x28;/*P3^4,P3^5为推挽输出,P3^4为漏极开路,配置为数字输入口*/
P7MDOUT = 0xff;
P0MDOUT=0xfe;//P0^0设为漏极输出
P0=0xff;//P0^0为数字输入
XBR2 = 0x40;/*使能交叉开关*/
}
void Oscillator_Init()
{
unsigned int i = 0;
SFRPAGE=0x0F;
OSCXCN = 0x67;/*使能外部振荡器*/
for (i = 0; i < 3000; i++); /*等待至少1ms*/
while ((OSCXCN & 0x80) == 0);/*查询XTLVLD是否由0到1*/
CLKSEL=0x01;/*将系统时钟切换到外部振荡器*/
}
void delay_us(unsigned int time)
{
time*=2;
for(;time>0;time--)
_nop_();
}
void delay_ms(unsigned int i)
{while(--i)
delay_us(1000);
}
void delay(unsigned int time)
{
unsigned int n;
n=0;
while(n<time)
{n++;}
return;
}
void LCD_init()
{
LCDRES=0;
delay_ms(5);
LCDRES=1;
LCDREQ=0;
LCDBUSY=0;
delay_ms(5);
}
void write_data(unsigned char data writedata)
{SFRPAGE=0x0F;
while(LCDBUSY==1);//查询LCDBUSY是否为0
LCDREQ=0;//请求信号置零
LCDDATA=writedata;//将数据写到LCDDATA数据线上
delay_us(40);//稍微延时
LCDREQ=1;//请求信号置高电平,请求LCD处理数据
delay_us(400);//延时,保证CUP将数据都送出
while(LCDBUSY==1);//查询LCDBUSY是否为零
LCDREQ=0;//准备第二次传送数据
}
void send_hz(unsigned char x,unsigned char y,unsigned char *hz_p)
{
while((*hz_p)!=0)
{
write_data(0xf0);
write_data(x+0x02);
write_data(y);
write_data(*hz_p-0xa0);
hz_p++;
write_data(*hz_p-0xa0);
hz_p++;
if(x<0x07)
x++;
else
{
x=0x00;
y++;
if(y==0x05)
y=0;
}
}
}
void send_ascii16(unsigned char x,unsigned char y,unsigned char ascii)
{
write_data(0xF9);
write_data(x+0x04);
write_data(y);
write_data(ascii);//写要显示的ASCII码;
}
void send_asc16str(unsigned char x,unsigned char y,unsigned char *ascii)
{
while((*ascii)!=0)
{
write_data(0xF9);
write_data(x+0x04);
write_data(y);
write_data(*ascii);//写要显示的ASCII码;
ascii++;
if(x<0x0f)
x++;
else
{
x=0x00;
y+=16;
if(y==0x40)
y=0;
}
}
}
Init_DS18B20(void)
{
unsigned char x=0;
DQ=1;
delay(8);
DQ=0;
delay(85);
DQ=1;
delay(14);
x=DQ;
delay(20);
}
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat=0;
for(i=8;i>0;i--)
{DQ=1;
delay(1);
DQ=0;
dat>>=1;
DQ=1;
if{DQ}
dat|=0x80;
delay(4);
}
return(dat);
}
WriteOneChar(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(4);
}
ReadTemperature(void)
{
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0x44);
delay(125);
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0xbe);
tempL=ReadOneChar();
tempH=ReadOneChar();
temperature=((tempH*256)+tempL)*0.0625;
delay(200);
return(temperature);
}
void main()
{
unsigned int data temperture_int, temperture_frac; // 温度的整数和小数部分
unsigned char data temperture_1,temperture_2,temperture_3;//LCD显示转换中间变量
WDTCN=0xde;
WDTCN=0xad;
Device_init();
LCD_init();
Oscillator_Init();
send_hz(0,0,"当前温度");
while(1)
{
ReadTemperature(void);
delay_ms(100);
temperture=temperture*100;
temperture_int=temperture/1000;
send_ascii16(9,16,temperture_int+0x30);
temperture_frac=temperture-temperture_int;
temperture_1=temperture_frac/100;
send_ascii16(10,16,temperture_1+0x30);
send_ascii16(11,16,'.');
temperture_2=temperture_frac%100/10;
send_ascii16(12,16,temperture_2+0x30);
temperture_3=temperture_frac%100%10;
send_ascii16(13,16,temperture_3+0x30);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -