单片机——矩阵键盘秒表

该程序实现的功能是通过矩阵键盘实现秒表的功能,即按1键时表示开始和暂停,0键表示清零
#include
#include "key.h"
#include "./delay/delay.h"
#define SEGPORT P0
#define KEYPORT P1
sbit seg_sel = P2^1;   
sbit bit_sel = P2^0;
unsigned char segdata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char disdata[8] = {0x3f,0x3f,0x40,0x3f,0x3f,0x40,0x03,0x03};   
unsigned char ms = 0;
unsigned char s = 0;
unsigned char m = 0;
void display()
{
   static unsigned char i = 0;
 
   SEGPORT = 0x0;   
  seg_sel = 1;
  seg_sel = 0;
     
  SEGPORT = 0xff;
  bit_sel = 1;
  bit_sel = 0;
       
  SEGPORT = disdata[i];
  seg_sel = 1;
  seg_sel = 0;
       
  SEGPORT = bitdata[i];
  bit_sel = 1;
  bit_sel = 0;
 
    i++;
 
   if(i == 8)
  {
     i = 0;
  }
}
void timer0_init()
{
   EA = 1;
   TH0 = (65536 - 20000)/256;
   TL0 = (65536 - 20000)%256;
   TMOD |= 0x01;
   ET0 = 1;
   TR0 = 1;
}
void timer1_init()
{
   EA = 1;
   TH1 = (65536 - 1000)/256;
   TL1 = (65536 - 1000)%256;
   TMOD |= 0x10;
   ET1 = 1;
   TR1 = 1;
}

void timer0_isr() interrupt 1
{
   static unsigned char ms = 0; 
 
   TH0 = (65536 - 20000)/256;
   TL0 = (65536 - 20000)%256;
 
   ms++;
   if(50 == ms)
  {
     ms = 0;
     s++;
     if(60 == s)
    {
       s = 0;
       m++;
       if(60 == m)
      {
         m = 0;
        
      }
    }
    }
   disdata[0] = segdata[m/10];
  disdata[1] = segdata[m%10];
 
   disdata[3] = segdata[s/10];
  disdata[4] = segdata[s%10];
 
   disdata[6] = segdata[ms/10];
  disdata[7] = segdata[ms%10];
  
}
void timer1_isr() interrupt 3
{
 
   TH1 = (65536 - 1000)/256;
   TL1 = (65536 - 1000)%256;
 
   display();
}
void main()
{
   unsigned char keyvalue;
    timer0_init();
    timer1_init();
 
   while(1)
  {
   
     keyvalue = key();
    
     if(keyvalue == 0)
    {
       TR0 = ~TR0; 
    }
    
    if(keyvalue == 1)
    {
       TR0 = 0;
     
       disdata[0] = 0x3f;
       disdata[1] = 0x3f;
       disdata[2] = 0x40;
       disdata[3] = 0x3f;
       disdata[4] = 0x3f;
       disdata[5] = 0x40;
       disdata[6] = 0x3f;
       disdata[7] = 0x3f;
     
       ms = 0;
       m = 0;
       s = 0;
    }
       
  }
  
}

你可能感兴趣的:(单片机)