|
代碼如下,沒用定時(shí)器數(shù)碼管正常顯示溫度,用定時(shí)器后亂跳(定時(shí)器用來調(diào)節(jié)占空比調(diào)速,做的是一個(gè)溫控風(fēng)扇,部分函數(shù)省略)
麻煩各位大佬指點(diǎn),萬分感謝
#include "reg52.h"
#include"temp.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit IN1=P3^0;
sbit IN2=P3^1;
sbit ENA=P3^2;
u8 DisplayData[8];
u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint n;
uchar time;
uint flag=100;
bit flag1s=0;
void delay(u16 i)//延時(shí)函數(shù),i=1時(shí),大約延時(shí)10us
{
while(i--);
}
void datapros(int temp) //溫度讀取處理
{
float tp;
if(temp< 0) //當(dāng)溫度值為負(fù)數(shù)
{
DisplayData[0] = 0x40; // 第一位數(shù)碼管顯示負(fù)號(hào)
//因?yàn)樽x取的溫度是實(shí)際溫度的補(bǔ)碼,所以減1,再取反求出原碼
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入
}
else
{
DisplayData[0] = 0x00;
tp=temp;//如果溫度為正,讀取的溫度為其本身
temp=tp*0.0625*100+0.5;
}
DisplayData[1] = smgduan[temp % 10000 / 1000];
DisplayData[2] = smgduan[temp % 1000 / 100]|0x80;//顯示小數(shù)點(diǎn)
DisplayData[3] = smgduan[temp % 100 / 10];
DisplayData[4] = smgduan[temp % 10 / 1];
if(temp>1400){
n=100;}
else{n=50;}
}
/*******************************************************************************
* 函數(shù)名 :DigDisplay()
* 函數(shù)功能 :數(shù)碼管顯示函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void DigDisplay()
{
u8 i;
for(i=0;i<6;i++)
{
switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
{
case(0):
LSA=1;LSB=1;LSC=1; break;//顯示第0位
case(1):
LSA=0;LSB=1;LSC=1; break;//顯示第1位
case(2):
LSA=1;LSB=0;LSC=1; break;//顯示第2位
case(3):
LSA=0;LSB=0;LSC=1; break;//顯示第3位
case(4):
LSA=1;LSB=1;LSC=0; break;//顯示第4位
case(5):
LSA=0;LSB=1;LSC=0; break;//顯示第5位
}
P0=DisplayData[i];//發(fā)送數(shù)據(jù)
delay(100); //間隔一段時(shí)間掃描
P0=0x00;//消隱
}
}
void interrupt_init()
{
TMOD=0x01;
TH0=0xfc;
TL0=0x18;
ET0=1;
TR0=1;
EA=1;
}
void main()//主函數(shù)
{
interrupt_init();
while(1)
{
datapros(Ds18b20ReadTemp()); //數(shù)據(jù)處理函數(shù)
DigDisplay(); //數(shù)碼管顯示函數(shù)
}
}
void Timer0() interrupt 1
{
TR0=0;
TH0=0xfc;
TL0=0x18;
TR0=1;
time++;
if(time=flag)
{
time=0;
}
if(time<=n)
{
ENA=1;
IN1=1;
IN2=0;
}
}
|
|