ds18b20溫度顯示于數碼管,包含程序和仿真
單片機源程序如下:
- /*-----------------------------------------------
- 名稱:DS18b20數碼管顯示溫度
- 編寫:林雁翔
- 日期:2013.6.16
- 修改:無
- 內容:顯示格式 符號 xxx C
- 可以顯示負溫度
- ------------------------------------------------*/
- #include <reg52.h>
- #include "delay.h"
- #include "ds18b20.h"
- #define uint unsigned int
- #define uchar unsigned char
- #define duanMa P0
- #define weiMa P2
- uchar code disCode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
- 0x82,0xf8,0x80,0x90,0xff,0xBF,0xc6}; //共陽極數碼管顯示字符編碼
- uchar code disBit[]={0x01,0x02,0x04,0x08,0x10,0x20}; //數碼管顯示位碼
- uchar disBuf[]={6,6,6,9,9,9}; //準備顯示的數字
- uint tempNum=0,tempH,tempL;
- bit tempFlag=0;
- void Init();
- void Display();
- void main()
- {
- Init();
- while(1)
- {
- tempNum=ReadTemperature();
- if(tempNum&(1<<15)) //如果溫度為負數,則取原碼
- {
- tempNum=~tempNum;
- tempNum+=1;
- tempFlag=1; //標志為負數
- }
- //tempL=tempNum|0x0F; //獲取低四位的小數溫度位
- tempH=tempNum>>4; //獲取整數位溫度位
- if(tempFlag==1)
- {
- tempFlag=0;
- disBuf[0]=11; //顯示負號
- }
- else
- {
- disBuf[0]=10; //不顯示任何數據
- }
- disBuf[1]=tempH/100;
- disBuf[2]=tempH%100/10;
- disBuf[3]=(tempH%10);
- disBuf[4]=10; //不顯示小數
- disBuf[5]=12; //顯示C
- Display();
- }
- }
- void Init()
- {
- duanMa=0xFF; //初始化數碼管斷碼
- weiMa=0x00; //初始化數碼管位碼
- }
- void Display()
- {
- uchar i=0;
- for(i=0;i<6;i++)
- {
- duanMa=disCode[disBuf[i]];
- weiMa=disBit[i];
- DelayMs(1);
- weiMa=0x00;
- }
- }
復制代碼
所有資料51hei提供下載:
在數碼管上顯示.rar
(68.87 KB, 下載次數: 46)
2018-3-8 13:32 上傳
點擊文件名下載附件
|