蓝桥杯单片机_14届源代码

#include "reg52.h"
#include "iic.h"
#include "ds1302.h"
#include "onewire.h"

#define TSMG 500
sfr P4 = 0xc0;
sbit R1 = P3 ^ 3;
sbit R2 = P3 ^ 2;
sbit C1 = P4 ^ 4;
sbit C2 = P4 ^ 2;

code unsigned char SMG_NoDot[18] = {
    0xc0, 0xf9,
    0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80,
    0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};

code unsigned char SMG_Dot[10] = {
    0x40, 0x79,
    0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00,
    0x10}; //-0x80就好了其实

code unsigned char READ_RTC_ADDR[7] = {
    0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};

code unsigned char WRITE_RTC_ADDR[7] = {
    0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};

unsigned char Time[7] = {
    0x30, 0x59, 0x23, 0x19, 0x02, 0x01, 0x24};

void Set_HC573(unsigned char channel, unsigned char dat)
{
    P0 = dat;
    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;
    case 0:
        P2 = (P2 & 0x1f) | 0x00;
        break;
    }
    P2 = (P2 & 0x1f) | 0x00;
}

void DelaySMG(unsigned int t)
{
    while (t--)
        ;
}

void DisplaySMG_Bit(unsigned char pos, unsigned char value)
{
    Set_HC573(6, 0x01 << pos);
    Set_HC573(7, value);
    DelaySMG(TSMG);
    Set_HC573(6, 0x01 << pos);
    Set_HC573(7, 0xff);
}

void DisplaySMG_All(unsigned char value)
{
    Set_HC573(6, 0xff);
    Set_HC573(7, value);
}

//================定义变量区=================
unsigned char hour = 0;
unsigned char min = 0;
unsigned char sec = 0;

unsigned char hour_trigger = 0;
unsigned char min_trigger = 0;

unsigned char stat_smg = 1;
unsigned char stat_smg_old = 1;
unsigned char stat_trigger = 0;
unsigned char stat_led = 0xff;

unsigned int temp_smg = 0;
unsigned int temp_average = 0;
unsigned int temp_max = 0;
unsigned int temp_param = 0;

unsigned int humi_smg = 0;
unsigned int humi_average = 0;
unsigned int humi_max = 0;

unsigned int temp[10] = {0};
unsigned int humi[10] = {0};

unsigned char trigger = 0;
unsigned int freq = 0;
unsigned int count_t1 = 0;
unsigned int count_t2 = 0;
unsigned int count_t3 = 0;
unsigned int count_key = 0;

unsigned char adc_value0 = 0; // 两次光照
unsigned char adc_value1 = 0; // 上一次光照
bit f_key = 0;

void Display_Info()
{
    stat_led |= 0x07;
    switch (stat_smg)
    {
    case 1:
        stat_led &= ~0x01;
        DisplaySMG_Bit(0, SMG_NoDot[hour >> 4]);
        DisplaySMG_Bit(1, SMG_NoDot[hour & 0x0f]);
        DisplaySMG_Bit(2, 0xbf);
        DisplaySMG_Bit(3, SMG_NoDot[min >> 4]);
        DisplaySMG_Bit(4, SMG_NoDot[min & 0x0f]);
        DisplaySMG_Bit(5, 0xbf);
        DisplaySMG_Bit(6, SMG_NoDot[sec >> 4]);
        DisplaySMG_Bit(7, SMG_NoDot[sec & 0x0f]);
        break;
    case 2:
        stat_led &= ~0x01;
        DisplaySMG_Bit(0, 0xc6);
        if (trigger != 0)
        {
            DisplaySMG_Bit(2, SMG_NoDot[temp_max / 100]);
            DisplaySMG_Bit(3, SMG_NoDot[temp_max / 10 % 10]);

            DisplaySMG_Bit(5, SMG_NoDot[temp_average / 100]);
            DisplaySMG_Bit(6, SMG_NoDot[temp_average / 10 % 10]);
            DisplaySMG_Bit(7, SMG_NoDot[temp_average % 10]);
        }
        break;
    case 3:
        stat_led &= ~0x02;
        DisplaySMG_Bit(0, 0x89);
        if (trigger != 0)
        {
            DisplaySMG_Bit(2, SMG_NoDot[humi_max / 100]);
            DisplaySMG_Bit(3, SMG_NoDot[humi_max / 10 % 10]);

            DisplaySMG_Bit(5, SMG_NoDot[humi_average / 100]);
            DisplaySMG_Bit(6, SMG_NoDot[humi_average / 10 % 10]);
            DisplaySMG_Bit(7, SMG_NoDot[humi_average % 10]);
        }
        break;
    case 4:
        stat_led &= ~0x02;
        DisplaySMG_Bit(0, 0x8e);
        DisplaySMG_Bit(1, SMG_NoDot[trigger / 10]);
        DisplaySMG_Bit(2, SMG_NoDot[trigger % 10]);
        if (trigger != 0)
        {
            DisplaySMG_Bit(3, SMG_NoDot[hour >> 4]);
            DisplaySMG_Bit(4, SMG_NoDot[hour & 0x0f]);
            DisplaySMG_Bit(5, 0xbf);
            DisplaySMG_Bit(6, SMG_NoDot[min >> 4]);
            DisplaySMG_Bit(7, SMG_NoDot[min & 0x0f]);
        }
        break;
    case 5:
        DisplaySMG_Bit(0, 0x8c);
        if (trigger != 0)
        {
            DisplaySMG_Bit(6, SMG_NoDot[temp_param / 100]);
            DisplaySMG_Bit(7, SMG_NoDot[temp_param / 10 % 10]);
        }
        break;

    case 6:
        stat_led &= ~0x04;
        DisplaySMG_Bit(0, 0x86);
        DisplaySMG_Bit(3, SMG_NoDot[temp_smg / 100]);
        DisplaySMG_Bit(4, SMG_NoDot[temp_smg / 10 % 10]);
        DisplaySMG_Bit(5, 0xbf);
        if (humi_smg == 5555)
        {
            DisplaySMG_Bit(6, SMG_NoDot[10]);
            DisplaySMG_Bit(7, SMG_NoDot[10]);
        }
        else
        {
            DisplaySMG_Bit(3, SMG_NoDot[humi_smg / 100]);
            DisplaySMG_Bit(4, SMG_NoDot[humi_smg / 10 % 10]);
        }
        break;
    }
    Set_HC573(4, stat_led);
}

void Init_Timer0_Timer1()
{
    TH1 = (65536 - 10000) / 256;
    TL1 = (65536 - 10000) % 256;
    TH0 = 0;
    TL0 = 0;
    TMOD = 0x15;

    ET1 = 1;
    EA = 1;
    TR0 = 1;
    TR1 = 1;
}

void Service_Timer0() interrupt 3
{
    // Pro
    TH1 = (65536 - 10000) / 256;
    TL1 = (65536 - 10000) % 256;
    count_t1++;
    count_t2++;
    if (count_t2 == 10)
    {
        count_t2 = 0;
        if ((stat_led & 0x08) == 0x00)
        {
            stat_led |= 0x08;
            Set_HC573(4, stat_led);
        }
    }
    if (count_t1 == 10)
    {
        count_t1 = 0;
        freq = (TH0 << 8) | TL0;
        TH0 = 0;
        TL0 = 0;
    }
    if(stat_trigger == 1)
    {
        count_t3++;
        if(count_t3 == 300)
        {
            stat_smg = stat_smg_old;
            count_t3 = 0;
            stat_trigger=  0;
        }
    }
    if(f_key == 1)
    {
        count_key++;
    }
}

void Init_ds1302()
{
    unsigned char i;
    Write_Ds1302_Byte(0x8E, 0x00);
    for (i = 0; i < 3; i++)
    {
        Write_Ds1302_Byte(WRITE_RTC_ADDR[i], Time[i]);
    }
    Write_Ds1302_Byte(0x8E, 0x80);
}

void Read_ds1302()
{
    sec = Read_Ds1302_Byte(0x81);
    min = Read_Ds1302_Byte(0x83);
    hour = Read_Ds1302_Byte(0x85);
}

void Delay750ms(void) //@12.000MHz
{
    unsigned char data i, j, k;

    _nop_();
    _nop_();
    i = 35;
    j = 51;
    k = 182;
    do
    {
        do
        {
            while (--k)
                ;
        } while (--j);
    } while (--i);
}

void Begin_18b20()
{
    init_ds18b20();
    Write_DS18B20(0xcc);
    Write_DS18B20(0x44);
    Delay750ms();
}

void Handle_Temp()
{
    unsigned int sum = 0;
    unsigned char i;
    temp[(trigger - 1) % 10] = temp_smg;
    if (temp_smg > temp_max)
    {
        temp_max = temp_smg;
    }
    if (trigger < 10)
    {
        for (i = 0; i < trigger; i++)
        {
            sum += temp[i];
        }
        temp_average = sum / trigger;
    }
    else
    {
        for (i = 0; i < 10; i++)
        {
            sum += temp[i];
        }
        temp_average = sum / 10;
    }
}

void Read_ds18b20_adc()
{
    unsigned char LSB, MSB;
    unsigned int tmp = 0;
    float temp = 0;
    init_ds18b20();
    Write_DS18B20(0xcc);
    Write_DS18B20(0x44);

    init_ds18b20();
    Write_DS18B20(0xcc);
    Write_DS18B20(0xbe);
    LSB = Read_DS18B20();
    MSB = Read_DS18B20();
    tmp = (LSB << 8) | MSB;
    if ((tmp & 0xf800) == 0x0000)
    {
        temp = tmp * 0.0625;
    }
    temp_smg = temp * 10;
    Display_Info();
    if (humi_smg != 5555)
    {
        Handle_Temp();
    }
}

void Handle_humi()
{
    unsigned int sum = 0;
    unsigned char i;
    humi[(trigger - 1) % 10] = humi_smg;
    if (humi_smg > humi_max)
    {
        humi_max = humi_smg;
    }
    if (trigger < 10)
    {
        for (i = 0; i < trigger; i++)
        {
            sum += humi[i];
        }
        humi_average = sum / trigger;
    }
    else
    {
        for (i = 0; i < 10; i++)
        {
            sum += humi[i];
        }
        humi_average = sum / 10;
    }
}

void Freq_to_Humi()
{
    float humi = 0;
    if (freq >= 200 && freq <= 2000)
    {
        humi = (80.0 / 1800.0) * (freq - 200) + 10;
        humi_smg = humi * 10;
        stat_led |= 0x10;
        Set_HC573(4, stat_led);

        trigger++;
        Read_ds1302();
        hour_trigger = hour;
        min_trigger = min;

        Handle_humi();
    }
    else
    {
        humi_smg = 5555;
        stat_led &= ~0x10;
        Set_HC573(4, stat_led);
    }
}

void Compare_temp_humi()
{
    unsigned char n;
    if (trigger >= 2)
    {
        n = (trigger - 1) % 10;
        if (n == 0)
        {
            if (temp[0] > temp[9] && humi[0] > humi[9])
            {
                stat_led &= ~0x20;
                Set_HC573(4, stat_led);
            }
            else
            {
                stat_led |= 0x20;
                Set_HC573(4, stat_led);
            }
        }
        else
        {
            if (temp[n] > temp[n - 1] && humi[n] > humi[n - 1])
            {
                stat_led &= ~0x20;
                Set_HC573(4, stat_led);
            }
            else
            {
                stat_led |= 0x20;
                Set_HC573(4, stat_led);
            }
        }
    }
}

void Set_PCF8591(unsigned char ain)
{
    I2CStart();
    I2CSendByte(0x90);
    I2CWaitAck();
    I2CSendByte(ain);
    I2CWaitAck();
    I2CStop();
}

unsigned char Read_PCF8591_ADC()
{
    unsigned char tmp;
    I2CStart();
    I2CSendByte(0x91);
    I2CWaitAck();
    tmp = I2CReceiveByte();
    I2CSendAck(1);
    I2CStop();
    return tmp;
}

void GetADC_and_Check_Trigger()
{
    if (stat_trigger == 0)
    {
        adc_value1 = Read_PCF8591_ADC();
        if (adc_value1 <= 30 && adc_value0 > 30)
        {
            stat_trigger = 1;
        }
        adc_value0 = adc_value1;
    }
}

void S4_Func()
{
    if (stat_smg == 1)
    {
        stat_smg = 2;
    }
    else if (stat_smg == 2 || stat_smg == 3 || stat_smg == 4)
    {
        stat_smg = 5;
    }
    else if (stat_smg == 5)
    {
        stat_smg = 1;
    }
}

void S5_Func()
{
    if (stat_smg == 2)
    {
        stat_smg = 3;
    }
    else if (stat_smg == 3)
    {
        stat_smg = 4;
    }
    else if (stat_smg == 4)
    {
        stat_smg = 2;
    }
}

void S8_Func()
{
    if (stat_smg == 5)
    {
        if (temp_param < 990)
        {
            temp_param = temp_param + 10;
        }
    }
}

void S9_Func()
{
    if (stat_smg == 5)
    {
        if (temp_param > 0)
        {
            temp_param = temp_param - 10;
        }
    }
    else if (stat_smg == 4)
    {
        count_key = 0;
        f_key = 1;
        while (C2 == 0)
        {
            Display_Info();
        }
        f_key = 1;
        if (count_key > 200)
        {
            trigger = 0;
            temp_max = 0;
            temp_average = 0;
            humi_max = 0;
            humi_average = 0;
        }
    }
}

void Scan_key()
{
    R1 = 0;
    R2 = 1;
    if (C1 == 0)
    {
        DelaySMG(1000);
        if (C1 == 0)
        {
            S4_Func();
            while (C1 == 0)
            {
                Display_Info();
            }
        }
    }
    if (C2 == 0)
    {
        DelaySMG(1000);
        if (C2 == 0)
        {
            S8_Func();
            while (C2 == 0)
            {
                Display_Info();
            }
        }
    }

    R1 = 1;
    R2 = 0;
    if (C1 == 0)
    {
        DelaySMG(1000);
        if (C1 == 0)
        {
            S5_Func();
            while (C1 == 0)
            {
                Display_Info();
            }
        }
    }
    if (C2 == 0)
    {
        DelaySMG(1000);
        if (C2 == 0)
        {
            S9_Func();
            while (C2 == 0)
            {
                Display_Info();
            }
        }
    }
}

void Init_sys()
{
    Set_HC573(0, 0x00);
    Set_HC573(5, 0x00);
    Set_HC573(4, 0xff);
    DisplaySMG_All(0xff);
}

void main()
{
    Init_sys();
    Begin_18b20();
    Init_ds1302();
    Set_PCF8591(0x01);
    Read_PCF8591_ADC();
    Init_Timer0_Timer1();
    while (1)
    {
        Read_ds1302();
        GetADC_and_Check_Trigger();
        if (stat_trigger == 1)
        {
            Freq_to_Humi();
            Read_ds18b20_adc();
            Compare_temp_humi();

            stat_smg_old = stat_smg;
            stat_smg = 6;
            while (stat_trigger == 1)
            {
                Display_Info();
            }
        }
        Scan_key();
        Display_Info();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Caihua_X

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值