1. 項目:使用stm32寄存器點亮LED, 分别點亮紅、綠、藍3個燈。
2. 步驟
- 先建立個檔案夾儲存項目
- 再建立項目
- 将startup_stm32f10x_hd.s拷貝到該檔案夾下
- 建立main.c子項目
- 再次在檔案夾中建立stm32f10.h檔案
- 編寫main.c,将stm32f10.h導入編譯
- 編寫stm32f10.h子項目
3. 代碼
main.c
#include "stm32f10x.h"
int main(void)
{
//開啟GPIOB端口時鐘
RCC_APB2ENR |= (1<< 3);
//清空控制PB0的端口位
GPIOB_CRL &= ~(0xf << (4*0));
//配置PB0為通用推挽輸出,速度為10M
GPIOB_CRL |= (1<<4*0);
//PB0輸出低電平
GPIOB_ODR |= (0<<0);
// while(1);
}
//函數為空目的是為了騙過編譯器不報錯
void SystemInit(void)
{
}
stm32f10.h
//清單3:代碼清單:點亮LED-3外設位址定義
/*片上外設基位址*/
#define PERIPH_BASE ((unsigned int )0x40000000)
/*總線基位址,GPIO 都挂載到APB2上*/
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
/*AHB系統總線,APB1,APB2總線都挂載到AHB系統總線上*/
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
/*GPIOB外設基位址*/
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0c00)
/*GPIOB寄存器位址,強制轉換成指針*/
#define GPIOB_CRL *(unsigned int*)(GPIOB_BASE + 0x00)
#define GPIOB_CRH *(unsigned int*)(GPIOB_BASE + 0x04)
#define GPIOB_IDR *(unsigned int*)(GPIOB_BASE + 0x08)
#define GPIOB_ODR *(unsigned int*)(GPIOB_BASE + 0x0c)
#define GPIOB_BSRR *(unsigned int*)(GPIOB_BASE + 0x10)
#define GPIOB_BRR *(unsigned int*)(GPIOB_BASE + 0x14)
#define GPIOB_LCKR *(unsigned int*)(GPIOB_BASE + 0x18)
/*RCC外設基位址*/
#define RCC_BASE (AHBPERIPH_BASE + 0x1000)
/*RCC的AHB1時鐘使能寄存器位址,強制轉換成指針*/
#define RCC_APB2ENR *(unsigned int*)(RCC_BASE + 0x18)
4. 這裡我使用的時野火的指南者開發闆,點亮的綠燈,如果需要點亮紅藍燈,隻需要修改main.c 中的PB1, PB5端口設定即可。
5. 參考圖檔
