LED灯1连接到GPIOA9引脚LED灯2连接到GPIOA10引脚 编写void LED_Initvoid函数对LED GPIO引脚进行初始化 编写 void WaterFlow_LEDuint16_t xms函数实现流水灯程序: 其中参数 xms表示延时时间单位为ms delay_msxms表示延时函数调用在本题中直接调用。 选做:尝试变换流水灯延迟周期实现系统上电时两个LED灯以2秒为周期亮1
void LED_Init(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void WaterFlow_LED(uint16_t xms) { static uint8_t led_state = 0; static uint32_t delay_time = 0; static float period = 2000.0; // 周期初始值为2秒 static float period_step = -100.0; // 周期递减步长为0.1秒
if (delay_time >= period / 2) {
led_state = !led_state;
GPIO_WriteBit(GPIOA, GPIO_Pin_9, led_state ? Bit_SET : Bit_RESET);
GPIO_WriteBit(GPIOA, GPIO_Pin_10, !led_state ? Bit_SET : Bit_RESET);
delay_time = 0;
}
delay_ms(xms);
delay_time += xms;
if (period_step < 0 && period <= 200.0) {
period_step = 100.0;
}
if (period_step > 0 && period >= 2000.0) {
period_step = -100.0;
}
period += period_step;
原文地址: https://www.cveoy.top/t/topic/cSwg 著作权归作者所有。请勿转载和采集!