#include<reg52.h>
unsigned char discode_bit[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char discode_segment[] = {0x0a,0x3f,0x46,0x26,0x33,0xa2,0x82,0x3e,0x02,0x22,0xff};
/*******************正常走时时的指针变量*************************/
unsigned char *pDiscode_segment_hourgewei = discode_segment;
unsigned char *pDiscode_segment_hourshiwei = discode_segment;
unsigned char *pDiscode_segment_minshiwei = discode_segment;
unsigned char *pDiscode_segment_mingewei = discode_segment;
unsigned char *pDiscode_segment_secshiwei = discode_segment;
unsigned char *pDiscode_segment_secgewei = discode_segment;
/*********************设置闹钟时的指针变量******************************/
unsigned char *pDisAlarm_segment_hourgewei = discode_segment;
unsigned char *pDisAlarm_segment_hourshiwei = discode_segment;
unsigned char *pDisAlarm_segment_mingewei = discode_segment;
unsigned char *pDisAlarm_segment_minshiwei = discode_segment;
unsigned char TL0_temp; //暂存TL0的初值
unsigned char TH0_temp; //暂存TH0的初值
unsigned int setAlarm_time = 0;
unsigned int Alarm_time = 0;//闹钟响铃时间
bit BIT_5ms = 0; //10ms到标志
bit BIT_1s = 0; //1s到标志
#define INT_CLOCK 5 //INT_CLOCK为定时值,单位为ms ,此处定义为5ms
#define CRY_FREQUENCY 11059200 //CRY_FREQUENCY为晶振频率,单位为Hz
#define ALARM_TIME 12000;//闹钟闹的时间
sbit bHour_set = P2^0;
//sbit bHour_sub = P2^1;
sbit bMin_set = P2^1;
sbit bAlarm_set = P2^2;
sbit bAlarm_led = P2^3;
sbit bPlay_Rec = P2^4 ;
//sbit bConform = P2^4;*/
bit bSetAlarm_state = 0; //设置闹钟状态标志位
bit bSetAlarm_hour = 0; //闹钟设置状态下的时钟按键被按下的标志位
bit bSetAlarm_min = 0; //闹钟设置状态下的分钟按键被按下的标志位
unsigned char alarm_count = 0; //闹钟设置键按下的次数计数器
unsigned char Alarm_save[4] = {0xff,0xff,0xff,0xff}; //保存闹钟设置值的数组 ,初始值设为ff是为了防止上电就闹
/********** 定时器0初始化函数 **************/
void timer0_init()
{
unsigned long T0_temp; //暂存T0的初始值
EA = 1;//开总中断
T0_temp = 65536-((INT_CLOCK *CRY_FREQUENCY /1000)/12); //T0初始值计算公式,参考教科书
TL0_temp = T0_temp & 0xff;
TH0_temp = T0_temp >> 8;
TL0 = TL0_temp;
TH0 = TH0_temp;
TMOD |= 0x01;
TR0 = 1;
ET0 = 1; //开定时器0中断
}
/************延时函数***********************/
void delay(unsigned char time)
{
unsigned char i;
for(i = 0;i < time;i++)
{
while(!BIT_5ms);
BIT_5ms = 0;
}
}
/*********定时器0中断服务程序*****************/
unsigned char count_5ms = 0; //5毫秒计数器
unsigned int count_500ms = 0;//50毫秒计数器
unsigned char count_1s = 0;//1秒计数器
bit bValid_key = 0;//两次闹钟设置键之间是否有其他键按下的标志
void T0_interrupt(void) interrupt 1 // T0中断服务程序
{
TH0 += TH0_temp;
TL0 += TL0_temp;
BIT_5ms = 1;
count_5ms++;
count_1s ++;
if(4 == count_5ms)
count_5ms = 0;
if(0 == count_5ms)
{
P0 = discode_bit[0];
if(bSetAlarm_state)
P1 = *pDisAlarm_segment_hourgewei;
else
P1 = *pDiscode_segment_hourgewei;
}
else if(1 == count_5ms)
{
P0 = discode_bit[1];
if(bSetAlarm_state)
P1 = *pDisAlarm_segment_hourshiwei;
else
P1 = *pDiscode_segment_hourshiwei;
}
else if (2 == count_5ms)
{
P0 = discode_bit[3];
if(bSetAlarm_state)
P1 = *pDisAlarm_segment_minshiwei;
else
P1 = *pDiscode_segment_minshiwei;
}
else if (3 == count_5ms)
{
P0 = discode_bit[2];
if(bSetAlarm_state)
P1 = *pDisAlarm_segment_mingewei;
else
P1 = *pDiscode_segment_mingewei;
}
if(200 == count_1s) //1秒计时到
{
count_1s = 0;
pDiscode_segment_secgewei ++;
if(0xff == *pDiscode_segment_secgewei)
{
pDiscode_segment_secgewei = discode_segment;
pDiscode_segment_secshiwei ++;
}
if(0x82 == *pDiscode_segment_secshiwei)
{
pDiscode_segment_secshiwei = discode_segment;
pDiscode_segment_mingewei ++;
}
if(0xff == *pDiscode_segment_mingewei)
{
pDiscode_segment_mingewei = discode_segment;
pDiscode_segment_minshiwei ++;
}
if(0x82 == *pDiscode_segment_minshiwei)
{
pDiscode_segment_minshiwei = discode_segment;
pDiscode_segment_hourgewei ++;
}
if(0xff == *pDiscode_segment_hourgewei)
{
pDiscode_segment_hourgewei = discode_segment;
pDiscode_segment_hourshiwei ++;
}
if((0x46 == *pDiscode_segment_hourshiwei) && (0x33 == *pDiscode_segment_hourgewei))
{
pDiscode_segment_hourshiwei = discode_segment;
pDiscode_segment_hourgewei = discode_segment;
}
}
if(setAlarm_time)
{
setAlarm_time --;
if(bSetAlarm_hour)
{
bValid_key = 1;
bSetAlarm_hour = 0;
setAlarm_time = 1000;
}
else if(bSetAlarm_min)
{
bValid_key = 1;
bSetAlarm_min = 0;
setAlarm_time = 1000;
}
if(0 == alarm_count) //第二次按下了闹钟设置键
{
if(bValid_key) //如果有时间设置键按下,则保存当前闹钟设置值
{
bValid_key = 0;
Alarm_save[0] = *pDisAlarm_segment_hourshiwei;
Alarm_save[1] = *pDisAlarm_segment_hourgewei;
Alarm_save[2] = *pDisAlarm_segment_minshiwei;
Alarm_save[3] = *pDisAlarm_segment_mingewei;
}
setAlarm_time = 0;
bAlarm_led = 1;
count_500ms = 0;
}
else //等待用户操作,用LED灯闪烁表示
{
count_500ms ++;
if(100 == count_500ms)
{
count_500ms = 0;
bAlarm_led = !bAlarm_led;
}
}
if(0 == setAlarm_time) //如果5秒之内没有任何其他操作(按键按下)
{
bAlarm_led = 1;
count_500ms = 0;
alarm_count = 0;
bSetAlarm_state = 0; //退出闹钟设置状态
}
}
if(Alarm_time) //开始闹铃
{
Alarm_time --;
count_500ms ++;
if(50 == count_500ms)
{
count_500ms = 0;
bAlarm_led = !bAlarm_led;
bPlay_Rec = !bPlay_Rec; //每隔一段时间触发一次,以实现连续播放
}
if(0 == Alarm_time)
{
bAlarm_led = 1; // 灯灭
bPlay_Rec = 1; // 停止播放
}
}
}
/************************************/
/***********主函数*******************/
int main()
{
// unsigned char i;
bit a = 0;
timer0_init();
bHour_set = 1;
bMin_set = 1;
bAlarm_set = 1;
while(1)
{
if(!bHour_set) //小时设置键被按下
{
delay(10); //防抖
if(!bHour_set)
{
if(bSetAlarm_state) //当前是闹钟设置状态
{
bSetAlarm_hour = 1;
pDisAlarm_segment_hourgewei ++;
if(0xff == *pDisAlarm_segment_hourgewei)
{
pDisAlarm_segment_hourgewei = discode_segment;
pDisAlarm_segment_hourshiwei ++;
}
if((0x46 == *pDisAlarm_segment_hourshiwei) && (0x33 == *pDisAlarm_segment_hourgewei))
{
pDisAlarm_segment_hourshiwei = discode_segment;
pDisAlarm_segment_hourgewei = discode_segment;
}
}
else //当前为正常走时状态
{
bSetAlarm_hour = 0;
pDiscode_segment_hourgewei ++;
if(0xff == *pDiscode_segment_hourgewei)
{
pDiscode_segment_hourgewei = discode_segment;
pDiscode_segment_hourshiwei ++;
}
if((0x46 == *pDiscode_segment_hourshiwei) && (0x33 == *pDiscode_segment_hourgewei))
{
pDiscode_segment_hourshiwei = discode_segment;
pDiscode_segment_hourgewei = discode_segment;
}
}
while(!bHour_set);
}
}
if(!bMin_set) //分钟设置键被按下
{
delay(10);
if(!bMin_set)
{
if(bSetAlarm_state) //当前为闹钟设置状态
{
bSetAlarm_min = 1;
pDisAlarm_segment_mingewei ++;
if(0xff == *pDisAlarm_segment_mingewei)
{
pDisAlarm_segment_mingewei = discode_segment;
pDisAlarm_segment_minshiwei ++;
}
if(0x82 == *pDisAlarm_segment_minshiwei)
{
pDisAlarm_segment_minshiwei = discode_segment;
pDisAlarm_segment_mingewei = discode_segment;
}
}
else //当前为正常走时状态
{
bSetAlarm_min = 0;
pDiscode_segment_mingewei ++;
if(0xff == *pDiscode_segment_mingewei)
{
pDiscode_segment_mingewei = discode_segment;
pDiscode_
- 1
- 2
前往页