|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-12-12 23:16:36
|
显示全部楼层
我今天用下面程序调试了还是没结果
#include <mega16.h>
#define uchar unsigned char
#define uint unsigned int
/*******************lcd define*************/
#define lcdopen DDRB = 0xff;DDRC = 0xc0
#define lcden PORTC.6
#define lcdrs PORTC.7
/*******************DS18B20 define **********************/
#define dqout DDRA.7 = 1
#define dqin DDRA.7 = 0
#define dqset PORTA.7 = 1
#define dqcli PORTA.7 = 0
#define dq PINA.7
/*****************variable define************************/
uchar numtable[] = "0123456789.+-";
uchar table1[]="hello20121212!";
uchar table2[]="temperature:";
uchar temperature[]={0,0,0,0,0,0};
/*****************delay function*****************/
void delayus(uchar xus)
{
uchar a,b;
for(b = xus;b > 0;b--)
for(a = 5;a > 0;a--);
}
void delay1ms()
{
uchar a,b,c;
for(c = 17;c > 0;c--)
for(b = 134;b > 0;b--)
for(a = 2;a > 0;a--);
}
void delayms(uint xms)
{
uint i;
for(i = 0;i < xms;i++)
delay1ms();
}
/*****************lcd function******************/
void lcdwritedat(uchar rs,uchar dat)
{
lcdrs = rs;
PORTB = dat;
delayus(16);
lcden = 1;
lcden = 0;
if(!rs)delayus(16);
}
void lcdinit()
{
delayms(40);
lcdopen;
lcdwritedat(0,0x38);
lcdwritedat(0,0x01);
lcdwritedat(0,0x06);
lcdwritedat(0,0x0c);
}
void lcddis(uchar line,uchar addr,uchar digit,uchar *dat)
{
uchar linecode,i;
switch(line)
{
case 1:linecode = 0x00;break;
case 2:linecode = 0x40;break;
case 3:linecode = 0x14;break;
case 4:linecode = 0x54;break;
}
lcdwritedat(0,0x80+addr+linecode);
for(i = 0;i < digit;i++)
lcdwritedat(1,dat[i]);
}
/**********************************DS18B20 function********************/
void dsinit()
{
dqout;
dqcli;
delayus(480);
dqset;
delayus(480);
}
uchar dsread()
{
uchar i,k = 0;
for(i = 8;i > 0;i--)
{
dqout;
dqcli;
delayus(1);
dqset;
dqin;
k >>= 1;
if(dq)k |= 0x80;
delayus(60);
}
return (k);
}
void dswrite(uchar dat)
{
uchar i;
dqout;
for(i = 0;i < 8;i++)
{
dqcli;
delayus(1);
if(dat & 0x01)
{
dqset;
delayus(60);
}
else
{
dqcli;
delayus(60);
dqset;
delayus(2);
}
dat >>= 1;
}
}
void dschange()
{
dsinit();
dswrite(0xcc);
dswrite(0x44);
}
void dstmp()
{
uchar tmpl = 0,tmph = 0;
uint t = 0;
dsinit();
dswrite(0xcc);
dswrite(0xbe);
tmpl = dsread();
tmph = dsread();
t = tmph;
t <<= 8;
t += tmpl;
if(t <=0x7ff)
temperature[0] = numtable[11];
else
{
temperature[0] = numtable[12];
t = ~t+1;
}
t *= 0.625;
temperature[1] = numtable[t/1000];
temperature[2] = numtable[(t % 1000)/100];
temperature[3] = numtable[(t % 100)/10];
temperature[4] = numtable[10];
temperature[5] = numtable[t % 10];
}
//void displayt(uchar line,uchar address,uint t)
//{
// t *= 0.625;
// lcdwritedat(0,0x80+(line-1)*0x40 +address);
// if(tflag == 1)
// lcdwritedat(1,'-');
// else lcdwritedat(1,' ');
//
// lcdwritedat(1,numtable[t / 1000]);
// lcdwritedat(1,numtable[(t % 1000)/100]);
// lcdwritedat(1,numtable[(t%100)/10]);
// lcdwritedat(1,'.');
// lcdwritedat(1,numtable[t%10]);
//}
void main()
{
lcdinit();
lcddis(1,0,14,table1);
lcddis(2,0,12,table2);
while(1)
{
dschange();
delayms(750);
dstmp();
lcddis(3,0,6,temperature);
}
}
得到的显示的数据时-000.0,估计是没读到真实数据。求高手点解~ |
|