/*-----------------------------------------------
名称:温度显示
日期:2012.11
P0.0-P0.7 LCD_1602 IO
P0.8=LCD_RS, P0.9=LCD_RW,
P0.10=LCD_E, P0.11=DQ
问题应该是在 uchar ReadOneChar(void)
刚接触ARM 求指点一下
------------------------------------------------*/
#include <LPC21xx.H>
#define uchar unsigned char
#define uint unsigned int
char table[]="l_Temp ";
char table1[]=" : ";
uchar num,temp,ftemp;
/*------------------------------------------------
延迟
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
初始化ds18b20
------------------------------------------------*/
void Init_DS18B20(void)
{
IO0SET=0x00000800; //DQ
DelayUs2x(5); //稍做延时
IO0CLR=0x00000800;
DelayUs2x(100); //精确延时 大于 480us 小于960us
IO0SET=0x00000800;
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
}
/*------------------------------------------------
读取一个字节
------------------------------------------------*/
uchar ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
IO0DIR=0x000007ff; //设置DQ为输入模式
for (i=8;i>0;i--)
{
dat>>=1;
IO0DIR=0x00000fff;
IO0SET=0x00000800;
IO0CLR=0x00000800;
IO0SET=0x00000800;
IO0DIR=0x000007ff;
if(IO0PIN&0x00000800==0x00000800) //读取引脚状态,DQ=1,插入1,DQ=0,直接移位
dat|=0x00000080;
DelayUs2x(10);
}
return(dat);
}
/*------------------------------------------------
写入一个字节
------------------------------------------------*/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
IO0DIR=0x00000fff; //设置DQ为输出模式
for (i=8; i>0; i--)
{
IO0SET=0x00000800;
IO0CLR=0x00000800;
if(dat&0x00000001)
IO0SET=0x00000800;
else IO0CLR=0x00000800;
DelayUs2x(10);
IO0SET=0x00000800;
dat>>=1;
}
DelayUs2x(25);
}
/*------------------------------------------------
读取温度
------------------------------------------------*/
int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就
a=ReadOneChar(); //低位
b=ReadOneChar(); //高位
temp=b;
temp<<=4;
temp<<=4;
temp=temp|a;
ftemp=temp*0.0625;
temp=ftemp*10+0.5;
ftemp=ftemp+0.05;
return temp;
}
/*------------------------------------------------
LCD1602初始化
------------------------------------------------*/
void write_com(uchar com)
{
IO0CLR=0x00000700;
IO0SET=com;
IO0CLR=(~com&0x000000ff);
DelayMs(5);
IO0SET=0x00000400;
DelayMs(5);
IO0CLR=0x00000400;
}
void write_date(uchar date)
{
IO0CLR=0x00000200;
IO0SET=0x00000100;
IO0SET=date;
IO0CLR=(~date&0x000000ff);
DelayMs(5);
IO0SET=0x00000400;
DelayMs(5);
IO0CLR=0x00000400;
}
void Init_1602()
{
IO0CLR=0x00000600;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+3);
for(num=0;num<11;num++)
{
write_date(table[num]);
DelayMs(30);
}
write_com(0x80+0x40+4);
for(num=0;num<9;num++)
{
write_date(table1[num]);
DelayMs(30);
}
}
void write_1602(uint date)
{ uint qian,bai,shi;
qian=date/100;
write_com(0xc6);
write_date(0x30+qian);
bai=date%100/10;
write_com(0xc8);
write_date(0x30+bai);
shi=date%10;
write_com(0xc9);
write_date(0x30+shi);
write_com(0xcb);
write_date('C');
}
/*------------------------------------------------
主程序
------------------------------------------------*/
int main()
{
PINSEL0 = 0x00000000; //低16位设置为I/O模式
IO0DIR = 0x00000fff; //0-11输出
Init_1602();
while(1)
{
write_1602(ReadTemperature());
DelayMs(20);
}
} |