蓝桥杯单片机_15届源代码

#include "reg52.h"
#include "iic.h"
#include "ds1302.h"
#include "math.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[16] = {
    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};

unsigned char code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
unsigned char TIME[7] = {0x05, 0x03, 0x13, 0x18, 0x03, 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 8:
        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 stat_smg = 0;
unsigned char stat_led = 0xff;
unsigned char hour = 0;
unsigned char min = 0;
unsigned char sec = 0;
unsigned char hour_record = 0;
unsigned char min_record = 0;
unsigned char sec_record = 0;
long freq = 0;
long freq_smg = 0;
long freq_max = 0;
int freq_jz = 0;
int freq_jz_smg = 0;
int freq_chao = 2000;
bit f_freq = 0;
bit f_jz = 0;
unsigned int count_t1 = 0;
unsigned int count_t2 = 0;
unsigned int count_t3 = 0;
unsigned char dac_value = 0;

void Display_SMG()
{
    switch (stat_smg)
    {
    // 1-频率界面
    case 1:
        DisplaySMG_Bit(0, SMG_NoDot[15]);
        if (f_jz == 0)
        {
            if (freq_smg > 9999)
            {
                DisplaySMG_Bit(3, SMG_NoDot[freq_smg / 10000]);
            }
            if (freq_smg > 999)
            {
                DisplaySMG_Bit(4, SMG_NoDot[freq_smg / 1000 % 10]);
            }
            if (freq_smg > 99)
            {
                DisplaySMG_Bit(5, SMG_NoDot[freq_smg / 100 % 10]);
            }
            if (freq_smg > 9)
            {
                DisplaySMG_Bit(6, SMG_NoDot[freq_smg / 10 % 10]);
            }
            DisplaySMG_Bit(7, SMG_NoDot[freq_smg % 10]);
        }
        else
        {
            DisplaySMG_Bit(6, 0xc7);
            DisplaySMG_Bit(7, 0xc7);
        }
        break;

    case 2:
        DisplaySMG_Bit(0, 0x8c);
        DisplaySMG_Bit(1, SMG_NoDot[1]);
        DisplaySMG_Bit(4, SMG_NoDot[freq_chao / 1000]);
        DisplaySMG_Bit(5, SMG_NoDot[freq_chao / 100 % 10]);
        DisplaySMG_Bit(6, SMG_NoDot[freq_chao / 10 % 10]);
        DisplaySMG_Bit(7, SMG_NoDot[freq_chao % 10]);
        break;

    case 3:
        freq_jz_smg = abs(freq_jz);
        DisplaySMG_Bit(0, 0x8c);
        DisplaySMG_Bit(1, SMG_NoDot[2]);
        if (freq_jz == 0)
        {
            DisplaySMG_Bit(7, SMG_NoDot[0]);
        }
        else
        {
            DisplaySMG_Bit(5, SMG_NoDot[freq_jz_smg / 100]);
            DisplaySMG_Bit(6, SMG_NoDot[freq_jz_smg / 10 % 10]);
            DisplaySMG_Bit(7, SMG_NoDot[freq_jz_smg % 10]);
            if (freq_jz < 0)
            {
                DisplaySMG_Bit(4, 0xbf);
            }
        }
        break;

    case 4:
        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 5:
        DisplaySMG_Bit(0, 0x89);
        DisplaySMG_Bit(1, 0x8e);
        if (freq_max > 9999)
        {
            DisplaySMG_Bit(3, SMG_NoDot[freq_max / 10000]);
        }
        if (freq_max > 999)
        {
            DisplaySMG_Bit(4, SMG_NoDot[freq_max / 1000 % 10]);
        }
        if (freq_max > 99)
        {
            DisplaySMG_Bit(5, SMG_NoDot[freq_max / 100 % 10]);
        }
        if (freq_max > 9)
        {
            DisplaySMG_Bit(6, SMG_NoDot[freq_max / 10 % 10]);
        }
        DisplaySMG_Bit(7, SMG_NoDot[freq_max % 10]);
        break;

    case 6:
        DisplaySMG_Bit(0, 0x89);
        DisplaySMG_Bit(1, SMG_NoDot[10]);
        DisplaySMG_Bit(2, SMG_NoDot[hour_record >> 4]);
        DisplaySMG_Bit(3, SMG_NoDot[hour_record & 0x0f]);
        DisplaySMG_Bit(4, SMG_NoDot[min_record >> 4]);
        DisplaySMG_Bit(5, SMG_NoDot[min_record & 0x0f]);
        DisplaySMG_Bit(6, SMG_NoDot[sec_record >> 4]);
        DisplaySMG_Bit(7, SMG_NoDot[sec_record & 0x0f]);
    }
}

void Init_DS1302()
{
    unsigned char i;
    Write_Ds1302_Byte(0x8e, 0x00);
    for (i = 0; i < 7; i++)
    {
        Write_Ds1302_Byte(WRITE_RTC_ADDR[i], TIME[i]);
    }
    Write_Ds1302_Byte(0x8e, 0x80);
}
void Read_DS1302_Time()
{
    sec = Read_Ds1302_Byte(0x81);
    min = Read_Ds1302_Byte(0x83);
    hour = Read_Ds1302_Byte(0x85);
}

void Set_PCF8591_DAC(unsigned char dat)
{
    I2CStart();
    I2CSendByte(0x90);
    I2CWaitAck();
    I2CSendByte(0x40);
    I2CWaitAck();
    I2CSendByte(dat);
    I2CWaitAck();
    I2CStop();
}

void Init_Timer0_Timer1()
{
    TH1 = (65536 - 10000) / 256;
    TL1 = (65536 - 10000) % 256;
    TH0 = 0;
    TL0 = 0;
    TMOD = 0x15;
    ET0 = 1;
    ET1 = 1;
    EA = 1;
    TR0 = 1;
    TR1 = 1;
}

void Service_Timer0() interrupt 3
{
    TH1 = (65536 - 10000) / 256;
    TL1 = (65536 - 10000) % 256;
    count_t1++;
    if (count_t1 == 100)
    {
        count_t1 = 0;
        freq = (TH0 << 8) | TL0;
        TH0 = 0;
        TL0 = 0;
        f_freq = 1;
    }
		count_t2++;
    if (count_t2 == 20)
    {
        count_t2 = 0;
        // L1
        if ((stat_led & 0x01) == 0x00)
        {
            stat_led |= 0x01;
            Set_HC573(4, stat_led);
        }
        else if (stat_smg == 1)
        {
            stat_led &= ~0x01;
            Set_HC573(4, stat_led);
        }
        // L2
        if ((stat_led & 0x02) == 0x00 && f_jz == 0)
        {
            stat_led |= 0x02;
            Set_HC573(4, stat_led);
        }
        else if (freq_smg > freq_chao)
        {
            stat_led &= ~0x02;
            Set_HC573(4, stat_led);
        }
    }
}

void Handle_Freq()
{
    freq_smg = freq + freq_jz;
    if (freq_smg >= 0)
    {
        f_jz = 0;
        if (freq_smg > freq_max)
        {
            freq_max = freq_smg;
            Read_DS1302_Time();
            hour_record = hour;
            min_record = min;
            sec_record = sec;
        }
    }
    else
    {
        f_jz = 1;
        stat_led &= ~0x02;
        Set_HC573(4, stat_led);
    }
}

void Handle_DAC()
{
    if (f_jz == 0)
    {
        if (freq_smg < 500)
        {
            Set_PCF8591_DAC(51);
        }
        else if (freq_smg < freq_chao)
        {
            dac_value = ((255.0 - 51.0) / (freq_chao - 500.0)) * (freq_smg - 500) + 51;
            Set_PCF8591_DAC(dac_value);
        }
        else
        {
            Set_PCF8591_DAC(255);
        }
    }
    else
    {
        Set_PCF8591_DAC(0);
    }
}

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

void S5_Funtion()
{
    if (stat_smg == 2)
    {
        stat_smg = 3;
    }
    else if (stat_smg == 3)
    {
        stat_smg = 2;
    }
    else if (stat_smg == 5)
    {
        stat_smg = 6;
    }
    else if (stat_smg == 6)
    {
        stat_smg = 5;
    }
}

void S8_Function()
{
    if (stat_smg == 2)
    {
        freq_chao += 1000;
        if (freq_chao > 9000)
        {
            freq_chao = 9000;
        }
    }
    else if (stat_smg == 3)
    {
        freq_jz += 100;
        if (freq_jz > 900)
        {
            freq_jz = 900;
        }
    }
}

void S9_Function()
{
    if (stat_smg == 2)
    {
        freq_chao -= 1000;
        if (freq_chao < 1000)
        {
            freq_chao = 1000;
        }
    }
    else if (stat_smg == 3)
    {
        freq_jz -= 100;
        if (freq_jz < -900)
        {
            freq_jz = -900;
        }
    }
}

void Scan_Keys()
{
    R1 = 0;
    R2 = 1;
    if (C1 == 0)
    {
        DelaySMG(1000);
        if (C1 == 0)
        {
            S4_Funtion();
            while (C1 == 0)
            {
                Display_SMG();
            }
        }
    }

    if (C2 == 0)
    {
        DelaySMG(1000);
        if (C2 == 0)
        {
            S8_Function();
            while (C2 == 0)
            {
                Display_SMG();
            }
        }
    }

    R1 = 1;
    R2 = 0;
    if (C1 == 0)
    {
        DelaySMG(1000);
        if (C1 == 0)
        {
            S5_Funtion();
            while (C1 == 0)
            {
                Display_SMG();
            }
        }
    }

    if (C2 == 0)
    {
        DelaySMG(1000);
        if (C2 == 0)
        {
            S9_Function();
            while (C2 == 0)
            {
                Display_SMG();
            }
        }
    }
}
void Init_sys()
{
    Set_HC573(0, 0x00);
    Set_HC573(5, 0x00);
    Set_HC573(4, 0xff);
    DisplaySMG_All(0xff);
}

void main()
{
    Init_sys();
    Init_Timer0_Timer1();
    Init_DS1302();
    Set_PCF8591_DAC(dac_value);
    while (f_freq == 0)
        ;
    stat_smg = 1;
    while (1)
    {
        if (f_freq == 1)
        {
            Handle_Freq();
            Handle_DAC();
            f_freq = 0;
        }
        Read_DS1302_Time();
        Scan_Keys();
        Display_SMG();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Caihua_X

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

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

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

打赏作者

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

抵扣说明:

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

余额充值