今天通過(guò)DS18B20讀取溫度并通過(guò)LED數(shù)碼管顯示,最后一位明顯更亮,為啥呢?
IMG_20181126_004916.jpg (2.53 MB, 下載次數(shù): 31)
下載附件
TX-1C的板子,第四位明顯更亮
2018-11-26 01:28 上傳
下面是我的函數(shù) 分3個(gè)文件:頭文件,DS18B20系列子函數(shù)文件,主函數(shù)部分(既數(shù)據(jù)處理和顯示部分)
頭文件: #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系列子函數(shù)文件
#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; }
主函數(shù)部分 #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時(shí)延時(shí)10us *******************************************************************************/ void delay(u16 i) { while(i--); }
/******************************************************************************處理讀取的原碼,使之成為對(duì)應(yīng)溫度值 并存儲(chǔ)正負(fù)符號(hào)于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; }
/******************************************************************************函數(shù)名: display 功能:顯示輸入的unsigned char型溫度的數(shù)值在2、3、4位,第一位顯示正負(fù) 輸入: 讀取并處理后的溫度 輸出: 無(wú) *******************************************************************************/ 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上編譯的文件的中文部分下下來(lái)就亂碼了。。。。,抱歉
|