通过寄存器方式和库函数方式使指定LED灯闪烁
1.库函数点灯
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出模式(高低电平均可驱动)
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOG,&GPIO_InitStructure);
while(1)
{
GPIO_ResetBits(GPIOG,GPIO_Pin_6);
GPIO_ResetBits(GPIOG,GPIO_Pin_8);
Delay_ms(10);
GPIO_SetBits(GPIOG,GPIO_Pin_6);
GPIO_SetBits(GPIOG,GPIO_Pin_8);
Delay_ms(10);
}
}
2.寄存器点灯
(1)io.h文件
#ifndef __IO_H
#define __IO_H
#define uint32_t unsigned int
#define uint8_t unsigned char
typedef struct
{
uint32_t CRL;
uint32_t CRH;
uint32_t IDR;
uint32_t ODR;
uint32_t BSR;
uint32_t BSRR;
uint32_t LCKR;
}GPIO_REG;
#define PIN(n) n
#define GPIO_APB1 0x40000000
#define GPIO_APB2 0x40021018
#define RCC_APB2ENR *(uint32_t*)(GPIO_APB2)
#define GPIOA_BASE 0x40010800
#define GPIOB_BASE (GPIOA_BASE + 0x00000400)
#define GPIOC_BASE (GPIOB_BASE + 0x00000400)
#define GPIOD_BASE (GPIOC_BASE + 0x00000400)
#define GPIOE_BASE (GPIOD_BASE + 0x00000400)
#define GPIOF_BASE (GPIOE_BASE + 0x00000400)
#define GPIOG_BASE (GPIOF_BASE + 0x00000400)
#define GPIOA ((GPIO_REG *)GPIOA_BASE) //指向这个GPIOA_BASE的地址,将它赋给结构体的首地址
#define GPIOB ((GPIO_REG *)GPIOB_BASE)
#define GPIOC ((GPIO_REG *)GPIOC_BASE)
#define GPIOD ((GPIO_REG *)GPIOD_BASE)
#define GPIOE ((GPIO_REG *)GPIOE_BASE)
#define GPIOF ((GPIO_REG *)GPIOF_BASE)
#define GPIOG ((GPIO_REG *)GPIOG_BASE)
typedef enum
{
GPIO_SPEED_2MHz = 2,
GPIO_SPEED_10MHz = 1,
GPIO_SPEED_50MHz = 3,
}GPIO_SPEED;
typedef enum
{
PIN_0 = 0,
PIN_1,
PIN_2,
PIN_3,
PIN_4,
PIN_5,
PIN_6,
PIN_7,
PIN_8,
PIN_9,
PIN_10,
PIN_11,
PIN_12,
PIN_13,
PIN_14,
PIN_15,
}PIN_NUM;
typedef enum
{
GPIO_MODE_AIN = 0x10,
GPIO_MODE_IPU = 0x58,
GPIO_MODE_IPD = 0x98,
GPIO_MODE_IN_FLOATING = 0x14,
GPIO_MODE_OUT_OD = 0x24,
GPIO_MODE_OUT_PP = 0x20,
GPIO_MODE_AF_OD = 0x2c,
GPIO_MODE_AF_PP = 0x28,
}GPIO_MODE;
typedef struct
{
GPIO_MODE MODE;
PIN_NUM PIN;
GPIO_SPEED SPEED;
}GPIO_PARA;
#define GPIO_MODE_PP 0x3
#define GPIO
void GPIO_Init1(GPIO_REG *GPIOx,GPIO_PARA *GPIO_PARA);
void GPIO_Set(GPIO_REG *GPIOx,uint8_t Pin_x,uint8_t mode);
#endif
(2)main.c文件
#include "io.h"
void GPIO_Init1(GPIO_REG *GPIOx,GPIO_PARA *GPIO_PARA) //传参为结构体指针类型,指向其首地址
{
uint32_t mode;
RCC_APB2ENR |= 1 << 7;
mode = GPIO_PARA->MODE&0x0f;
if(GPIO_PARA->MODE & (1 << 5))
mode |= GPIO_PARA->SPEED;
if(GPIO_PARA -> PIN < 8)
{
GPIOx -> CRL &= ~(0xf << (4 * GPIO_PARA -> PIN));
GPIOx -> CRL |= GPIO_PARA -> MODE << (4 * GPIO_PARA -> PIN);
}
else
{
GPIOx -> CRH &= ~(0xf << 4 * (GPIO_PARA -> PIN - 8));
GPIOx -> CRH |= GPIO_PARA -> MODE << 4 * (GPIO_PARA -> PIN - 8);
}
if(GPIO_PARA->MODE & (1<<6))
{
GPIOx -> ODR |= 1 << GPIO_PARA ->PIN;
}
if(GPIO_PARA->MODE & (1<<7))
{
GPIOx -> ODR &= ~(1 << GPIO_PARA ->PIN);
}
}
void GPIO_Set(GPIO_REG *GPIOx,uint8_t Pin_x,uint8_t mode)
{
if(mode)
{
GPIOx -> ODR |= 1 << Pin_x;
}
else
{
GPIOx -> ODR &= ~(1 << Pin_x);
}
}
主函数中,调用封装好的寄存器操作函数实现LED闪烁:
int main(void)
{
GPIO_PARA GPIO_Init_Struct;
GPIO_Init_Struct.MODE = GPIO_MODE_PP;
GPIO_Init_Struct.PIN = PIN_8;
GPIO_Init_Struct.SPEED = GPIO_SPEED_50MHz;
GPIO_Init1(GPIOG,&GPIO_Init_Struct);
while(1)
{
GPIO_Set(GPIOG,8,1);
Delay_ms(500);
GPIO_Set(GPIOG,8,0);
Delay_ms(500);
}
}