標(biāo)題:
溫度單片機P0口用74HC373或74HC573鎖存
[打印本頁]
作者:
bibi
時間:
2015-4-19 03:29
標(biāo)題:
溫度單片機P0口用74HC373或74HC573鎖存
#include <AT89X52.h> //調(diào)用51單片機的頭文件
#include <Intrins.h>
sbit ds18b20=P3^7;
sbit OK=P3^6;
unsigned char TT,temp1,num,TL=20,TH=27,w,s,m,n;
unsigned char temp,temp2,key,dat;
unsigned char code tabcc[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xff};//共陰極
unsigned char code tabca[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF};//共陽極
//---------------------------------------
//名稱:復(fù)位DS18B20函數(shù)
//---------------------
void delay(int ms)
{
int i;
while(ms--)
for(i=0;i<120;i++);
}
void disp(w,s)
{
unsigned char temp=0x01;
///////////////////
P0=tabca[s]; //顯示一位數(shù)據(jù)
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
P0=1<<w-1; //一位位選
P2 =0x01;
_nop_();
P2 |=0x03;
_nop_();
delay(6);
/////////////////////////
P0=0xff; //一位顯示完后清屏
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
///////////////////
}
void main()
{
char i;
while(1)
{
TT++;
if(TT==100)
TT=0;
n=TT/10;
m=TT%10;
for(i=0;i<20;i++)
{
disp(1,m); //個位
disp(2,n); //十位
disp(6,10);//右杠
disp(7,2);//區(qū)間
disp(8,10);//左杠
}
}
}
復(fù)制代碼
#include <AT89X52.h> //調(diào)用51單片機的頭文件
#include <Intrins.h>
sbit ds18b20=P3^7;
sbit OK=P3^6;
unsigned char TT,temp1,num,TL=20,TH=27,y;
unsigned char temp,temp2,key,dat;
unsigned char code tabcc[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xff};//共陰極
unsigned char code tabca[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x00};//共陽極
//---------------------------------------
//名稱:復(fù)位DS18B20函數(shù)
//---------------------
void delay(int ms)
{
int i;
while(ms--)
for(i=0;i<120;i++);
}
void disp(void)
{
unsigned char temp=0x01;
///////////////////
P0=tabca[TT%10]; //第一位顯示數(shù)據(jù)
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
P0=0x01; //第一位位選
P2 =0x01;
_nop_();
P2 |=0x03;
_nop_();
delay(6);
/////////////////////////
P0=0xff; //第一位顯示數(shù)據(jù)
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
///////////////////
P0=tabca[TT/10]; //第二位顯示數(shù)據(jù)
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
P0=0x02; //第二位位選
P2 =0x01;
_nop_();
P2 |=0x03;
_nop_();
delay(6);
//////////////////////
P0=0xff; //第一位顯示數(shù)據(jù)
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
/////////////////////
}
void main()
{
char i;
while(1)
{
TT++;
if(TT==100)
TT=0;
for(i=0;i<20;i++)
{
disp();
}
}
}
復(fù)制代碼
#include <AT89X52.h> //調(diào)用51單片機的頭文件
#include <Intrins.h>
sbit ds18b20=P3^7;
sbit OK=P3^6;
unsigned char TT,temp1,num,TL=20,TH=27,w,s,m,n;
unsigned char temp,temp2,key,dat;
unsigned char code tabcc[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xff};//共陰極
unsigned char code tabca[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF,0xFF};//共陽極
//---------------------------------------
//名稱:ms延遲
//---------------------
void delay(int ms)
{
int i;
while(ms--)
for(i=0;i<120;i++);
}
//---------------------------------------
//名稱:復(fù)位DS18B20函數(shù)
//---------------------
bit Reset(void)
{
unsigned int i;
bit k;
ds18b20=0;
i=95;
while(i--);
ds18b20=1;
i=9;
while(i--);
k=ds18b20;
i=55;
while(i--);
return k;
}
//---------------------------------------
//名稱:讀一字節(jié)函數(shù)
//---------------------------------------
unsigned char ReadByte(void)
{
unsigned int i;
unsigned char j,buf=0;
for(j=0;j<8;j++)
{
buf=buf>>1;
ds18b20=0;
_nop_();
_nop_();
ds18b20=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(ds18b20==1)
buf|=0x80;
i=7;
while(i--);
}
return buf;
}
//---------------------------------------
//名稱:寫一字節(jié)函數(shù)
//---------------------------------------
void WriteByte(unsigned char dat)
{
unsigned int i;
unsigned char j;
for(j=0;j<8;j++)
{
if(dat&0x01) //如果寫1
{
ds18b20=0; //拉低
_nop_(); //維持2US,符合大于1US小于15US的規(guī)范
_nop_(); //維持2US,符合大于1US小于15US的規(guī)范
ds18b20=1; //拉高
i=7; //維持約63US,符合不低于60US的要求
while(i--); //維持約63US,符合不低于60US的要求
}
else //如果寫0
{
ds18b20=0; //拉低
i=7; //維持約63US,符合不低于60US的要求
while(i--); //維持約63US,符合不低于60US的要求
ds18b20=1; //拉高
_nop_(); //維持2US,符合大于1US的規(guī)范
_nop_(); //維持2US,符合大于1US的規(guī)范
}
dat=dat>>1; //寫入字節(jié)右移1位
}
}
//---------------------------------------
//名稱:DS18B20溫度轉(zhuǎn)換函數(shù)
//---------------------------------------
bit Convert(void)
{
if(Reset()==0) //復(fù)位DS18B20成功
{
WriteByte(0xcc); //寫入跳過序列號命令字 Skip Rom
WriteByte(0x44); //寫入溫度轉(zhuǎn)換命令字 Convert T
return 1; //啟動溫度轉(zhuǎn)換成功
}
else //失敗
{
return 0; //啟動溫度轉(zhuǎn)換失敗
}
}
//---------------------------------------
//名稱:轉(zhuǎn)換結(jié)束處理函數(shù)
//---------------------------------------
void ReadFlash(void)
{
unsigned char Lsb,Msb;
if(Reset()==0) //復(fù)位DS18B20成功
{
WriteByte(0xcc); //寫入跳過序列號命令字 Skip Rom
WriteByte(0xbe); //寫入讀取數(shù)據(jù)令字 Read Scratchpad
Lsb=ReadByte(); //讀出第一個字節(jié)暫存于LSB
Msb=ReadByte(); //讀出第二個字節(jié)暫存于MSB
temp1=Lsb; //temp1內(nèi)裝溫度參數(shù)的小數(shù)部分
temp2=Msb; //(Msb>>4)|(Lsb<<4);//temp2內(nèi)裝溫度參數(shù)的整數(shù)部分
}
else
{
temp1=0; //如果復(fù)位失敗,溫度參數(shù)清零
temp2=0; //如果復(fù)位失敗,溫度參數(shù)清零
}
}
void disp(w,s)
{
unsigned char temp=0x01;
///////////////////
P0=tabca[s]; //顯示一位數(shù)據(jù)
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
P0=1<<w-1; //一位位選
P2 =0x01;
_nop_();
P2 |=0x03;
_nop_();
delay(4);
/////////////////////////
P0=0xff; //一位顯示完后清屏
P2 &=0xFC;
_nop_();
P2 |=0x03;
_nop_();
///////////////////
}
void main()
{
while(1)
{
{
if(Convert()==1);
{
ReadFlash();
TT=(temp2<<4)|(temp1>>4);
}
if(TT<TL)
num=0;
else if(TT>TH)
num=2;
else //((TT>=TL)&(TT<=TH))
num=1;
disp(1,TT%10); //個位
disp(2,TT/10); //十位
disp(6,10);//右杠
disp(7,num);//區(qū)間
disp(8,10);//左杠
}
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1