?? temp_contorl.c
字號:
#include "reg52.h"
#include "absacc.h"
#include <stdio.h>
#include <math.h>
#define uchar unsigned char
#define display_single XBYTE[0x0000]
#define display_point XBYTE[0x0100]
uchar code single[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code single_point[9]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};
uchar point,indiv,ten,hun;
#define SLAVE 0x02
sbit dq=P0^0;
sbit key_1=P1^0;
sbit key_2=P1^2;
uchar point,indiv,ten,hun;
uchar temp1,temp2,ready=0,flag=1;
void _nop()
{;
}
/***********************************************************\
|數(shù)碼顯示程序 |
| |
\***********************************************************/
void display(void)
{
display_single=single[point];
display_point=single_point[0];
_nop();
_nop();
display_point=0xff;
display_single=(single[indiv]&0x7f);
display_point=single_point[1];
_nop();
_nop();
display_point=0xff;
display_single=single[ten];
display_point=single_point[2];
_nop();
_nop();
display_point=0xff;
display_single=single[hun];
display_point=single_point[3];
_nop();
_nop();
display_point=0xff;
}
void delay(unsigned int count)
{
unsigned int i;
while(count)
{
i=20;
while(i>0)
{
display();
i--;
}
count--;
}
}
/*******************************************************\
| 函數(shù)名 Reset_18B20 |
| 函數(shù)功能 復位18B20芯片 |
| 參數(shù) 無 |
| 返回值 無 |
\*******************************************************/
bit Reset_18B20()
{
bit answer;
unsigned char i;
int j;
//置總線為低電平并保持至少480us
dq = 0;
for(i=0; i<7;i++) //780us
{
display();
}
//等電阻拉高總線并保持15-60us
dq=1;
for(i=0; i<9; i++) //65us
{
_nop();
}
//接受應答信號
answer=dq;
//延時60-240us
for(i=0; i<2;i++) //240us
{
display();
for(j=0;j<1;j++);
}
return(answer);
}
/*******************************************************\
| 函數(shù)名 read_byte_18B20 |
| 函數(shù)功能 從18B20中讀出一位數(shù)據(jù) |
| 參數(shù) 無 |
| 返回值 讀出的數(shù)據(jù) |
\*******************************************************/
bit read_bit_18B20(void)
{
bit bitval;
unsigned char i;
dq=0;
_nop();
dq=1;
_nop();
_nop();
bitval = dq;
for(i=0; i<8; i++)
{
_nop();
}
return(bitval);
}
/*******************************************************\
| 函數(shù)名 write_bit_18B20 |
| 函數(shù)功能 向18B20中寫入一位數(shù)據(jù) |
| 參數(shù) bitval : 待寫入的數(shù)據(jù) |
| 返回值 無 |
\*******************************************************/
void write_bit_18B20(unsigned char bitval)
{
unsigned char i;
dq=0;
for(i=0; i<1; i++)
{
_nop();
_nop();
}
if (bitval==1)
{
dq=1;
}
for(i=0; i<8; i++)
{
_nop();
}
dq=1;
}
/*******************************************************\
| 函數(shù)名 read_byte_18B20 |
| 函數(shù)功能 從18B20中讀出一個字節(jié)的數(shù)據(jù) |
| 參數(shù) 無 |
| 返回值 讀出的數(shù)據(jù) |
\*******************************************************/
unsigned char read_byte_18B20(void)
{
unsigned char i;
unsigned char j;
unsigned char value = 0;
for (i=0;i<8;i++)
{
j = read_bit_18B20();
if (j) //如果是 1 置1,否則置 0
{
value|= (0x01<<i); //先讀低位,再讀高位
}
_nop();
}
return(value);
}
/*******************************************************\
| 函數(shù)名 write_byte_18B20 |
| 函數(shù)功能 向18B20中寫入一個字節(jié)的數(shù)據(jù) |
| 參數(shù) val : 待寫入的數(shù)據(jù) |
| 返回值 無 |
\*******************************************************/
void write_byte_18B20(unsigned char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++)
{
temp = val>>i;
temp&= 0x01;
write_bit_18B20(temp);
}
_nop();
}
/*******************************************************\
| 函數(shù)名 GetTemperat |
| 函數(shù)功能 利用18B20芯片檢測溫度,并在LED上顯示 |
| 參數(shù) 無 |
| 返回值 溫度 |
\*******************************************************/
void GetTemperat()
{
while(Reset_18B20()); //復位等待從機應答
write_byte_18B20(0xCC); //忽略ROM匹配
write_byte_18B20(0x44); //發(fā)送溫度轉(zhuǎn)化命令
delay(300); //延時
while(Reset_18B20()); //再次復位,等待從機應答
write_byte_18B20(0xCC); //忽略ROM匹配
write_byte_18B20(0xBE); //發(fā)送讀溫度命令
temp1= read_byte_18B20(); //讀出溫度低8位
temp2= read_byte_18B20(); //讀出溫度高8位
dq=1; //釋放總線
ready=0x01;
}
/***************************************\
|溫度值轉(zhuǎn)換 |
\***************************************/
void tmpchangebcd(void)
{float a;
uchar c,d,f,e;
c=temp2&0xf8;
if(c)
{temp1=~temp1+1; //////
temp2=~temp2;}//////////////////////
d=temp1&0x0f;
f=temp1>>4;
e=temp2&0x07;
e<<=4;
f|=e;
a=d*0.0625;
point=(uchar)(a*10);
indiv=f%10;
ten=f%100/10;
hun=f/100;
}
void reachmassege()interrupt 4 using 1
{
uchar a;
while(RI!=1);RI=0;
ES=0;
if(flag)
{
a=SBUF;
if(a!=0x02){ES=1;goto reti;}
SM2=0;
SBUF=SLAVE;
while(TI!=1);TI=0;
while(RI!=1);RI=0;
}
key_2=~key_2;
if((RB8==1)&&(SBUF==0xff)){SM2=1;ES=1;flag=1;goto reti;}
a=SBUF;
if(a==0x00)
{
if(ready==0x01)SBUF=0x01;
else SBUF=0x00;
while(TI!=1);TI=0;
while(RI!=1);RI=0;
if((RB8==1)&&(SBUF==0xff)){SM2=1;ES=1;flag=1;goto reti;}
else if(SBUF==0x01)
{SBUF=temp1;
while(TI!=1);TI=0;
SBUF=temp2;
while(TI!=1);TI=0;}
else if(SBUF==0x02)
{SBUF=point;
while(TI!=1);TI=0;
SBUF=indiv;
while(TI!=1);TI=0;
SBUF=ten;
while(TI!=1);TI=0;
SBUF=hun;
while(TI!=1);TI=0;
}
}
flag=0;
reti:ES=1;key_1=1;
}
void main()
{ TMOD=0x20;
TL1=0xfd;
TH1=0xfd;
PCON=0x00;
TR1=1;
SCON=0xf0;
ES=1;
EA=1;
key_1=1;
key_2=0;
while(1)
{
GetTemperat();
tmpchangebcd();
key_1=~key_1;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -