单片机==矩阵键盘+时钟+键入时间+闹钟(12)

#include 
#include 
#include "delay.h"

#define SEGPORT P0
#define KEYPORT P1

sbit bit_select = P2^0;
sbit seg_select = P2^1;
sbit beep = P2^7;
sbit key6 = P3^2;

unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char segdata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code freq[][2]={ 
  0xD8,0xF7,//00440HZ 1   
  0xBD,0xF8,//00494HZ 2  
  0x87,0xF9,//00554HZ 3  
  0xE4,0xF9,//00587HZ 4    
  0x90,0xFA,//00659HZ 5     
  0x29,0xFB,//00740HZ 6     
  0xB1,0xFB,//00831HZ 7      
  0xEF,0xFB,//00880HZ `1
};
unsigned char tempdata[8];
unsigned char sec = 0;
unsigned char min = 0;
unsigned char hour = 0;

unsigned char alarm_hour = 19;
unsigned char alarm_min = 8;
unsigned char alarm_sec = 21;


void timer0_init(void)
{
    EA = 1;
    TMOD |= 0x01;
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;
    ET0 = 1;
    TR0 = 1;
}

void timer1_init(void)
{
    EA = 1;
    TMOD |= 0x10;
    TH1 = (65536 - 1000) / 256;
    TL1 = (65536 - 1000) % 256;
    ET1 = 1;
    TR1 = 1;
}

void timer0_isr() interrupt 1
{
    static unsigned char j = 0;

    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;

    j++;

    if(20 == j)
    {
        j = 0;
        sec++;
        if(60 == sec)
        {
            sec = 0;
            min++;
            if(60 == min)
            {
                min = 0;
                hour++;
                if(24 == hour)
                {
                    hour = 0;
                }
            }
        }
    }
    tempdata[6] = segdata[sec / 10];
    tempdata[7] = segdata[sec % 10];
    tempdata[5] = 0x40;
    tempdata[3] = segdata[min / 10];
    tempdata[4] = segdata[min % 10];
    tempdata[2] = 0x40;
    tempdata[0] = segdata[hour / 10];
    tempdata[1] = segdata[hour % 10];
}

void display()
{
    static unsigned char i = 0;

    SEGPORT = 0x0;
    seg_select = 1;
    seg_select = 0;
    SEGPORT = 0xff;
    bit_select = 1;
    bit_select = 0;

    SEGPORT = tempdata[i];
    seg_select = 1;
    seg_select = 0;

    SEGPORT = bitdata[i];
    bit_select = 1;
    bit_select = 0;
    i++;
    if(8 == i)
    {
        i = 0;
    }
}

void timer1_isr() interrupt 3
{
    TH1 = (65536 - 1000) / 256;
    TL1 = (65536 - 1000) % 256;

    display();
}

void int0_init()
{
    EA = 1;
    IT0 = 1;
    EX0 = 1;
}

unsigned char key_scan(void)
{
    unsigned char keyvalue;
    KEYPORT = 0xf0;
    if(0xf0 != KEYPORT)
    {
        delay_ms(10);
        if(0xf0 != KEYPORT)
        {
            KEYPORT = 0xfe;
            if(0xfe != KEYPORT)
            {
                keyvalue = KEYPORT;
                keyvalue &= 0xf0;
                keyvalue += 0x0e;
                while(0xfe != KEYPORT);
                delay_ms(10);
                while(0xfe != KEYPORT);
                return keyvalue;
            }
            KEYPORT = 0xf7;
            if(0xf7 != KEYPORT)
            {
                keyvalue = KEYPORT;
                keyvalue &= 0xf0;
                keyvalue += 0x07;
                while(0xf7 != KEYPORT);
                delay_ms(10);
                while(0xf7 != KEYPORT);
                return keyvalue;
            }
            KEYPORT = 0xfd;
            if(0xfd != KEYPORT)
            {
                keyvalue = KEYPORT;
                keyvalue &= 0xf0;
                keyvalue += 0x0d;
                while(0xfd != KEYPORT);
                delay_ms(10);
                while(0xfd != KEYPORT);
                return keyvalue;
            }
            KEYPORT = 0xfb;
            if(0xfd != KEYPORT)
            {
                keyvalue = KEYPORT;
                keyvalue &= 0xf0;
                keyvalue += 0x0b;
                while(0xfb != KEYPORT);
                delay_ms(10);
                while(0xfb != KEYPORT);
                return keyvalue;
            }       
        }
    }
    return 0xff;
}

unsigned char key_pro()
{
    unsigned char keynum;
    keynum = key_scan();
    switch(keynum)
    {
        case 0x7e : return 0;break;
        case 0xbe : return 1;break;
        case 0xde : return 2;break;
        case 0xee : return 3;break;
        case 0x7d : return 4;break;
        case 0xbd : return 5;break;
        case 0xdd : return 6;break;
        case 0xed : return 7;break;
        case 0x7b : return 8;break;
        case 0xbb : return 9;break;
        case 0xdb : return 10;break;
        case 0xeb : return 11;break;    
        default : return 16;break;
    }
}

void int0_isr(void) interrupt 0
{

    EX0 = 0;

    delay_ms(10);
    if(0 == key6)
    {
        while(!key6)
        delay_ms(10);
        while(!key6);
        TR0 = ~TR0;
    }



        EX0 = 1;

}




void change()
{   
    static unsigned char i = 0;
    unsigned char key;
    key = key_pro();
        if(key < 10)
        {
            switch(i)
            {
                case 0:
                { 
                    hour = key * 10 + hour % 10;
                    i++;
                    break;
                }
                case 1:
                {
                    hour = (hour / 10) * 10 + key;
                    i++;
          break;                    
                }
                case 2:
                {
                    i++;
                }
                case 3:
                {
                    min = key * 10 + min % 10;
                    i++;
                    break;
                }
                case 4:
                {
                    min = (min / 10) * 10 + key;
                    i++;
          break;                    
                }
                case 5:
                {
                    i++;
                }
                case 6:
                {
                    sec = key * 10 + sec % 10;
                    i++;
                    break;
                }
                case 7:
                {
                    sec = (sec / 10) * 10 + key;
                    i = 0;
          break;                    
                }

            }
        }
        if(10 == key)
        {
            i--;
            if(2 == i || 5 == i)
            {
                i--;
            }

        }
        if(11 == key)
        {
            i++;
            if(2 == i || 5 == i)
            {
                i++;
            }
        }

        if(8 == i)
        {
            i = 0;
        }
        if(-1 == i)
        {
            i = 7;
        }

        tempdata[0] = segdata[hour / 10];
        tempdata[1] = segdata[hour % 10];
        tempdata[2] = 0x40;
        tempdata[3] = segdata[min / 10];
        tempdata[4] = segdata[min % 10];
        tempdata[5] = 0x40;
        tempdata[6] = segdata[sec / 10];
        tempdata[7] = segdata[sec % 10];
}

void alarm()
{ 

    if(alarm_hour == hour && alarm_min == min)
    { 
    beep = ~beep;          
    }
}

void main()
{       
         timer0_init();
         timer1_init();
         int0_init();
         while(1)
         {
             change();
             alarm();
         }
}

你可能感兴趣的:(单片机软件,单片机,矩阵键盘,时钟,键入时间,闹钟)