一、资料下载
1.1、开发板官网地址:https://www.st.com/zh/evaluation-tools/nucleo-c031c6.html
1.2、STM32CubeC0下载地址:https://www.st.com/zh/embedded-software/stm32cubec0.html
二、安装pack包
2.1、pack包下载地址:https://www.keil.arm.com/packs/stm32c0xx_dfp-keil/boards/
2.2、也可以在下载的STM32Cube_FW_C0_V1.1.0文件中找到
按照提示默认安装。
三、硬件部分
测试驱动开发板上的LD4灯,硬件电路部分如下:
LD4指示灯对应的IO口是PA5。
四、程序部分
4.1、led.c
include "main.h"
void init_led(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
LD4_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = LD4_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LD4_GPIO_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(LD4_GPIO_PORT, LD4_PIN, GPIO_PIN_SET);
led4_off();
}
4.2、led.h
#ifndef __LED_H
#define __LED_H
#define LD4_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)
#define LD4_GPIO_CLK_DISABLE() do{ __HAL_RCC_GPIOA_CLK_DISABLE(); }while(0)
#define LD4_GPIO_PORT GPIOA
#define LD4_PIN GPIO_PIN_5
#define led4_on() HAL_GPIO_WritePin(LD4_GPIO_PORT, LD4_PIN, GPIO_PIN_RESET)
#define led4_off() HAL_GPIO_WritePin(LD4_GPIO_PORT, LD4_PIN, GPIO_PIN_SET)
#define led4_tog() HAL_GPIO_TogglePin(LD4_GPIO_PORT, LD4_PIN)
void init_led(void);
#endif
4.3、main.c
#include "main.h"
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
init_led();
while (1)
{
led4_tog();
HAL_Delay(100);
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
while(1)
{
}
}
五、搭建开发环境
根据开发板的例程来创建开发工程文件,具体步骤不详细列了,创建后项目如下。
六、测试
编译项目后,下载到开发板后,LD4指示灯闪烁。
|