1.Proteus设计一个STM32最小系统板+LED流水灯实验原理图+仿真
STM32最小系统板+LED流水灯实验原理图
代码:
#define GPIOB_BASE 0x40010C00
#define GPIOC_BASE 0x40011000
#define GPIOA_BASE 0x40010800
#define RCC_APB2ENR (*(unsigned int *)0x40021018)
#define GPIOB_CRH (*(unsigned int *)0x40010C00)
#define GPIOC_CRH (*(unsigned int *)0x40011004)
#define GPIOA_CRL (*(unsigned int *)0x40010800)
#define GPIOB_ODR (*(unsigned int *)0x40010C0C)
#define GPIOC_ODR (*(unsigned int *)0x4001100C)
#define GPIOA_ODR (*(unsigned int *)0x4001080C)
void SystemInit(void);
void Delay_ms(volatile unsigned int);
void A_LED_LIGHT(void);
void B_LED_LIGHT(void);
void C_LED_LIGHT(void);
void Delay_ms( volatile unsigned int t)
{
unsigned int i;
while(t--)
for (i=0;i<800;i++);
}
void A_LED_LIGHT(){
GPIOA_ODR=0x0<<5; //PA5低电平
GPIOB_ODR=0x1<<0; //PB0高电平
GPIOC_ODR=0x1<<14; //PC14高电平
}
void B_LED_LIGHT(){
GPIOA_ODR=0x1<<5; //PA5高电平
GPIOB_ODR=0x0<<0; //PB0低电平
GPIOC_ODR=0x1<<14; //PC14高电平
}
void C_LED_LIGHT(){
GPIOA_ODR=0x1<<5; //PA5高电平
GPIOB_ODR=0x1<<0; //PB0高电平
GPIOC_ODR=0x0<<14; //PC14低电平
}
int main()
{
int j=100;
// 开启时钟
RCC_APB2ENR |= (1<<3); // 开启 GPIOB 时钟
RCC_APB2ENR |= (1<<4); // 开启 GPIOC 时钟
RCC_APB2ENR |= (1<<2); // 开启 GPIOA 时钟
// 设置 GPIO 为推挽输出
GPIOB_CRH&= 0xffffff0f; //设置位 清零
GPIOB_CRH|=0x00000020; //PB0推挽输出
GPIOC_CRH &= 0x0fffffff; //设置位 清零
GPIOC_CRH|=0x30000000; //PC14推挽输出
GPIOA_CRL &= 0xfff0ffff; //设置位 清零
GPIOA_CRL|=0x00010000; //PA5推挽输出
// 3个LED初始化为不亮(即高点位)
GPIOB_ODR |= (1<<0);
GPIOC_ODR |= (1<<14);
GPIOA_ODR |= (1<<5);
while(j)
{
B_LED_LIGHT();
Delay_ms(1000);
C_LED_LIGHT();
Delay_ms(1000);
A_LED_LIGHT();
Delay_ms(1000);
}
}
void SystemInit()
{
}
仿真
2.STM32F103C8T6下C语言寄存器方式实现led流水灯
设计思路:
我们通过初始化GPIO来实现点亮LED灯。通过找到GPIOA-5、GPIOB-0、GPIOC-14的地址,然后通过GPIO的初始化,控制寄存器将片IO口拉低拉高,输出高低电平,以控制LED灯灭。GPIOA的地址为0x40010800,GPIOB的地址为0x40010C00,GPIOC的地址为0x40011000。
实作:
20231004_221629