甘肃做网站的公司有哪些,常州网约车哪个平台最好,网站对应的ip,成都网站开发建设公司第十二届省赛
按键代码
——自认为比较巧妙#xff0c;定时器3被设置为10ms进入一次中断#xff0c;代替了HAL_Delay(10)的方法消抖#xff1b;
运用状态机机思想实现检测多个按键检测——且分为两个状态#xff0c;其中一个状态PB#xff11;和PB#xff12;的按键不…第十二届省赛
按键代码
——自认为比较巧妙定时器3被设置为10ms进入一次中断代替了HAL_Delay(10)的方法消抖
运用状态机机思想实现检测多个按键检测——且分为两个状态其中一个状态PB和PB的按键不可使用
struct Key key[4] {0, 0, 0};
uint8_t f;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if (htim-Instance TIM3){key[0].Key_sta HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);key[2].Key_sta HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);key[3].Key_sta HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);key[1].Key_sta HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);}//注意B2和B3对应Key[2]和Key[3];而B4对应Key[1];// Note that B2 and B3 correspond to Key[2] and Key[3]; B4 corresponds to Key[1];if (Para_flag0) f2;else f4;//让按键B2~B3不可以在界面一中使用//注意上面的赋值// Make keys B2 to B3 unavailable in interface 1// Note the above assignmentfor (int i0; if; i){switch (key[i].sta){case 0:if (key[i].Key_sta0){key[i].sta1;}break;case 1:if (key[i].Key_sta0){key[i].sta2;}else{key[i].sta0;}break;case 2:if(key[i].Key_sta1) {key[i].sta0;if(key[i].time100){key[i].flag1;key[i].time0;}}else{key[i].time;if (key[i].time100){key[i].long_flag1;key[i].time0;}}break;}}
}
//注意Time是用于检测长按按键的本体用不上
菜单函数
在菜单中实现了按键二次检测 伏笔回收
#include menu.h
uint8_t data12, data24, data32;
double data43.5, data52;
char text1[30] , text2[30] ,text3[30] , text4[30] ;
char flag_m0,Para_flag0,PWM_Flag1;void Menu(void)
{if (key[0].flag1){flag_m!flag_m;LCD_Clear(Black);//每次展示菜单前先清屏key[0].flag0;}if (key[1].flag1){PWM_Flag!PWM_Flag;key[1].long_flag0;key[1].flag0;}//PWM和低电平的设置if (PWM_Flag0) __HAL_TIM_SetCompare(htim15, TIM_CHANNEL_1, 20);else __HAL_TIM_SetCompare(htim15, TIM_CHANNEL_1, 0);if (flag_m0){Para_flag 0;show_Data();}else{Para_flag 1;show_Para();}
}
void show_Data(void)
{data38-data1-data2;sprintf(text1, Data );sprintf(text2, CNBR:%d ,data1);sprintf(text3, VNBR:%d ,data2);sprintf(text4, IDLE:%d ,data3);LCD_DisplayStringLine(Line2, (unsigned char *)text1);LCD_DisplayStringLine(Line4, (unsigned char *)text2);LCD_DisplayStringLine(Line6, (unsigned char *)text3);LCD_DisplayStringLine(Line8, (unsigned char *)text4);
}void show_Para(void)
{sprintf(text1, Para );sprintf(text2, CNBR:%.2f ,data4);sprintf(text3, VNBR:%.2f ,data5);LCD_DisplayStringLine(Line2, (unsigned char *)text1);LCD_DisplayStringLine(Line4, (unsigned char *)text2);LCD_DisplayStringLine(Line6, (unsigned char *)text3);if (key[2].flag1){data40.5;data50.5;key[2].long_flag0;key[2].flag0;}if (key[3].flag1){data4-0.5;data5-0.5;key[3].long_flag0;key[3].flag0;}
}
我认为最难的就是串口部分
也可能是我的方法过于难了,应该可以使用先把串口数据存到数组,然后在数组中依靠下标来检测错误 遇到的难题--就是串口接收时检测到数据不符合格式,虽然重新开始存入数据,但是数据缓存区的数据没有被清空,会被带到下一次数据中,因此在寻找清空缓存区的方法过程中浪费很多时间,所幸也找到了对应的方法--暂时关闭中断接收,再在while循环中打开
//printf函数重写
int fputc(int c, FILE *stream)
{uint8_t ch[1]{c};HAL_UART_Transmit(huart1, (unsigned char *)ch, 1, 0xFFFF);return c;
}uint8_t rx0;
uint8_t Index0;
char temp_t[5];
char temp_n[5];
char temp_time[11];
char temp_minute[3];
char trash;
uint8_t usart_state0;//用于标志是否接收完数据 是否可以进入处理函数//方法1 在串口处初步处理信息——出现bug是无法清除串口缓存区导致的
//串口接收中断函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if (huart-Instance USART1){if (Index4){HAL_UART_Receive_IT(huart1, rx, 1);temp_t[Index]rx;}else if(Index4){HAL_UART_Receive_IT(huart1, rx, 1);if (rx!:){//目的出现错误时会打印出两个Error尝试在出现错误后读取数据并清空数据来解决问题//方法一失败——找不到对应的寄存器//把剩下的数据读出即可//实现方法whilesrrx_flagxxxdr
// while(USART1-ISR0x10)
// {
// HAL_UART_Receive_IT(huart1, rx, 1);
// trashrx;
// }//方法二这个函数似乎不起作用
// __HAL_UART_FLUSH_DRREGISTER(huart1);
// //方法三似乎也不起作用
// while(__HAL_UART_GET_FLAG(huart1,UART_FLAG_RXNE)SET)
// {
// HAL_UART_Receive_IT(huart1, rx, 1);
// }
// __HAL_UART_CLEAR_FLAG(huart1,UART_FLAG_RXNE);//方法三直接在这关闭串口中断接收然后在while循环中再打开 效果出奇地好//可能关闭中断接收后对应的缓存区的数据也被清除了HAL_UART_AbortReceive_IT(huart1);Index0;printf(Erorr\r\n);}else{Index;}}else if (Index9){HAL_UART_Receive_IT(huart1, rx, 1);temp_n[(Index)-5]rx;}else if (Index9){HAL_UART_Receive_IT(huart1, rx, 1);if (rx!:){
// while(__HAL_UART_GET_FLAG(huart1,UART_FLAG_RXNE)SET)
// {
// HAL_UART_Receive_IT(huart1, rx, 1);
// }
// __HAL_UART_CLEAR_FLAG(huart1,UART_FLAG_RXNE);HAL_UART_AbortReceive_IT(huart1);Index0;printf(Erorr\r\n);}else{Index;}}else if (Index9)//前大段数字{if(Index20){HAL_UART_Receive_IT(huart1, rx, 1);temp_time[(Index)-10]rx;}else//只有分{HAL_UART_Receive_IT(huart1, rx, 1);temp_minute[(Index)-20] rx;}}if (Index22){usart_state1; Index0;//表示一组数据接收完成 置标志位 可以进入数据处理函数}
// if (Index22)
// {
// for (int i0;i4;i) printf(%c, temp_t[i]);
// printf(:);
// for (int i0;i4;i) printf(%c, temp_n[i]);
// printf(:);
// for (int i0;i12;i) printf(%c, temp_time[i]);
// }}
}#include Myusart.hstruct In in[8]{0};
uint8_t yn0;//是否在车库内
uint8_t outcar_index0;
double Money0;
uint16_t Time_parking0;
struct Time temp_time_struct;void USART_Deal(void)
{if (usart_state1){//置零以便下次工作usart_state0;//先把字符串数组的最后一位置为\0——必要操作temp_n[4]\0;temp_t[4]\0;temp_time[10]\0;temp_minute[2]\0;//对时间进行处理//注意strtol最大只能转int32_t的数 uint64_t numberstrtol(temp_time, NULL, 10);//字符串数字转int类型uint8_t number1strtol(temp_minute, NULL, 10);temp_time_struct.Ynumber/1000000;//yeartemp_time_struct.Mnumber/10000%100;//monthtemp_time_struct.Dnumber/100%100;//daytemp_time_struct.hnumber%100;//hourtemp_time_struct.mnumber1;//min//Error情况if ((temp_time_struct.M12) (temp_time_struct.D31) (temp_time_struct.h23) (temp_time_struct.m59)){printf(Erorr\r\n);return ;}//遍历结构体数组寻找number1;//min是否已经停在车库for(int i0; i8; i){if (in[i].YN1){if (!strcmp(temp_n, in[i].num)){outcar_indexi;yn1;//出停车库//并把对应车辆所有清零}else {yn0;}}else {yn0;}}//是否在车库内之后的操作if (yn1){//出停车库//并把对应车辆所有清零in[outcar_index].YN0;Time_parking (temp_time_struct.Y-in[outcar_index].t.Y)*8760(temp_time_struct.M-in[outcar_index].t.M)*720(temp_time_struct.D-in[outcar_index].t.D)*24(temp_time_struct.h-in[outcar_index].t.h)(bool)(temp_time_struct.m-in[outcar_index].t.m);if(!(strcmp(temp_t, CNBR\0))){MoneyTime_parking*data4;data1--;}if ((!strcmp(temp_t, VNBR\0))){MoneyTime_parking*data5;data2--;}printf(%s:%s:%d:%.2f\r\n, temp_t, temp_n, Time_parking, Money);printf(%d\r\n, in[outcar_index].YN);}else//进入停车库 记录信息{//判断车位是否已满 如果已满则取消操作data38-data1-data2;if ((data3-1)0){printf(Erorr\r\n);return ;}else{if(!(strcmp(temp_t, CNBR\0))){data1;}if ((!strcmp(temp_t, VNBR\0))){data2;}}//寻找结构体数组中的空位uint8_t temp_index0;for(int i0; i8; i){if (in[i].YN0){temp_indexi;}}//赋值strcpy(in[temp_index].type, temp_t);strcpy(in[temp_index].num, temp_n);//注意strtol最大只能转int32_t的数 uint64_t numberstrtol(temp_time, NULL, 10);//字符串数字转int类型uint8_t number1strtol(temp_minute, NULL, 10);in[temp_index].t.Ynumber/1000000;//yearin[temp_index].t.Mnumber/10000%100;//monthin[temp_index].t.Dnumber/100%100;//dayin[temp_index].t.hnumber%100;//hourin[temp_index].t.mnumber1;//minin[temp_index].YN1;printf(%s-,in[temp_index].type);printf(%s-,in[temp_index].num);printf(%d-%d-%d-%d-%d\r\n,in[temp_index].t.Y,in[temp_index].t.M,in[temp_index].t.D,in[temp_index].t.h,in[temp_index].t.m);}}}
串口代码的优化
这次采取的方法是先把数据存到数组中,再根据下标判断数据是否符合规范
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if (huart-Instance USART1){HAL_UART_Receive_IT(huart1, rx, 1);data[data_index] rx;if (data_index22){//关闭串口接收中断——清空缓存区以免有多余信息HAL_UART_AbortReceive_IT(huart1);data_index0;usart_state1;
// printf(%s, data);}}
}
void Usart_Deal_Better(void)
{if (usart_state1){//置零以便下次工作usart_state0;if (!(data[3]:)!(data[9]:))//判断:格式{printf(Error);return ;}//判断数字是否正确strncpy(temp_time, data10, 10);strncpy(temp_minute, data20, 2);temp_time[10]\0;temp_minute[2]\0;uint64_t numberstrtol(temp_time, NULL, 10);//字符串数字转int类型uint8_t number1strtol(temp_minute, NULL, 10);temp_time_struct.Ynumber/1000000;//yeartemp_time_struct.Mnumber/10000%100;//monthtemp_time_struct.Dnumber/100%100;//daytemp_time_struct.hnumber%100;//hourtemp_time_struct.mnumber1;//minif ((temp_time_struct.Y2099)||(temp_time_struct.Y2000)||(temp_time_struct.M12)||(temp_time_struct.D31)||(temp_time_struct.h23)||(temp_time_struct.m59)){printf(Error\r\n);return ;}//判断是否在停车场中//复制编号出来strncpy(temp_n, data5, 4); for (int i0; i8; i){if (in[i].YN1){if (!strcmp(temp_n, in[i].num))//如果在停车场中{if (strncmp(data, in[i].type, 4)){printf(Error\r\n);return ;}else {outcar_indexi;yn1;break;}}else//不在停车场中{yn0;}}else yn0;}uint8_t temp_index0;//用于寻找结构体数组内的空位if (yn0)//新来的{if (strncmp(data,CNBR, 4)strncmp(data,VNBR, 4))//判断停车类型{printf(Error\r\n);return ;}else//类型输入正确{data38-data1-data2;if ((data3-1)0){printf(Error\r\n);return ;}else//停车位充足{if (!strncmp(data,CNBR, 4)){data1;}if (!strncmp(data,VNBR, 4)){data2;}for (int i0; i8; i)//寻找结构体数组中的空位{if (in[i].YN0){temp_indexi;break;}}//登记信息strncpy(in[temp_index].type, data, 4);strncpy(in[temp_index].num, data5, 4);strncpy(temp_time, data10, 10);strncpy(temp_minute, data20, 2);temp_time[10]\0;temp_minute[2]\0;uint64_t numberstrtol(temp_time, NULL, 10);//字符串数字转int类型uint8_t number1strtol(temp_minute, NULL, 10);in[temp_index].t.Ytemp_time_struct.Y;in[temp_index].t.Mtemp_time_struct.M;in[temp_index].t.Dtemp_time_struct.D;in[temp_index].t.htemp_time_struct.h;in[temp_index].t.mtemp_time_struct.m;in[temp_index].YN1;printf(%s-,in[temp_index].type);printf(%s-,in[temp_index].num);printf(%d-%d-%d-%d-%d\r\n,in[temp_index].t.Y,in[temp_index].t.M,in[temp_index].t.D,in[temp_index].t.h,in[temp_index].t.m);}}}else//出停车库 {in[outcar_index].YN0;strncpy(temp_t, data, 4);Time_parking (temp_time_struct.Y-in[outcar_index].t.Y)*8760(temp_time_struct.M-in[outcar_index].t.M)*720(temp_time_struct.D-in[outcar_index].t.D)*24(temp_time_struct.h-in[outcar_index].t.h);if ((temp_time_struct.m-in[outcar_index].t.m0)){Time_parking;} if(!(strcmp(temp_t, CNBR\0))){MoneyTime_parking*data4;data1--;}if ((!strcmp(temp_t, VNBR\0))){MoneyTime_parking*data5;data2--;}printf(%s:%s:%d:%.2f\r\n, temp_t, temp_n, Time_parking, Money);}}
}
十三届省赛
IIC部分
IIC的读写eeprom和读读eeprom之间必须要有时间间隔否则会出现写入错误或者读出错误 例如图中的Delay10ms ADC采样部分
由于之前ADC采集两个电位器电压是用了不同的两个ADC而这次题目要求是用同一个ADC的不同通道这就涉及到了ADC多通道采集的问题了
首先cubemx配置打开对应的输入引脚 再打开扫描模式连续转换DMA转运 再 代码甚至只需要一句
自己创建一个数组存储数据
HAL_ADC_Start_DMA(hadc2, (uint32_t *)ADC_value,2);
仔细看芯片手册板子上的电压采集是J11和J12口因为没看清楚导致浪费了3个小时检查错误 且要注意一直打开DMA转运会严重拖慢程序的执行即导致板子上程序运行极为缓慢所以可以在交互后需要获取ADC的值时打开ADC_DMA转运接收好数据后立马关闭这样就不会太大地影响板子的运行速度了
if (B40)//Data{HAL_ADC_Start_DMA(hadc2, (uint32_t *)ADC_value,2);HAL_Delay(10);data1ADC_value[0]*3.3/4096;data2ADC_value[1]*3.3/4096;recd1;recd1_5;recdPA4_buff[PA4_Index]data1;recdPA5_buff[PA5_Index]data2;if (recd2data1) {recd2data1;}if (recd3data1) {recd3data1;}if (recd2_5data2) {recd2_5data2;}if (recd3_5data2) {recd3_5data2;}HAL_ADC_Stop_DMA(hadc2);//记得关闭DMA否则会导致程序运行缓慢}