蓝桥杯 嵌入式 14省赛 真题
·
#include "fun.h"
void led_show(uint8_t led, uint8_t mode)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
if(mode)
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8<<(led-1), GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8<<(led-1), GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
//
uint16_t free_input;
uint16_t free_output = 4000;
float adc_value;
uint8_t lcd_mode;
uint8_t count_flag;
//
uint8_t data_HL;
uint8_t data_P;
float data_V;
uint16_t count_5s;
uint8_t data_flag;
uint8_t P_flag;
uint8_t flag_HL;
//
uint8_t para_RK[2] = {1, 1};
uint8_t para_RK_last[2] = {1, 1};
uint8_t RK_indax;
//
uint8_t PECD_N;
float PECD_MH;
float PECD_ML;
//
uint8_t led_flag;uint8_t long_flag;uint16_t count_2s;
uint8_t B1_data,B2_data,B3_data,B4_data,B1_last,B2_last,B3_last,B4_last;
void key_scan(void)
{
B1_data = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);B2_data = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
B3_data = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);B4_data = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
if(!B1_data&B1_last) // B1
{
if(++lcd_mode>2) lcd_mode = 0;
if(lcd_mode == 2)
{
para_RK_last[0] = para_RK[0];
para_RK_last[1] = para_RK[1];
}
}
if(!B2_data&B2_last) // B2
{
if(lcd_mode == 0)
{
if(!count_flag)
{
data_HL ^= 1; // 0 -> 1 4Khz - 8khz
data_flag = 1;
count_flag = 1;
}
}
else if(lcd_mode == 1) RK_indax ^= 1;
}
if(!B3_data&B3_last) // B3
{
if(lcd_mode == 1)
{
if(++para_RK[RK_indax]>10) para_RK[RK_indax] = 1;
}
}
if(lcd_mode != 0)
{
if(!B4_data&B4_last) // B4
{
if(lcd_mode == 1)
{
if(--para_RK[RK_indax]<1) para_RK[RK_indax] = 10;
}
}
}
else
{
if(!P_flag)
{
if(!B4_data&!B4_last)
{
long_flag = 1;
if(count_2s>2000)
{
long_flag = 0;
count_2s = 0;
P_flag = 1;
}
}
else if(B4_data&!B4_last)
{
long_flag = 0;
count_2s = 0;
}
}
else
{
if(!B4_data&B4_last) P_flag = 0;
}
}
B1_last = B1_data;B2_last = B2_data;B3_last = B3_data;B4_last = B4_data;
}
char text[20];
void lcd_show(void)
{
uint16_t temp = GPIOC -> ODR;
if(lcd_mode == 0)
{
sprintf(text, " DATA ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
if(!flag_HL) sprintf(text, " M=L ");
else sprintf(text, " M=H ");
LCD_DisplayStringLine(Line3, (uint8_t *)text);
sprintf(text, " P=%d%% ", data_P); //
LCD_DisplayStringLine(Line4, (uint8_t *)text);
sprintf(text, " V=%.1f ", data_V);
LCD_DisplayStringLine(Line5, (uint8_t *)text);
}
else if(lcd_mode == 1)
{
sprintf(text, " PARA ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
sprintf(text, " R=%d ", para_RK[0]);
LCD_DisplayStringLine(Line3, (uint8_t *)text);
sprintf(text, " K=%d ", para_RK[1]);
LCD_DisplayStringLine(Line4, (uint8_t *)text);
LCD_ClearLine(Line5);
}
else
{
sprintf(text, " RECD ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
sprintf(text, " N=%d ", PECD_N);
LCD_DisplayStringLine(Line3, (uint8_t *)text);
sprintf(text, " MH=%.1f ", PECD_MH);
LCD_DisplayStringLine(Line4, (uint8_t *)text);
sprintf(text, " ML=%.1f ", PECD_ML);
LCD_DisplayStringLine(Line5, (uint8_t *)text);
}
GPIOC -> ODR = temp;
}
float adc_read(ADC_HandleTypeDef *hadc)
{
HAL_ADC_Start(hadc);
uint32_t value_adc = HAL_ADC_GetValue(hadc);
return 3.3f*value_adc/4096;
}
uint16_t count_2s_recd;
float data_V_last;
uint8_t count_2s_flag;
void data_proc(void)
{
adc_value = adc_read(&hadc2);
if(!P_flag)
{
if(adc_value > 3) data_P = 85;
else if(adc_value < 1) data_P = 10;
else data_P = 37.5f*adc_value-27.5f;
}
TIM2->CCR2 = data_P;
//
if(count_5s>5000)
{
count_5s = 0;
data_flag = 0;
count_flag = 0;
if(data_HL) free_output = 8000;
else free_output = 4000;
flag_HL^=1;
PECD_N++;
}
if(data_flag)
{
if(data_HL) // 4 - 8
{
free_output = 4*count_5s/5+4000;
if(free_output>8000) free_output = 8000;
}
else // 8 - 4
{
free_output = 8000 - 4*count_5s/5;
if(free_output<4000) free_output = 4000;
}
}
data_V = free_input*6.27*para_RK_last[0]/(100*para_RK_last[1]);
TIM2->PSC = 800000/free_output - 1;
if(data_V_last==data_V) count_2s_flag = 1;
else count_2s_flag = 0;
if(count_2s_recd>2000)
{
if(PECD_MH<data_V && data_HL) PECD_MH = data_V;
else if(PECD_ML<data_V && !data_HL) PECD_ML = data_V;
count_2s_recd = 0;
count_2s_flag = 0;
}
data_V_last = data_V;
}
void main_pros(void)
{
led_show(1,(lcd_mode==0));
led_show(2, led_flag);
led_show(3,P_flag);
lcd_show();
key_scan();
data_proc();
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM17)
{
free_input = 1000000/(TIM17->CCR1+1);
TIM17->CNT = 0;
}
}
uint8_t count_01s;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM4)
{
if(data_flag)
{
if(++count_01s>100)
{
count_01s = 0;
led_flag ^= 1;
}
}
else led_flag = 0;
if(data_flag) count_5s++;
if(long_flag) count_2s++;
if(count_2s_flag) count_2s_recd++;
else count_2s_recd = 0;
}
}
更多推荐
所有评论(0)