|
一個(gè)51單片機(jī)的項(xiàng)目提供源碼 供參考
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^4; //define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative
sbit dula=P2^0; //段選信號(hào)的鎖存器控制
sbit wela=P2^1; //位選信號(hào)的鎖存器控制
sbit cs88=P2^2; //點(diǎn)陣管的鎖存器控制 cs88=0;//關(guān)點(diǎn)陣管
sbit LCD1602=P2^5; //定義LCD1602使能端,用于HJ-C52實(shí)驗(yàn)板復(fù)位,與本實(shí)驗(yàn)無關(guān)
sbit DS1302=P1^2; //定義DS1302時(shí)鐘使能端,用于HJ-C52實(shí)驗(yàn)板復(fù)位,與本實(shí)驗(yàn)無關(guān)
sbit SD=P2^6; //定義SD卡使能端,用于HJ-C52實(shí)驗(yàn)板復(fù)位,與本實(shí)驗(yàn)無關(guān)
void cmg88()//關(guān)數(shù)碼管,點(diǎn)陣函數(shù) cmg88();//關(guān)數(shù)碼管,點(diǎn)陣函數(shù)
{
dula=1;
P0=0x00;
dula=0;
cs88=0x00;
P0=0x00;
cs88=1;
}
sbit beep=P1^3; // 定義高溫時(shí)L3 點(diǎn)亮
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef};
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //讀出的數(shù)據(jù)最低位在最前面,這樣剛好一個(gè)字節(jié)在DAT里
}
return(dat);
}
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void display(uint temp) //顯示程序
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
dula=0;
P0=table[A1]; //顯示百位
dula=1;
dula=0;
wela=0;
P0=0x7e;
wela=1;
wela=0;
delay(1);
dula=0;
P0=table1[A2]; //顯示十位
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
delay(1);
P0=table[A3]; //顯示個(gè)位
dula=1;
dula=0;
P0=0x7b;
wela=1;
wela=0;
delay(1);
}
void main()
{
uchar a;
//主板初始化 因51單片機(jī)芯片開機(jī)時(shí)各個(gè)IO口輸出為高電平,加上以下源代碼是為了防止各種外部模塊誤動(dòng)作
//DS1302=0; //關(guān)DS1302模塊,與本實(shí)驗(yàn)無關(guān)
SD=0;//關(guān)SD卡模塊,與本實(shí)驗(yàn)無關(guān)
LCD1602=0;//關(guān)LCD1602模塊,與本實(shí)驗(yàn)無關(guān)
cs88=0;//關(guān)點(diǎn)陣管
do
{
tmpchange();
for(a=10;a>0;a--)
{
display(tmp());
}
if(temp>=310) //當(dāng)溫度超過31度(僅作試驗(yàn)用,實(shí)際可設(shè)為其他更高的值),L3發(fā)光管便會(huì)發(fā)光。
{
P1=0xFE;
beep=0;
}
else
{
beep=1;
P1=0xfE;
}
} while(1);
}
|
-
-
超溫報(bào)警項(xiàng)目.7z
2019-5-8 02:14 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
10.17 KB, 下載次數(shù): 4, 下載積分: 黑幣 -5
51單片機(jī) 超溫警報(bào)
|