main.c
#include "bsp.h"
void main(void)
{
bsp_init();
// Led_On();
while(1)
{
key_scan();
key_scan2();
key_scan3();
time_sem();
//得到时,分,秒
Set_ShowValue();
//显示时分秒
seg7_show_CC();
//与数码管显示连接起来
//seg7_show_CA();
//delay_ms(251);
delay_ms(2);
//进行延时
}
}
bsp_seg7.c
void seg7_show_CC(void)
{
static unsigned char j = 0;
SEGPORT = 0x00;
//消影
DUAN = 1;
DUAN = 0;
DUAN = 1;
SEGPORT = CLOCK[j];
DUAN = 0;
SEGPORT = TABLEWEI[j];
WEI = 1;
WEI = 0;
j++;
if(j == 8) j=0;
}
void Set_ShowValue(void)
{
yy++;
CLOCK[7] = TABLECC[sec%10];//秒
CLOCK[6] = TABLECC[sec/10];
if (keyvalue == 2)
CLOCK[4] = TABLECC[min%10] + 0x80;
else
CLOCK[4] = TABLECC[min%10];
CLOCK[3] = TABLECC[min/10];
if (yy < 375)
{
CLOCK[2] = 0x40;
CLOCK[5] = 0X40;
}
else
{
CLOCK[2] = 0x00;
CLOCK[5] = 0X00;
}
if (yy == 750)
yy = 0;
if (keyvalue == 3)
CLOCK[1] = TABLECC[hour%10] + 0x80;
else
CLOCK[1] = TABLECC[hour%10] ;
CLOCK[0] = TABLECC[hour/10];
}
bsp_delay.c
#include "bsp.h"
unsigned char hour=16,min=30,sec=0;
unsigned int tick;
sbit LED=P1^0;
void delay_ms(unsigned char t)
{
while(--t)
{
delay_us(245);
delay_us(245);
}
}
void delay_us(unsigned char t)
{
while(--t);
}
void time_sem(void)
{
if(keyvalue == 0)
{
tick++;
}
if(tick == 750)
{
//LED = ~LED;
sec++;
tick = 0;
}
if(sec == 60)
{
min++;
sec = 0;
}
if(min == 60)
{
hour++;
min = 0;
}
if(hour == 24)
hour = 0;
if (keyvalue == 2)
{
if (keyvalue2 == 1)
{
min++;
if (min == 60)
min = 0;
keyvalue2 = 0;
}
}
if (keyvalue == 3)
{
if (keyvalue3 == 1)
{
hour++;
if (hour == 24)
hour = 0;
keyvalue3 = 0;
}
}
}
bsp_key.c
#include "bsp.h"
sbit S2=P3^0; //增加计数
sbit S3=P3^1; //减少计数
sbit S4=P3^2;
unsigned char keyvalue = 0,keyvalue2 = 0,keyvalue3 = 0;
void key_scan(void)
{
if(S2 == 0)
{
if(keyvalue <= 3)
keyvalue++;
if(keyvalue == 4)
keyvalue = 0;
while(!S2); //等待按键释放
}
}
void key_scan2(void)
{
if(S3 == 0)
{
keyvalue2 = 1;
while(!S3); //等待按键释放
}
}
void key_scan3(void)
{
if(S4 == 0)
{
keyvalue3 =1;
while(!S4); //等待按键释放
}
}