找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

藍(lán)橋杯單片機開發(fā)第3屆溫度監(jiān)控器及源代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:656536 發(fā)表于 2020-3-24 19:01 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include <stc15f2k60s2.h>

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit dianji = P3^4;

unsigned char code LedChar[] = {
  0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
        0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF,0x7F
};
unsigned char LedBuff[8] = {
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char temp_min = 23;  //溫度下限
unsigned char temp_max = 30;  //溫度上限
unsigned char get_temp = 0;   //獲取電機溫度

void InitSystem();
void ScanSMG();
void Timer0Init(void);                //10微秒@12.000MHz
void Timer1Init(void);                //1毫秒@12.000MHz
void ConfigWorking();
void ConfigSMG();
void ScanKeys();
extern void I2CWrite(unsigned char addr,unsigned char dat); //I2C寫操作
extern unsigned char I2CRead(unsigned char addr);          //I2C讀操作
extern bit Init18B20();     //18B20初始化
extern unsigned char Get18B20Temp();  //獲取18B20溫度

void main()
{
  Timer0Init();
        Timer1Init();
        InitSystem();
        temp_min = I2CRead(0x01);  //讀取溫度下限
        temp_max = I2CRead(0x00);  //讀取溫度上限
        while(1)
        {
          ConfigWorking();
                ConfigSMG();
                ScanKeys();
        }
}
/******HC573選擇函數(shù)*******/
void SeceltHC573(unsigned char channel)
{
  switch(channel)
        {
          case 4:P2 = (P2&0x1F)|0x80;break;
                case 5:P2 = (P2&0x1F)|0xA0;break;
                case 6:P2 = (P2&0x1F)|0xC0;break;
                case 7:P2 = (P2&0x1F)|0xE0;break;
                default:P2 = (P2&0x1F)|0x00;break;
        }
}
/*******關(guān)閉LED和蜂鳴器、繼電器******/
void InitSystem()
{
  SeceltHC573(5);
        P0 = 0x00;
        SeceltHC573(4);
        P0 = 0xFF;
        SeceltHC573(0);
}
/******工作模式配置*******/
void ConfigWorking()
{
  Init18B20();
        get_temp = Get18B20Temp();
        if(get_temp<temp_min)
        {
          EA = 0;        //存儲的溫度下限數(shù)據(jù), 繼電器打開
                SeceltHC573(5);
                P0 = 0x10;
                SeceltHC573(0);
                EA = 1;
        }
        else
        {
          EA = 0;        //存儲的溫度下限數(shù)據(jù), 繼電器打開
                SeceltHC573(5);
                P0 = 0x00;
                SeceltHC573(0);
                EA = 1;
        }
        
}
/******數(shù)碼管的動態(tài)顯示******/
void ConfigSMG()
{
  LedBuff[0] = LedChar[temp_max/10];
        LedBuff[1] = LedChar[temp_max%10];
        LedBuff[2] = LedChar[temp_min/10];
        LedBuff[3] = LedChar[temp_min%10];
        LedBuff[4] = 0xFF;
        LedBuff[5] = 0xFF;
        LedBuff[6] = LedChar[get_temp/10];
        LedBuff[7] = LedChar[get_temp%10];
}
/******按鍵處理相關(guān)函數(shù)******/
void Delay1ms()                //@12.000MHz
{
        unsigned char i, j;

        i = 12;
        j = 169;
        do
        {
                while (--j);
        } while (--i);
}
void ScanKeys()
{
        if(S7==0)         //溫度下限減一
        {
          Delay1ms();
                if(S7==0)
                {
                        if(temp_min>0)
                                temp_min--;
                        else
                                temp_min = 0;
                        I2CWrite(0x01,temp_min);   //存儲溫度下限
                        Delay1ms();
                  while(S7==0)
                        {
                          ConfigSMG();
                        }
                }
        }
  if(S6==0)         //溫度上限減一
        {
          Delay1ms();
                if(S6==0)
                {
                        if((temp_max>temp_min)&&(temp_max>0))
                        {
                          temp_max--;
                        }
                        else
                        {
                          temp_max = temp_min;
                        }
                        I2CWrite(0x00,temp_max);   //存儲溫度上限
                        Delay1ms();
                  while(S6==0)
                        {
                          ConfigSMG();
                        }
                }
        }
        if(S5==0)         //溫度下限加一
        {
          Delay1ms();
                if(S5==0)
                {
                        if((temp_min<temp_max)&&(temp_min<99))
                        {
                          temp_min++;
                        }
                        else
                        {
                          temp_min = temp_max;
                        }
                        I2CWrite(0x01,temp_min);   //存儲溫度下限
                        Delay1ms();
                  while(S5==0)
                        {
                          ConfigSMG();
                        }
                }
        }
        if(S4==0)         //溫度上限加一
        {
          Delay1ms();
                if(S4==0)
                {
                        temp_max++;
                        if(temp_max>=99)
                                temp_max = 99;
                        I2CWrite(0x00,temp_max);   //存儲溫度上限
                        Delay1ms();
                  while(S4==0)
                        {
                          ConfigSMG();
                        }
                }
        }
}
/******數(shù)碼管動態(tài)刷新*****/
void ScanSMG()
{
  static unsigned char i = 0;
        P0 = 0xFF;
        SeceltHC573(6);
        P0 = 0x01<<i;
        SeceltHC573(7);
        P0 = LedBuff[i++];
        if(i>=8)
                i = 0;
        SeceltHC573(0);
}
/******定時器0初始化函數(shù)******/
void Timer0Init(void)                //10微秒@12.000MHz
{
        AUXR &= 0x7F;                //定時器時鐘12T模式
        TMOD &= 0xF0;                //設(shè)置定時器模式
        TL0 = 0xF6;                //設(shè)置定時初值
        TH0 = 0xFF;                //設(shè)置定時初值
        TF0 = 0;                //清除TF0標(biāo)志
        TR0 = 1;                //定時器0開始計時
        ET0 = 1;
        EA = 1;
}
void InterruptTimer0()  interrupt 1
{
  static unsigned char pwn= 0;
        pwn++;
        if(pwn<=30)
        {
          dianji = 0;
        }
        else if(pwn>100)
        {
          dianji = 0;
                pwn = 0;
        }
        else
        {
          dianji = 1;
        }
}
/******定時器0初始化函數(shù)******/
void Timer1Init(void)                //1毫秒@12.000MHz
{
        AUXR &= 0xBF;                //定時器時鐘12T模式
        TMOD &= 0x0F;                //設(shè)置定時器模式
        TL1 = 0x18;                //設(shè)置定時初值
        TH1 = 0xFC;                //設(shè)置定時初值
        TF1 = 0;                //清除TF1標(biāo)志
        TR1 = 1;                //定時器1開始計時
        ET1 = 1;
        EA = 1;
}
void InterruptTimer1()  interrupt 3
{
  ScanSMG();   //數(shù)碼管動態(tài)刷新
}

省賽試題-第3屆-溫度監(jiān)控儀.pdf

154.82 KB, 下載次數(shù): 16, 下載積分: 黑幣 -5

第3屆溫度監(jiān)控器_1.rar

52.11 KB, 下載次數(shù): 16, 下載積分: 黑幣 -5

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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