微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机通过串口通信调整时间的程序

单片机通过串口通信调整时间的程序

时间:12-02 来源:互联网 点击:
本程序所用的原理图下载:点这里,单片机芯片使用的stc89c51;找到时钟部分的原理图修改为你自己的电路上的io即可.这是一整个单片机开发板的电路图其他的忽略.

/**************************通过串口通信调整时间******************************/
/**
*功能:单片机开始上电时点阵显示倒计时3,2,1,0,然后点阵LED关闭显示
* 定时器和串口开始工作,定时器开始一秒计时,并在数码管上显示时,分,秒
* 此计时器可以通过串口发送数据调整时间,可调整时与分,当调整分钟时秒清零
* 附加功能:当单片机收到数据时有蜂鸣器响一声作为提示
* 彩色流水灯以20毫秒的速度向左流动
*作者:徐冉
*日期:2013-06-15-09:15 - 12:10
*备注:程序已经顺利通过调试
*/
/***********51hei开发板************/
/********AT89C52-RC MCU***********/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
sbit FM = P2^3;
sbit din = P2^0;
sbit cs = P2^1;
sbit clk = P2^2;
/************点阵LED显示取模编码表***********************/
uchar code dis[][8] = {
{0x00,0x3E,0x02,0x02,0x3E,0x02,0x02,0x3E},//3
{0x00,0x3E,0x02,0x02,0x3E,0x20,0x20,0x3E},//2
{0x00,0x18,0x38,0x08,0x08,0x08,0x08,0x3E},//1
{0x00,0x3C,0x24,0x24,0x24,0x24,0x24,0x3C},//0
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}//关闭显示
};
/*****************数码管显示编码表***************************/
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};

uchar shi = 0, fen = 0, miao = 0, counter = 0,flag= 0, j = 0;
/*************点阵LED-MAX7219***********/
/***************写字节************************/
void write_byte(uchar date)
{
uchar i = 0;
cs = 0;
for(i = 0; i < 8; i++)
{
clk = 0;
din = date & 0x80;
date <= 1;
clk = 1;
}
}
/*****************写MAX7219**********************/
void write_max7219(uchar add, uchar date)
{
cs = 0;
write_byte(add);
write_byte(date);
cs = 1;
}
/*****************MAX7219初始化**********************/
void max7219_init()
{
write_max7219(0x0c, 0x01);
write_max7219(0x09, 0x00);
write_max7219(0x0a, 0x08);
write_max7219(0x0b, 0x07);
write_max7219(0x0f, 0x00);
}
/*********延时*********/
void delayms(uint xms)
{
uint x, y;
for(x = xms; x > 0; x--)
for(y = 110; y > 0; y--);
}
/************串口和定时器T0初始化********************/
void init()
{
TMOD = 0x21;//T1工作方式2,T0工作方式1
TH0 = 0xB8;
TL0 = 0x00;
TH1 = 0xFA;
TL1 = 0xFA;
TR1 = 1;
SM0 = 0;
SM1 = 1;
REN = 1;
EA = 1;
ES = 1;
ET0 = 1;
}
/*************数码管显示时间************************/
void display(uchar shi, uchar fen, uchar miao)
{
uchar shi_s, shi_g;
uchar fen_s, fen_g;
uchar miao_s, miao_g;

shi_s = shi / 10 % 10;
shi_g = shi % 10;
fen_s = fen / 10 % 10;
fen_g = fen % 10;
miao_s = miao / 10 % 10;
miao_g = miao % 10;

dula = 1;
P0 = table[shi_s];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delayms(1);

dula = 1;
P0 = table[shi_g];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delayms(1);

dula = 1;
P0 = table[fen_s];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
P0 = 0x00;
delayms(1);

dula = 1;
P0 = table[fen_g];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xf7;
wela = 0;
P0 = 0x00;
delayms(1);

dula = 1;
P0 = table[miao_s];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xef;
wela = 0;
P0 = 0x00;
delayms(1);

dula = 1;
P0 = table[miao_g];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xdf;
wela = 0;
P0 = 0x00;
delayms(1);
}
/*****************点阵显示程序****************/
void display_max7219()
{
uchar m, n;
for(m = 0; m < 5; m++)
{
for(n = 0; n < 8; n++)
{
write_max7219(n+1, dis[m][n]);
delayms(1);
}
delayms(1000);
}
TR0 = 1;
}
/****************主程序********************/
void main(void)
{
init();
max7219_init();
display_max7219();

while(1)
{
display(shi, fen, miao);
if(flag == 1)
{
flag= 0;
ES = 0;
SBUF = shi;
while(!TI);
TI = 0;
SBUF = fen;
while(!TI);
TI = 0;
SBUF = miao;
while(!TI);
TI = 0;
ES = 1;
}
}
}
/***************定时器T0中断程序**********************/
void timer0_int() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
P1 = ~(1 < j++);
if(j >= 8)
{
j = 0;
}
if(counter == 50)
{
counter = 0;
miao++;
if(miao >= 60)
{
miao = 0;
fen++;
if(fen >= 60)
{
fen = 0;
shi++;
if(shi >= 24)
{
shi = 0;
}
}
}
}
}
/*************串口中断服务程序***********************/
void uart_int() interrupt 4
{
RI = 0;
if(SBUF >= 23 && SBUF <= 59)
{
fen = SBUF;
miao = 0;
FM = 0;
delayms(30);
FM = 1;
}
if(SBUF >= 0 && SBUF <= 23)
{
shi = SBUF;
fen = SBUF;
FM = 0;
delayms(30);
FM = 1;
}
flag= 1;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top