不懂错在哪了,实在调不出来了,求大家帮忙解答一下,谢谢
以下为代码:
#include<iom16v.h>
#include<macros.h>
#define uint unsigned int
#define uchar unsigned char
#define sclon PORTA|=BIT(0)
#define scloff PORTA&=~BIT(0)
#define dion PORTA|=BIT(1)
#define dioff PORTA&=~BIT(1)
#define dqon PORTD|=BIT(2)
#define dqoff PORTD&=~BIT(2)
float temperature;
uchar templ=0;
uchar temph=0;
float t;
extern const unsigned char
tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/*void delay(uint z)
{
uint x,y;
for(x=0;x<110;x++)
for(y=z;y>0;y--);
}*/
void dataout(uchar temp)
{
uchar i,temp1;
temp1=tab[temp];
for(i=0;i<8;i++)
{
scloff;
NOP();
if((temp1&0x80)!=0x80)
{
dioff;
NOP();
}
else
{
NOP();
dion;
NOP();
}
sclon;
NOP();
NOP();
temp1<<=1;
NOP();
NOP();
//scloff;
}
}
void delay1(void)
{
uint i;
for(i=0;i<200;i++);
}
void delay(uint k)
{
uint n;
n=0;
while(n<k)
{
n++;
}
return;
}
void display(uint k)
{
PORTA&=0x03;
dataout(k/1000);
delay(200);
PORTA|=0x04;
dataout(k/100%10);
delay(200);
PORTA&=0x0b;
PORTA|=0x08;
dataout(k/10%10);
delay(200);
PORTA|=0x0c;
dataout(k%10);
delay(200);
}
void init_18b20(void)
{
uchar x=0;
dqon;
delay(8);
dqoff;
delay(85);
dqon;
delay(14);
}
read_18b20(void)
{
uchar i=0,data=0;
for(i=10;i>2;i--)
{
dqon;
delay(1);
dqoff;
data>>=1;
dqon;
DDRD=0xfb;
if(PIND&0x04)
data|=0x80;
DDRD=0xff;
delay(4);
}
return (data);
}
write_18b20(uchar data)
{
uchar i=0;
for(i=10;i>2;i--)
{
dqoff;
if(data&0x01)
{
dqon;
}
else
{
dqoff;
}
delay(5);
dqon;
data>>=1;
}
delay(4);
}
read_temp(void)
{
init_18b20();
write_18b20(0xcc);
write_18b20(0x44);
delay(125);
init_18b20();
write_18b20(0xcc);
write_18b20(0xbe);
templ=read_18b20();
temph=read_18b20();
temperature=((temph*256)+templ)*0.0625;
delay(200);
return (temperature);
}
void main(void)
{
DDRA=0xff;
DDRD=0xff;
display(0);
while(1)
{
//delay();
t=read_temp();
display(t);
//delay();
}
NOP();
图片如下:
}
|