打印

STM32学习GPIO

[复制链接]
1509|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tier|  楼主 | 2012-7-18 23:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
昨天学了一下用嘀嗒定时器延时,现在开始第一个实验,控制I/O口的输出。学习51的时候,第一个实验就是点亮LED,现在stm32的第一个实验也要点亮LED。
首先,初始化所要用到的I/O。
第一步:声明一个结构体,名字是GPIO_InitStructure。
结构体原型由GPIO_InitTypeDef 确定:
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
GPIO_Init (GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)里面调用。
第二步:使能端口时钟,这步一定要注意
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOX, ENABLE); //使能PX端口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOX1|RCC_APB2Periph_GPIOX2, ENABLE); //使能PX1,PX1端口时钟,使能多个时钟的时候用
第三步:配置第一步结构体当中的内容吧
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PA.8 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
好了,要用的端口已经配置好了。现在开始输出:
GPIO_ResetBits(GPIOA,GPIO_Pin_8);//PA8输出低电平,灯灭。
GPIO_SetBits(GPIOA,GPIO_Pin_8); //PA8输出高电平,灯亮。
现在来写完整的函数:
LED源文件:
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PA.8 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
LED头文件:
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
void LED_Init(void);//初始化
#endif
主函数:
#include "led.h"
#include "delay.h" //等下控制LED时候要用到延时,就是调用上次说的Systick定时器做的延时函数。
int main(void)
{
SystemInit(); //系统时钟初始化为72M
delay_init(72); //延时函数初始化
LED_Init(); //LED端口初始化
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(300);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
}
}
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

31

帖子

0

粉丝