微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > PIC16F877A例程---DS18B20

PIC16F877A例程---DS18B20

时间:11-24 来源:互联网 点击:
#include

#include

__CONFIG(0x3F32); //芯片配置字

#define LCDRS RB2

#define LCDRW RB1

#define LCDE RB0

#define LCDDATA PORTD

#define DS18B20 RE0

#define TRIS_B20 TRISE0

void LCD1602_INIT(void);

void WRITE_LCD_CMD (char cmd);

void WRITE_LCD_DATA (char data);

void LCD_Display(char *s);

void LCD_Display_location (char line, char col);

void LCD1602_BUSY();

void US_delay ( int t);

void itoa10(unsigned char *buf, int i);

int strlen (const char *s);

void DS18B20_INIT();

void WRITE_DS18B20_CMD(char cmd);

float READ_DS18B20 ();

char bank1 DAT_BUFF1[]= "Temperature:";

char Found_DS18B20[]="DS1820 FOUND";

char DS18B20_NOT_Found[]="DS1820 NOT FOUND";

char bank1 DAT_BUFF2[]= " ";

#define CLRLCD 0X01 //清屏命令

#define LCDMOD 0X38 //8位,两行,5*7点

#define TURNON 0X0F //

#define CURMODE 0X06 //

#define ORG1 0X80 //LCD 第一行首地址

#define ORG2 0XC0 //LCD 第二行首地址

#define SKIPROM 0XCC

#define READSCRACHPAD 0XBE

#define TCONVERT 0X44

#define DS18b20_RESET 10

#define DSRECOVER 1

float temperature;

char bank1 atemperature[10]; // used for store ascii code of digital temperature */

void main()

{

int len;

ADCON1=0X07;

LCD1602_INIT();

LCD_Display(DAT_BUFF1);

LCDE=0;

LCD_Display_location (1,0);

while (1)

{

DS18B20_INIT();

WRITE_DS18B20_CMD(SKIPROM);

WRITE_DS18B20_CMD(TCONVERT);

TRIS_B20=0;

DS18B20=0;

TRIS_B20=1;

while(1)

{ //判断转换是否完成

if (DS18B20) break;

else continue;

}

DS18B20_INIT();

WRITE_DS18B20_CMD(SKIPROM);

WRITE_DS18B20_CMD(READSCRACHPAD);

temperature=READ_DS18B20();

itoa10(atemperature, (int) (temperature * 10));

// so use temperature * 10 to enlarge the temperature

len=strlen(atemperature);

atemperature[len]=atemperature[len-1];

atemperature[len-1]=.;

atemperature[len+1]=0;

LCD_Display_location (0,0);

LCD_Display (DAT_BUFF1); //LCD第一行显示Tempereture: 字符

LCD_Display_location (1,0);

LCD_Display (DAT_BUFF2);

LCD_Display_location (1,2);

LCD_Display (atemperature); //LCD第二行显示当前温度值

LCD_Display (DAT_BUFF2);

US_delay(5000); //每隔0.5秒读取一次温度值

}

}

int strlen(const char * s)

{

const char *cp;

cp = s;

while(*cp++)

continue;

return cp-s-1;

}

void LCD_Display(char *s)

{

int len;

int atmp;

len=strlen(s);

for (atmp=0; atmp

WRITE_LCD_DATA(s[atmp]);

}

void LCD1602_BUSY()

{

// char tmpbusy;

TRISB &= 0xF8;

TRISD=0XFF;

LCDRS=0;

LCDRW=1;

LCDE=1;

while(LCDDATA & 0X80);

}

void LCD_Display_location (char line, char col)

{

char pos;

LCD1602_BUSY();

pos = line * 0X40 + 0X80 + col;

WRITE_LCD_CMD(pos);

}

void WRITE_LCD_DATA(char data)

{

LCD1602_BUSY();

TRISB & =0XF8;

TRISD=0;

LCDDATA=data;

LCDRS=1;

LCDRW=0;

LCDE=0;

asm("NOP");

asm("NOP");

LCDE=1;

}

void WRITE_LCD_CMD(char cmd)

{

LCD1602_BUSY();

TRISB & =0XF8;

TRISD=0;

LCDDATA=cmd;

LCDRS=0;

LCDRW=0;

LCDE=0;

asm("NOP");

asm("NOP");

LCDE=1;

}

void LCD1602_INIT(void)

{

LCD1602_BUSY();

TRISB &= 0XF8;

TRISD=0;

WRITE_LCD_CMD(CLRLCD);

WRITE_LCD_CMD(LCDMOD);

WRITE_LCD_CMD(TURNON);

WRITE_LCD_CMD(CURMODE);

LCD_Display_location (0,0);

}

void DS18B20_INIT()

{

TRIS_B20=0;

DS18B20=0;

US_delay(20);

TRIS_B20=1;

US_delay(10);

}

void WRITE_DS18B20_CMD(char cmd)

{

char tmp;

char i;

TRIS_B20=0;

for(tmp=8;tmp>0;tmp--)

{

TRIS_B20=0;

DS18B20= 0;

asm ("NOP");

asm ("NOP");

asm ("NOP");

asm ("NOP");

asm ("NOP");

if (cmd & 0x01)

{

TRIS_B20=1;

US_delay(1);

for (i=5;i>0;i--);

}

else

{

DS18B20=0 ;

US_delay(1);

for (i=5;i>0;i--);

TRIS_B20=1;

}

cmd=cmd/2;

}

}

float READ_DS18B20 ()

{

char tmp=0x01;

float t;

union

{

char c[2];

int x;

}temp;

temp.x=0;

while (tmp)

{ // read first 8 bits

TRIS_B20=0;

DS18B20=0;

asm("NOP");

TRIS_B20=1;

if (DS18B20)

temp.c[0] |= tmp;

tmp=tmp<1;

US_delay(2);

}

tmp=1;

while (tmp)

{ // read first 8 bits

TRIS_B20=0;

DS18B20=0;

asm("NOP");

TRIS_B20=1; // release the bus

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top