1、下载CubeMX,STM32CubeMX - STM32Cube initialization code generator - STMicroelectronics?
2、运行CubeMX
3、选择使用的MCU型号,这里选择的MCU是STM32F105RCT6?
?4、点Start Project开始下一步
?
?5、选择Pinout & Configuration里的System Core里的RCC,选择使用外部高速时钟
?
?6、在Clock Configuration中配置时钟,外部晶振选25MHz,HCLK选72MHz
?
?7、再回到Pinout & Configuration里,进入GPIO,配置芯片引脚,将PB3-PB9和PA15配置为输出模式,默认为点电平
?
还使用了定时器TIM4,配置方法如下
??8、设置工程的参数,选择工程保存的路径,我们后面使用keil的编译器,这里选择MDK-ARM
?
我习惯每个外设产生一个文件
?
?9、最后再点击“GENERATE CODE”,就可以生成代码了
?
?10、使用Keil软件打开生成的工程文件,还需要添加一些代码,在gpio.c文件中需要添加LED的控制函数。注意代码的添加一定要在?/* USER CODE BEGIN 2 */ 和?/* USER CODE END 2 */之间,否则下次在CubeMX中更改硬件配置后重新生成的代码将丢掉用户补充的代码。
/* USER CODE BEGIN 2 */
void LEDWork(unsigned char Status)
{
if(Status & 0x01)
HAL_GPIO_WritePin(LED1_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED1_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
if(Status & 0x02)
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
if(Status & 0x04)
HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
if(Status & 0x08)
HAL_GPIO_WritePin(LED1_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED1_GPIO_Port, LED3_Pin, GPIO_PIN_SET);
if(Status & 0x10)
HAL_GPIO_WritePin(LED1_GPIO_Port, LED4_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED1_GPIO_Port, LED4_Pin, GPIO_PIN_SET);
if(Status & 0x20)
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_SET);
if(Status & 0x40)
HAL_GPIO_WritePin(LED1_GPIO_Port, LED6_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED1_GPIO_Port, LED6_Pin, GPIO_PIN_SET);
if(Status & 0x80)
HAL_GPIO_WritePin(LED7_GPIO_Port, LED7_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(LED7_GPIO_Port, LED7_Pin, GPIO_PIN_SET);
}
/* USER CODE END 2 */
?11、因为LED控制引脚有PB4和PA13原本是JTAG的引脚,我们这是用作LED的控制引脚,需要禁能JTAG,使用函数? __HAL_AFIO_REMAP_SWJ_NOJTAG(); 禁用JTAG之前还需要调用__HAL_RCC_AFIO_CLK_ENABLE();
int main(void)
{
/* USER CODE BEGIN 1 */
unsigned char Index = 0;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
__HAL_RCC_AFIO_CLK_ENABLE();
/* 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_TIM4_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
__HAL_AFIO_REMAP_SWJ_NOJTAG(); //禁用JTAG
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(Counter1000ms >= 500)
{
Counter1000ms = 0;
LEDWork(1 << Index);
if(++Index >= 8) Index = 0;
}
}
/* USER CODE END 3 */
}
12、定时器补充代码如下,在初始化函数使能中断
/* TIM4 init function */
void MX_TIM4_Init(void)
{
/* USER CODE BEGIN TIM4_Init 0 */
/* USER CODE END TIM4_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM4_Init 1 */
/* USER CODE END TIM4_Init 1 */
htim4.Instance = TIM4;
htim4.Init.Prescaler = 72-1;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 1000-1;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM4_Init 2 */
HAL_TIM_Base_Start_IT(&htim4);//以中断方式启动定时器
/* USER CODE END TIM4_Init 2 */
}
补充中断回调函数,每1ms,Counter1000ms变量+1
/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(++Counter1000ms > 10000) Counter1000ms = 0;
}
/* USER CODE END 1 */
13、编译后通过JLink下载,然后就可以运行了
?14、最后的运行效果
|