注册 登录
编程论坛 单片机编程

秒表怎么加中断?能够·暂停,继续,计数

尘曦 发布于 2018-12-06 20:26, 3549 次点击
#include "reg52.h"
#define ucher unsigned char
#define uint unsigned  int                                                                        
typedef unsigned char u8;                                                   

sbit LCD_RW=P2^5;
sbit LCD_RS=P2^6;
sbit LCD_E=P2^7;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
ucher count,fen,miao,hao;
u8   disp[]="11                       ";
ucher code disp1[]="    00:00:0";

void delay(uint c)
{
    uint a,b;
    for (a=0; a<c; a++)
        for (b=0; b<124; b++);            
}

void lcdwrite_com(ucher com)
{
    LCD_E=0;
    LCD_RS=0;
    LCD_RW=0;

    LCD_DATAPINS=com;
    delay(5);

    LCD_E=1;
    delay(5);
    LCD_E=0;

}
void lcdwrite_date(ucher date)
{
    LCD_E=0;
    LCD_RS=1;
    LCD_RW=0;

    LCD_DATAPINS=date;
    delay(5);

    LCD_E=1;
    delay(5);
    LCD_E=0;


}
void init()
{   
    fen=00;
    miao=00;
    hao=00;
    lcdwrite_com(0x38);
    lcdwrite_com(0x38);
    lcdwrite_com(0x38);
    lcdwrite_com(0x0c);
    lcdwrite_com(0x06);
    lcdwrite_com(0x01);
    {
    ucher num;
    lcdwrite_com(0x00+0x80);
    for(num=0;num<16;num++)
        {
        lcdwrite_date(disp[num]);
        }
    lcdwrite_com(0x80+0x40);
    for(num=0;num<11;num++)
        {
        lcdwrite_date(disp1[num]);            
        }
    TMOD=0x01;
    TH0=(65536-50000)/256;
    TL0=(65536-50000)%256;
    EA=1;
    ET0=1;
    TR0=1;
}
}
void lcdwrite_fmh1(ucher add,ucher date)
{
    ucher h;
    h=date%10;
    lcdwrite_com(0x80+0x40+add);
    lcdwrite_date(0x30+h);
}

void lcdwrite_fmh(ucher add,ucher date)
{
    ucher s,g;
    s=date/10;
    g=date%10;
    lcdwrite_com(0x80+0x40+add);
    lcdwrite_date(0x30+s);
    lcdwrite_date(0x30+g);        
}         
void main()
{
    init();
    while(1)
    {
        if(count==2)
        {
            count=0;
            hao++;
            if(hao==10)
            {
                hao=0;
                miao++;
                if(miao==60)
                {
                    miao=0;
                    fen++;
                    if(fen==60)
                    {
                        fen=0;     
                    }
                    lcdwrite_fmh(4,fen);   
                }
                lcdwrite_fmh(7,miao);
            }
            lcdwrite_fmh1(10,hao);               
        }
    }
}

void timer0() interrupt 1
{
    TH0=(65536-50000)/256;
    TL0=(65536-50000)%256;
    count++;   
}
3 回复
#2
小焦叔叔2020-02-15 21:00
我帮你写了一个秒表的程序,估计LCD1602时序时间跟不上,写了用5位LED显示的程序,给你参考:

程序代码:
#include"STC12C5A60S2.H"

sbit DIG_0=P2^0;
sbit DIG_1=P2^1;
sbit DIG_2=P2^2;
sbit DIG_3=P2^3;
sbit DIG_4=P2^4;

bit Flag_Run;

unsigned char code LED[10]=
{
    0xC0,0xF9,0xA4,0xB0,0x99,
    0x92,0x82,0xF8,0x80,0x90
};

void Init_Timer();
void Init_INT();
void PRI_Seting();

void main()
{
   Init_Timer();
   Init_INT();
   PRI_Seting();

   while(1);
}

void Init_Timer()
{
   TMOD=0x02;
   TH0=TL0=256-200;        //定时0.2mS
   TR0=1;
   ET0=1;
}

void Init_INT()
{
   EX0=1;
   EX1=1;
   EA=1;
}

void PRI_Seting()
{
   IPH=0x02;            //优先级设置:T0>INT0>INT1
   IP=0x01;
}

void Run_Timer() interrupt 1
{
   static unsigned char i=0;
   static unsigned int t=0;

   if(Flag_Run==1)
   {
      i++;
      if(i>=5)
      {
         i=0;
         t++;
      }
   }      
   if(t>=60000)
   {
      t=0;
      P2=0xFF;
      switch(i)
      {
         case 0:P0=LED[t%10];DIG_0=0;break;
         case 1:P0=LED[t/10%10];DIG_1=0;break;
         case 2:P0=LED[t/100%10];DIG_2=0;break;
         case 3:P0=LED[t/1000%10];DIG_3=0;break;
         case 4:P0=LED[t/10000%10];DIG_4=0;break;
         default:break;
      }
   }
}

void Sotp() interrupt 0
{
   Flag_Run=0;
}

void Run() interrupt 2
{
   Flag_Run=1;
}
#3
小焦叔叔2020-02-15 21:13
好像显示部分的DIG_1=0了,要关上DIG_0,......要不然动态显示不了
#4
xianfajushi2020-02-16 17:42
可参https://blog.
1