程序如下:
//PIC C DS18B20測溫,LCD顯示.c 時間:2008-8-20
//適合3ePIC實驗板
//C語言寫的溫度計,DS18B20測溫,LCD顯示,顯示到0.1度
//USING 16F877 READ MAXIM DS18b20 AND DISPLAY ON LCD1602 */
#include
//連線說明
#define LCDRS RB1
#define LCDRW RB2
#define LCDE RB3
#define LCDDATA PORTD
#define DSDQ RB0 /* 連接ds18b20 */
#define TRISDQ TRISB0
//函數(shù)定義
void lcdinit(); //雙線模式
void wcmd (char cmd);
void wdata (char data);
void poscursor(char line, char col);
void print(char *s);
void lcdbusy();
void delay ( int t);
void itoa10(unsigned char *buf, int i);
int strlen (const char *s);
void dsinit();
void wdscmd(char cmd);
float dsread ();
//實數(shù)定義
char bank1 title[]= "Temperature is";
char dsfound[]="DS1820 FOUND";
char dsnotfound[]="DS1820 NOT FOUND";
char bank1 blank[]= " ";
// LCD 命令定義
#define CLRLCD 0X01
#define LCDMOD 0X38 /* 8 BIT, TWO LINE, 5X7 DOT MATRIX */
#define TURNON 0X0F /* turn on LCD, CURSOR, BLINKING */
#define CURMODE 0X06 /* character don't move, cursor move right automaticall */
// DS1820 命令定義
#define SKIPROM 0XCC
#define READSCRACHPAD 0XBE
#define TCONVERT 0X44
/* ORIGINAL CURSOR POSITION */
#define ORG1 0X80
#define ORG2 0XC0
//TMR0 中斷
static int count;
#define PRETMR0 210 // 50us時間間隔
float temperature;
#define DSRESET 10
#define DSRECOVER 1
char bank1 atemperature[10]; // 數(shù)子溫度
//主函數(shù)
void main()
{
int len;
ADCON1=0X07; //設(shè)置端口A為I/O
lcdinit();
print(title);
LCDE=0; /* 當 LCDE=0,LCD不起作用,操縱DS1820 */
/* 中斷TIMER0 */
OPTION=0B11001111; // bit5=0,利用內(nèi)部循環(huán)時鐘
TMR0IE=1; //timer0 使能端
PEIE=0; //關(guān)閉所有外設(shè)中斷
GIE=1;//開總中斷
poscursor(1,0);
while (1) {
dsinit();
wdscmd(SKIPROM);
wdscmd(TCONVERT);
TRISDQ=0;
DSDQ=0;
TRISDQ=1;
while(1)
{ //轉(zhuǎn)換是否完成
if (DSDQ)
break;
else
continue;
}
dsinit();
wdscmd(SKIPROM);
wdscmd(READSCRACHPAD);
temperature=dsread(); // 讀溫度
itoa10(atemperature, (int) (temperature * 10)); //itoa10只有是正數(shù)時起作用
// 所以乘以10,擴大10倍,然后增加小數(shù)點
// 再最后一個數(shù)前增加小數(shù)點
len=strlen(atemperature);
atemperature[len]=atemperature[len-1];
atemperature[len-1]='.';
atemperature[len+1]=0;
poscursor(0,0);
print (title);
poscursor(1,0);
print (blank);
poscursor(1,2);
print (atemperature);
print (blank);
delay(10000); //再得到新的溫度前延時10000*50 us=0.5 s
}
}
//延時函數(shù)
void delay (int t){
count=0; // 重設(shè)置中斷 timer 0 */
TMR0=PRETMR0;
while (count
static void interrupt isr(void) // 中斷函數(shù)
{
if(TMR0IF) // 中斷是否溢出
count++; // Add 1 to count - insert idle comment
TMR0IF = 0; // 清除中斷標志位
TMR0=PRETMR0;
}
int strlen(const char * s)
{
const char *cp;
cp = s;
while(*cp++)
continue;
return cp-s-1;
}
void print(char *s){
int len;
int atmp;
len=strlen(s);
for (atmp=0; atmp
}
// 檢查總線是否忙
void lcdbusy(){
// char tmpbusy;
TRISB &= 0xF1;
TRISD=0XFF;
LCDRS=0;
LCDRW=1;
LCDE=1; /* SET the LCD read/writing operation IN TO IDLE */
while(LCDDATA & 0X80);
}
/* 光標, line= 0 for first line, or 1 for second line,col=0 - 15 ;*/
void poscursor(char line, char col){
char pos;
lcdbusy();
pos = line * 0X40 + 0X80 + col;
wcmd(pos);
}
//寫數(shù)據(jù)
void wdata(char data) {
lcdbusy();
TRISB & =0XF1;
TRISD=0;
LCDDATA=data;
LCDRS=1;
LCDRW=0;
LCDE=0;
asm("NOP");
asm("NOP");
LCDE=1;
}
//寫命令
void wcmd(char cmd){
lcdbusy();
TRISB & =0XF1;
TRISD=0;
LCDDATA=cmd;
LCDRS=0;
LCDRW=0;
LCDE=0;
asm("NOP");
asm("NOP");
LCDE=1;
}
//LCD初始化
void lcdinit() {
lcdbusy();
TRISB &= 0XF1;
TRISD=0;
wcmd(CLRLCD);
wcmd(LCDMOD);
wcmd(TURNON);
wcmd(CURMODE);
poscursor(0,0); // 設(shè)置光標的起點
}
//DS18B20初始化設(shè)置,RB0連接DQ
void dsinit(){
TRISDQ=0;
DSDQ=0;
delay(20); //保持DSDQ低為10*50 us,重啟DS1820 */
TRISDQ=1;
delay(1); //延時,跳出采樣窗口
if (!DSDQ) /* sample window between min end:15+60, max begin,60 */
/* garrente sample window is between 60 to 75 us */
print(dsfound);
else
print(dsnotfound);
delay(10); //延時480us
}
/* write command to DS1820*/
void wdscmd(char cmd)
{
char tmp;
char i;
TRISDQ=0;
for(tmp=8;tmp>0;tmp--)
{
TRISDQ=0;
DSDQ= 0;
asm ("NOP");
asm ("NOP");
asm ("NOP");
asm ("NOP");
asm ("NOP");
if (cmd & 0x01)
{
TRISDQ=1; //釋放總線
delay(1); //延時60 us
for (i=5;i>0;i--);
}
else {
DSDQ=0 ;
delay(1);
for (i=5;i>0;i--);
TRISDQ=1;
}
cmd=cmd/2;
}
}
//讀溫度
float dsread () {
char tmp=0x01;
float t;
union{
char c[2];
int x;
}temp;
temp.x=0;
while (tmp){ //讀8字節(jié)
TRISDQ=0;
DSDQ=0;
asm("NOP");
TRISDQ=1; // 釋放總線
if (DSDQ) // "1" presented
temp.c[0] |= tmp;
tmp=tmp<<1;
delay(2);
}
tmp=1;
while (tmp){ // 讀八字節(jié)
TRISDQ=0;
DSDQ=0;
asm("NOP");
TRISDQ=1; // 釋放總線
asm("NOP");
if (DSDQ) // "1" presented
temp.c[1] |= tmp;
tmp=tmp<<1;
delay(2);
}
t=((float) temp.x)/16.0 ;
return t;
}
/* Convert integer 'i', radix 10 to a string */
void itoa10(unsigned char *buf, int i) {
unsigned int rem;
unsigned char *s,length=0;
s = buf;
if (i == 0)
*s++ = '0';
else {
if (i < 0) {
*buf++ = '-';
s = buf;
i = -i;
}
while (i)
{
++length;
rem = i % 10;
*s++ = rem + '0';
i /= 10;
}
for(rem=0; ((unsigned char)rem)
*(buf+length) = *(buf+((unsigned char)rem));
*(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1));
*(buf+(length-((unsigned char)rem)-1)) = *(buf+length);
}
}
*s=0;
}