欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1968|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

藍(lán)橋杯“溫度監(jiān)控器”源代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:307055 發(fā)表于 2018-4-13 07:22 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
1、main.c文件
#include "common.h"
#include "timer.h"
#include "ds18b20.h"
#include "display.h"
#include "ds1302.h"
#include "led.h"
/**********************************************************
函數(shù)名稱(chēng):SysInit
***********************************************************/
void SysInit(void)
{
/*總線(xiàn)設(shè)備初始化*/
LED=0xff;              //LED燈全滅
DISPBIT=0x00;          //數(shù)碼管位碼賦值全0
DISPSEG=0xff;          //數(shù)碼管段碼賦值全1
//
ReadTemp();            //初始化的時(shí)候讀一次溫度同時(shí)啟動(dòng)一次溫度轉(zhuǎn)換
DispIniData();         //設(shè)置第1頁(yè)初始界面待顯示的數(shù)據(jù)“-01”
/*定時(shí)器0和中斷系統(tǒng)初始化*/
TMOD=0x11;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;        //1ms時(shí)基
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;        //50ms時(shí)基       
ET0=1;                        //中斷分開(kāi)關(guān)
ET1=1;
EA=1;                  //中斷總開(kāi)關(guān)
TR0=1;                 //定時(shí)器0啟動(dòng)定時(shí)
TR1=1;
}
/**********************************************************
函數(shù)名稱(chēng):main
***********************************************************/
void main(void)
{
SysInit();
while(1)
{
////////////////////////////////////////////////////
  if(GETSTART)         //在第2頁(yè)上開(kāi)始走數(shù)并采集溫度
  {
        sec=ReadSec();
    min=ReadMin();
    hou=ReadHou();
        //根據(jù)分鐘和秒的數(shù)據(jù)換算成秒為單位的時(shí)間
        if(min==0x59)
        sec_total=(sec>>4)*10+(sec&0x0f)-50;
        else
    sec_total=(min+1)*60+(sec>>4)*10+(sec&0x0f)-50;
        //判斷滿(mǎn)足下面的間隔時(shí)間,開(kāi)始采集溫度一次(4種不同時(shí)間間隔采集溫度 )
        if  (((S4_Cou==0)&&(sec_total-sec_before==1))
          ||((S4_Cou==1)&&(sec_total-sec_before==5))
          ||((S4_Cou==2)&&(sec_total-sec_before==30))
          ||((S4_Cou==3)&&(sec_total-sec_before==60)))
    {
         sec_before=sec_total; //把當(dāng)前計(jì)算出來(lái)的秒時(shí)間總數(shù)賦給sec_before,為下一次做準(zhǔn)備
     ReadTemp();           //讀取DS18B20溫度數(shù)據(jù)
         GetTemper[Temp_cou]=((TempData[0]&0xf0)>>4)|((TempData[1]&0x0f)<<4);
         Temp_cou++;
         if(Temp_cou==10)
         {
          Temp_cou=0;
          sec_total=0;
          sec_before=0;
          GETSTART=0;
          GETOVER=1;
         }
    }
        DispClockData();      //初始為23小時(shí)59分59秒的電子鐘數(shù)據(jù)設(shè)置
  }
////////////在第2頁(yè)上溫度采集完畢后顯示第3頁(yè)的初始數(shù)值,但未進(jìn)入第3頁(yè)///////////////////////////////////
  if(GETOVER)
  {
   if(LED500MSFLAG)        LedOn();
   else             LedOff();
   DispTempIniData();
  }
//////////在第3頁(yè)依次顯示10個(gè)溫度數(shù)據(jù)/////////
  if(DISPTEMPER)
  {
    GETOVER=0;                 //為下次采集作準(zhǔn)備
           sec=ReadSec();
        sec_total=(sec>>4)*10+sec&0x0f;
    if(sec_total-sec_before==1)
        {
         DispTemp10Data();
         sec_before=sec_total;                           //把當(dāng)前計(jì)算出來(lái)的秒時(shí)間總數(shù)賦給sec_before,為下一次做準(zhǔn)備
         Temp_cou++;
         if(Temp_cou==9)
         {
          Temp_cou=0;
          sec_total=0;
          sec_before=0;
          DISPTEMPER=0;
          DISPSTOP=1;
         }
        }
  }
/////////第3頁(yè)顯示10個(gè)溫度數(shù)據(jù)后停止走數(shù),停留在最后1個(gè)溫度數(shù)據(jù)的顯示上///
  if(DISPSTOP) DispTempEndData();
/////////在第3頁(yè)顯示界面上按下S7鍵后,重新回到參數(shù)設(shè)置界面//////////
  if(RESTART)
  {
   DispIniData();          
   DISPSTOP=0;
   GETSTART=0;
   DISPTEMPER=0;
   S4_Cou=0;
   RESTART=0;
  }
}
}  
2、timer.c文件
#include "common.h"
#include "display.h"
#include "keyboard.h"
#include "ds1302.h"
#include "led.h"
uchar PageCode=1;
bit GETSTART;
bit GETOVER;
bit DISPTEMPER;
bit DISPSTOP;
bit RESTART;
uint LedCou;
/**********************************************************
函數(shù)名稱(chēng):Timer0
功    能: 定時(shí)器T0中斷服務(wù)程序,為L(zhǎng)ED、數(shù)碼管提供時(shí)間基數(shù)
參    數(shù): 無(wú)參數(shù)
返回值  : 無(wú)
***********************************************************/
void Timer0(void) interrupt 1
{
TR0=0;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;                         //定時(shí)器計(jì)數(shù)器重賦初值
DispScan();
LedCou++;
if(LedCou==500)
{
  LedCou=0;
  LED500MSFLAG=!LED500MSFLAG;
}
TR0=1;
}  
/**********************************************************
函數(shù)名稱(chēng):Timer1
功    能: 定時(shí)器T1中斷服務(wù)程序,為鍵盤(pán)提供時(shí)間基數(shù)
參    數(shù): 無(wú)參數(shù)
返回值  : 無(wú)
***********************************************************/
void Timer1(void) interrupt 3
{
TR1=0;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;                             //定時(shí)器計(jì)數(shù)器重賦初值
KeyScan();
switch(Trg)
{
  case 0x01:
  {
   if(PageCode==3)                           //如果在第3頁(yè)顯示界面上按下S7,則回到初始界面
   {
    PageCode=1;
        RESTART=1;            
   }
   break;
  }
  case 0x02:
  {
   if((PageCode==2)&&GETOVER)
   {
        DISPTEMPER=1;
    PageCode=3;
    Afresh_DS1302();                        //重新計(jì)時(shí)
        LedOff();                               //L1燈熄滅         
   }
   break;
  }
  case 0x04:
  {
   if(PageCode==1)
   {
    PageCode=2;
        GETSTART=1;
        Init_DS1302();
   }
   break;
  }
  case 0x08:
  {
   if(PageCode==1)
   {
    S4_Cou++;if(S4_Cou==4)S4_Cou=0;
    DispInterval();
   }
   break;
  }
  default:{break;}
}
TR1=1;
}
3、led.c文件
#include "common.h"
#include "timer.h"
/**********************************************************
函數(shù)名稱(chēng): LedOn
功    能: LED發(fā)光二極管亮
參    數(shù): 無(wú)參數(shù)
返回值  : 無(wú)
***********************************************************/
void LedOn(void)
{
  LED=0xfe;
}
/**********************************************************
函數(shù)名稱(chēng): LedOff
功    能: LED發(fā)光二極管滅
參    數(shù): 無(wú)參數(shù)
返回值  : 無(wú)
***********************************************************/
void LedOff(void)
{
  LED=0xff;
}

4、display.c文件
#include "common.h"
//定義數(shù)碼顯示段碼0、1、2、3、4、5、6、7、8、9、-、不亮共12個(gè)元素的數(shù)組
uchar code Disp_Seg[12] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar code Disp_Bit[8]  = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar Disp_Dec[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//存放待顯示的8個(gè)數(shù)據(jù)的十進(jìn)制數(shù)組
bit LED500MSFLAG;
uchar code InterVal[4]={1,5,30,60};
uchar S4_Cou=0;
uchar GetTemper[10];
uchar Temp_cou;
uchar sec,min,hou;
uchar sec_total;
uchar sec_before;
/**********************************************************
函數(shù)名稱(chēng): DispScan
***********************************************************/
void DispScan(void)
{
static uchar ScanPoint=0;
/*每隔1ms對(duì)某位數(shù)碼管進(jìn)行掃描顯示*/
DISPBIT=0x00;
DISPSEG=0xff;                                      //循環(huán)顯示下一位數(shù)碼管前先關(guān)閉數(shù)碼管顯示以避免重影
DISPBIT=Disp_Bit[ScanPoint];                       //數(shù)碼管位碼先賦值
DISPSEG=Disp_Seg[Disp_Dec[ScanPoint]];             //數(shù)碼管段碼再賦值
ScanPoint++;
if(ScanPoint==8) ScanPoint=0;
}
/**********************************************************
函數(shù)名稱(chēng): DispIniData
***********************************************************/
void DispIniData(void)
{
Disp_Dec[0]=11;
Disp_Dec[1]=11;
Disp_Dec[2]=11;
Disp_Dec[3]=11;
Disp_Dec[4]=11;
Disp_Dec[5]=10;
Disp_Dec[6]=0;
Disp_Dec[7]=1;
}
/**********************************************************
函數(shù)名稱(chēng): DispInterval,采樣溫度數(shù)據(jù)的時(shí)間間隔的顯示
***********************************************************/
void DispInterval(void)
{
Disp_Dec[0]=11;Disp_Dec[1]=11;Disp_Dec[2]=11;
Disp_Dec[3]=11;Disp_Dec[4]=11;
Disp_Dec[5]=10;
Disp_Dec[6]=InterVal[S4_Cou]/10;
Disp_Dec[7]=InterVal[S4_Cou]%10;
}
/**********************************************************
函數(shù)名稱(chēng): DispClockData,顯示電子鐘的數(shù)據(jù)
***********************************************************/
void DispClockData(void)
{
Disp_Dec[0]=hou>>4;
Disp_Dec[1]=hou&0x0f;
Disp_Dec[3]=min>>4;
Disp_Dec[4]=min&0x0f;
Disp_Dec[6]=sec>>4;
Disp_Dec[7]=sec&0x0f;
if(LED500MSFLAG){Disp_Dec[2]=10;Disp_Dec[5]=10;}        //"-"閃爍
else            {Disp_Dec[2]=11;Disp_Dec[5]=11;}
}
/**********************************************************
函數(shù)名稱(chēng): DispTempIniData,采樣溫度數(shù)據(jù)的初始顯示
***********************************************************/
void DispTempIniData(void)
{
Disp_Dec[0]=10;
Disp_Dec[1]=0;
Disp_Dec[2]=0;
Disp_Dec[3]=11;
Disp_Dec[4]=11;
Disp_Dec[5]=10;
Disp_Dec[6]=GetTemper[0]/10;
Disp_Dec[7]=GetTemper[0]%10;
}
/**********************************************************
函數(shù)名稱(chēng): DispTemp10Data,顯示10個(gè)采樣的溫度數(shù)據(jù)
***********************************************************/
void DispTemp10Data(void)
{
Disp_Dec[0]=10;
Disp_Dec[1]=sec_total/10;
Disp_Dec[2]=sec_total%10;
Disp_Dec[3]=11;
Disp_Dec[4]=11;
Disp_Dec[5]=10;
Disp_Dec[6]=GetTemper[Temp_cou]/10;
Disp_Dec[7]=GetTemper[Temp_cou]%10;
}
/**********************************************************
函數(shù)名稱(chēng): DispTempEndData,顯示10個(gè)采樣溫度數(shù)據(jù)的最后一個(gè)
***********************************************************/
void DispTempEndData(void)
{
Disp_Dec[0]=10;
Disp_Dec[1]=0;
Disp_Dec[2]=9;
Disp_Dec[3]=11;
Disp_Dec[4]=11;
Disp_Dec[5]=10;
Disp_Dec[6]=GetTemper[9]/10;
Disp_Dec[7]=GetTemper[9]%10;
}

5、keyboard.c文件
#include "common.h"
uchar Trg,Cont,ReadData;
/**********************************************************
函數(shù)名稱(chēng):KeyScan
功    能:獲取鍵值
參    數(shù):無(wú)參數(shù)
返回值  :4個(gè)獨(dú)立鍵盤(pán)的鍵值
***********************************************************/
void KeyScan(void)
{
  P3=0xff;
  ReadData=P3^0xff;
  Trg=ReadData&(ReadData^Cont);               //觸發(fā)鍵
  Cont=ReadData;                               //長(zhǎng)按鍵
}

6、ds18b20.c文件
#include "common.h"
#include "intrins.h"
uchar TempData[2]={0x00,0x00};
sbit DQ=P1^4;
/**********************************************************
函數(shù)名稱(chēng): Delay11us
功    能: 11us延時(shí)函數(shù)
參    數(shù): 無(wú)參數(shù)
返回值  : 無(wú)
***********************************************************/
void Delay11us(uint t)
{for(;t>0;t--);}
/**********************************************************
函數(shù)名稱(chēng): Ds18b20_Rst
功    能: DS18B20復(fù)位函數(shù)
參    數(shù): 無(wú)參數(shù)
返回值  : 無(wú)
***********************************************************/
void Ds18b20_Rst(void)
{
uchar presence=1;      //定義存在脈沖
EA=0;
while(presence)
{
  while(presence)
  {
   DQ=1;_nop_();_nop_();
   DQ=0;
   Delay11us(50);           //延時(shí)550us
   DQ=1;
   Delay11us(6);
   presence=DQ;
  }
  Delay11us(45);            //延時(shí)500us
  presence=~DQ;
}
DQ=1;
EA=1;
}
/**********************************************************
函數(shù)名稱(chēng): Read_byte
功    能: DS18B20讀一個(gè)字節(jié)的函數(shù)
參    數(shù): 無(wú)參數(shù)
返回值  : 返回讀取的字節(jié)value
***********************************************************/
uchar Read_byte(void)
{
uchar i;
uchar value=0;
for(i=8;i>0;i--)
{
  EA=0;
  DQ=1;_nop_();_nop_();
  value>>=1;
  DQ=0;
  _nop_();_nop_();_nop_();_nop_();   //4us
  DQ=1;
  _nop_();_nop_();_nop_();_nop_();   //4us
  if(DQ) value|=0x80;
  Delay11us(6);
  EA=1;
}
DQ=1;
return(value);
}
/**********************************************************
函數(shù)名稱(chēng): Write_byte
功    能: DS18B20寫(xiě)一個(gè)字節(jié)的函數(shù)
參    數(shù): 8位命令val
返回值  : 返回讀取的字節(jié)
***********************************************************/
void Write_byte(uchar val)     
{                       
unsigned char i;
for(i=8;i>0;i--)
{
  EA=0;
  DQ=1;_nop_();_nop_();
  DQ=0;_nop_();_nop_();_nop_();_nop_();_nop_();   //5us
  DQ=val&0x01;
  Delay11us(6);
  val=val/2;
  EA=1;
}
DQ=1;
Delay11us(1);       
}
/**********************************************************
函數(shù)名稱(chēng): ReadTemp
功    能: 讀取溫度函數(shù)
參    數(shù): 無(wú)
返回值  : 無(wú)
***********************************************************/
void ReadTemp(void)
{
Ds18b20_Rst();            //總線(xiàn)復(fù)位
Write_byte(0xcc);         //發(fā)skim Rom命令
Write_byte(0xbe);         //發(fā)讀命令
TempData[0]=Read_byte();  //存放溫度低8位
TempData[1]=Read_byte();  //存放溫度高8位
Ds18b20_Rst();
Write_byte(0xcc);         //發(fā)skim Rom命令,啟用存儲(chǔ)器操作命令前跳過(guò)64位Rom編碼命令
Write_byte(0x44);         //啟動(dòng)一次溫度轉(zhuǎn)換
}

7、ds1302文件
#include "common.h"
sbit DATA_IO=P2^3;
sbit SCLK=P1^7;
sbit RST=P1^3;
/**********************************************************
函數(shù)名稱(chēng): Write1302Byte
功    能: 寫(xiě)一個(gè)字節(jié)到1302中
參    數(shù): dat
返回值  : 無(wú)
***********************************************************/
void Write1302Byte(uchar dat)
{
uchar i,j;
j=dat;
SCLK=0;
for(i=0;i<8;i++)
{
  DATA_IO=0;
  DATA_IO=j&0x01;
  j=j>>1;
  SCLK=1;
  SCLK=0;
}
SCLK=0;
}
/**********************************************************
函數(shù)名稱(chēng): Read1302Byte
功    能: 從1302中讀出一個(gè)字節(jié)
參    數(shù): 無(wú)
返回值  : 讀出字節(jié)返回給函數(shù)
***********************************************************/
uchar Read1302Byte(void)
{
uchar i,dat;
dat=0;
for(i=0;i<7;i++)
{
  if(DATA_IO==1)
  {
   dat=dat|0x80;
  }
  else
  {
   dat=dat&0x7f;
  }
  dat=dat>>1;
  SCLK=1;
  SCLK=0;
}
return(dat);
}
/**********************************************************
函數(shù)名稱(chēng): WriteSet1302
功    能: 向1302寄存器地址寫(xiě)一個(gè)字節(jié)數(shù)據(jù)
參    數(shù): RegAddr,RegNum
返回值  : 無(wú)
***********************************************************/
void WriteSet1302(uchar RegAddr,uchar RegNum)
{
RST=0;
RST=1;
Write1302Byte(RegAddr);  //寫(xiě)地址
Write1302Byte(RegNum);   //寫(xiě)數(shù)據(jù)
RST=0;
}
/**********************************************************
函數(shù)名稱(chēng): ReadSet1302
功    能: 從1302寄存器地址讀出所設(shè)置的數(shù)據(jù)
參    數(shù): RegAddr
返回值  : 無(wú)
***********************************************************/
uchar ReadSet1302(uchar RegAddr)
{
uchar Ds1302Temp;
RST=0;
RST=1;
Write1302Byte(RegAddr);        //寫(xiě)寄存器地址
Ds1302Temp=Read1302Byte();                //讀寄存器數(shù)據(jù)
RST=0;
return(Ds1302Temp);
}
//////////////功能程序/////////////////////////////
/**********************************************************
函數(shù)名稱(chēng): WriteSec
功    能: 向1302秒寄存器寫(xiě)入秒數(shù)據(jù)
參    數(shù): SecNum
返回值  : 無(wú)
***********************************************************/
void WriteSec(uchar SecNum)
{
WriteSet1302(0x80,SecNum);
}
/**********************************************************
函數(shù)名稱(chēng): ReadSec
功    能: 從1302秒寄存器讀出秒數(shù)據(jù)
參    數(shù): 無(wú)
返回值  : 將秒寄存器中值返回給函數(shù)
***********************************************************/
uchar ReadSec(void)
{
uchar SecTemp;
SecTemp=ReadSet1302(0x81);
return(SecTemp);
}
/**********************************************************
函數(shù)名稱(chēng): WriteMin
功    能: 向1302分鐘寄存器寫(xiě)入分鐘數(shù)據(jù)
參    數(shù): MinNum
返回值  : 無(wú)
***********************************************************/
void WriteMin(uchar MinNum)
{
WriteSet1302(0x82,MinNum);
}
/**********************************************************
函數(shù)名稱(chēng): ReadMin
功    能: 從1302分鐘寄存器讀出分鐘數(shù)據(jù)
參    數(shù): 無(wú)
返回值  : 將分鐘寄存器中值返回給函數(shù)
***********************************************************/
uchar ReadMin(void)
{
uchar MinTemp;
MinTemp=ReadSet1302(0x83);
return(MinTemp);
}
/**********************************************************
函數(shù)名稱(chēng): WriteHou
功    能: 向1302小時(shí)寄存器寫(xiě)入小時(shí)數(shù)據(jù)
參    數(shù): HouNum
返回值  : 無(wú)
***********************************************************/
void WriteHou(uchar HouNum)
{
WriteSet1302(0x84,HouNum);
}
/**********************************************************
函數(shù)名稱(chēng): ReadHou
功    能: 從1302小時(shí)寄存器讀出小時(shí)數(shù)據(jù)
參    數(shù): 無(wú)
返回值  : 將分鐘寄存器中值返回給函數(shù)
***********************************************************/
uchar ReadHou(void)
{
uchar HouTemp;
HouTemp=ReadSet1302(0x85);
return(HouTemp);
}
/**********************************************************
函數(shù)名稱(chēng): DisableWP
功    能: 打開(kāi)寫(xiě)保護(hù)
參    數(shù): 無(wú)
返回值  : 無(wú)
***********************************************************/
void DisableWP(void)
{
WriteSet1302(0x8e,0x00);
}
/**********************************************************
函數(shù)名稱(chēng): EnableWP
功    能: 啟動(dòng)寫(xiě)保護(hù)
參    數(shù): 無(wú)
返回值  : 無(wú)
***********************************************************/
void EnableWP(void)
{
WriteSet1302(0x8e,0x80);
}
/**********************************************************
函數(shù)名稱(chēng): Init_Ds1302
功    能: 初始化DS1302器件
參    數(shù): 無(wú)
返回值  : 無(wú)
***********************************************************/
void Init_DS1302(void)
{       
   DisableWP();         //非寫(xiě)保護(hù)
   WriteSec(0x50);      //寫(xiě)秒
   WriteMin(0x59);      //寫(xiě)分
   WriteHou(0x23);      //寫(xiě)時(shí)(24小時(shí)制)
   EnableWP();
}
/**********************************************************
函數(shù)名稱(chēng): Init_Ds1302
功    能: 顯示溫度時(shí)重新計(jì)時(shí)
參    數(shù): 無(wú)
返回值  : 無(wú)
***********************************************************/
void Afresh_DS1302(void)
{       
   DisableWP();         //非寫(xiě)保護(hù)
   WriteSec(0x00);      //寫(xiě)秒
   WriteMin(0x00);      //寫(xiě)分
   WriteHou(0x00);      //寫(xiě)時(shí)(24小時(shí)制)
   EnableWP();
}







分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表