1. 項目:使用stm32寄存器點亮LED, 分別點亮紅、綠、藍3個燈。
2. 代碼:
只需要編寫main.c程序,stm3210x.h程序為空(只需要新建即可)。
2.1 點亮綠燈main.c程序
#include "stm32f10x.h"
int main(void)
{
//打開GPIOB端口的時鍾
*(unsigned int * )0x40021018 |= ((1)<<3); //置位1
//配置IO口為輸出,設置PB0輸出低電平,點亮綠燈
*(unsigned int * )0x40010c00 |= ((1)<<(4*0)); //置位1
//控制ODR寄存器
*(unsigned int *)0x40010c0c &= ~(1<<0); //清0
}
void SystemInit(void)
{
//函數體為空給,為了騙過編譯器不報錯
}

2.2 點亮紅燈main.c程序
這里只需要修改port口PB5輸出低電平即可
#include "stm32f10x.h"
int main(void)
{
//打開GPIOB端口的時鍾
*(unsigned int * )0x40021018 |= ((1)<<3); //置位1
//配置IO口為輸出,設置PB5輸出低電平,點亮紅燈
*(unsigned int*)0x40010c00 |= ((1)<<(4*5)); //置位1
//控制ODR寄存器
*(unsigned int *)0x40010c0c &= ~(1<<0); //清0
}
void SystemInit(void)
{
//函數體為空給,為了騙過編譯器不報錯
}
2.3 點亮藍燈main.c程序
#include "stm32f10x.h"
int main(void)
{
//打開GPIOB端口的時鍾
*(unsigned int * )0x40021018 |= ((1)<<3); //置位1
//配置IO口為輸出,設置PB1輸出低電平,點亮藍燈
*(unsigned int*)0x40010c00 |= ((1)<<(4*1)); //置位1
//控制ODR寄存器
*(unsigned int *)0x40010c0c &= ~(1<<0); //清0
}
void SystemInit(void)
{
//函數體為空給,為了騙過編譯器不報錯
}
3. 思路總結
stm32點亮LED需要3步:
(1)打開GPIO端口時鍾
(2)配置GPIO工作模式/速度,輸入/輸出
(3)控制GPIO輸出高低電平



