作者happierway (杰夫)
看板C_and_CPP
标题Re: [问题] 让两颗LED用不同的频率闪烁
时间Sat Aug 5 01:24:27 2017
本版第一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);
}
}
--
村上春树:『诗人在21岁死去,革命家和摇滚乐手在24岁死去。』
秋使人忧郁,连悲观的句子都能变成明日的准则。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 173.167.109.114
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1501867489.A.4F1.html
2F:推 Lipraxde: 控制LED亮灭的部分可以放在中断里,回圈里休眠省电 08/05 22:26