登录  | 立即注册

游客您好!登录后享受更多精彩

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] 倒计时闹钟

[复制链接]
发表于 2021-2-2 23:12:47 | 显示全部楼层 |阅读模式
学习笔记
学习科目: 51单片机
学习安排: 51单片机
开始时间: 2021-02-01
结束时间: 2021-02-02
自制倒计时闹钟

开发板:金沙滩,stc12c5a60s2,晶振:11.0592mhz

手把手教你学51单片机学到蜂鸣器之后,有自己制作一个倒计时闹钟的想法

花了一天,终于完善了所有代码,并且调试成功

代码中的按键扫描,数码管扫描,蜂鸣器音乐直接套用了,金沙滩的代码

其他的是自己想的,蜂鸣器音乐也给换了换,自己一个音符打出来的

毕竟有自己敲的代码,所以代码可能不是那么规范,清晰

代码太长,发评论区了

演示视频如下
  1. https://b23.tv/asJXkR
复制代码

好懒~~不想说~~~
 楼主| 发表于 2021-2-2 23:14:06 | 显示全部楼层
本帖最后由 物电王铖浩 于 2021-2-2 23:20 编辑

#include <STC12C5A60S2.H>


sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY_IN_1  = P2^4;
sbit KEY_IN_2  = P2^5;
sbit KEY_IN_3  = P2^6;
sbit KEY_IN_4  = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;


sbit BUZZ = P1^6;


unsigned char code LedChar[] = {  
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E, 0x7F
};


unsigned char LedBuff[6] = {  
    0x7F,0x7F,0x7F,0x7F,0x7F,0x7F};


unsigned char code KeyCodeMap[4][4] = {
    { 0x31, 0x32, 0x33, 0x26 },
    { 0x34, 0x35, 0x36, 0x25 },
    { 0x37, 0x38, 0x39, 0x28 },
    { 0x30, 0x1B, 0x0D, 0x27 }  
};
unsigned char KeySta[4][4] = {  
    {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1}
};


unsigned char colon =0;


unsigned int count = 0;


unsigned long hms[3] = {0,0,0};


unsigned flag1s = 0;


unsigned char now = 0;




unsigned int code NoteFrequ[] = {
        523,587,659,698,784,880,988,
        1047,1175,1319,1397,1568,1760,1976
};


unsigned int code NoteReload[] = {


                65536 - (11059200/12) / (523*2),  //中音1
    65536 - (11059200/12) / (587*2),  //2
    65536 - (11059200/12) / (659*2),  //3
    65536 - (11059200/12) / (698*2),  //4
    65536 - (11059200/12) / (784*2),  //5
    65536 - (11059200/12) / (880*2),  //6
    65536 - (11059200/12) / (988*2),  //7
    65536 - (11059200/12) / (1047*2), //高音1
    65536 - (11059200/12) / (1175*2), //2
    65536 - (11059200/12) / (1319*2), //3
    65536 - (11059200/12) / (1397*2), //4
    65536 - (11059200/12) / (1568*2), //5
    65536 - (11059200/12) / (1760*2), //6
    65536 - (11059200/12) / (1976*2), //7
};


bit enable = 1;
bit tmrflag = 0;
unsigned char T0RH = 0xFF;
unsigned char T0RL = 0x00;


void TokyoHot();
void KeyDriver();
void KeyAction(unsigned char keycode);
void ShowNumber(unsigned char h,unsigned char m,unsigned char s);
               
void main()
{
        unsigned long num = 0;
        unsigned char j = 0;
        EA = 1;      
  ENLED = 0;   
  ADDR3 = 1;
        TMOD = 0x01;  
  TH0  = 0xFC;  
  TL0  = 0x67;
  ET0  = 1;     
  TR0  = 1;  
        while(1)
        {
                switch (now)
                {
                        case 0:KeyDriver();break;
                        case 1:KeyDriver();break;
                        case 2:
                                if(flag1s == 1)
                                {
                                        flag1s = 0;
                                        if(hms[2]>0)
                                                hms[2]--;
                                        else if((hms[0]>0) && (hms[1]>0) && (hms[2] == 0))
                                        {
                                                hms[1]--;hms[2] = 59;
                                        }
                                        else if((hms[0]>0) && (hms[1] == 0) && (hms[2] == 0))
                                        {
                                                hms[0]--;hms[1] = 59;hms[2] = 59;
                                        }
                                        else if((hms[0] == 0) && (hms[1] > 0) && (hms[2] == 0))
                                        {
                                                hms[1]--;hms[2] = 59;
                                        }
                                       
                                        num = hms[0]*10000 +hms[1]*100+hms[2];
  
                                        for (j=0; j<6; j++)   
                                        {
                                                        LedBuff[j] = LedChar[num % 10];
                                                        num = num / 10;
                                        }
                                       
                                        if(hms[0]+hms[1]+hms[2] == 0)
                                        {       
                                                TH0 = T0RH;
                                                TL0 = T0RL;
                                                now = 3;
                                                TokyoHot();
                                        }
                                }


                                break;
                        case 3: TokyoHot();break;
                               
                }
        }
}


void NO0();
void NO1();
void NO2();
void NO3();
void KeyScan();
void colonscan1();
void colonscan2();


void InterruptTimer0() interrupt 1
{
        TH0  = 0xFC;  
  TL0  = 0x67;
        switch (now)
        {
                case 0:NO0();KeyScan();break;
                case 1:
                        if(!colon)
                                NO1();
                        else
                                colonscan1();
                        KeyScan();break;
                case 2:
                        if(!colon)
                                NO2();
                        else
                                colonscan2();
                        break;
                case 3: NO3();
        }
}


void NO0()
{
        static unsigned char i = 0;
        count++;
        if(count == 1000)
                {ENLED = ~ENLED;count = 0;}
        P0 = 0xFF;
                P1 = (P1 & 0xF8) | i;
                P0 = LedBuff;
                if(i<5)
                        i++;
                else
                        i = 0;
}


void NO1()
{
        static unsigned char i = 0;
        count++;
        if(count == 500)
                {ENLED = ~ENLED;count = 0;}
        P0 = 0xFF;
        P1 = (P1 & 0xF8) | i;
        P0 = LedBuff;
        if(i<5)
                i++;
        else
        {i = 0;colon = 1;}
}


void NO2()
{
        static unsigned char i = 0;
        ENLED = 0;
        count++;
        if(count == 1000)
                {flag1s = 1;count = 0;}
        P0 = LedBuff;
        P1 = (P1 & 0xF8) | i;
        P0 = LedBuff;
        if(i<5)
                i++;
        else
        {i = 0;colon = 1;}
}


void NO3()
{
        TH0 = T0RH;
        TL0 = T0RL;
        tmrflag = 1;
        if(enable)
        {BUZZ = ~BUZZ;
                P0 = ~P0;}
        else
                BUZZ = 1;
}


void colonscan1()
{
        static unsigned char i = 0;
        count++;
        if(count == 500)
                {ENLED = ~ENLED;count = 0;}
        P0 = 0xFF;
        P1 = (P1 & 0xF8) | (2*i);
        P0 = 0x7F;
        if(i<2)
                i++;
        else
        {i = 0;colon = 0;}
}


void colonscan2()
{
        static unsigned char i = 0;
        count++;
        if(count == 1000)
                {flag1s = 1;count = 0;}
        P0 = 0xFF;
        P1 = (P1 & 0xF8) | (2*i);
        P0 = 0x7F;
        if(i<2)
                i++;
        else
        {i = 0;colon = 0;}
}


void KeyScan()
{
    unsigned char i;
    static unsigned char keyout = 0;   
    static unsigned char keybuf[4][4] = {  
        {0xFF, 0xFF, 0xFF, 0xFF},  {0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF},  {0xFF, 0xFF, 0xFF, 0xFF}
    };


   
    keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1;
    keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2;
    keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3;
    keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4;
   
    for (i=0; i<4; i++)  
    {
        if ((keybuf[keyout] & 0x0F) == 0x00)
        {   
            KeySta[keyout] = 0;
        }
        else if ((keybuf[keyout] & 0x0F) == 0x0F)
        {   
            KeySta[keyout] = 1;
        }
    }
   
    keyout++;               
    keyout = keyout & 0x03;  
    switch (keyout)         
    {
        case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
        case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
        case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
        case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
        default: break;
    }
}


void KeyDriver()
{
    unsigned char i, j;
    static unsigned char backup[4][4] = {  
        {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1}
    };
   
    for (i=0; i<4; i++)  
    {
        for (j=0; j<4; j++)
        {
            if (backup[j] != KeySta[j])   
            {
                if (backup[j] != 0)           
                {
                    KeyAction(KeyCodeMap[j]);
                }
                backup[j] = KeySta[j];     
            }
        }
    }
}


void KeyAction(unsigned char keycode)
{       
        static unsigned long num ;
        static unsigned char i = 0;
        unsigned char j;
    if ((keycode>=0x30) && (keycode<=0x39))  
    {
        hms[i/2] = ((hms[i/2]*10)+(unsigned long)keycode-0x30);
                                i++;
                       
       
                num = hms[0]*10000 +hms[1]*100+hms[2];
   
    for (j=0; j<6; j++)   
    {
        LedBuff[j] = LedChar[num % 10];
        num = num / 10;
    }
        
               
    }
   
    else if (keycode == 0x1B)  
    {
      now = 1;   
    }
                else if (keycode == 0x0D)  
    {
      now = 2;
                        count = 0;
                       
                        ET0  = 0;     
                        TR0  = 0;
                        TH0  = 0xFC;  
                        TL0  = 0x67;
                        ET0  = 1;     
                        TR0  = 1;
    }
}






void TokyoHot()
{
        unsigned int beat;
        unsigned char note;
        unsigned int time = 0;
        unsigned int beatTime = 0;
        unsigned int soundTime = 0;
       
        unsigned char code TwoTigerNote[] = {
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        6,8,10,12,13,12,10,8,
        7,9,12,13,14,13,12,9,       
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        6,8,10,12,13,12,10,8,
        7,9,12,13,14,13,12,9,
       
        10,12,10,12,14,10,12,10,12,14,
        14, 10,12,  10,12,10,12,14,
               
        10, 10,12,10,12,14,  10,10,12,6,
        8,10,12, 11,13,11,  11,9,11,
        10,12,10,12,14, 10,12,10,12,14,
        14,10,12,  
        12,12,12,12,12,12,12,12,12,12,12,12,
        13,13,13,13,13,13,13,13,13,13,13,13,
        11,11,11,11,11,11,11,11,11,11,11,11,
        12,12,12,12,12,12,12,12,12,12,12,12,
        12,12,12,12,12,12,12,12,12,12,12,12,
        10,10,10,10,10,10,10,10,10,10,10,10,
        14,14,14,14,14,14,14,14,14,14,14,14,
       
        5, 3,5,7,10, 5,7,10,12,
        7,10,12,14,  10,12,14,
       
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        8,8,8,8,8,8,8,7,7,8,5,5,6,
        6,8,10,12,13,12,10,8,
        7,9,12,13,14,13,12,9,       
        10,
        };
        unsigned char code TwoTigerBeat[] = {
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
       
        4,2,2,2,4,2,2,2,2,4,
        2,        2,4,  2,2,2,2,4,
        2, 4,2,2,2,4,  4,2,2,4,
        2,2,4, 2,2,4, 2,2,4,
        2,2,2,2,4, 2,2,2,2,4,
       
        2,2,2,
        1,1,2,1,1,2,1,1,2,1,1,2,
        1,1,2,1,1,2,1,1,2,1,1,2,
        1,1,2,1,1,2,1,1,2,1,1,2,
        1,1,2,1,1,2,1,1,2,1,1,2,
        1,1,2,1,1,2,1,1,2,1,1,2,
        1,1,2,1,1,2,1,1,2,1,1,2,
        1,1,2,1,1,2,1,1,2,1,1,2,
        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,2,
       
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1, 1,1,1,1, 2,1,2,1,1,2,
        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
  16,
        };
       
        ADDR2 = 1;
  ADDR1 = 1;
  ADDR0 = 0;
        P0 = 0x00;
        for(beat = 0;beat<sizeof(TwoTigerNote);)
        {
                while(!tmrflag);
                tmrflag = 0;
                if(time == 0)
                {
                        note = TwoTigerNote[beat] - 1;
                        T0RH = NoteReload[note]>>8;
                        T0RL = NoteReload[note];
                        beatTime = (TwoTigerBeat[beat]*NoteFrequ[note])>>2;
                        soundTime =beatTime - (beatTime>>2);
                        enable = 1;
                        time++;
                       
                }
               
                else
                {
                        if(time>=beatTime)
                        {
                                time = 0;
                                beat++;
                        }       
                        else
                        {
                                time++;
                                if(time == soundTime)
                                {
                                        enable = 0;
                                }
                        }
                }
        }
}
好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表