串口修改时间

#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();  
        }
}
   
   
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值