注:一般蓝桥杯省赛会出温度传感器整数显示;而蓝桥杯国赛则会出温度传感器浮点数显示
unsigned char rd_temperature(void);
官方给的数据包中延时函数有问题需修改 整数
//延时函数修改
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
char i;
while(t--)
for(i = 0;i<12;i++);
}
//添加读取温度函数
unsigned char rd_temperature(void)
{
unsigned char low,high;
char temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low = Read_DS18B20();
high = Read_DS18B20();
temp = high <<4;
temp |=(low >> 4);
return temp;
}
float rd_temperature(void);
//延时函数修改
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
char i;
while(t--)
for(i = 0;i<12;i++);
}
//添加读取温度函数
float rd_temperature(void)
{
unsigned int temp;
float temperature;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44); //启动温度转换
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE); //读取寄存器
low = Read_DS18B20(); //低字节
high = Read_DS18B20(); //高字节
/** 精度为0.0625摄氏度 */
temp = (high&0x0f);
temp <<= 8;
temp |= low;
temperature = temp*0.0625;
return temperature;
}
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned char rd_temperature(void); //; ;
#endif
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
char i;
while(t--)
for(i = 0;i<12;i++);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned char rd_temperature(void)
{
unsigned char low,high;
char temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low = Read_DS18B20();
high = Read_DS18B20();
temp = high <<4;
temp |=(low >> 4);
return temp;
}
#include <stc15f2k60s2.h>
#include "onewire.h"
#define uchar unsigned char
#define uint unsigned int
code unsigned char tab[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar dspbuf[8] = {
10,10,10,10,10,10,10,10};
uchar intr = 0,temperature;
uchar temper_flag = 1;
void load();
void display();
void cls()
{
P2 = (P2 & 0xa0);
P0 = 0x00;
P2 = 0x1f;
P2 = (P2 & 0x80);
P0 = 0xff;
P2 = 0x1f;
}
void main()
{
cls();
AUXR = 0x80;
TMOD = 0x00;
TL0 = 0xcd;
TH0 = 0xd4;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
while(1)
{
if(temper_flag)
{
temper_flag = 0;
temperature = rd_temperature();
}
}
}
void time0(void) interrupt 1
{
if(++intr == 50){
temper_flag = 1;
intr = 0;
}
display();
}
void load()
{
dspbuf[7] = temperature % 10;
dspbuf[6] = temperature / 10;
}
void display()
{
static unsigned char dspcom = 0;
load();
P2 = (P2 & 0x1f) | 0xe0;
P0 = 0xff;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1<<dspcom;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[dspbuf[dspcom]];
P2 = 0x1f;
if(++dspcom == 8)dspcom = 0;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100139.html原文链接: