使用GPIOA与GPIOB实现流水灯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 宏定义,定义了各个寄存器的地址和偏移量
#define GPIOA_CRL (*(volatile unsigned*)(0X40010800+0x000))
#define GPIOA_CRH (*(volatile unsigned*)(0X40010800+0x004))
#define GPIOA_ODR (*(volatile unsigned*)(0X40010800+0x00C))
#define GPIOB_CRL (*(volatile unsigned*)(0x40010C00+0x000))
#define GPIOB_CRH (*(volatile unsigned*)(0x40010C00+0x004))
#define GPIOB_ODR (*(volatile unsigned*)(0x40010C00+0x00C))
#define RCC_APB2ENR (*(volatile unsigned*)(0x40021000+0x018))

// 延时函数,i为延时的时钟周期数
void delay(unsigned int i){
while(i--);
}

int main(void){
// 使能GPIOB和GPIOA时钟
RCC_APB2ENR|=1<<3;
RCC_APB2ENR|=1<<2;

// 初始化GPIOA和GPIOB的控制寄存器
GPIOA_CRL&=~(0xFFFFFFFF);
GPIOA_CRH&=~(0xFFFFFFFF);
GPIOA_CRL|=0x33333333;
GPIOA_CRH|=0x33333333;

GPIOB_CRL&=~(0xFFFFFFFF);
GPIOB_CRH&=~(0xFFFFFFFF);
GPIOB_CRL|=0x33333333;
GPIOB_CRH|=0x33333333;

int i = 0;

while(1){
// 用循环控制16个LED在GPIOA和GPIOB之间闪烁
for(i = 0;i<16;i++){
delay(50000);
GPIOA_ODR^=1<<i;
}
for(i = 0;i<16;i++){
delay(50000);
GPIOB_ODR^=1<<i;
}
}
}

https://pic.lingjun.life/Qexo/2023/4/图片_2b684ae41769418cdc63bce36c444599.png

键控数码管

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#define GPIOB_CRL (*(volatile unsigned int*)(0x40010C00+0x00))
#define GPIOB_IDR (*(volatile unsigned int*)(0x40010C00+0x08))
#define GPIOC_CRL (*(volatile unsigned int*)(0x40011000+0x00))
#define GPIOC_CRH (*(volatile unsigned int*)(0x40011000+0x04))
#define GPIOC_ODR (*(volatile unsigned int*)(0x40011000+0x0C))
#define RCC_APB2ENR (*(volatile unsigned int*)(0x40021000+0x18))


unsigned int num[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
void delay(int iterations) {
for (int i=0; i<iterations; i++) {
asm("nop");
}
}

int main(void){

int value = 0;

RCC_APB2ENR|=1<<3;
RCC_APB2ENR|=1<<4;

GPIOB_CRL &= ~0x0000000F;
GPIOB_CRL |= 0x00000008;

GPIOC_CRL &= ~(0xFFFF);
GPIOC_CRH &= ~((0xF<<20)|(0xF<<16));
GPIOC_CRL |= 0x33333333;

while(1){
if(!(GPIOB_IDR & 0x01)){
delay(20);
if(!(GPIOB_IDR & 0x01)){
value++;
while(!(GPIOB_IDR & 0x01));
}
}
if(!(GPIOB_IDR & 0x02)){
delay(20);
if(!(GPIOB_IDR & 0x02)){
value--;
while(!(GPIOB_IDR & 0x02));
}
}
if(value>9){
value = 0;
}
if(value<0){
value = 9;
}
GPIOC_ODR = (~num[value]) & 0x7F;
}
}

https://pic.lingjun.life/Qexo/2023/5/图片_bcc7ffbb99a752cfa49bbcc7d475bf76.png