请高手指点!
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_RS = P2 ^ 0;
sbit LCD_RW = P2 ^ 1;
sbit LCD_EN = P2 ^ 2;
sbit LCD_PSB = P2 ^ 3;
sbit LCD_RST = P2 ^ 5;
sbit DQ=P3^7;
sfr P1M0 =0X92;
uchar temp,I;
uchar code DIS[]={"当前温度27℃"};
uchar code shuzu[]={"0123456789"};
void delayds18b20(uchar i)
{
while(i--);
}
void delayms(uint ms)
{
uchar y;
while (ms--)
{
for (y = 0; y < 114; y++);
}
}
void delayNOP()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void lcd_wcmd(uchar cmd)
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=cmd;
delayms(5);
LCD_EN=1;
delayms(5);
LCD_EN=0;
}
void lcd_wdat(uchar dat)
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delayms(5);
LCD_EN=1;
delayms(5);
LCD_EN=0;
}
void lcd_init()//12864液晶初始化
{
LCD_PSB=1;
delayms(5);
lcd_wcmd(0x30);
delayms(5);
lcd_wcmd(0x0C);
delayms(5);
lcd_wcmd(0x06);
delayms(5);
lcd_wcmd(0x01);
delayms(5);
}
void lcd_pos(uchar X, uchar Y)
{
uchar pos;
if (X==1)
{
X=0x80;
}
else if (X==2)
{
X=0x90;
}
else if (X==3)
{
X=0x88;
}
else if (X==4)
{
X=0x98;
}
pos=X+Y; //确定显示地址
lcd_wcmd(pos); //写显示地址
}
void xianshi()
{
uchar k,l;
k=shuzu[temp%10];
l=shuzu[temp/10];
lcd_pos(2,4);
lcd_wdat(l+0x30);
lcd_wdat(k+0x30);
}
void init_ds18b20(void)
{
uchar n;
DQ=1;
delayds18b20(8);
DQ=0;
delayds18b20(80);
DQ=1;
delayds18b20(8);
n=DQ;
delayds18b20(4);
}
void ds18b20write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delayds18b20(4);
DQ=1;
dat>>=1;
}
delayds18b20(4);
}
uchar ds18b20read_byte(void)
{
uchar i,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ)
value|=0x80;
delayds18b20(4);
}
return value;
}
uchar readtemperature(void)
{
uchar a,b;
init_ds18b20();
ds18b20write_byte(0xcc); //跳过ROM
ds18b20write_byte(0x44); //启动温度测量
delayds18b20(300);
init_ds18b20();
ds18b20write_byte(0xcc);
ds18b20write_byte(0xbe);
a=ds18b20read_byte();
b=ds18b20read_byte();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void main(void)
{
P1M0=0XB0;
temp=readtemperature();
delayms(1);
P0=0xff;
P0=0;
lcd_init();
lcd_pos(2,0);
I=0;
while(DIS[I]!='\0')
{lcd_wdat(DIS[I]);
I++;}
delayms(1);
while(1)
{
xianshi();
}
}
这种程序网上一大堆,主要看一下读写时序,是否正确,不行直接用示波器看
谢谢!
看看时序对不,这样看也很难能够看出来
18B20的读写时序我已经详细看过没有错.也把18B20程序函数放到数码上读写显示正常,就是放到12864上就出这问题,难道是12864函数出错。没有老师带,自学真的很难。坚持..再坚持
终于找出问题出错在那里了。谢谢,小编和玉堂兄
问题出在哪里呢?希望能给我们新手解答
我的表达能力很弱,我把程序贴出来,大家自己对比看看OK!
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_RS = P2 ^ 0;
sbit LCD_RW = P2 ^ 1;
sbit LCD_EN = P2 ^ 2;
sbit LCD_PSB = P2 ^ 3;
sbit LCD_RST = P2 ^ 5;
sbit DQ=P3^7;
sfr P1M0 =0X92;
uchar I,temp;
uchar code DIS4[16]={"当前温度27℃"};
uchar code shuzu[]={"0123456789"};
void delayus(uchar i)
{
while(i--);
}
void delayms(uint ms)
{
uchar y;
while (ms--)
{
for (y = 0; y < 114; y++);
}
}
void delayNOP()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void lcd_wcmd(uchar cmd)
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=cmd;
delayms(5);
LCD_EN=1;
delayms(5);
LCD_EN=0;
}
void lcd_wdat(uchar dat)
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delayms(5);
LCD_EN=1;
delayms(5);
LCD_EN=0;
}
void lcd_init()//12864液晶初始化
{
LCD_PSB=1;
delayms(5);
lcd_wcmd(0x30);
delayms(5);
lcd_wcmd(0x0C);
delayms(5);
lcd_wcmd(0x06);
delayms(5);
lcd_wcmd(0x01);
delayms(5);
}
void lcd_pos(uchar X, uchar Y)
{
uchar pos;
if (X==1)
{
X=0x80;
}
else if (X==2)
{
X=0x90;
}
else if (X==3)
{
X=0x88;
}
else if (X==4)
{
X=0x98;
}
pos=X+Y; //确定显示地址
lcd_wcmd(pos); //写显示地址
}
void xianshi()
{
uchar k,l;
k=temp%10;
l=temp/10;
lcd_pos(4,4);
lcd_wdat(l+0x30);
lcd_wdat(k+0x30);
}
void init_ds18b20()
{
uchar j;
DQ=0;
delayus(100);
DQ=1;
delayus(5);
j=DQ;
delayus(15);
}
void ds18b20write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delayus(8);
DQ=1;
dat>>=1;
}
delayus(8);
}
uchar ds18b20read_byte(void)
{
uchar j,value;
for(j=0;j<8;j++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ==1)
value|=0x80;
delayus(6);
}
return value;
}
uchar readtemperature()
{
uchar a,b;
init_ds18b20();
ds18b20write_byte(0xcc); //跳过ROM
ds18b20write_byte(0x44); //启动温度测量
delayus(30);
init_ds18b20();
ds18b20write_byte(0xcc);
ds18b20write_byte(0xbe);
a=ds18b20read_byte();
b=ds18b20read_byte();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void wenduweiz()
{
{
lcd_pos(4,3);
lcd_wcmd(0x0f);
{
shuzu[temp];
}
}
xianshi();
}
void main(void)
{
P1M0=0XB0;
init_ds18b20();
delayms(10);
P0=0xff;
P0=0;
lcd_init();
lcd_pos(4,0);
I=0;
while(DIS4[I]!='\0')
{lcd_wdat(DIS4[I]);
I++;}
delayms(100);
while(1)
{
temp=readtemperature();
xianshi();
}
}