标题: 怎么让单片机计时器改成倒计时,可以通过独立按键设置倒计时(用的是普中板) [打印本页]

作者: arimotti    时间: 2023-9-28 14:49
标题: 怎么让单片机计时器改成倒计时,可以通过独立按键设置倒计时(用的是普中板)
#include <reg52.h>

typedef unsigned int u16;
typedef unsigned char u8;

#define KEY_PRESS1 1
#define KEY_PRESS2 2
#define KEY_PRESS3 3
#define KEY_PRESS4 4
#define KEY_UNPRESS 0

#define SMG_SORT P0   
#define LED_SORT P2

void delay_10us(u16 ten_us)
{
     while(ten_us--);
}

u8 NUM_SMG[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                0x7f,0x6f,0x40};
u8 SELECT_SMG[] = {0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c};

void time0_init()
{
    TMOD |= 0x01;  //设置工作方式;定时器0的工作模式1
    TH0 = 0xFC;
    TL0 = 0x18;
    EA = 1;        //中断总开关
    ET0 = 1;    //定时器0允许位
    TR0 = 1;    //打开定时器0
}

sbit LED1 = P2^0;
static u16 n = 0;

sbit KEY1 = P3^1;
sbit KEY2 = P3^0;
sbit KEY3 = P3^2;
sbit KEY4 = P3^3;

static u8 hour = 0;
static u8 min = 0;
static u8 time = 0;
static u8 scan_state = 0;

void Show_time(u8 ti, u8 loc)
{
    u8 a = ti/10;
    u8 b = ti%10;
    SMG_SORT = NUM_SMG;
   
P2 = SELECT_SMG[loc];
    delay_10us(100);

    SMG_SORT = NUM_SMG[a];
    P2 = SELECT_SMG[loc+1];
    delay_10us(100);
}

void show_line()
{
    SMG_SORT = 0x40;
    P2 = SELECT_SMG[2];
    delay_10us(100);

    SMG_SORT = 0x40;
    P2 = SELECT_SMG[5];
    delay_10us(100);
}

u8 key_scan() // 返回按键的状态  0  1  2  3  4
{
        if(scan_state==0&KEY1==0||KEY2==0||KEY3==0||KEY4==0)
        {
        delay_10us(1000);
        scan_state = 1;
        if(KEY1==0 )return 1;
        if(KEY2==0 )return 1;
        if(KEY3==0 )return 1;
        if(KEY4==0 )return 1;
        }
         else if(KEY1==1&KEY2==1&KEY3==1&KEY4==1)
          {
          scan_state=0;      
          }
          return 1;
}

void time_ADD()
{
    if(KEY4==0)
    {

        delay_10us(20);
        while(!KEY4);
            Show_time();
            time++;
            if(time>59)
            {
                min++;
                time = 0;
                if(min>59)
                {
                    hour++;
                    min = 0;
                }
            }
    }
}
void time_SUB()
{
    if(KEY3==0)
    {
        delay_10us(20);
        while(!KEY3);        //判断按键是否松手
            Show_time();
            if(min>0||time>0)
            {
                time--;
                if(time==0)
                {
                    min--;
                    time=59;
                }
            }   
    }
}

void main()
{
    time0_init();
    LED1 =0;
    while(1)
    {
        Show_time(time,0);
        Show_time(min,3);
        Show_time(hour,6);
        show_line();

    }
}



void time0() interrupt 1
{
    //注:每次进入中断意味着定时/和计数器已经溢出,若要继续重复工作,需要重新赋值
    TH0 = 0xFC;      
    TL0 = 0x18;
    n++;
    if(n==1000)
    {
        n = 0;
        time += 1;
        if(time >= 59)
        {
            time = 0;
            min +=1;
            if(min == 60)
            {
                min = 0;
                hour += 1;
                if(hour == 24)
                {
                    hour = 0;
                }
            }
        }
    }
}







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1