微控制器 01 STM32 LED燈

2022-07-22 13:57:23 字數 4450 閱讀 7186

標頭檔案 stm32f10x.h

1#ifndef __stm32f10x_h

2#define __stm32f10x_h34

//用來存放stm32暫存器對映的**56

//外設 perirhral78

#define periph_base ((unsigned int)0x40000000)

9#define apb1periph_base periph_base

10#define apb2periph_base (periph_base + 0x10000)

11#define ahbperiph_base (periph_base + 0x20000)

1213

14#define rcc_base (ahbperiph_base + 0x1000)

15#define gpiob_base (apb2periph_base + 0x0c00)

1617

18#define rcc_apb2enr *(unsigned int*)(rcc_base + 0x18)

1920

#define gpiob_crl *(unsigned int*)(gpiob_base + 0x00)

21#define gpiob_crh *(unsigned int*)(gpiob_base + 0x04)

22#define gpiob_idr *(unsigned int*)(gpiob_base + 0x08)

23#define gpiob_odr *(unsigned int*)(gpiob_base + 0x0c)

24#define gpiob_bsrr *(unsigned int*)(gpiob_base + 0x10)

25#define gpiob_brr *(unsigned int*)(gpiob_base + 0x14)

26#define gpiob_lckr *(unsigned int*)(gpiob_base + 0x18)

2728 typedef unsigned int

uint32_t;

29 typedef unsigned short

uint16_t;

3031 typedef struct

32gpio_typedef;

4142

43 typedef struct

44rcc_typedef;

5657

58#define gpiob ((gpio_typedef*)gpiob_base)

59#define rcc ((rcc_typedef*)rcc_base)

6061

#endif /* __stm32f10x_h */

標頭檔案 stm32f10x_gpio.h

1

#ifndef __stm32f10x_h

2#define __stm32f10x_h34

//用來存放stm32暫存器對映的**56

//外設 perirhral78

#define periph_base ((unsigned int)0x40000000)

9#define apb1periph_base periph_base

10#define apb2periph_base (periph_base + 0x10000)

11#define ahbperiph_base (periph_base + 0x20000)

1213

14#define rcc_base (ahbperiph_base + 0x1000)

15#define gpiob_base (apb2periph_base + 0x0c00)

1617

18#define rcc_apb2enr *(unsigned int*)(rcc_base + 0x18)

1920

#define gpiob_crl *(unsigned int*)(gpiob_base + 0x00)

21#define gpiob_crh *(unsigned int*)(gpiob_base + 0x04)

22#define gpiob_idr *(unsigned int*)(gpiob_base + 0x08)

23#define gpiob_odr *(unsigned int*)(gpiob_base + 0x0c)

24#define gpiob_bsrr *(unsigned int*)(gpiob_base + 0x10)

25#define gpiob_brr *(unsigned int*)(gpiob_base + 0x14)

26#define gpiob_lckr *(unsigned int*)(gpiob_base + 0x18)

2728 typedef unsigned int

uint32_t;

29 typedef unsigned short

uint16_t;

3031 typedef struct

32gpio_typedef;

4142

43 typedef struct

44rcc_typedef;

5657

58#define gpiob ((gpio_typedef*)gpiob_base)

59#define rcc ((rcc_typedef*)rcc_base)

6061

#endif /* __stm32f10x_h */

main.c

1

//#if 023

//#include 45

//sbit led = p0^067

//void main(void)8//

1314

//#endif

1516

1718 #include "

stm32f10x.h

"19 #include "

stm32f10x_gpio.h"20

21//

#define led_g_gpio_port gpiob

22//

#define led_g_gpio_clk_enable (rcc->apb2enr |= ( (1) << 3 ))

23//

#define led_g_gpio_pin gpio_pin_0

2425

#define led_g_gpio_port gpiob

26#define led_g_gpio_clk_enable (rcc->apb2enr |= ( (1) << 3 ))

27#define led_g_gpio_pin gpio_pin_0

2829

void

delay(uint32_t count)

3033

34int main (void)35

103104

#endif

105}

106107

108void systeminit(void

)109

**來自野火

實現從 記憶體—暫存器—結構體+指標—初始化結構體外設函式的過渡。

課程完成了對於內部函式組成的講解

點led燈的基本流程

1、gpiob的埠時鐘

2、pin/mode/speed設定並輸入init函式

3、通過gpio_setbits()/gpio_rsestbits()完成引腳的開關

將關鍵內容通過巨集定義,使得接下來遷移更方便

#define led_g_gpio_port gpiob

#define led_g_gpio_clk_enable (rcc->apb2enr |= ( (1) << 3 ))

#define led_g_gpio_pin gpio_pin_0

微控制器LED燈

include void delay unsigned char i 使用迴圈語句,實現延時 void delay unsigned int i 延時函式,i最大取值65535 最大延時0.65535s void fun1 void 全部亮 void fun2 void 全部滅掉 void fun3...

微控制器 LED燈課後練習

在這裡插入 片1.點亮開發板上的led2,然後每隔200ms閃爍一次。include void delay unsigned int z void main 或者 include void delay unsigned int z sbit led2 p1 1 位定義,led2對應p1的1埠 voi...

藍橋杯微控制器複習01 LED燈的控制

led複習 我們看到這些led二極體有一端都接到了vcc 高電平 說明這些led是共陽極的,那如果我們想讓這些led亮,就需要把這些led的另一端置低電平。可是我們又看到了m74hc573鎖存器,原理如下 我們可以從上面的資訊知道當y4c為高電平時,我們可以改變p0口的值並隨之改變q1 q7的值,反...