单片机==串口修改_数码管时间(18)

#include
#include"delay.h"

#define PORT P0

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 tempdata[8];
unsigned char ch[9] = {0};
unsigned char flag = 0;

unsigned char hour = 20;
unsigned char min = 39;
unsigned char sec = 45;

sbit bit_select = P2^0;
sbit seg_select = P2^1;

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

        PORT = 0xff;
        bit_select = 1;
        bit_select = 0;

        PORT = 0x0;
        seg_select = 1;
        seg_select = 0;

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

        PORT = bitdata[i];
        bit_select = 1;
        bit_select = 0;

        i++;
        if(8 == i)
        {
            i = 0;
        }

}


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

}

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

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

        j++;

        if(50 == j)
        {
            j = 0;
            sec++;

            if(60 == sec)
            {
                sec = 0;
                min++;

                if(60 == min)
                {
                    min = 0;
                    hour++;

                    if(24 == hour)
                    {
                        hour = 0;
                    }
                }
            }
        }   
            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 uart_init(void)
{
    SCON = 0x50;
    TMOD |= 0x20;//Ò»¶¨ÒªÓÃÒì»ò
    TH1 = 0xfd;
    TR1 =1;
    EA = 1;
  ES = 1;
}

void uart_send_byte(unsigned char byte);
void uart_send_str(unsigned char *str);

//´®¿ÚÖжϺ¯Êý
void uart_isr() interrupt 4
{
      static unsigned char i = 0;
      if(RI)
        {
          ES = 0;
                ch[i] = SBUF;
                i++;
        if(8 == i)
                {   ch[i] = '\0';
                  i = 0;
                      flag = 1;
                }                   
                RI = 0;     
                ES = 1;
      }
}

//Êä³ö×Ö·û
void uart_send_byte(unsigned char byte)
{
      SBUF = byte;
      while(!TI);
      TI = 0;
}

//Êä³ö×Ö·û´®
void uart_send_str(unsigned char *s)
{
    while(*s != '\0')
    {
        uart_send_byte(*s);
        s++;
    }
}

void main()
{         

        time0_init();
        uart_init();
        while(1)
        {  
              if(flag)
                {
                    tempdata[0] = segdata[ch[0] - '0'];
                    tempdata[1] = segdata[ch[1] - '0'];
                    tempdata[2] = 0x40;
                    tempdata[3] = segdata[ch[3] - '0'];
                    tempdata[4] = segdata[ch[4] - '0'];
                    tempdata[5] = 0x3f;
                    tempdata[6] = segdata[ch[6] - '0'];
                    tempdata[7] = segdata[ch[7] - '0'];
                    hour = (ch[0] - '0') * 10 +(ch[1] - '0');
                    min = (ch[3] - '0') * 10 +(ch[4] - '0');
                    sec = (ch[6] - '0') * 10 +(ch[7] - '0');
                    uart_send_str(ch);
                    flag = 0;
                }
                display();  
        }
}

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