本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
我用18B20测温度,然后用1602显示 ,,现在显示部分没问题了,,只是从18B20读取出来的数总是在13(十进制)以下跳动,,数不对,而且还不断跳动,,请问为什么
#include<reg52.h>
#include <intrins.h>
sbit rs=P3^5;
sbit lcd_en=P3^4;
sbit b1820_data=P2^2;
sbit led_en=P2^5;
#define uchar unsigned char
#define ushort unsigned short
uchar table[11]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x00};
void delayms(int ms);
void delayus(int us);
void lcd_init();
void lcd_wcom(uchar command);
void lcd_wdata(uchar command);
void b1820_getdata(uchar *th,uchar *tl);
uchar b1820_init();
void b1820_write(uchar command);
uchar b1820_read();
uchar b1820_readonebit();
void b1820_write0();
void b1820_write1();
uchar b1820_reset();
void main()
{
uchar tl,th;
ushort value;
lcd_init();
while(0 == b1820_init())
{
static int i=0;
if(0 == i)
{
lcd_wcom(0x01); //clear screen
lcd_wcom(0x80); //设置DDRAM地址
lcd_wdata(0x65); //e
lcd_wcom(0x80+0x01); //设置DDRAM地址
lcd_wdata(0x72); //r
lcd_wcom(0x80+0x02); //设置DDRAM地址
lcd_wdata(0x72); //r
lcd_wcom(0x80+0x03); //设置DDRAM地址
lcd_wdata(0x6f); //o
lcd_wcom(0x80+0x04); //设置DDRAM地址
lcd_wdata(0x72); //r
i=1;
}
}
lcd_init();
while(1)
{
b1820_getdata(&th,&tl);
value=th<<8|tl;
value>>=4;
th=value;
led_en=1;
P1=value;
if(th&0x80)
{
lcd_wcom(0x80+0x40); //设置DDRAM地址
lcd_wdata(0x2d); //-
th=~th;
}
else
{
lcd_wcom(0x80+0x40); //设置DDRAM地址
lcd_wdata(0x2b); //+
}
value=th;
th=value/10;
th%=10;
th=table[th];
lcd_wcom(0x80+0x41);
lcd_wdata(th);
th=value%10;
th=table[th];
lcd_wcom(0x80+0x42);
lcd_wdata(th);
if(tl&0x01)
{
//.0625
lcd_wcom(0x80+0x43);
lcd_wdata(0x2e); //.
lcd_wcom(0x80+0x44);
lcd_wdata(0x30); //0
lcd_wcom(0x80+0x45);
lcd_wdata(0x36); //6
lcd_wcom(0x80+0x46);
lcd_wdata(0x32); //2
lcd_wcom(0x80+0x47);
lcd_wdata(0x35); //5
}
else
{
if(tl&0x02)
{ //.125
lcd_wcom(0x80+0x43);
lcd_wdata(0x2e); //.
lcd_wcom(0x80+0x44);
lcd_wdata(0x31); //1
lcd_wcom(0x80+0x45);
lcd_wdata(0x32); //2
lcd_wcom(0x80+0x46);
lcd_wdata(0x35); //5
lcd_wcom(0x80+0x47);
lcd_wdata(0x20); //无
}
else
{
if(tl&0x04)
{
//.25
lcd_wcom(0x80+0x43);
lcd_wdata(0x2e); //.
lcd_wcom(0x80+0x44);
lcd_wdata(0x32); //2
lcd_wcom(0x80+0x45);
lcd_wdata(0x35); //5
lcd_wcom(0x80+0x46);
lcd_wdata(0x20); //无
lcd_wcom(0x80+0x47);
lcd_wdata(0x20); //无
}
else
{
if(tl&0x08)
{
//.5
lcd_wcom(0x80+0x43);
lcd_wdata(0x2e); //.
lcd_wcom(0x80+0x44);
lcd_wdata(0x35); //5
lcd_wcom(0x80+0x45);
lcd_wdata(0x20); //无
lcd_wcom(0x80+0x46);
lcd_wdata(0x20); //无
lcd_wcom(0x80+0x47);
lcd_wdata(0x20); //无
}
else
{
//清空小数位显示
lcd_wcom(0x80+0x43);
lcd_wdata(0x20); //无
lcd_wcom(0x80+0x44);
lcd_wdata(0x20); //无
lcd_wcom(0x80+0x45);
lcd_wdata(0x20); //无
lcd_wcom(0x80+0x46);
lcd_wdata(0x20); //无
lcd_wcom(0x80+0x47);
lcd_wdata(0x20); //无
}
}
}
}
delayms(1000);
}
}
void delayus(int us)
{
while(us--)
;
}
void delayms(int ms)
{
while(ms--)
{
int i=0;
for(;i<75;i++)
;
}
}
void lcd_init()
{
delayms(15);
lcd_wcom(0x38);
delayms(5);
lcd_wcom(0x38);
delayms(5);
lcd_wcom(0x38);
//设置显示模式
lcd_wcom(0x38);
delayms(100);
//显示关闭
lcd_wcom(0x08);
delayms(100);
//显示清屏
lcd_wcom(0x01);
delayms(100);
//显示光标移动设置
lcd_wcom(0x06);
delayms(100);
//显示开及关标设置
lcd_wcom(0x0c);
delayms(100);
//初始化数据
lcd_wcom(0x80); //设置DDRAM地址
lcd_wdata(0x74); //t
lcd_wcom(0x80+0x01); //设置DDRAM地址
lcd_wdata(0x65); //e
lcd_wcom(0x80+0x02); //设置DDRAM地址
lcd_wdata(0x6d); //m
lcd_wcom(0x80+0x03); //设置DDRAM地址
lcd_wdata(0x70); //p
lcd_wcom(0x80+0x04); //设置DDRAM地址
lcd_wdata(0x65); //e
lcd_wcom(0x80+0x05); //设置DDRAM地址
lcd_wdata(0x72); //r
lcd_wcom(0x80+0x06); //设置DDRAM地址
lcd_wdata(0x61); //a
lcd_wcom(0x80+0x07); //设置DDRAM地址
lcd_wdata(0x74); //t
lcd_wcom(0x80+0x08); //设置DDRAM地址
lcd_wdata(0x75); //u
lcd_wcom(0x80+0x09); //设置DDRAM地址
lcd_wdata(0x72); //r
lcd_wcom(0x80+0x0a); //设置DDRAM地址
lcd_wdata(0x65); //e
lcd_wcom(0x80+0x0b); //设置DDRAM地址
lcd_wdata(0x28); //(
lcd_wcom(0x80+0x0c); //设置DDRAM地址
lcd_wdata(0xdf); //'
lcd_wcom(0x80+0x0d); //设置DDRAM地址
lcd_wdata(0x43); //c
lcd_wcom(0x80+0x0e); //设置DDRAM地址
lcd_wdata(0x29); //)
lcd_wcom(0x80+0x0f); //设置DDRAM地址
lcd_wdata(0x3a); //:
}
void lcd_wcom(uchar command)
{
rs=0;
P0=command;
delayus(40);
lcd_en=1;
delayus(25);
delayus(150);
lcd_en=0;
delayus(25);
delayus(10);
}
void lcd_wdata(uchar command)
{
rs=1;
P0=command;
delayus(40);
lcd_en=1;
delayus(25);
delayus(150);
lcd_en=0;
delayus(25);
delayus(10);
}
uchar b1820_reset()
{
int i=1,flag=0;
b1820_data=1;
delayus(50);
b1820_data=0;
delayus(70); //700us
b1820_data=1;
delayus(5); //60us
//240us ???
while(i--)
{
if(0 == b1820_data&&0 == flag)
{
flag=1;
}
}
b1820_data=1;
delayus(23); //240us
if(1 == flag)
{
return 1;
}
else
{
return 0;
}
}
void b1820_write1()
{
b1820_data=1;
delayus(50); //50us
b1820_data=0;
_nop_(); //5us
_nop_();
_nop_();
_nop_();
_nop_();
b1820_data=1;
delayus(4); //56us
}
void b1820_write0()
{
b1820_data=1;
delayus(50);
b1820_data=0;
delayus(5); //60us
b1820_data=1;
delayus(30); //30us
}
uchar b1820_readonebit()
{
int flag;
b1820_data=1;
delayus(50);
b1820_data=0;
_nop_(); //3us
_nop_();
_nop_();
b1820_data=1;
_nop_(); //7us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
//5us
// _nop_();
flag=b1820_data;
// _nop_();
delayus(3); //45us
b1820_data=1;
_nop_(); //10us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(1 == flag)
{
return 1;
}
else
{
return 0;
}
}
uchar b1820_read()
{
int i;
uchar t;
for(i=0;i<8;i++)
{
t=t<<1;
t+=b1820_readonebit();
}
return t;
}
void b1820_write(uchar command)
{
int i;
for(i=0;i<8;i++)
{
if(command&0x01)
{
b1820_write1();
}
else
{
b1820_write0();
}
command=command>>1;
}
}
uchar b1820_init()
{
if(1 == b1820_reset() )
{
b1820_write(0xcc);
b1820_write(0x4e);
b1820_write(0x07);// set TH = 0000 0111
b1820_write(0xFF);// set TL = 1101 0000
b1820_write(0x7F);// CONFIG
b1820_reset();
return 1;
}
else
{
return 0;
}
}
void b1820_getdata(uchar *th,uchar *tl)
{
if(1 == b1820_reset())
{
b1820_write(0xcc); //skin rom command
b1820_write(0x44); //begin to convert
b1820_data=1;
delayms(750); //<tconv
b1820_reset();
b1820_write(0xcc); //skin rom command
b1820_write(0xbe); //read scratchpad command
*tl=b1820_read();
*th=b1820_read();
b1820_reset();
}
else
{
*th=0xff;
*tl=0xff;
}
} |