stm32 leds(gpio控制)
stm32 leds
=====================================
1. 使用库函数实现led灯点亮
ps: led灯的pin脚接在stm32的PA11上
#include "SysConfig.h" #include "config.h" #include "imu.h" #include "Altitude.h" #include "CommApp.h" #include "CommPC.h" #include "ConfigTable.h" #include "IMUSO3.h" #include "control.h" #include "FailSafe.h" #include <stdio.h> #include "stm32f10x.h" int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //设置GPIOA使能 GPIO_InitTypeDef GPIO_InitStructure_1; // 配置GPIO GPIO_InitStructure_1.GPIO_Pin = GPIO_Pin_11; // USART3 Rx (PA.11) GPIO_InitStructure_1.GPIO_Mode = GPIO_Mode_Out_PP; // GPIO_Mode_Out_PP GPIO_InitStructure_1.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &GPIO_InitStructure_1); GPIO_WriteBit(GPIOA, GPIO_Pin_11, Bit_SET); return 0; }
2. 不使用库函数实现led灯点亮
#include "SysConfig.h" #include "config.h" #include "imu.h" #include "Altitude.h" #include "CommApp.h" #include "CommPC.h" #include "ConfigTable.h" #include "IMUSO3.h" #include "control.h" #include "FailSafe.h" #include <stdio.h> #include "stm32f10x.h" int main(void) { *(volatile unsigned long *)0x40021018 |= 1<<2; //2 for A, 3 for B,参考图1, 图3 *(volatile unsigned long *)0x40010804 = (2<<12) | (0<<14); //11属于高位寄存器,参考图2, 图4 *(volatile unsigned long *)0x4001080C = 1<<11; //11 拉高点亮, 参考图5 return 0; }
ps:
图1: STM32103X8B.pdf手册的(4.Memory mapping), RCC地址0x40021000,
APB2外设时钟使能寄存器(RCC_APB2ENR)偏移地址:0x18, 则: 0x40021000+0x18=0x40021018

图2: PA寄存器地址为0x40010800, PA11位于高寄存器的(12-15), 则: 0x40010804

图3 (PA11对应2)

图4 (PA11对应12-14)

图5: 由结构体可以看出, ODR为偏移地址0x0C, 则:0x40010800+0x0C=0x4001080C

具体,参考:http://www.cnblogs.com/TS-qrt/articles/stm32.html
五,stm32f407gpio控制输出
int main() { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOE, & GPIO_InitStruct); while(1) { GPIO_ResetBits(GPIOE,GPIO_Pin_All); //pull en low //GPIO_SetBits(GPIOE,GPIO_Pin_All); //pull en hight //GPIO_ResetBits(GPIOE,GPIO_Pin_All); //pull en low } }
浙公网安备 33010602011771号