题目如下
主要代码如下
//main.c
#include "includeall.h"
__STATIC_INLINE uint32_t My_SysTick_Config(uint32_t ticks);
void Init(void)
{
My_SysTick_Config(SystemCoreClock/1000);
Delay_Ms(200);
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
Usart2_Init(9600);
printf("init\r\n");
i2c_init();
LED_Init();
KEY_Init();
EXTI_Key_Init();
TIM2_Init(7199, 4999);//2HZ LED
TIM3_Init(7199, 9);// 1kHZ PWM
TIM4_Init(7199, 499);//按键时间0.8m
RTC_Init();
}
//Main Body
int main(void)
{
Init();
printf("main\r\n");
TIM_SetCompare1(TIM3, 5759);
while(1)
{
//if(!K1)
// LEDINVERT(LED7);
//LED_Water(LED4, LED7);
//KEY_Scan(0);
}
}
__STATIC_INLINE uint32_t My_SysTick_Config(uint32_t ticks)
{
if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
SysTick->LOAD = ticks - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, 0); /* set Priority for Systick Interrupt */
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
return (0); /* Function successful */
}
//function.c
#include "includeall.h"
void LCD_Show(void)
{
u8 lcdnostr[20];
u8 lcdtistr[20];
u8 lcdststr[20];
sprintf((char *)lcdnostr, "No %d", g_page+1);
LCD_DisplayStringLine(Line1, lcdnostr);
sprintf((char *)lcdtistr, " %2d: %2d: %2d", timeq[g_page].hh, timeq[g_page].mm, timeq[g_page].ss);
LCD_DisplayStringLine(Line3, lcdtistr);
switch(g_underline){
case SS:sprintf((char *)lcdtistr, " __");
break;
case MM:sprintf((char *)lcdtistr, " __ ");
break;
case HH:sprintf((char *)lcdtistr, " __ ");
break;
default:sprintf((char *)lcdtistr, " ----------");
}
LCD_DisplayStringLine(Line4, lcdtistr);
sprintf((char *)lcdststr, " %s", statename[g_bootstate]);
LCD_DisplayStringLine(Line6, lcdststr);
// switch(g_page)
// {
// case P1:
// {
// LCD_DisplayStringLine(Line1, "No 1");
// sprintf(lcdnostr, "%2d: %2d: %2d", timeq[g_page].hh, timeq[g_page].mm, timeq[g_page].ss);
// LCD_DisplayStringLine(Line3, lcdnostr);
// }
// break;
// case P2:
// {
// LCD_DisplayStringLine(Line1, "No 2");
// }
// break;
// case P3:
// {
// LCD_DisplayStringLine(Line1, "No 3");
// }
// break;
// case P4:
// {
// LCD_DisplayStringLine(Line1, "No 4");
// }
// break;
// case P5:
// {
// LCD_DisplayStringLine(Line1, "No 5");
// }
// break;
// }
// switch(g_bootstate){
// case STANDBY:
// {
// LCD_DisplayStringLine(Line5, "STANDBY");
// }
// break;
// case SETTING:
// {
// LCD_DisplayStringLine(Line5, "SETTING");
// }
// break;
// case RUNNING:
// {
// LCD_DisplayStringLine(Line5, "RUNNING");
// }
// break;
// case PAUSE:
// {
// LCD_DisplayStringLine(Line5, "PAUSE");
// }
// break;
// }
}
void Key_Moni(void)
{
u8 key = KEY_Scan(0);
//timstr timtemp = {0, 0, 0};
switch(g_bootstate)
{
case STANDBY:
{
switch(key)
{
case K1_PRES:
{
if(g_page == 4)
g_page = P1;
else
g_page++;
}
break;
case K2_PRES:
{
g_bootstate = SETTING;
}
break;
case K4_PRES:
{
g_bootstate = RUNNING;
}
break;
}//switch(key)
}//case STANDBY:
break;
case SETTING:
{
//timtemp.hh = timeq[g_page].hh;
//timtemp.mm = timeq[g_page].mm;
//timtemp.ss = timeq[g_page].ss;
switch(key)
{
case K1_PRES:
{
g_page++;
if(g_page == 5)
g_page = P1;
}
break;//case K1_PRES:
case K2_PRES:
{
if(g_underline == 2)
g_underline = 0;
else
g_underline++;
}
break;//case K2_PRES:
case K2_LPRES:
{
// timeq[g_page].hh = timtemp.hh;
// timeq[g_page].mm = timtemp.mm;
// timeq[g_page].ss = timtemp.ss;
i2c_SendOneData(3*g_page, timeq[g_page].hh);
i2c_SendOneData(3*g_page+1, timeq[g_page].mm);
i2c_SendOneData(3*g_page+2, timeq[g_page].ss);
}
break;//case K2_LPRES:
case K3_PRES:
{
if(g_underline == SS)
{
if(timeq[g_page].ss == 59)
timeq[g_page].ss = 0;
else
timeq[g_page].ss++;
}
else if(g_underline == MM)
{
if(timeq[g_page].mm == 59)
timeq[g_page].mm = 0;
else
timeq[g_page].mm++;
}else if(g_underline == HH)
{
if(timeq[g_page].hh == 23)
timeq[g_page].hh = 0;
else
timeq[g_page].hh++;
}
}
break;//case K3_PRES:
case K3_LPRES:
{
while(!K3){
if(g_underline == SS)
{
if(timeq[g_page].ss == 59)
timeq[g_page].ss = 0;
else
timeq[g_page].ss++;
}
else if(g_underline == MM)
{
if(timeq[g_page].mm == 59)
timeq[g_page].mm = 0;
else
timeq[g_page].mm++;
}
else if(g_underline == HH)
{
if(timeq[g_page].hh == 23)
timeq[g_page].hh = 0;
else
timeq[g_page].hh++;
//Delay_Ms(10);
}
}
break;//case K3_LPRES:
}//while(!K3)
case K4_PRES:
{
g_bootstate = RUNNING;
}
break;
}//switch(key)
}//case SETTING:
break;
case RUNNING:
{
switch(key)
{
case K1_PRES:
{
g_page++;
if(g_page == 5)
g_page = P1;
g_bootstate = STANDBY;
}
break;
case K2_PRES:
{
g_bootstate = SETTING;
}
break;
case K4_PRES:
{
g_bootstate = PAUSE;
}
break;
case K4_LPRES:
{
g_bootstate = STANDBY;
}
break;
}//switch(key)
}//case RUNNING:
break;
case PAUSE:
{
switch(key)
{
case K1_PRES:
{
g_page++;
if(g_page == 5)
g_page = P1;
g_bootstate = STANDBY;
}
break;
case K2_PRES:
{
g_bootstate = SETTING;
}
break;
case K4_PRES:
{
g_bootstate = RUNNING;
}
break;
case K4_LPRES:
{
g_bootstate = STANDBY;
}
break;
}//switch(key)
}//case PAUSE:
break;
}
}
//key.c
#include "includeall.h"
#include "../Driver/KEY/key.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up = 1;
//if(mode) key_up = 1;
//printf("%d ", (!K1||!K2||!K3||!K4));
if(key_up && (!K1||!K2||!K3||!K4) )
{
//printf("2 ");
TIM_Cmd(TIM4, ENABLE);
key_up = 0;
Delay_Ms(10);
if(!K1)
{
//printf("%d ", !K1);
return K1_PRES;
}
else if(!K2)
{
while(!K2){
if(longpres == K2_LPRES)
{
longpres = 0;
return K2_LPRES;
}
}
return K2_PRES;
}
else if(!K3)
{
while(!K3)
{
if(longpres == K3_LPRES)
{
longpres = 0;
return K3_LPRES;
}
}
return K3_PRES;
}
else if(!K4)
{
while(!K4);
if(longpres == K4_LPRES)
{
longpres = 0;
return K4_LPRES;
}
return K4_PRES;
}
else return 0;
}
else if(key_up == 0)
{
//printf("3 ");
if(K1&&K2&&K3&&K4)
{
key_up = 1;
TIM_Cmd(TIM4, DISABLE);
}
}
return 0;
}
//exti.c
#include "includeall.h"
#include "../Driver/EXTI/exti.h"
void EXTI_Key_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
KEY_Init();
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//************************************************************
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//************************************************************
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//************************************************************
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
printf("0");
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
printf("1");
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
printf("2");
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line8) != RESET)
{
printf("8");
/* Clear the EXTI line 8 pending bit */
EXTI_ClearITPendingBit(EXTI_Line8);
}
}
// A code block
var foo = 'bar';