今天通过DS18B20读取温度并通过LED数码管显示,最后一位明显更亮,为啥呢? 下面是我的函数 分3个文件:头文件,DS18B20系列子函数文件,主函数部分(既数据处理和显示部分)
头文件: #ifndef __TEMP_H_ #define __TEMP_H_
#include<reg52.h> #ifndef uchar #define uchar unsigned char #endif
#ifndef uint #define uint unsigned int #endif
sbit DSPORT=P2^2;
void Delay1ms(uint ); uchar Ds18b20Init(); void Ds18b20WriteByte(uchar com); uchar Ds18b20ReadByte(); void Ds18b20ChangTemp(); void Ds18b20ReadTempCom(); int Ds18b20ReadTemp();
#endif
DS18B20系列子函数文件
#include"temp.h" /******************************************************************************* * : Delay1ms
*******************************************************************************/
void Delay1ms(uint y) { uint x; for( ; y>0; y--) { for(x=110; x>0; x--); } } /******************************************************************************* * : Ds18b20Init * : ʼ * : * : ʼɹ1ʧܷ0 *******************************************************************************/
uchar Ds18b20Init() { uchar i; DSPORT = 0; //-480us~960us i = 70; while(i--); DSPORT = 1; i = 0; while(DSPORT)
{ Delay1ms(1); i++; if(i>5)//ȴ>5MS { return 0; }
} return 1; }
/******************************************************************************* * : Ds18b20WriteByt * : * : *******************************************************************************/
void Ds18b20WriteByte(uchar dat) { uint i, j;
for(j=0; j<8; j++) { DSPORT = 0; i++; DSPORT = dat & 0x01; i=6; while(i--); DSPORT = 1; dat >>= 1; } } /******************************************************************************* * : Ds18b20ReadByte
*******************************************************************************/
uchar Ds18b20ReadByte() { uchar byte, bi; uint i, j; for(j=8; j>0; j--) { DSPORT = 0; i++; DSPORT = 1; i++; i++; bi = DSPORT;
byte = (byte >> 1) | (bi << 7); i = 4; while(i--); } return byte; } /******************************************************************************* * : Ds18b20ChangTemp * : * : *******************************************************************************/
void Ds18b20ChangTemp() { Ds18b20Init(); Delay1ms(1); Ds18b20WriteByte(0xcc); Ds18b20WriteByte(0x44); // Delay1ms(100);
} /******************************************************************************* *******************************************************************************/
void Ds18b20ReadTempCom() {
Ds18b20Init(); Delay1ms(1); Ds18b20WriteByte(0xcc); Ds18b20WriteByte(0xbe); } /******************************************************************************* *******************************************************************************/
int Ds18b20ReadTemp() { uchar temp = 0; uchar tmh, tml; Ds18b20ChangTemp(); Ds18b20ReadTempCom(); tml = Ds18b20ReadByte(); tmh = Ds18b20ReadByte(); /*temp = tmh; //temp <<= 8; temp |= tml;*/ tml>>=4; tmh<<=4; temp=tml|tmh; return temp; }
主函数部分 #include "reg52.h" #include"temp.h"
#define u16 unsigned int #define u8 unsigned char
sbit numchoose=P2^6; sbit wela=P2^7;
/*this code is the num form 0 to F in the LED tube*/ u8 duanxuantable[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};
char num=0; u8 DisplayData[8]; //u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/******************************************************************************* * : delay * i=1时延时10us *******************************************************************************/ void delay(u16 i) { while(i--); }
/******************************************************************************处理读取的原码,使之成为对应温度值 并存储正负符号于DisplayData[0]上 *******************************************************************************/ u8 datapros(u8 temp) { float tp; if(temp>127) { DisplayData[0] = 0x40; temp=temp-1; temp=~temp; tp=temp; // temp=tp/2; temp=tp*0.0625*100+0.5;
} else { DisplayData[0] = 0x00; tp=temp; temp=tp*0.625+0.5;
}
return temp; }
/******************************************************************************函数名: display 功能:显示输入的unsigned char型温度的数值在2、3、4位,第一位显示正负 输入: 读取并处理后的温度 输出: 无 *******************************************************************************/ void display(u8 num) { u8 weitable[]={0,0,0,0}; weitable[0]=DisplayData[0]; weitable[1]=num/100; weitable[2]=(num/10)%10; weitable[3]=num%10; wela=1; P0=0xfe; wela=0; numchoose=1; P0=duanxuantable[weitable[0]]; numchoose=0; delay(50);
wela=1; P0=0xfd; wela=0; numchoose=1; P0=duanxuantable[weitable[1]]; numchoose=0; delay(50);
wela=1; P0=0xfb; wela=0; numchoose=1; P0=duanxuantable[weitable[2]]; numchoose=0; delay(50);
wela=1; P0=0xf7; wela=0; numchoose=1; P0=duanxuantable[weitable[3]]; numchoose=0; delay(50); } /******************************************************************************* * : main * : * : * : *******************************************************************************/ void main() { while(1) { /*datapros(Ds18b20ReadTemp()); //ݴ DigDisplay(); while(1) { display(datapros(Ds18b20ReadTemp())); } } }
不知道为什么KEIL上编译的文件的中文部分下下来就乱码了。。。。,抱歉
|