返回顶部

课堂作业二

 

//contributed by LiuYH 

#include <reg52.h>

//定义显示缓冲区
unsigned char DispBuf[8];

//数码管显示电路管脚定义
sbit SS = P2^6;       //数码管段选信号
sbit CS = P2^7;       //数码管位选信号

sbit LED1 = P1^0;    //定义LED1由P1.0控制
sbit BUZZER = P2^3; //有源蜂鸣器驱动端口 

sbit KEY1 = P3^4;

code unsigned char Tab[] =
    {//定义0123456789AbCdEF的数码管字型数据
        0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
        0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71
    };

unsigned char sec_left = 99;    // 左侧100ms计数 
unsigned char sec_right = 0;    // 右侧100ms计数 
bit timer_start_flag = 1;       // 秒表运行标志位
bit flag_left = 1;                // 左侧标志位 
bit flag_alarm = 0;                // 报警标志位 


/*
函数:T1INTSVC()
功能:定时器T1的中断服务函数
*/
void T1INTSVC() interrupt 3    //定时器1的中断号:3
{
    code unsigned char com[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};    //显示位的端口控制字节
    static unsigned char n = 0;                        //n: 扫描显示位计数
    static unsigned int counter_1ms = 0;    //1ms计数
        static unsigned int counter_alarm = 0; //报警计数
    // 数码管定时扫描驱动显示
    TR1 = 0;
    TH1 = 0xFC;
    TL1 = 0x66;
    TR1 = 1;
        
    P0 = 0xFF;        //消隐
    CS = 1;
    CS = 0;
    P0 = DispBuf[n];    //更新扫描显示数据
    SS = 1;
    SS = 0;
    P0 = ~com[n];    //重新显示
    CS = 1;
    CS = 0;
    n++;                        //指向下一位扫描
    n &= 0x07;

    // 精度0.1秒的跑表
    if (timer_start_flag)
    {
        counter_1ms ++;    //1ms加1
        if (counter_1ms == 100)
        {
            counter_1ms = 0;            //0.1s到0 
            
                    sec_right ++;            //0.1s加1
            
                    if (flag_left == 1)
                        sec_left --;
            
                        DispBuf[6] = Tab[sec_left % 10];            //查表取出显示数字对应的段码,存入显示缓冲器数组
                        DispBuf[7] = Tab[sec_left / 10 % 10] | 0x80;    // "| 0x80"可使该位带小数点显示
               
            
                        DispBuf[0] = Tab[sec_right % 10];            
                        DispBuf[1] = Tab[sec_right / 10 % 10] | 0x80;    
                        DispBuf[2] = Tab[sec_right / 100];
            
                        if (sec_right >= 80)
                        {
                            if (counter_alarm == 0)
                            {
                                flag_alarm = 1;
                                counter_alarm ++;
                            }
                            else if (counter_alarm == 3 )
                            {
                                flag_alarm = 0;
                                counter_alarm =0;
                            }
                            else
                            {
                                flag_alarm = 0;
                                counter_alarm ++;
                            }
                        }

                if (sec_left == 0 & KEY1!=0)
                    flag_left = 0;
                if (sec_left ==0 &KEY1==0)
                {    
                flag_left = 1;
                    sec_left = 99;
        }
                    if (sec_right == 120)
                    sec_right = 0;        //满12.0s置0
        }
    }

}

/*
函数:DispClear()
功能:清除数码管的所有显示
*/
void DispClear()
{
    unsigned char i;
    for (i=0; i<8; i++)
    {
        DispBuf[i] = 0x00;    //i值代表数码管的位数
    }
}

/*
函数:Delay()
功能:延时
说明:
    晶振频率为11.0592MHz
    延时长度 = 1ms * t
*/
void Delayms(unsigned int t)
{
    unsigned int us_ct;
    for (;t > 0;t --)        //执行代码消耗CPU时间
        for (us_ct = 113;us_ct > 0;us_ct --);    
}


/*
函数:SysInit()
功能:系统初始化
*/
void SysInit()
{
    DispClear();    //初始为全灭

    TMOD = 0x11;    //设置T0为16位计时器,定时器T1为16位计时器
    EA = 0;
    TH1 = 0xFC;       
    TL1 = 0x66;
    ET1 = 1;        
    EA = 1;            
    TR1 = 1;        
}

void main()
{
    SysInit();
    /*
    DispBuf[7] = Tab[1];
    DispBuf[6] = Tab[2];
    DispBuf[5] = Tab[3];
    DispBuf[4] = Tab[4];
    DispBuf[3] = Tab[5];
    DispBuf[2] = Tab[6];
    DispBuf[1] = Tab[7];
    DispBuf[0] = Tab[8];
    Delayms(2000);    
*/
    contributed by LiuYH 
    timer_start_flag = 1;
      DispClear(); 
    while (1)
        {
            if (flag_alarm)
            {
                LED1 = 0;
//                BUZZER = 0;
            }
            else
            {
                LED1 = 1;
                BUZZER = 1;
            }
        }
}

 

posted @ 2023-09-13 22:56  YuhangLiuCE  阅读(10)  评论(0编辑  收藏  举报