【單片機】串口修改數碼管時間

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 <reg52.h>

#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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章