×
单片机 > 单片机程序设计 > 详情

stm32 起步 gpio操作

发布时间:2020-09-03 发布时间:
|

在工程中增加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操作成功。




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
单片机中高阻态的实质及意义