【单片机】串口修改数码管时间

main.c

#include "head.h"

code unsigned char timedata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
code unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char disdata[8];
unsigned char temp[7];

unsigned char sec = 0;
unsigned char min = 0;
unsigned char hour = 0;

unsigned char temp_buf[20];
unsigned char flag = 0;

void main()
{
	unsigned char i = 0;
	
    timer0_init();
	uart_init();
	
	while(1)
	{
	    display();
		manage();
			
	    ES = 1;                    //打开串口中断
		TR0 = 1;                   //打开定时器0,开始计数
		flag = 0;                  //标志位清0
	}
}


display.c

#include "head.h"

/*
======================
功能:修改时间显示值
======================
*/
void change_time()
{
    disdata[0] = timedata[hour / 10];
    disdata[1] = timedata[hour % 10];
	disdata[2] = 0x40;
	disdata[3] = timedata[min / 10];
    disdata[4] = timedata[min % 10];
	disdata[5] = 0x40;
	disdata[6] = timedata[sec / 10];
    disdata[7] = timedata[sec % 10]; 
}

/*
==========================
功能:动态扫描,显示数据
==========================
*/
void display()
{
    static unsigned char i = 0;
	
    TIMEPORT = 0x00;            //清空数据,防止重影
    seg_select = 1;
    seg_select = 0;

    TIMEPORT = 0xff;            //清空数据,防止重影
    bit_select = 1;
    bit_select = 0;

    TIMEPORT = disdata[i];      //取出显示值
    seg_select = 1;
    seg_select = 0;

    TIMEPORT = bitdata[i];      //取出位码
    bit_select = 1;
    bit_select = 0;	
	
	i++;
	if(8 == i)
	{
	    i = 0;
	}
}

uart.c

#include "head.h"

unsigned char buf[10];

/*
==================
功能:串口初始化
==================
*/
void uart_init()
{
    SCON = 0x50;    //设置串口的工作方式,8N1
	TMOD |= 0x20;   //设置定时器的工作方式,用作串口波特率
	TH1 = 0xfd;     //设置串口的波特率为9600
	TR1 = 1;
	EA = 1;
	ES = 1;
}

/*
============================
功能:串口中断服务函数
============================
*/
void uart_isr() interrupt 4
{
		static unsigned char i = 0;
		if(RI)
		{
			if(SBUF != ' ')
			{
				if(SBUF >= '0' && SBUF <= '9')
				{
					buf[i++] = SBUF;
				}
			}
			else
			{
					i = 0;
					flag = 1;
			}
			RI = 0;
		}
}

init.c

#include "head.h"

/*
===================
功能:初始化中断0
===================
*/
void timer0_init()
{
    EA = 1;
	TMOD |= 0x01;
	TH0 = (65536 - 20000) / 256;
	TL0 = (65536 - 20000) % 256;
	ET0 = 1;
	TR0 = 1;
}

/*
==========================
功能:中断0服务函数,计数
==========================
*/
void timer0_isr() interrupt 1
{
    static unsigned char i = 0;
	
	TH0 = (65536 - 20000) / 256;
	TL0 = (65536 - 20000) % 256;

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

        if(60 == sec)
        {
	        sec = 0;
		    min++;
		
            if(60 == min)
		    {
		        min = 0;
			    hour++;
			
			    if(24 == hour)
			    {
			        hour = 0;
			    }
		    }
		}
	}
	
	change_time();    //修改显示时间
}

manage.c

#include "head.h"

unsigned char j = 0;

void manage()
{
	if(1 == flag)    //收到串口发送的信息
    {
	    TR0 = 0;     //关闭定时器0,即计数
	    ES = 0;      //关闭串口中断
	
		for(j = 0; j < 6; j++)    //将字符转换为整数
		{
			temp_buf[j] = buf[j] - '0';
		}
		
		/*如果小时的十位为2,则个位小于4,超过24小时置0*/
		if(temp_buf[0] == 2)      
		{
			if(temp_buf[1] < 4)
			{
				hour = temp_buf[0] * 10 + temp_buf[1];
			}
			else
			{
				hour = temp_buf[0] * 10;
			}
		}
		
		else if(temp_buf[0] < 2)    //小时十位小于2
		{
			hour = temp_buf[0] * 10 + temp_buf[1];
		}
		
		if(temp_buf[2] < 6)        //如果分钟十位小于6
		{
			min = temp_buf[2] * 10 + temp_buf[3];
		}
		else                       //否则将分钟第一位置0
		{
			min = temp_buf[3];
		}
		
		if(temp_buf[4] < 6)        //如果秒第一位小于6
		{
			sec = temp_buf[4] * 10 + temp_buf[5];
		}
		else                       //否则将秒的第一位置0
		{
			sec = temp_buf[5];
		}
	}
}

head.h

#ifndef _HEAD_H_
#define _HEAD_H_

#include 

#define TIMEPORT P0

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

extern code unsigned char timedata[10];
extern code unsigned char bitdata[8];
extern unsigned char disdata[8];
extern unsigned char temp[7];

extern unsigned char buf[10];
extern unsigned char temp_buf[20];
extern unsigned char flag;

extern unsigned char sec;
extern unsigned char min;
extern unsigned char hour;

extern void change_time();
extern void display();
extern void timer0_init();
extern void uart_init();
extern void manage();

#endif

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