Notifications
Clear all

测试

   RSS

0
Topic starter

烦烦烦

#include "reg51.h"
#define OSC 12000000 //单片机晶振频率
#define FREQ 800     //蜂鸣器鸣叫的频率
char current_second = 0, current_minute = 0, current_hour = 0;
char alarm_second = 0, alarm_minute = 0, alarm_hour = 23;
sbit key0 = P1 ^ 0;
sbit key1 = P1 ^ 1;
sbit key2 = P1 ^ 2;
sbit key3 = P1 ^ 3;
sbit key4 = P1 ^ 4;
sbit buzzer = P1 ^ 5;
bit clock_mode = 1; //为1表示显示当前时间,为0表示显示闹钟设定时间。
void LedDisplay();  //函数声明,显示当前模式下应该显示的数值。
char GetKey();      //函数声明,该函数得到按键的值,返回-1表示按键没按下。
void Timer0Init();  //函数声明,定时器0初始化函数.
void BuzzerInit();  //函数声明,蜂鸣器初始化,用到定时器1。
void main()
{
    Timer0Init(); //定时器0初始化
    BuzzerInit();
    while (1)
    {
        LedDisplay(); //显示当前位
        switch (GetKey())
        {
        case 0:
            ;    //按键0 启动暂停切换
            break;
        case 1:
            if (clock_mode)
            {
                ;//按键1,时钟模式下秒加一
            }
            else
            {
                ;//按键1,闹钟模式下秒加一
            }
            break;
        case 2:
            if (clock_mode)
            {
                ;//按键2,时钟模式分加一
            }
            else
            {
                ;//按键2,闹钟模式分加一
            }
            break;
        case 3:
            if (clock_mode)
            {
                ;//按键3,时钟模式下分加一
            }
            else
            {
                ;//按键3,闹钟模式分加一
            }
            break;
        case 4:
            ; //时钟闹钟模式切换
            break;
        default:; //无按键按下后要做的事
            break;
        }
        if (current_hour == alarm_hour && current_minute == alarm_minute)
        {
            ; //蜂鸣器发声
        }
        else
        {
            ; //蜂鸣器停止发声
        }
    }
}
void Timer0Init()
{
    TMOD = (TMOD & 0xf0) | 0x01;                    //设定时器0为模式1且不修改T1的设置。
    TH0 = (int)(65536 - ((OSC / 12) * 0.01)) / 256; //定时10000us,即0.01秒
    TL0 = (int)(65536 - ((OSC / 12) * 0.01)) % 256;
    EA = 1;
    ET0 = 1;
    TR0 = 1;
}
void BuzzerInit()
{
    ;    //T1定时器初始化
}
void LedDisplay()
{
    static unsigned char current_bit = 0; //当前要显示的位
    unsigned char bit_code[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};
    unsigned char seg_code[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66,
                                0x6d, 0x7d, 0x07, 0x7f, 0x6f};
    unsigned char seg_code_point[] = {0xbf, 0x86, 0xdb, 0xcf, 0xe6,
                                      0xed, 0xfd, 0x87, 0xff, 0xef};
    P2 = 0x00;                  //消重影
    P3 = bit_code[current_bit]; //送当前位的位码
    switch (current_bit)
    {
    case 0:  //送第0位的段码
        P2 = clock_mode ? seg_code[current_hour / 10] : seg_code[alarm_hour / 10]; 
        break;
    case 1:
        ; //送第1位的段码
        break;
    case 2:
        ;//送第2位的段码
        break;
    case 3:
        ;//送第3位的段码
        break;
    case 4:
        ;//送第4位的段码
        break;
    case 5:
       ;//送第5位的段码
        break;
    case 6:
        ;//送第5位的段码
        break;
    case 7:
       ;//送第7位的段码
        break;
    default:
        break;
    }
    current_bit++;
    if (current_bit == 8)
        current_bit = 0;
}
//如果返回0,1,2,3,4分别表示0,1,2,3,4号键按下,返回-1表示无键按下。
char GetKey()
{
    static unsigned char current_key0 = 1;
    static unsigned char current_key1 = 1;
    static unsigned char current_key2 = 1;
    static unsigned char current_key3 = 1;
    static unsigned char current_key4 = 1;
    static unsigned char former_key0 = 1;
    static unsigned char former_key1 = 1;
    static unsigned char former_key2 = 1;
    static unsigned char former_key3 = 1;
    static unsigned char former_key4 = 1;
 
    former_key0 = current_key0;
    current_key0 = key0;
    former_key1 = current_key1;
    current_key1 = key1;
    former_key2 = current_key2;
    current_key2 = key2;
    former_key3 = current_key3;
    current_key3 = key3;
    former_key4 = current_key4;
    current_key4 = key4;
 
    if (former_key0 == 0 && current_key0 == 1)
    {
        return 0;
    }
    if (former_key1 == 0 && current_key1 == 1)
    {
        return 1;
    }
    if (former_key2 == 0 && current_key2 == 1)
    {
        return 2;
    }
    if (former_key3 == 0 && current_key3 == 1)
    {
        return 3;
    }
    if (former_key4 == 0 && current_key4 == 1)
    {
        return 4;
    }
    return -1;
}
void Ms100() interrupt 1
{
    /* 
    0.01秒到了,进入此中断子程序,进入100次后,表示1秒到,然后秒加一,秒到了60秒,分加一,分到了60分,
    小时加一,24小时到了,小时清零。
    */
   ;
 
}
void BuzzerISR() interrupt 3
{
   ;//重新加载T1定时器初值
   ;//蜂鸣器端口取反,发出方波
}

方法

好像可以说明了啊

test

Get in touch

Quickly communicate covalent niche markets for maintainable sources. Collaboratively harness resource sucking experiences whereas cost effective meta-services.