有大佬幫我看看么,謝謝 ,最高顯示到15
#include "reg52.h"
#include "display.h"
#include "delay.h"
#include "18b20.h"
/*********************函數(shù)申明*************************/
void TimerInit(void); //2ms@11.0592MHz
void key_deal(void);
void Temp_read(void);
/*********************按鍵變量*************************/
unsigned char key_read_value;
/*********************數(shù)碼管顯示變量*************************/
unsigned char dispbuf[]={11,11,11,11,11,11,11,11};
/************************溫度讀取變量**********************/
unsigned char temp,temperature,tempL,Tmax=30,Tmin=25;
bit Readtempflag;
/*********************顯示切換變量*************************/
unsigned char switch_mod;
/*********************主函數(shù)*************************/
void main()
{
TimerInit();
while(1)
{
Temp_read();
dispbuf[5]=(temperature/100);
dispbuf[6]=(temperature%100)/10;
dispbuf[7]=(temperature%100)%10;
}
}
void TimerInit(void)
{
TMOD|=0x11;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
TH1=(65536-2000)/256;
TL1=(65536-2000)%256;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
}
void Timer0(void) interrupt 1
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
SMG_display(dispbuf); //2ms進一次中斷
}
void Timer1(void) interrupt 3 //溫度讀取標志位和動態(tài)掃描放一起會出問題
{ //別問我為什么,我也不知道!
static unsigned int i;
TH1=(65536-2000)/256;
TL1=(65536-2000)%256;
i++;
if( i==300 )
{
i=0;
Readtempflag=1;
}
}
void key_deal(void) //按鍵消息處理
{
key_read_value = key_read();
if(key_read_value!=0xff)
{
}
}
void Temp_read(void)
{
if( Readtempflag==1 )
{
Readtempflag=0;
temp=ReadTemperature();
temperature=(float)temp*0.0625;
}
}
#include "display.h"
unsigned char code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40,0x00};
unsigned char dispcom;
void SMG_display(unsigned char *dispbuf)
{
P0 = 0x00;
Dula = 1;
Dula = 0;
P0 = ~(1<<dispcom);
Wela = 1;
Wela = 0;
P0 = table[ dispbuf[dispcom] ];
Dula=1;
Dula=0;
(dispcom==7)?(dispcom=0):(dispcom++);
}
#ifndef _DISPLAY_H_
#define _DISPLAY_H_
#include <reg52.h>
sbit Dula = P2^1;
sbit Wela = P2^0;
void SMG_display(unsigned char *dispbuf);
#endif
#include "delay.h"
/*------------------------------------------------
uS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時請使用匯編,大致延時
長度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時請使用匯編
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延時1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
#ifndef __DELAY_H__
#define __DELAY_H__
/*------------------------------------------------
uS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時請使用匯編,大致延時
長度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t);
/*------------------------------------------------
mS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時請使用匯編
------------------------------------------------*/
void DelayMs(unsigned char t);
#endif
/*-----------------------------------------------
名稱:18B20溫度傳感器
日期:2009.5
修改:無
內(nèi)容:18B20單線溫度檢測的應(yīng)用樣例程序
------------------------------------------------*/
#include"delay.h"
#include"18b20.h"
/*------------------------------------------------
18b20初始化
------------------------------------------------*/
bit Init_DS18B20(void)
{
bit dat=0;
DQ = 1; //DQ復(fù)位
DelayUs2x(5); //稍做延時
DQ = 0; //單片機將DQ拉低
DelayUs2x(200); //精確延時 大于 480us 小于960us
DelayUs2x(200);
DQ = 1; //拉高總線
DelayUs2x(50); //15~60us 后 接收60-240us的存在脈沖
dat=DQ; //如果x=0則初始化成功, x=1則初始化失敗
DelayUs2x(25); //稍作延時返回
return dat;
}
/*------------------------------------------------
讀取一個字節(jié)
------------------------------------------------*/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 給脈沖信號
dat>>=1;
DQ = 1; // 給脈沖信號
if(DQ)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
/*------------------------------------------------
寫入一個字節(jié)
------------------------------------------------*/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
DelayUs2x(25);
DQ = 1;
dat>>=1;
}
DelayUs2x(25);
}
/*------------------------------------------------
讀取溫度
------------------------------------------------*/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳過讀序號列號的操作
WriteOneChar(0x44); // 啟動溫度轉(zhuǎn)換
DelayMs(10);
Init_DS18B20();
WriteOneChar(0xCC); //跳過讀序號列號的操作
WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
a=ReadOneChar(); //低位
b=ReadOneChar(); //高位
b<<=8;
t=a+b;
return(t);
}
#ifndef __DS18B20_H__
#define __DS18B20_H__
#include<reg52.h> //包含頭文件,一般情況不需要改動,頭文件包含特殊功能寄存器的定義
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int;
/*------------------------------------------------
端口定義
------------------------------------------------*/
sbit DQ=P3^2;//ds18b20 端口
/*------------------------------------------------
函數(shù)聲明
------------------------------------------------*/
unsigned int ReadTemperature(void);
bit Init_DS18B20(void);
unsigned char ReadOneChar(void);
void WriteOneChar(unsigned char dat);
#endif
|