- /*申明一下部分代码是来自于网络的整合 我自己做了一些修改 更加完善 取之于网络用之于网络 */
- /*按键队列执行函数*/
- void task_run_key(void)
- {
- KeyValue_Info KeyValueInfo;
- unsigned char len = 0;
-
- if(Read_RingStrEx(&Key_Value,(unsigned char *)&KeyValueInfo,&len) == 0)
- {
- if(len != sizeof(KeyValue_Info))
- {
- return;
- }
-
- if(KeyValueInfo.KeyGpNum < KeyGPCnt && KeyGpInfo[KeyValueInfo.KeyGpNum].KeyGPRun != NULL)
- {
-
- KeyGpInfo[KeyValueInfo.KeyGpNum].KeyGPRun();
-
- }
- else if(KeyValueInfo.KeyNum < KeyCnt)
- {
- if(KeyValueInfo.KeyStatus == SP && KeyInfo[KeyValueInfo.KeyNum].KeySPRun != NULL)
- {
-
- KeyInfo[KeyValueInfo.KeyNum].KeySPRun();
-
- }
- else if(KeyValueInfo.KeyStatus == LP && KeyInfo[KeyValueInfo.KeyNum].KeyLPRun != NULL)
- {
-
- KeyInfo[KeyValueInfo.KeyNum].KeyLPRun();
-
- }
- else if(KeyValueInfo.KeyStatus == LOOP && KeyInfo[KeyValueInfo.KeyNum].KeyLOOPRun != NULL)
- {
-
- KeyInfo[KeyValueInfo.KeyNum].KeyLOOPRun();
-
- }
- }
- }
- }
- //监听函数 --放在1ms循环里面执行
- void task_listen_key(void)
- {
- unsigned char i;
- KeyValue_Info KeyValueInfo;
-
- //单键检测
- for(i = 0; i < KeyCnt;i++)
- {
- if(KeyInfo[i].useFlg == KEY_USE)
- {
- CheckKey((Key_Num)i,KeyInfo[i].mode);
- }
- if(Get_keyValueX[i] == KEY_ON && Get_keyValue[i] == KEY_OFF && KeyGPFlag[i] == 0)
- {
- if(KeyInfo[i].mode == KEY_CLOCK)
- {
- #if DEBUG_PRINTF
- //printf("非连发模式:key%d按键已松开,键入时间为%dMs\r\n",(unsigned char)i+1,Get_keyChooseTimeCnt[i]);
- #endif
- //短按
- if((Get_keyChooseTimeCnt[i] != 0) && (Get_keyChooseTimeCnt[i] < LP_TIME) && (Get_keyChooseTimeCnt[i] >= SP_TIME))
- {
- KeyValueInfo.KeyNum = (Key_Num)i;
- KeyValueInfo.KeyGpNum = (KeyGroup_Num)KeyGPCnt;
- KeyValueInfo.KeyStatus = SP;
- Write_RingStr(&Key_Value,(unsigned char *)&KeyValueInfo,sizeof(KeyValueInfo));
- }
- //长按
- else if((Get_keyChooseTimeCnt[i] != 0) && (Get_keyChooseTimeCnt[i] >= LP_TIME))
- {
- KeyValueInfo.KeyNum = (Key_Num)i;
- KeyValueInfo.KeyGpNum = (KeyGroup_Num)KeyGPCnt;
- KeyValueInfo.KeyStatus = LP;
- Write_RingStr(&Key_Value,(unsigned char *)&KeyValueInfo,sizeof(KeyValue_Info));
- }
- }
-
- else if (KeyInfo[i].mode == KEY_NOCLOCK)
- {
- #if DEBUG_PRINTF
- //printf("连发模式:key%d按键键入时间为%dMs 自动连发时间:%dMs\r\n",(unsigned char)i+1,Get_keyChooseTimeCnt[i],KeyInfo[i].AutoTime);
- #endif
- if(Get_keyChooseTimeCnt[i] != 0)
- {
- KeyValueInfo.KeyNum = (Key_Num)i;
- KeyValueInfo.KeyGpNum = (KeyGroup_Num)KeyGPCnt;
- KeyValueInfo.KeyStatus = LOOP;
- Write_RingStr(&Key_Value,(unsigned char *)&KeyValueInfo,sizeof(KeyValue_Info));
- }
- }
-
- Get_keyChooseTimeCnt[i] = 0;
- }
- Get_keyValueX[i] = Get_keyValue[i];
- }
-
- //组合检测
- for(i = 0; i < KeyGPCnt;i++)
- {
- if(KeyGpInfo[i].useFlg == KEYGP_USE)
- {
- if(KeyGpInfo[i].GetKeyNumStrict == KEYSTRICT_OFF)
- CheckKeyGP_NoStrict((KeyGroup_Num)i,KeyGpInfo[i]);
- else if(KeyGpInfo[i].GetKeyNumStrict == KEYSTRICT_ON)
- CheckKeyGP_Strict((KeyGroup_Num)i,KeyGpInfo[i]);
- }
- if(Get_keyGPStatusX[i] == KEYGP_OFF && Get_keyGPStatus[i] == KEYGP_ON)
- {
- #if DEBUG_PRINTF
- //printf("组合 %d已触发\r\n",(unsigned char)i+1);
- #endif
- KeyValueInfo.KeyNum = (Key_Num)KeyCnt;
- KeyValueInfo.KeyGpNum = (KeyGroup_Num)i;
- KeyValueInfo.KeyStatus = 0xFF;
- Write_RingStr(&Key_Value,(unsigned char *)&KeyValueInfo,sizeof(KeyValue_Info));
- //完成一次组合,重置键值。
- KeyGPStatusValueNow[i] = 0;
- }
- Get_keyGPStatusX[i] = Get_keyGPStatus[i];
- }
- }
- /* TIM1 init PWM function */
- void MX_TIM1_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 60-1;
- htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED2;
- htim1.Init.Period = 1000;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 500;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
- sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
- {
- Error_Handler();
- }
- sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
- sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
- sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
- sBreakDeadTimeConfig.DeadTime = 0;
- sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
- sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
- sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
- if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_TIM_MspPostInit(&htim1);
- }
复制代码 全部代码51hei下载地址:
STM32F103C8T6.7z
(205.19 KB, 下载次数: 107)
|