嵌入式系统——基于Keil5的STM32固件库寄存器编程实现流水灯

//PF9-LED0,PF10-LED1
//PF13-LED2,PF14-LED3
#define rRCCAHB1CLKEN *((volatile unsigned long *) 0x40023830) 

#define rGPIOF_MODER *((volatile unsigned long *) 0x40021400)
#define rGPIOE_MODER *((volatile unsigned long *) 0x40021000)
	
#define rGPIOF_OTYPER *((volatile unsigned long *) 0x40021404)
#define rGPIOE_OTYPER *((volatile unsigned long *) 0x40021004)

#define rGPIOF_OSPEEDR *((volatile unsigned long *) 0x40021408)
#define rGPIOE_OSPEEDR *((volatile unsigned long *) 0x40021008)

#define rGPIOF_PUPDR *((volatile unsigned long *) 0x4002140C)
#define rGPIOE_PUPDR *((volatile unsigned long *) 0x4002100C)

#define rGPIOF_ODR *((volatile unsigned long *) 0x40021414)
#define rGPIOE_ODR *((volatile unsigned long *) 0x40021014)

#define time 0x300000
#define on 1
#define off 0
void led_init(){
	//GPIOE -- rRCCAHB1CLKEN[4]   GPIOF -- rRCCAHB1CLKEN[5]
	rRCCAHB1CLKEN |= (1<<4)|(1<<5);
	
	//MODER-OUT--01 PF9,PF10 [21:18]<------- 0101
	unsigned long r_value;
	r_value = rGPIOF_MODER;//read
	r_value &= ~(0xf << 18);//clear
	r_value |= (1 << 18)|(1 << 20);
	rGPIOF_MODER = r_value;//write
	
	//MODER-OUT--01 PF13,PF14 [29:26]<------- 0101
	r_value = rGPIOE_MODER;//read
	r_value &= ~(0xf << 26);//clear
	r_value |= (1 << 26)|(1 << 28);
	rGPIOE_MODER = r_value;//write
	
	//OTYPER-PP--0 PF9,PF10 [10:9]<--------00
	r_value = rGPIOF_OTYPER;//read
	r_value &= ~(0x3 << 9);//clear
	rGPIOF_OTYPER = r_value;//write
	
	//OTYPER-PP--0 PF13,PF14 [14:13]<--------00
	r_value = rGPIOE_OTYPER;//read
	r_value &= ~(0x3 << 13);//clear
	rGPIOE_OTYPER = r_value;//write
	
	//OSPEEDR- PF9,PF10 [21:18]<-------1010
	r_value = rGPIOF_OSPEEDR;//read
	r_value &= ~(0xf << 18);//clear
	r_value |= (1 << 19)|(1 << 21);
	rGPIOF_OSPEEDR = r_value;//write
	
	//OSPEEDR- PF13,PF14 [29:26]<-------1010
	r_value = rGPIOE_OSPEEDR;//read
	r_value &= ~(0xf << 26);//clear
	r_value |= (1 << 27)|(1 << 29);
	rGPIOE_OSPEEDR = r_value;//write
	
	//PUPDR PF9,PF10 [21:18]<--------0000
	r_value = rGPIOF_PUPDR;//read
	r_value &= ~(0xf << 18);//clear
	rGPIOF_PUPDR = r_value;//write
	
	//PUPDR PF13,PF14 [29:26]<--------0000
	r_value = rGPIOE_PUPDR;//read
	r_value &= ~(0xf << 26);//clear
	rGPIOE_PUPDR = r_value;//write
	
	//ODR PF9,PF10 [10:9]<-------11
	r_value = rGPIOF_ODR;//read
	r_value |= (1<<9)|(1<<10);//clear
	rGPIOF_ODR = r_value;//write
	
	//ODR PF13,PF14 [14:13]<-------11
	r_value = rGPIOE_ODR;//read
	r_value |= (1<<13)|(1<<14);//clear
	rGPIOE_ODR = r_value;//write
}

void led0_ctr(int is_on){
	unsigned long r_value;
	r_value = rGPIOF_ODR;
	r_value |= (1<<9);//clear
	if(is_on) r_value &= ~(1<<9);
	rGPIOF_ODR = r_value;
}

void led1_ctr(int is_on){
	unsigned long r_value;
	r_value = rGPIOF_ODR;
	r_value |= (1<<10);//clear
	if(is_on) r_value &= ~(1<<10);
	rGPIOF_ODR = r_value;
}

void led2_ctr(int is_on){
	unsigned long r_value;
	r_value = rGPIOE_ODR;
	r_value |= (1<<13);//clear
	if(is_on) r_value &= ~(1<<13);
	rGPIOE_ODR = r_value;
}

void led3_ctr(int is_on){
	unsigned long r_value;
	r_value = rGPIOE_ODR;
	r_value |= (1<<14);//clear
	if(is_on) r_value &= ~(1<<14);
	rGPIOE_ODR = r_value;
}

void ledAll_ctr1(int flag){
	unsigned long r_value;
	if(flag == 0){//LED0 off, LED1 off, LED2 off, LED3 off
		led0_ctr(off);
		led1_ctr(off);
		led2_ctr(off);
		led3_ctr(off);
	}
	if(flag == 1){//LED0 on, LED1 off, LED2 off, LED3 off
		led0_ctr(on);
		led1_ctr(off);
		led2_ctr(off);
		led3_ctr(off);
	}
	else if(flag == 2){//LED0 on, LED1 on, LED2 off, LED3 off
		led0_ctr(on);
		led1_ctr(on);
		led2_ctr(off);
		led3_ctr(off);
	}
	else if(flag == 3){//LED0 on, LED1 on, LED2 on, LED3 off
		led0_ctr(on);
		led1_ctr(on);
		led2_ctr(on);
		led3_ctr(off);
	}
	else if(flag == 4){//LED0 on, LED1 on, LED2 on, LED3 on
		led0_ctr(on);
		led1_ctr(on);
		led2_ctr(on);
		led3_ctr(on);
	}
}

void delay(int v){
	while(v--);
}

int main(){
	led_init();
	unsigned int flag = 0;
	while(1){
		for (int i=0;i<5;i++){
			ledAll_ctr1(i);
			delay(time);
		}
		for (int i=3;i>=0;i--){
			ledAll_ctr1(i);
			delay(time);
		}
	}
}

上一篇:keil5工程重命名


下一篇:【keil5】安装及注册