【嵌入式系统实验】2 跑马灯实验

实验2 跑马灯实验

1 开发环境

  • STM32F407ZGT6
  • Keil uVision 5

2 实验内容

  • 建立如下函数:
  1. led_left()函数,实现led灯左移,右边灯亮,然后右边灯灭的同时左边灯亮,然后全部灭掉。循环次数由函数入口参数决定。
  2. led_right()函数,实现led灯右移,左边灯亮,然后左边灯灭的同时右边灯亮,然后全部灭掉。循环次数由函数入口参数决定。
  3. led_blink()函数,实现led灯闪烁,哪一个灯闪,闪的次数,都由函数入口参数决定。
  4. led_bcd()函数,实现0-3的数值的显示,比如0-00,1-01,2-10,3-11,显示的数值由函数入口参数决定。
  • 函数写好后,在main()中分别进行调用演示。
[main.c]
int main(void)
{
    
     
	delay_init(168);		//延时初始化 
	LED_Init();				//LED初始化
	
	led_left(3);			//LED右移3次
	led_right(3);			//LED左移3次
	
	led_blink(0,0,3);		//LED0和LED1不闪烁,3次
	led_blink(0,1,3);		//LED0不闪烁,LED1闪烁3次
	led_blink(1,0,3);		//LED0闪烁3次,LED1不闪烁
	led_blink(1,1,3);		//LED0和LED1同时闪烁3次
	
	led_bcd(0);				//显示0——00
	delay_ms(1000);
	led_bcd(1);				//显示1——01
	delay_ms(1000);
	led_bcd(2);				//显示2——10
	delay_ms(1000);
	led_bcd(3); 			//显示3——11

	while(1);
}

3 实验代码

[main.c]

void led_left(int times){
    
    
	int t=0;
	while(t<times)
	{
    
    
		GPIO_ResetBits(GPIOF,GPIO_Pin_10);
		GPIO_SetBits(GPIOF,GPIO_Pin_9);
		delay_ms(500);
		GPIO_ResetBits(GPIOF,GPIO_Pin_9);
		GPIO_SetBits(GPIOF,GPIO_Pin_10);
		delay_ms(500);
		GPIO_SetBits(GPIOF,GPIO_Pin_9);
		delay_ms(500);
		t++;
	}
}
void led_right(int times){
    
    
	int t=0;
	while(t<times)
	{
    
    
		GPIO_ResetBits(GPIOF,GPIO_Pin_9);
		GPIO_SetBits(GPIOF,GPIO_Pin_10);
		delay_ms(500);
		GPIO_ResetBits(GPIOF,GPIO_Pin_10);
		GPIO_SetBits(GPIOF,GPIO_Pin_9);
		delay_ms(500);
		GPIO_SetBits(GPIOF,GPIO_Pin_10);
		delay_ms(500);
		t++;
	}
}
void led_blink(int pick_9,int pick_10,int times){
    
    
/*注释部分为异步闪烁*/
//	if(pick_9){
    
    
//		int t=0;
//		while(t<times){
    
    
//			GPIO_ResetBits(GPIOF,GPIO_Pin_9);
//			delay_ms(500);
//			GPIO_SetBits(GPIOF,GPIO_Pin_9);
//			delay_ms(500);
//			t++;
//		}
//	}
//	if(pick_10){
    
    
//		int t=0;
//		while(t<times){
    
    
//			GPIO_ResetBits(GPIOF,GPIO_Pin_10);
//			delay_ms(500);
//			GPIO_SetBits(GPIOF,GPIO_Pin_10);
//			delay_ms(500);
//			t++;
//		}
//	}

	if(pick_9&&pick_10){
    
    
		int t=0;
		while(t<times){
    
    
			GPIO_ResetBits(GPIOF,GPIO_Pin_9);
			GPIO_ResetBits(GPIOF,GPIO_Pin_10);
			delay_ms(500);
			GPIO_SetBits(GPIOF,GPIO_Pin_9);
			GPIO_SetBits(GPIOF,GPIO_Pin_10);
			delay_ms(500);
			t++;
		}
	}
	else{
    
    
		if(pick_9){
    
    
			int t=0;
			while(t<times){
    
    
				GPIO_ResetBits(GPIOF,GPIO_Pin_9);
				delay_ms(500);
				GPIO_SetBits(GPIOF,GPIO_Pin_9);
				delay_ms(500);
				t++;
			}
		}
		else if(pick_10){
    
    
			int t=0;
			while(t<times){
    
    
				GPIO_ResetBits(GPIOF,GPIO_Pin_10);
				delay_ms(500);
				GPIO_SetBits(GPIOF,GPIO_Pin_10);
				delay_ms(500);
				t++;
			}
		}
		else{
    
    
			int t=0;
			while(t<times){
    
    
				GPIO_SetBits(GPIOF,GPIO_Pin_10);
				delay_ms(500);
				GPIO_SetBits(GPIOF,GPIO_Pin_10);
				delay_ms(500);
				t++;
			}
		}
	}
}
void led_bcd(int num){
    
    
	switch(num){
    
    
		case 0:
		default:
			GPIO_SetBits(GPIOF,GPIO_Pin_9);
			GPIO_SetBits(GPIOF,GPIO_Pin_10);
			break;
		case 1:
			GPIO_SetBits(GPIOF,GPIO_Pin_9);
			GPIO_ResetBits(GPIOF,GPIO_Pin_10);
			break;
		case 2:
			GPIO_ResetBits(GPIOF,GPIO_Pin_9);
			GPIO_SetBits(GPIOF,GPIO_Pin_10);
			break;
		case 3:
			GPIO_ResetBits(GPIOF,GPIO_Pin_9);
			GPIO_ResetBits(GPIOF,GPIO_Pin_10);
			break;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_44714521/article/details/108703814