嵌入式蓝桥杯时钟、闹钟

可调时钟闹钟数据,保存修改次数。

#include "stm32f10x.h"
#include "lcd.h"
#include "key.h"
#include "i2c.h"
#include "stm32f10x.h"
#include "led.h"
#include "tim.h"

void Keypro(void);
void LCDpro(void);
void SUB(void);
void ADD(void);

unsigned char time[3] = {11,59,50};
unsigned char alarm[3] = {12,0,0};
unsigned char pucStr[16];
unsigned char pucStr1[16];
u32 state;
u32 ntime=0;
u32 num;
u8 i=8;

int main(void)
{
 	
	KeyInit();
	STM3210B_LCD_Init();
	SysTick_Config(8000);

	i2c_init();
	num=Read(0xff);

	LedInit();

	LCD_Clear(White);
	LCD_SetTextColor(Blue);
	LCD_SetBackColor(White);

//	
//	LCD_DisplayStringLine(Line1,"        CLOCK        ");
//	LCD_DisplayStringLine(Line3,"    you can change  ");		
//
//	LCD_SetTextColor(White);
//	LCD_SetBackColor(Blue);
//
//	LCD_DisplayStringLine(Line5,"please press button");

	TimInit();
	TIM_SetCompare1(TIM2,500); //占空比为500/1000=50%
	TIM_SetCompare2(TIM2,700); //占空比为700/1000=70%
	while(1)
	{
		Keypro();
		LCDpro();
		sprintf((char*)pucStr1,"     NUM:%d        ",num);
        LCD_DisplayStringLine(Line9, pucStr1);
	}

}

void LCDpro(void)
{
	switch(state)
	{
		case 0:
		LCD_DisplayStringLine(Line1,"        MAIN        ");
		sprintf((char*)pucStr, "    TIME:%02u:%02u:%02u   ",time[0], time[1], time[2]);
        LCD_DisplayStringLine(Line7, pucStr);
		sprintf((char*)pucStr, "   Alarm:%02u:%02u:%02u   ",alarm[0],alarm[1],alarm[2]);
        LCD_DisplayStringLine(Line8, pucStr);
		
		break;
		case 1:
		case 2:
		case 3:
		LCD_DisplayStringLine(Line1, (unsigned char*)"    RTC-SETTING     ");
		if(state==1)
		LCD_SetBackColor(Red);
		LCD_DisplayChar(Line7, 176, time[0]/10+0x30);
		LCD_DisplayChar(Line7, 160, time[0]%10+0x30);
		LCD_SetBackColor(White);

		LCD_DisplayChar(Line7, 144, ':');

		if(state==2)
		LCD_SetBackColor(Red);
		LCD_DisplayChar(Line7, 128, time[1]/10+0x30);
		LCD_DisplayChar(Line7, 112, time[1]%10+0x30);
		LCD_SetBackColor(White);
		
		LCD_DisplayChar(Line7, 96, ':');

		if(state==3)
		LCD_SetBackColor(Red);
		LCD_DisplayChar(Line7, 80, time[2]/10+0x30);
		LCD_DisplayChar(Line7, 64, time[2]%10+0x30);
		LCD_SetBackColor(White);
		break;
		case 4:
		case 5:
		case 6:
		LCD_DisplayStringLine(Line1, (unsigned char*)"    ALARM-SETTING     ");
		if(state==4)
		LCD_SetBackColor(Red);
		LCD_DisplayChar(Line8, 176, alarm[0]/10+0x30);
		LCD_DisplayChar(Line8, 160, alarm[0]%10+0x30);
		LCD_SetBackColor(White);

		LCD_DisplayChar(Line8, 144, ':');

		if(state==5)
		LCD_SetBackColor(Red);
		LCD_DisplayChar(Line8, 128, alarm[1]/10+0x30);
		LCD_DisplayChar(Line8, 112, alarm[1]%10+0x30);
		LCD_SetBackColor(White);
		
		LCD_DisplayChar(Line8, 96, ':');

		if(state==6)
		LCD_SetBackColor(Red);
		LCD_DisplayChar(Line8, 80, alarm[2]/10+0x30);
		LCD_DisplayChar(Line8, 64, alarm[2]%10+0x30);
		LCD_SetBackColor(White);
	}
}

void Keypro(void)
{
	u16 key;
	key=Keyscan();
	switch(key)
	{
		case 1:
		if(state==0)
		state=1;
		else if(state<4)
		state=0;
		
		else if(++state==7)
		state=4;
		break;

		case 2:
		if(state==0)
		state=4;
		else if(state==4)
		state=0;
		else if(++state==4)
		state=1;
		break;
		case 3:
		ADD();break;
		case 4:
		SUB();break;
	}				
}

void SysTick_Handler(void)
{
	ntime++;
	
	if(ntime==1000)
	{	
		ntime=0;
		time[2]++;
		if(time[2]==60)
		{
			time[2]=0;
			time[1]++;
			if(time[1]==60)
			{
				time[1]=0;
				time[0]++;
				if(time[0]==24)
				time[0]=0;
			}
		}
		GPIO_Write(GPIOC,~(1<<i));
		GPIO_SetBits(GPIOD,GPIO_Pin_2);
		GPIO_ResetBits(GPIOD,GPIO_Pin_2);
		if(++i==16)
		i=8;
	}
	if((time[0]==alarm[0])&&(time[1]==alarm[1])&&(time[2]==alarm[2]))
	{
		GPIO_Write(GPIOC,0x00);
		GPIO_SetBits(GPIOD,GPIO_Pin_2);
		GPIO_ResetBits(GPIOD,GPIO_Pin_2);
	}
}

void ADD(void)
{
	switch(state)
	{
		case 1:
		time[0]++;
		if(time[0]==24)
		time[0]=0;
		num++;
		Write(0xff,num);
		break;
		case 2:
		time[1]++;
		if(time[1]==60)
		time[1]=0;
		num++;
		Write(0xff,num);
		break;
		case 3:
		time[2]++;
		if(time[2]==60)
		time[2]=0;
		num++;
		Write(0xff,num);
		break;
		case 4:
		alarm[0]++;
		if(alarm[0]==60)
		alarm[0]=0;
		num++;
		Write(0xff,num);
		break;
		case 5:
		alarm[1]++;
		if(alarm[0]==24)
		alarm[1]=0;
		num++;
		Write(0xff,num);
		break;
		case 6:
		alarm[2]++;
		if(alarm[2]==60)
		alarm[2]=0;
		num++;
		Write(0xff,num);
		break;
	}	
}

void SUB(void)
{
	switch(state)
	{
		case 1:
		time[0]--;
		if(time[0]==0)
		time[0]=23;
		num++;
		Write(0xff,num);
		break;
		case 2:
		time[1]--;
		if(time[1]==0)
		time[1]=59;
		num++;
		Write(0xff,num);
		break;
		case 3:
		time[2]--;
		if(time[2]==0)
		time[2]=59;
		num++;
		Write(0xff,num);
		break;
		case 4:
		alarm[0]--;
		if(alarm[0]==0)
		alarm[0]=23;
		num++;
		Write(0xff,num);
		case 5:
		alarm[1]--;
		if(alarm[0]==0)
		alarm[1]=59;
		num++;
		Write(0xff,num);
		break;
		case 6:
		alarm[2]--;
		if(alarm[2]==0)
		alarm[2]=59;
		num++;
		Write(0xff,num);
		break;
	}	
}

你可能感兴趣的:(嵌入式蓝桥杯时钟、闹钟)