登录  | 立即注册

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

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] 秒表

[复制链接]
发表于 2022-1-13 21:39:30 | 显示全部楼层 |阅读模式
学习笔记
学习科目: 单片机
学习安排: 秒表
开始时间: 2022-01-13
结束时间: 2022-01-13
本帖最后由 房一鹏 于 2022-1-22 09:16 编辑

秒表程序,此程序在实际使用中,数码管的亮度不高
和之前的加法计算机一样很搞人心态,总之能用。
数码管亮度不高的原因:在此程序中进入中断的时间为2毫秒,而6个数码管都刷新一遍的时间为6*2=12毫秒大于10毫秒,根据视觉暂留效果,会对亮度造成一定影响,但很小,可以忽略(并且事实证明,即使中断时间为1毫秒,亮度也不会有什么提高);更重要的原因是在每一次对按键状态进行检测是重新赋值了P20、P21、P22的值,令其为1、1、1,根据原理图,此时数码管和led小灯都不会亮,导致了单个数码管亮的时间只有数码管刷新程序运行的那一瞬间(时间非常短),而不是2毫秒内一直亮,所以数码管亮度不高。
(这只是我的个人见解,若有其余见解可以回复)

另外可以对程序进行修改,提高数码管的亮度,只需在中断的按键状态检测程序后对P20,P21,P22重新赋值,便可让数码管亮度变得很亮。之后我会再发一遍程序。

注:在此程序中有很多的地方【i】都没有显示,复制粘贴后要加上[i ]注意(这就是网站问题了,我不想在下面一点一点加,所以之后要自己加,至于在哪,就自己找吧)!!!!!!

#include<reg52.h>

sbit ENLED=P1^1;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sbit kout1=P2^4;
sbit kout2=P2^5;
sbit kout3=P2^6;
sbit kout4=P2^7;

unsigned char ledchar[]={
        0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
        0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,
};
unsigned char keysta[4]={1,1,1,1};
unsigned char ledbuff[6]={
        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF           
};
unsigned char T0RH=0;
unsigned char T0RL=0;
unsigned char xiaoshu=0;
unsigned int zhengshu=0;
bit stopwatchrefresh=1;
bit stopwatchrunning=0;

void configtimer0(unsigned int ms);
void stopwatchdisplay();
void keydrive();

void main()
{
        EA=1;
        ENLED=0;
        kout1=0;kout2=1;kout3=1;kout4=1;        
        configtimer0(2);                     
        TR0=1;                              
        ET0=1;                              
        
        while(1)
        {
                if(stopwatchrefresh)
                {
                        stopwatchrefresh=0;
                        stopwatchdisplay();               
                }
                keydrive();                          
        }
}

void configtimer0(unsigned int ms)
{
        unsigned long tmp;
        tmp=32768000/12;
        tmp=(tmp*ms)/1000;
        tmp=65536-tmp;
        
//        tmp = 65536-(ms/1000)*(32768000/12);   
        T0RH=(unsigned char)(tmp>>8);         
        T0RL=(unsigned char)tmp;               
        TMOD=0x01;
//        TMOD&=0xF0;
//        TMOD|=0x01;
        TH0=T0RH;
        TL0=T0RL;
}

void stopwatchdisplay()
{
        signed char i;
        unsigned char buf[4];
        
        ledbuff[0]=ledchar[xiaoshu%10];
        ledbuff[1]=ledchar[xiaoshu/10];
        buf[0]=zhengshu%10;
        buf[1]=zhengshu/10%10;
        buf[2]=zhengshu/100%10;
        buf[3]=zhengshu/1000%10;
        for(i=3;i>0;i--)
        {
                if(buf==0)
                {
                        ledbuff[i+2]=0xFF;
                }
                else
                        break;
        }
        for(;i>=0;i--)
        {
                ledbuff[i+2]=ledchar[buf];
        }
        ledbuff[2]&=0x7F;
}

void stopwatchresect()
{
        stopwatchrunning=0;
        xiaoshu=0;
        zhengshu=0;
        stopwatchrefresh=1;
}

void stopwatchaction()
{
  if(stopwatchrunning)
                stopwatchrunning=0;
        else
                stopwatchrunning=1;
}

void keydrive()
{
        unsigned char i=0;
        static unsigned char backup[4]={1,1,1,1};
        
        for(i=0;i<4;i++)
        {
                if(keysta!=backup)
                {
                        if(backup!=0)
                        {
                                if(i==1)                     
                                {
                                        stopwatchresect();         
                                }
                                else if(i==2)            
                                {
                                        stopwatchaction();         
                                }
                        }
                        backup=keysta;
                }
        }
}
void ledscan()
{
        static unsigned int i=0;
        P0=0xFF;
        if(i==0)
        {P20=121=022=1;i++0=ledbuff[0];}
        else if(i==1)
        {P20=021=022=1;i++0=ledbuff[1];}
        else if(i==2)
        {P20=121=122=0;i++0=ledbuff[2];}
        else if(i==3)
        {P20=021=1;P22=0;i++;P0=ledbuff[3];}
        else if(i==4)
        {P20=1;P21=0;P22=0;i++;P0=ledbuff[4];}
        else if(i==5)
        {P20=0;P21=0;P22=0;i=0;P0=ledbuff[5];}
        
//        P2=(P2&0xF8)|i;
//        P0=ledbuff;
//        if(i<5)
//                i++;
//        else
//                i=0;                    
}

void keyscan()
{
        unsigned char i=0;
        static unsigned char keybuf[4]={
                0xFF,0xFF,0xFF,0xFF
        };
        P20=1;P21=1;P22=1;P23=1;
        
        keybuf[0]=(keybuf[0]<<1)|P20;
        keybuf[1]=(keybuf[1]<<1)|P21;
        keybuf[2]=(keybuf[2]<<1)|P22;
        keybuf[3]=(keybuf[3]<<1)|P23;
        
        for(i=0;i<4;i++)
        {
                if(keybuf==0x00)
                {
                        keysta=0;
                }
                else if(keybuf==0xFF)
                {
                        keysta=1;
                }
                else{}
        }
//        P20=0;P21=0;P22=0;  
}

void stopwatchcount()
{
        if(stopwatchrunning)
        {
                xiaoshu++;
                if(xiaoshu>=100)
                {
                        xiaoshu=0;
                  zhengshu++;
                        if(zhengshu>=10000)
                        {
                                zhengshu=0;
                        }
                }
                stopwatchrefresh=1;
        }
}
void interruptTime() interrupt 1
{
        static unsigned char tem10ms=0;
        
        TH0=T0RH;
        TL0=T0RL;

        ledscan();
        keyscan();
        tem10ms++;
        if(tem10ms>=5)
        {
                tem10ms=0;
                stopwatchcount();
        }
}



捕获.PNG
2.PNG
3.PNG
4.PNG
5.PNG
6.PNG
7.PNG
好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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