单片机==lcd_串口修改数码管时间_闹钟(19)

#include 
#include "delay.h"



#define LCDPORT P0
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;

#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM 0

unsigned char hour = 0;
unsigned char min = 0;
unsigned char sec = 0;
unsigned char ch[9];
unsigned char flag = 0;

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

sbit beep = P2^7;


void alarm()
{ 

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


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

void timer0_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;
                }
            }
        }
    }
}

void lcd_write(unsigned char byte, unsigned char flag)
{
    if(flag)
    {
        RS = 1;
    }
    else
    {
        RS = 0;
    }
    RW = 0;
    E = 1;
    LCDPORT = byte;
    delay_us(5);
    E = 0;
}

void lcd_init()
{
    delay_ms(16);
    lcd_write(0x38, LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x38, LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x38, LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x38, LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x08, LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x01, LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x06, LCD_WRITE_COM);
    delay_ms(5);
    lcd_write(0x0C, LCD_WRITE_COM);
    delay_ms(5);
}


void dis_clock()
{
    lcd_write(0x80 + 4, LCD_WRITE_COM);
    lcd_write((hour / 10) + 0x30, LCD_WRITE_DATA);
    lcd_write((hour % 10) + 0x30, LCD_WRITE_DATA);
    lcd_write(':', LCD_WRITE_DATA);
    lcd_write((min / 10) + 0x30, LCD_WRITE_DATA);
    lcd_write((min % 10) + 0x30, LCD_WRITE_DATA);
    lcd_write(':', LCD_WRITE_DATA);
    lcd_write((sec / 10) + 0x30, LCD_WRITE_DATA);
    lcd_write((sec % 10) + 0x30, LCD_WRITE_DATA);
}

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()
{
    timer0_init();
    lcd_init();
    uart_init();
    while(1)
    {
        if(flag)
        {
            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;
        }
        dis_clock();
        alarm();
    }
}

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