高级会员
- 积分
- 581
- 威望
- 256
- 贡献
- 273
- 兑换币
- 154
- 注册时间
- 2012-10-28
- 在线时间
- 26 小时
|
#include <hidef.h>
#include "derivative.h"
#define LCD_EN PORTA_PA2
#define LCD_RW PORTA_PA1
#define LCD_RS PORTA_PA0
#define LCD_PSB PORTA_PA3
#define DQ PORTA_PA4
#define LCD_DAT PORTB
#define uchar unsigned char
#define uint unsigned int
uchar tem;
uchar disps1[]={"温度"};
uchar disps2[]={" "};
uchar rade_byte();
uchar readtemp();
/**************************************延时函数******************************/
void delay_50us(uint t)
{ uchar j;
for(;t>0;t--)
for(j=1;j>0;j--);
}
/*************************************ds18b20初始化***************************/
void init_ds18b20()
{ uchar n;
DQ=1;
delay_50us(2);
DQ=0;
delay_50us(5);
DQ=1;
delay_50us(2);
n=DQ;
delay_50us(4);
}
/*******************************************************************************/
void write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{ DQ=0;
DQ=dat&0x01;
delay_50us(4);
DQ=1;
dat>>=1;
}
delay_50us(4);
}
/*****************************LCD写命令************************/
void write_command(unsigned char cmd)
{ LCD_RS=0;
LCD_RW=0;
write_byte(cmd);
write_byte(cmd<<4);
}
/****************************LCD写数据*************************/
void write_data(unsigned char dat)
{ LCD_RS=1;
LCD_RW=0;
write_byte(dat);
write_byte(dat<<4);
}
/***************************************************************/
uchar read_byte()
{
uchar i ,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ) ////采样
value|=0x80;
delay_50us(4);
}
return value;
}
/********************************读温度*************************/
uchar readtemp()
{ uchar a,b;
init_ds18b20();
write_byte(0xcc); //跳过rom
write_byte(0x44); //启动温度测量
delay_50us(60);
init_ds18b20();
write_byte(0xcc);
write_byte(0xbe); //读温度
a=read_byte();
b=read_byte();
b>>=4;
b+=(a&0xf0)>>4;
return b;}
/*********************************************************************/
void LCD_pos(uchar X,uchar Y)
{ uchar pos;
if(X==0)
{
X=0x80;
}
else if(X==1)
{ X=0x90;
}
pos=X+Y;
write_command(pos);
}
/*************************************LCD初始化*************************/
void lcd_init()
{ LCD_PSB=1;
write_command(0x30);
delay_50us(10);
write_command(0x0c);
delay_50us(10);
write_command(0x01);
delay_50us(10);
}
/***************************************主函数***************************/
void main(void)
{
uchar s;
uchar a,b;
for(;;)
{
lcd_init();
LCD_pos(0,0);
tem=readtemp();
s=0;
while(disps1[s]!='\0')
{
write_data(disps1[s]);
s++;
}
LCD_pos(1,0);
a=tem%10;
b=tem/10;
while(disps2[s]!='\0')
{ write_data(b);
write_data(a);
delay_50us(1);
s++;
}
}
}
|
|