第十届省赛真题主观题
第十届省赛体研究生组与本科组只是相差了EEPROM模块其他模块完全一样,难度相差不大,只是多了个I2C的使用。通过做研究生组的真题也能熟练一下I2C的使用。
主函数代码(不包含各模块初始化代码)
#include "main.h"
#include "rcc.h"
#include "led_key.h"
#include "i2c.h"
#include "adc.h"
#include "lcd.h"
__IO uint32_t uwTick_LED_Speed_Ctrl;
__IO uint32_t uwTick_KEY_Speed_Ctrl;
__IO uint32_t uwTick_LCD_Speed_Ctrl;
__IO uint32_t uwTick_LED_Bulingbuling_Ctrl;
uint8_t ucLED;
uint8_t key_value,key_down,key_up;
static uint8_t key_old;
uint8_t LCD_String_Disp[21];
uint8_t State_Ctrl;
float R37_Volt;
uint8_t Setting_Data[4]={24,12,1,2};
uint8_t Volt_Status;
void SystemClock_Config(void);
void LED_Proc(void);
void KEY_Proc(void);
void LCD_Proc(void);
void Volt_Status_Judge(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_KEY_Init();
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(Magenta);
ADC2_Init();
I2CInit();
I2C_24c02_Read(Setting_Data , 0 ,4);
while (1)
{
LED_Proc();
KEY_Proc();
LCD_Proc();
}
}
void LED_Proc(void)
{
if((uwTick - uwTick_LED_Speed_Ctrl)<200) return;
uwTick_LED_Speed_Ctrl = uwTick;
if(State_Ctrl!=0)
ucLED = 0x00;
LED_Disp(ucLED);
}
void KEY_Proc(void)
{
if((uwTick - uwTick_KEY_Speed_Ctrl)<100) return;
uwTick_KEY_Speed_Ctrl = uwTick;
key_value = KEY_Scan();
key_down = key_value & (key_value ^ key_old);
key_up = ~key_value & (key_value ^ key_old);
key_old = key_value;
switch(key_down)
{
case 1:
if(State_Ctrl == 0)
{
State_Ctrl = 1;
LCD_Clear(Black);
}
else
{
State_Ctrl = 0;
LCD_Clear(Black);
I2C_24c02_Write(Setting_Data , 0 ,4);
}
break;
case 2:
if(State_Ctrl != 0)
State_Ctrl++;
if(State_Ctrl == 5)
State_Ctrl = 1;
break;
case 3:
if(State_Ctrl != 0)
{
switch(State_Ctrl)
{
case 1:
Setting_Data[0] += 3;
if(Setting_Data[0] == 36)
Setting_Data[0] = 33;
break;
case 2:
Setting_Data[1] += 3;
if(Setting_Data[1] > Setting_Data[0])
Setting_Data[1] = Setting_Data[0];
break;
case 3:
Setting_Data[2] += 1;
if(Setting_Data[2] == Setting_Data[3])
Setting_Data[2] += 1;
if(Setting_Data[2] >= 9)
Setting_Data[2] = 1;
if(Setting_Data[2] == Setting_Data[3])
Setting_Data[2] += 1;
break;
case 4:
Setting_Data[3] += 1;
if(Setting_Data[3] == Setting_Data[2])
Setting_Data[3] += 1;
if(Setting_Data[3] >= 9)
Setting_Data[3] = 1;
if(Setting_Data[3] == Setting_Data[2])
Setting_Data[3] += 1;
break;
}
}
break;
case 4:
if(State_Ctrl != 0)
{
switch(State_Ctrl)
{
case 1:
Setting_Data[0]-=3;
if(Setting_Data[0] < Setting_Data[1])
Setting_Data[0] = Setting_Data[1];
break;
case 2:
Setting_Data[1]-=3;
if(Setting_Data[1] > 200)
Setting_Data[1] = 0;
break;
case 3:
Setting_Data[2] -= 1;
if(Setting_Data[2] == Setting_Data[3])
Setting_Data[2] -= 1;
if(Setting_Data[2] == 0)
Setting_Data[2] = 8;
if(Setting_Data[2] == Setting_Data[3])
Setting_Data[2] -= 1;
break;
case 4:
Setting_Data[3] -= 1;
if(Setting_Data[3] == Setting_Data[2])
Setting_Data[3] -= 1;
if(Setting_Data[3] == 0)
Setting_Data[3] = 8;
if(Setting_Data[3] == Setting_Data[2])
Setting_Data[3] -= 1;
break;
}
}
break;
}
}
void Volt_Status_Judge(void)
{
if(R37_Volt > (float)Setting_Data[0]/10)
{
Volt_Status = 1;
if((uwTick - uwTick_LED_Bulingbuling_Ctrl)>200)
{
uwTick_LED_Bulingbuling_Ctrl = uwTick;
ucLED ^= (1<<(Setting_Data[2]-1));
}
}
else if(R37_Volt < (float)Setting_Data[1]/10)
{
Volt_Status = 2;
if((uwTick - uwTick_LED_Bulingbuling_Ctrl)>200)
{
uwTick_LED_Bulingbuling_Ctrl = uwTick;
ucLED ^= (1<<(Setting_Data[3]-1));
}
}
else
{
Volt_Status = 3;
ucLED = 0x00;
}
}
void LCD_Proc(void)
{
if((uwTick - uwTick_LCD_Speed_Ctrl)<150) return;
uwTick_LCD_Speed_Ctrl = uwTick;
R37_Volt = Get_ADC_Value()*3.3/4096;
Volt_Status_Judge();
if(State_Ctrl == 0)
{
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Main");
LCD_DisplayStringLine(Line1 , LCD_String_Disp);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Volt: %4.2fV",R37_Volt);
LCD_DisplayStringLine(Line4 , LCD_String_Disp);
switch(Volt_Status)
{
case 1:
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Status: Upper ");
LCD_DisplayStringLine(Line6 , LCD_String_Disp);
break;
case 2:
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Status: Lower ");
LCD_DisplayStringLine(Line6 , LCD_String_Disp);
break;
case 3:
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Status: Normal ");
LCD_DisplayStringLine(Line6 , LCD_String_Disp);
break;
}
}
else
{
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Setting");
LCD_DisplayStringLine(Line1 , LCD_String_Disp);
if(State_Ctrl == 1) LCD_SetBackColor(Yellow);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Max Volt: %3.1f",(float)Setting_Data[0]/10);
LCD_DisplayStringLine(Line3 , LCD_String_Disp);
LCD_SetBackColor(Black);
if(State_Ctrl == 2) LCD_SetBackColor(Yellow);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Min Volt: %3.1f",(float)Setting_Data[1]/10);
LCD_DisplayStringLine(Line5 , LCD_String_Disp);
LCD_SetBackColor(Black);
if(State_Ctrl == 3) LCD_SetBackColor(Yellow);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Upper: LD%1d",Setting_Data[2]);
LCD_DisplayStringLine(Line7 , LCD_String_Disp);
LCD_SetBackColor(Black);
if(State_Ctrl == 4) LCD_SetBackColor(Yellow);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Lower: LD%1d",Setting_Data[3]);
LCD_DisplayStringLine(Line9 , LCD_String_Disp);
LCD_SetBackColor(Black);
}
}
按键与LCD逻辑图(仅供参考)
|