GPIO初始化代码:
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, LED4_Pin|MotoControlC_Pin|LED3_Pin|MotoControlCN_Pin
|LED2_Pin|MotoControlD_Pin|MotoControlDN_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, MotoControlA_Pin|MotoControlAN_Pin|MotoControlB_Pin|MotoControlBN_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : PCPin PCPin PCPin PCPin
PCPin PCPin PCPin */
GPIO_InitStruct.Pin = LED4_Pin|MotoControlC_Pin|LED3_Pin|MotoControlCN_Pin
|LED2_Pin|MotoControlD_Pin|MotoControlDN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PAPin PAPin PAPin PAPin */
GPIO_InitStruct.Pin = MotoControlA_Pin|MotoControlAN_Pin|MotoControlB_Pin|MotoControlBN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PB2 PB10 PB11 PB12
PB5 PB6 */
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12
|GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : PC10 PC11 PC12 */
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : PBPin PBPin PBPin */
GPIO_InitStruct.Pin = Key4_Pin|Key3_Pin|Key2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
main中的回调函数:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_8)
{
if(dir==0&&whiletime<1620)
{
HAL_Delay(10);
__HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_2,whiletime);
whiletime+=10;
}else
{
dir=1;
}
}else if(GPIO_Pin == GPIO_PIN_7)
{
if(dir==1&&whiletime>740)
{
HAL_Delay(10);
__HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_2,whiletime);
whiletime-=10;
}else
{
dir=0;
}
}
}
main函数:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_TIM8_Init();
/* USER CODE BEGIN 2 */
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,2000);
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,3000);
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,4000);
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,5000);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,9000);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,9000);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3,4000);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4,5000);
__HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_1,2000);
__HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_2,740);
__HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_3,4000);
__HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_4,5000);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_4);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4|GPIO_PIN_6,GPIO_PIN_RESET);
// HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4|GPIO_PIN_6,GPIO_PIN_SET);
// HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4|GPIO_PIN_6,GPIO_PIN_RESET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// if(dir==0&&whiletime<1620)
// {
// HAL_Delay(10);
// __HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_2,whiletime);
// whiletime+=10;
// }else
// {
// dir=1;
// }
// if(dir==1&&whiletime>740)
// {
// HAL_Delay(10);
// __HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_2,whiletime);
// whiletime-=10;
// }else
// {
// dir=0;
// }
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
时钟配置:
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
引脚浮空,按下按键接地
在中断函数上设置断点,按键没反应,求助?
一周热门 更多>