深夜发帖!这是avr驱动DS18B20的源码,数码管显示温度。源码如下:
#include <mega16.h>
#include<delay.h>
#define CLRBIT(x,y) (x&=~(1<<y))//set appoint bit low
#define SETBIT(x,y) (x|=(1<<y))//set appoint bit high
#define CHKBIT(x,y) (x&(1<<y))//get appoint bit level
#define uchar unsigned char
#define uint unsigned int
//reset ds1820
void res1820 (void) // Reset TX
{
SETBIT(DDRA,0);//SET PA0 OUTPUT
CLRBIT(PORTA,0);//PA0 0
delay_us(750); // Approx 750 uS
SETBIT(PORTA,0);//PA0 1
delay_us(15);//15US
}
//wait for presence of 1820
void check1820 (void) // Wait for Presence RX 'ó?úó|'e
{
CLRBIT(DDRA,0);//SET PA0 INPUT
while (CHKBIT(PINA,0));
while (!CHKBIT(PINA,0));
delay_us(40);//??
}
//read bit data from 1820
unsigned char read1820bit (void) // read one bit
{
unsigned char dat;
SETBIT(DDRA,0);//SET PA0 OUTPUT
CLRBIT(PORTA,0);
delay_us(1);
SETBIT(PORTA,0);
CLRBIT(DDRA,0);//SET PA0 INPUT
delay_us(2);
dat=0X01&CHKBIT(PINA,0);
delay_us(40);
return (dat);
}
//read one byte from 1820
unsigned char read1820byte (void) // read one byte
{
unsigned char i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j = read1820bit ();
dat = (j << 7) | (dat >> 1);
}
return (dat);
}
//write one byte to 1820
void write1820byte (unsigned char dat) // write one byte
{
unsigned char j;
unsigned char testb;
SETBIT(DDRA,0);//SET PA0 OUTPUT;
for (j=1;j<=8;j++) {
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
CLRBIT(PORTA,0); // Write 1
delay_us(2);
SETBIT(PORTA,0);
delay_us(45);
}
else
{
CLRBIT(PORTA,0); // Write 0
delay_us(45);
SETBIT(PORTA,0);
delay_us(2);
}
}
}
//start conver temperature
void start1820 (void) // ds1820 start convert
{
res1820 ();
check1820 ();
//delay_ms(1);
write1820byte (0xcc); // skip rom
write1820byte (0x44); // convert
}
//read temperature from1820
//TH,high 4 bit of temperature
//TL,low 8 bit of temperature
void read1820 (unsigned char *TH,unsigned char *TL) // read temp
{
res1820 ();
check1820 ();
//delay_ms(1);
write1820byte (0xcc); // skip rom
write1820byte (0xbe); // convert
*TL=read1820byte(); // LSB
*TH=read1820byte(); // MSB
}
//x^y function,do not beyond the range!
long pow(unsigned char x,unsigned char y)
{
long ans;
unsigned char i;
ans=x;
if(y==0)return 1;
for(i=0;i<y-1;i++)ans*=x;
return ans;
}
//get data from 1820
//p memory the temperature(bin 11 bit)
void get1820data(unsigned char *p)
{
unsigned char j,temp;
unsigned char *TL,*TH;
start1820 (); // ds1820 start convert
read1820 (TH,TL); // read temperature
for(j=1;j<=8;j++)
{
temp=*TL&0x01;
*TL=*TL>>1;
p[j-1]=temp;
}
for(j=1;j<=3;j++)
{
temp=*TH&0x01;
*TH=*TH>>1;
p[j+7]=temp;
}
}
void display(unsigned char number,unsigned char position)//digtial show function
{
DDRB=0Xff;//set PORTA output
 ORTB=0XFF;//set PORTB output in high level
switch(position)//choice the position
{
case 1:{PORTB&=0xf8;break;}
case 2:{PORTB&=0xf9;break;}
case 3:{PORTB&=0Xfa;break;}
case 4:{PORTB&=0Xfb;break;}
case 5:{PORTB&=0Xfc;break;}
case 6:{PORTB&=0Xfd;break;}
case 7:{PORTB&=0Xfe;break;}
case 8:{PORTB&=0Xff;break;}
}
switch(number)//show number
{
case 0:{PORTB&=0x87;break;} //0
case 1:{PORTB&=0x8f;break;} //1
case 2:{PORTB&=0x97;break;} //2
case 3:{PORTB&=0x9f;break;} //3
case 4:{PORTB&=0xa7;break;} //4
case 5:{PORTB&=0xaf;break;} //5
case 6:{PORTB&=0xb7;break;} //6
case 7:{PORTB&=0xbf;break;} //7
case 8:{PORTB&=0xc7;break;} //8
case 9:{PORTB&=0xcf;break;} //9
default:{PORTB=0xff;break;} //null
}
}
void main()
{
unsigned char p[11];
uchar j;
long t;
unsigned char b1,b2,b3,b4,b5,b6,b7,bj;
while(1)
{
b1=t%10;display(b1,8);delay_ms(3);
b2=(t/10)%10;display(b2,7);delay_ms(3);
b3=(t/100)%10;display(b3,6);delay_ms(3);
b4=(t/1000)%10;display(b4,5);delay_ms(3);
b5=(t/10000)%10;display(b5,4);CLRBIT(PORTB,7);delay_ms(3);SETBIT(PORTB,7);
b6=(t/100000)%10;display(b6,3);delay_ms(3);
b7=(t/1000000)%10;display(b7,2);delay_ms(3);
if(bj<80)delay_ms(3);
else
{
get1820data(p);
t=0;
for(j=0;j<11;j++)
{
t+=p[j]*pow(2,j);
}
t*=625;
bj=0;
}
bj++;
}
} |