51单片机—串口时钟

来源:互联网 发布:php .net哪个好 编辑:程序博客网 时间:2024/05/22 16:45
#include <reg52.h>#include "delay.h"#define SEGPORT P0#define uchar unsigned char sbit seg_select = P2^1;sbit bit_select = P2^0;sbit beep = P2^2;uchar segdata[16] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};uchar bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};uchar tempdata[8];uchar shi = 0;uchar fen = 1;uchar miao = 0;uchar cshi = 0;uchar cfen = 0;uchar cmiao = 0;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 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 uchar i = 0;   TH0 = (65536 - 20000) / 256;   TL0= (65536 - 20000) % 256;   i++;     if(50 == i) {    i = 0;          miao++;          if(60 == miao)          {   miao = 0;   fen++;   if(60 == fen) {    fen  = 0;      shi++;    if(24 == shi){    shi = 0;                    }               }            }     }     tempdata[0] = segdata[shi / 10];     tempdata[1] = segdata[shi % 10];     tempdata[2] = 0x40;     tempdata[3] = segdata[fen / 10];     tempdata[4] = segdata[fen % 10];     tempdata[5] = 0x40;     tempdata[6] = segdata[miao / 10];     tempdata[7] = segdata[miao % 10];           } void uart_init(void){   SCON = 0x50;   TMOD |= 0x20;   TH1 = 0xfd;   TR1 = 1;    EA = 1;    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 uart_isr() interrupt 4{   unsigned char i = 0;   unsigned char temp[6];   uart_send_str("Please input time :");for(i = 0; i < 6; i++){while(1){              if(RI)               {        temp[i] = SBUF;      RI = 0;break;} }}        uart_send_str("you input is :");    uart_send_str(temp);        uart_send_str("\r\n");             shi = ((unsigned int)temp[0] - 48) * 10 + ((unsigned int)temp[1]  - 48);    fen = ((unsigned int)temp[2] - 48)* 10 + ((unsigned int)temp[3]  - 48);    miao = ((unsigned int)temp[4] - 48)* 10 + ((unsigned int)temp[5]  - 48) ;     }void main(){     timer0_init();  uart_init();  while(1){display();delay_us(200);     }}

0 1