PIC入门6,DS18B20测温度
扫描二维码
随时随地手机看文章
程序如下:
//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
//函数定义
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 ();
//实数定义
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]; // 数子温度
//主函数
void main()
{
int len;
ADCON1=0X07; //设置端口A为I/O
lcdinit();
print(title);
LCDE=0; /* 当 LCDE=0,LCD不起作用,操纵DS1820 */
/* 中断TIMER0 */
OPTION=0B11001111; // bit5=0,利用内部循环时钟
TMR0IE=1; //timer0 使能端
PEIE=0; //关闭所有外设中断
GIE=1;//开总中断
poscursor(1,0);
while (1) {
dsinit();
wdscmd(SKIPROM);
wdscmd(TCONVERT);
TRISDQ=0;
DSDQ=0;
TRISDQ=1;
while(1)
{ //转换是否完成
if (DSDQ)
break;
else
continue;
}
dsinit();
wdscmd(SKIPROM);
wdscmd(READSCRACHPAD);
temperature=dsread(); // 读温度
itoa10(atemperature, (int) (temperature * 10)); //itoa10只有是正数时起作用
// 所以乘以10,扩大10倍,然后增加小数点
// 再最后一个数前增加小数点
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
}
}
//延时函数
void delay (int t){
count=0; // 重设置中断 timer 0 */
TMR0=PRETMR0;
while (count
static void interrupt isr(void) // 中断函数
{
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);
}
//写数据
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); // 设置光标的起点
}
//DS18B20初始化设置,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字节
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){ // 读八字节
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;
}