Re: [问题] 让两颗LED用不同的频率闪烁

楼主: happierway (杰夫)   2017-08-05 01:24:27
本版第一PO就献给LED blink了 :P
我觉得在写嵌入式系统有个很重要的观念,就是让结果看起来是多工就好了。
解法一:闪烁的频率在你的PWM可以接受的范围内的话,
可以直接用两个PWM output控制 :)
解法二:用一个timer加上两个LED各自的state machine,以下是其中一种实作方法:
uint16_t led_1_counter = 0;
uint16_t led_2_counter = 0;
#define led_1_ON_threshold < LED_1 暗的时间 >
#define led_1_OFF_threshold < LED_1 亮的时间 > + led_1_ON_threshold
#define led_2_ON_threshold < LED_2 暗的时间 >
#define led_2_OFF_threshold < LED_2 亮的时间 > + led_2_ON_threshold
#define LED_ON 1
#define LED_OFF 0
uint8_t led_1_action = LED_OFF;
uint8_t led_2_action = LED_OFF;
void timer_interrupt_handler()
{
led_1_counter++;
led_2_counter++;
if(led_1_counter == led_1_ON_threshold) led_1_action = LED_ON;
if(led_1_counter >= led_1_OFF_threshold)
{
led_1_counter = 0;
led_1_action = LED_OFF;
}
if(led_2_counter == led_2_ON_threshold) led_2_action = LED_ON;
if(led_2_counter >= led_2_OFF_threshold)
{
led_2_counter = 0;
led_2_action = LED_OFF;
}
}
void main()
{
// 初始化你的 timer,timer中断时间的长短依据你的闪烁频率调整
initialize_timer_module(&timer_interrupt_handler);
while(1)
{
if(led_1_action == LED_ON) set_LED_ON(LED_1);
else if(led_1_action == LED_OFF) set_LED_OFF(LED_1);
if(led_2_action == LED_ON) set_LED_ON(LED_2);
else if(led_2_action == LED_OFF) set_LED_OFF(LED_2);
}
}
楼主: happierway (杰夫)   2017-08-05 01:30:00
作者: Lipraxde (Lipraxde)   2017-08-05 22:26:00
控制LED亮灭的部分可以放在中断里,回圈里休眠省电

Links booklink

Contact Us: admin [ a t ] ucptt.com