概述
- 在工程中增加gpio lib.h和stm32头文件,如下:
#include “stm32f10x.h” // Device header
#include “stm32f10x_gpio.h” 调用lib配置gpio工作方式,先开启时钟,选GPIO,设置GPIO mode,gpio speed,主要看GPIO_InitTypeDef 结构体,芯片不同结构体不同,开始不清楚具体使用可以参考系统自带的example code,my setting如下:
void GPIO_Initmain(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure all unused GPIO port pins in Analog Input mode (floating input
trigger OFF), this will reduce the power consumption and increase the device
immunity against EMI/EMC ***************************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}- 初始化后,操作GPIO,GPIO操作可以参考gpio的lib,调用lib实现gpio操作,如下:
/**增加一个delay
- @brief Inserts a delay time.
- @param nCount: specifies the delay time length.
- @retval None
*/
void Delay(__IO u32 nCount)
{
for(; nCount != 0; nCount–);
}
int main(void)
{
GPIO_Initmain(); //初始化
while (1)
{
/* Turn on LD1 */
GPIO_ResetBits(GPIOD,GPIO_Pin_2); // IO 口拉低
GPIO_SetBits(GPIOD,GPIO_Pin_3); // IO 口拉高
GPIO_SetBits(GPIOD,GPIO_Pin_4);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_3);
GPIO_SetBits(GPIOD,GPIO_Pin_4);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_SetBits(GPIOD,GPIO_Pin_3);
GPIO_ResetBits(GPIOD,GPIO_Pin_4);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
}
}
4.编译后down load运行,GPIO操作成功。
最后
以上就是聪慧小霸王为你收集整理的stm32 起步 gpio操作的全部内容,希望文章能够帮你解决stm32 起步 gpio操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复