相当年,为了找一个可以用的DS18B20程序,不止在网上搜了多久,下过来的基本不能用.现在DS18B20的程序满天飞,这个CVAVR版本的DS18B20驱动程式,估计是我最老的版本了,这里连同1602,做了个类似温度计的程式.源码如下:
#include <mega16.h>
#include<delay.h>
#define DQ PORTD.0//set appoint bit low
#define DDQ DDRD.0//set appoint bit high
#define GDQ PIND.0//get appoint bit level
#define LCD_DATA_PORT PORTA //the next three port should be the same
#define LCD_DATA_DDR DDRA //must use the high 4bit
#define LCD_DATA_PIN PINA
#define LCD_RS PORTA.0 //RS lcd
#define LCD_WR PORTA.1//WR lcd
#define LCD_EN PORTA.2 //EN lcd
#define LCD_DRS DDRA.0 //WR direction define
#define LCD_DWR DDRA.1 //RS direction define
#define LCD_DEN DDRA.2 //EN direction define
#define LCD_DATA 0xf0 //DATA PORT
#define uchar unsigned char
#define uint unsigned int
/*
LCD_Write(1,command)
1602 command:0x0f show cursor and flash
0x0c do now show cursor
*/
void LCD_en_write(void) //enable LCD
{
LCD_DEN=1;//SET LCD_EN OUTPUT
LCD_EN=1;//EN=1
delay_us(10);
LCD_EN=0;//EN=0
}
//cord:when 1,write command when 0,write data
//data:command or data you want to write to 1602
void LCD_Write(char cord,unsigned char data) //write data
{
delay_us(25);
LCD_DRS=1;//SET RS OUTPUT
if(cord==0)LCD_RS=1; //RS=1,write data
else LCD_RS=0;//RS=0,write command
LCD_DATA_PORT&=0X0f; //clr high 4bit
LCD_DATA_PORT|=data&0xf0; //wirte high 4bit
LCD_en_write();
data=data<<4; //turn the low 4bit to high 4bit
LCD_DATA_PORT&=0X0f; //clr high 4bit
LCD_DATA_PORT|=data&0xf0; //write low 4bit
LCD_en_write();
}
void LCD_init(void) //lcd init
{
LCD_DWR=1;//set en output
LCD_WR=0;//write enable
LCD_DATA_DDR|=LCD_DATA; //set data port out
LCD_EN=1;// set EN out
LCD_RS=1;// set RS out
delay_us(40);
LCD_Write(1,0x28); //4bit show
LCD_Write(1,0x0c); //do now show cursor
LCD_Write(1,0x01); //clr
delay_ms(2);
}
//orientation a dress
//x:0 or 1
//y:0-15
void LCD_set_xy( unsigned char x, unsigned char y ) //write address funcation
{
unsigned char address;
if (y==0) address=0x80+x;
else address=0xc0+x;
LCD_Write(1,address);
}
//from a appointed address write string to 1602
//col x=0~15,row y=0,1
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char flash *s)
{
LCD_set_xy(X,Y); //write address
while (*s) // write the char to show
{
LCD_Write(0,*s);
s++;
}
}
//write a char to appointed address
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //col x=0~15,row y=0,1
{
LCD_set_xy(X,Y); //write address
LCD_Write(0,data);
}
//reset ds1820
void res1820 (void) // Reset TX
{
DDQ=1;//SET PA0 OUTPUT
DQ=0;//PA0 0
delay_us(750); // Approx 750 uS
DQ=1;//PA0 1
delay_us(15);//15US
}
//wait for presence of 1820
void check1820 (void) // Wait for Presence RX 'ó?úó|'e
{
DDQ=0;//SET PA0 INPUT
while (GDQ==1);
while (GDQ==0);
delay_us(45);//??
}
//read bit data from 1820
unsigned char read1820bit (void) // read one bit
{
unsigned char dat;
DDQ=1;//SET PA0 OUTPUT
DQ=0;
delay_us(1);
DQ=1;
DDQ=0;//SET PA0 INPUT
delay_us(2);
dat=GDQ;
delay_us(45);
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;
DDQ=1;//SET PA0 OUTPUT;
for (j=1;j<=8;j++) {
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DQ=0; // Write 1
delay_us(2);
DQ=1;
delay_us(45);
}
else
{
DQ=0; // Write 0
delay_us(45);
DQ=1;
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;//x^0=1
for(i=0;i<y-1;i++)ans*=x;
return ans;
}
//get data from 1820
//return temperture range:-55.0~+125.9
int get1820temp(void)
{
unsigned char TL,TH,j,jud;
float t;
start1820 ();// ds1820 start convert
read1820 (&TH,&TL);// read temperature
jud=TH;//juder weather temperature is >0 or <0
for(j=0;j<11;j++)//get the temperature
{
if((jud>>4)==0)//T>0
{
if(j<8)//low 8 bit
{
t+=(TL&0x01)*pow(2,j);
TL>>=1;
}
else //high 3 bit
{
t+=(TH&0x01)*pow(2,j);
TH>>=1;
}
}
else //t<0
{
if(j<8)//low 8 bit
{
t+=(!(TL&0x01))*pow(2,j);
TL>>=1;
}
else //high 3 bit
{
t+=(!(TH&0x01))*pow(2,j);
TH>>=1;
}
}
}
t*=0.625;//change
if(jud>>4)return -t;//T<0
else return t;//T>0
}
void main(void)
{
unsigned char conv[10]={'0','1','2','3','4','5','6','7','8','9'};
int tem;
delay_ms(200);//delay 4s
LCD_init();
LCD_write_string(0,0," Temperture is:");
while(1)
{
tem=get1820temp();
LCD_write_char(11,1,'C');
LCD_write_char(10,1,223);
LCD_write_char(8,1,conv[tem%10]);
LCD_write_char(7,1,'.');
LCD_write_char(6,1,conv[(tem/10)%10]);
LCD_write_char(5,1,conv[(tem/100)%10]);
delay_ms(300);
}
} |