欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
藍橋杯“溫度監控器”源代碼
[打印本頁]
作者:
shijian2018
時間:
2018-4-13 07:22
標題:
藍橋杯“溫度監控器”源代碼
1、main.c文件
#include "common.h"
#include "timer.h"
#include "ds18b20.h"
#include "display.h"
#include "ds1302.h"
#include "led.h"
/**********************************************************
函數名稱:SysInit
***********************************************************/
void SysInit(void)
{
/*總線設備初始化*/
LED=0xff; //LED燈全滅
DISPBIT=0x00; //數碼管位碼賦值全0
DISPSEG=0xff; //數碼管段碼賦值全1
//
ReadTemp(); //初始化的時候讀一次溫度同時啟動一次溫度轉換
DispIniData(); //設置第1頁初始界面待顯示的數據“-01”
/*定時器0和中斷系統初始化*/
TMOD=0x11;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256; //1ms時基
TH1=(65536-50000)/256;
TL1=(65536-50000)%256; //50ms時基
ET0=1; //中斷分開關
ET1=1;
EA=1; //中斷總開關
TR0=1; //定時器0啟動定時
TR1=1;
}
/**********************************************************
函數名稱:main
***********************************************************/
void main(void)
{
SysInit();
while(1)
{
////////////////////////////////////////////////////
if(GETSTART) //在第2頁上開始走數并采集溫度
{
sec=ReadSec();
min=ReadMin();
hou=ReadHou();
//根據分鐘和秒的數據換算成秒為單位的時間
if(min==0x59)
sec_total=(sec>>4)*10+(sec&0x0f)-50;
else
sec_total=(min+1)*60+(sec>>4)*10+(sec&0x0f)-50;
//判斷滿足下面的間隔時間,開始采集溫度一次(4種不同時間間隔采集溫度 )
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; //把當前計算出來的秒時間總數賦給sec_before,為下一次做準備
ReadTemp(); //讀取DS18B20溫度數據
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小時59分59秒的電子鐘數據設置
}
////////////在第2頁上溫度采集完畢后顯示第3頁的初始數值,但未進入第3頁///////////////////////////////////
if(GETOVER)
{
if(LED500MSFLAG) LedOn();
else LedOff();
DispTempIniData();
}
//////////在第3頁依次顯示10個溫度數據/////////
if(DISPTEMPER)
{
GETOVER=0; //為下次采集作準備
sec=ReadSec();
sec_total=(sec>>4)*10+sec&0x0f;
if(sec_total-sec_before==1)
{
DispTemp10Data();
sec_before=sec_total; //把當前計算出來的秒時間總數賦給sec_before,為下一次做準備
Temp_cou++;
if(Temp_cou==9)
{
Temp_cou=0;
sec_total=0;
sec_before=0;
DISPTEMPER=0;
DISPSTOP=1;
}
}
}
/////////第3頁顯示10個溫度數據后停止走數,停留在最后1個溫度數據的顯示上///
if(DISPSTOP) DispTempEndData();
/////////在第3頁顯示界面上按下S7鍵后,重新回到參數設置界面//////////
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;
/**********************************************************
函數名稱:Timer0
功 能: 定時器T0中斷服務程序,為LED、數碼管提供時間基數
參 數: 無參數
返回值 : 無
***********************************************************/
void Timer0(void) interrupt 1
{
TR0=0;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256; //定時器計數器重賦初值
DispScan();
LedCou++;
if(LedCou==500)
{
LedCou=0;
LED500MSFLAG=!LED500MSFLAG;
}
TR0=1;
}
/**********************************************************
函數名稱:Timer1
功 能: 定時器T1中斷服務程序,為鍵盤提供時間基數
參 數: 無參數
返回值 : 無
***********************************************************/
void Timer1(void) interrupt 3
{
TR1=0;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256; //定時器計數器重賦初值
KeyScan();
switch(Trg)
{
case 0x01:
{
if(PageCode==3) //如果在第3頁顯示界面上按下S7,則回到初始界面
{
PageCode=1;
RESTART=1;
}
break;
}
case 0x02:
{
if((PageCode==2)&&GETOVER)
{
DISPTEMPER=1;
PageCode=3;
Afresh_DS1302(); //重新計時
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"
/**********************************************************
函數名稱: LedOn
功 能: LED發光二極管亮
參 數: 無參數
返回值 : 無
***********************************************************/
void LedOn(void)
{
LED=0xfe;
}
/**********************************************************
函數名稱: LedOff
功 能: LED發光二極管滅
參 數: 無參數
返回值 : 無
***********************************************************/
void LedOff(void)
{
LED=0xff;
}
4、display.c文件
#include "common.h"
//定義數碼顯示段碼0、1、2、3、4、5、6、7、8、9、-、不亮共12個元素的數組
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個數據的十進制數組
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;
/**********************************************************
函數名稱: DispScan
***********************************************************/
void DispScan(void)
{
static uchar ScanPoint=0;
/*每隔1ms對某位數碼管進行掃描顯示*/
DISPBIT=0x00;
DISPSEG=0xff; //循環顯示下一位數碼管前先關閉數碼管顯示以避免重影
DISPBIT=Disp_Bit[ScanPoint]; //數碼管位碼先賦值
DISPSEG=Disp_Seg[Disp_Dec[ScanPoint]]; //數碼管段碼再賦值
ScanPoint++;
if(ScanPoint==8) ScanPoint=0;
}
/**********************************************************
函數名稱: 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;
}
/**********************************************************
函數名稱: DispInterval,采樣溫度數據的時間間隔的顯示
***********************************************************/
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;
}
/**********************************************************
函數名稱: DispClockData,顯示電子鐘的數據
***********************************************************/
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;}
}
/**********************************************************
函數名稱: DispTempIniData,采樣溫度數據的初始顯示
***********************************************************/
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;
}
/**********************************************************
函數名稱: DispTemp10Data,顯示10個采樣的溫度數據
***********************************************************/
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;
}
/**********************************************************
函數名稱: DispTempEndData,顯示10個采樣溫度數據的最后一個
***********************************************************/
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;
/**********************************************************
函數名稱:KeyScan
功 能:獲取鍵值
參 數:無參數
返回值 :4個獨立鍵盤的鍵值
***********************************************************/
void KeyScan(void)
{
P3=0xff;
ReadData=P3^0xff;
Trg=ReadData&(ReadData^Cont); //觸發鍵
Cont=ReadData; //長按鍵
}
6、ds18b20.c文件
#include "common.h"
#include "intrins.h"
uchar TempData[2]={0x00,0x00};
sbit DQ=P1^4;
/**********************************************************
函數名稱: Delay11us
功 能: 11us延時函數
參 數: 無參數
返回值 : 無
***********************************************************/
void Delay11us(uint t)
{for(;t>0;t--);}
/**********************************************************
函數名稱: Ds18b20_Rst
功 能: DS18B20復位函數
參 數: 無參數
返回值 : 無
***********************************************************/
void Ds18b20_Rst(void)
{
uchar presence=1; //定義存在脈沖
EA=0;
while(presence)
{
while(presence)
{
DQ=1;_nop_();_nop_();
DQ=0;
Delay11us(50); //延時550us
DQ=1;
Delay11us(6);
presence=DQ;
}
Delay11us(45); //延時500us
presence=~DQ;
}
DQ=1;
EA=1;
}
/**********************************************************
函數名稱: Read_byte
功 能: DS18B20讀一個字節的函數
參 數: 無參數
返回值 : 返回讀取的字節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);
}
/**********************************************************
函數名稱: Write_byte
功 能: DS18B20寫一個字節的函數
參 數: 8位命令val
返回值 : 返回讀取的字節
***********************************************************/
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);
}
/**********************************************************
函數名稱: ReadTemp
功 能: 讀取溫度函數
參 數: 無
返回值 : 無
***********************************************************/
void ReadTemp(void)
{
Ds18b20_Rst(); //總線復位
Write_byte(0xcc); //發skim Rom命令
Write_byte(0xbe); //發讀命令
TempData[0]=Read_byte(); //存放溫度低8位
TempData[1]=Read_byte(); //存放溫度高8位
Ds18b20_Rst();
Write_byte(0xcc); //發skim Rom命令,啟用存儲器操作命令前跳過64位Rom編碼命令
Write_byte(0x44); //啟動一次溫度轉換
}
7、ds1302文件
#include "common.h"
sbit DATA_IO=P2^3;
sbit SCLK=P1^7;
sbit RST=P1^3;
/**********************************************************
函數名稱: Write1302Byte
功 能: 寫一個字節到1302中
參 數: dat
返回值 : 無
***********************************************************/
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;
}
/**********************************************************
函數名稱: Read1302Byte
功 能: 從1302中讀出一個字節
參 數: 無
返回值 : 讀出字節返回給函數
***********************************************************/
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);
}
/**********************************************************
函數名稱: WriteSet1302
功 能: 向1302寄存器地址寫一個字節數據
參 數: RegAddr,RegNum
返回值 : 無
***********************************************************/
void WriteSet1302(uchar RegAddr,uchar RegNum)
{
RST=0;
RST=1;
Write1302Byte(RegAddr); //寫地址
Write1302Byte(RegNum); //寫數據
RST=0;
}
/**********************************************************
函數名稱: ReadSet1302
功 能: 從1302寄存器地址讀出所設置的數據
參 數: RegAddr
返回值 : 無
***********************************************************/
uchar ReadSet1302(uchar RegAddr)
{
uchar Ds1302Temp;
RST=0;
RST=1;
Write1302Byte(RegAddr); //寫寄存器地址
Ds1302Temp=Read1302Byte(); //讀寄存器數據
RST=0;
return(Ds1302Temp);
}
//////////////功能程序/////////////////////////////
/**********************************************************
函數名稱: WriteSec
功 能: 向1302秒寄存器寫入秒數據
參 數: SecNum
返回值 : 無
***********************************************************/
void WriteSec(uchar SecNum)
{
WriteSet1302(0x80,SecNum);
}
/**********************************************************
函數名稱: ReadSec
功 能: 從1302秒寄存器讀出秒數據
參 數: 無
返回值 : 將秒寄存器中值返回給函數
***********************************************************/
uchar ReadSec(void)
{
uchar SecTemp;
SecTemp=ReadSet1302(0x81);
return(SecTemp);
}
/**********************************************************
函數名稱: WriteMin
功 能: 向1302分鐘寄存器寫入分鐘數據
參 數: MinNum
返回值 : 無
***********************************************************/
void WriteMin(uchar MinNum)
{
WriteSet1302(0x82,MinNum);
}
/**********************************************************
函數名稱: ReadMin
功 能: 從1302分鐘寄存器讀出分鐘數據
參 數: 無
返回值 : 將分鐘寄存器中值返回給函數
***********************************************************/
uchar ReadMin(void)
{
uchar MinTemp;
MinTemp=ReadSet1302(0x83);
return(MinTemp);
}
/**********************************************************
函數名稱: WriteHou
功 能: 向1302小時寄存器寫入小時數據
參 數: HouNum
返回值 : 無
***********************************************************/
void WriteHou(uchar HouNum)
{
WriteSet1302(0x84,HouNum);
}
/**********************************************************
函數名稱: ReadHou
功 能: 從1302小時寄存器讀出小時數據
參 數: 無
返回值 : 將分鐘寄存器中值返回給函數
***********************************************************/
uchar ReadHou(void)
{
uchar HouTemp;
HouTemp=ReadSet1302(0x85);
return(HouTemp);
}
/**********************************************************
函數名稱: DisableWP
功 能: 打開寫保護
參 數: 無
返回值 : 無
***********************************************************/
void DisableWP(void)
{
WriteSet1302(0x8e,0x00);
}
/**********************************************************
函數名稱: EnableWP
功 能: 啟動寫保護
參 數: 無
返回值 : 無
***********************************************************/
void EnableWP(void)
{
WriteSet1302(0x8e,0x80);
}
/**********************************************************
函數名稱: Init_Ds1302
功 能: 初始化DS1302器件
參 數: 無
返回值 : 無
***********************************************************/
void Init_DS1302(void)
{
DisableWP(); //非寫保護
WriteSec(0x50); //寫秒
WriteMin(0x59); //寫分
WriteHou(0x23); //寫時(24小時制)
EnableWP();
}
/**********************************************************
函數名稱: Init_Ds1302
功 能: 顯示溫度時重新計時
參 數: 無
返回值 : 無
***********************************************************/
void Afresh_DS1302(void)
{
DisableWP(); //非寫保護
WriteSec(0x00); //寫秒
WriteMin(0x00); //寫分
WriteHou(0x00); //寫時(24小時制)
EnableWP();
}
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1