一、程序开发方式
程序开发一般有两种方式,一种方式是用C语言操作寄存器,文件架构简单,一个启动文件,一个C语言文件直接进行操作,它不依赖其他的文件。第二种方式是使用库开发,比如hal库,它会有一堆的库文件。
这些库文件怎么来的?在STM提供的资源中,使用STM32 cubeMX去生成代码,然后再使用cubeIDE进行代码编辑+编译,接着进行文件烧写+监控(cube Monitor)。
二、创建工程
创建工程有2种,即自定义创建工程和使用STM32cubeMX。其中,前者适用于定制化程度高,干净简练,缺点是前期配置难度高,需要一定的经验。在开发用户手册里面,第七章介绍创建工程模板,可以看出步骤比较多。后者创建工程优点是上手难度低,可视化操作方便,缺点是必须按照它的规则来编写代码,代码注释很多,还不能随便删除。可以先用STM32cube MX来创建工程,这样简便快捷。
值得注意的是,创建工程时,所选路径千万不要有中文。我们在main函数中编写代码。
STM32cubeMX可以通过软件包进行安装。
三、相关接口:
1.HAL_GPIO_WritePin接口
用于控制GPIO,它有三个参数,第一个参数GPIOx表示端口,范围A~G,第二个参数GPIO_Pin表示具体那个引脚,如GPIO_PIN_1,第三个参数PinState表示pin状态,如SET/RESET。实例如HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET)是把GPIOA的pin1设置为低电平,一般用此引脚控制三极管的导通截止,从而让三极管连接的LED等亮灭。
2.HAL_Delay接口
用于延时,比如用于等待硬件操作稳定或者让硬件保持状态工作一段时间。实例如HAL_Delay(1000)表示延时1000ms即1s时间。
/**
* @brief Sets or clears the selected data port bit.
*
* @note This function uses GPIOx_BSRR register to allow atomic read/modify
* accesses. In this way, there is no risk of an IRQ occurring between
* the read and the modify access.
*
* @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
* @param GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* @param PinState: specifies the value to be written to the selected bit.
* This parameter can be one of the GPIO_PinState enum values:
* @arg GPIO_PIN_RESET: to clear the port pin
* @arg GPIO_PIN_SET: to set the port pin
* @retval None
*/
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(GPIO_Pin));
assert_param(IS_GPIO_PIN_ACTION(PinState));
if (PinState != GPIO_PIN_RESET)
{
GPIOx->BSRR = GPIO_Pin;
}
else
{
GPIOx->BSRR = (uint32_t)GPIO_Pin << 16u;
}
}
[参考:百问网(www.100ask.net)]