AVR 上使用延迟的软件 PWM

发布于 2025-01-01 09:36:30 字数 461 浏览 2 评论 0原文

我正在尝试使用 AVR ATMega16 上的延迟来完成简单的 PWM 生成

我正在尝试做的只是将 LED 占空比从 100% 淡入淡出到 0%,稍后我将扩展它以控制双色 LED 的两种颜色有很好的效果。

我以前用 PIC 在 mikroC 中完成过此操作,但不知何故,在 avr-gcc 中它并不那么容易,或者也许我遗漏了一些东西。

我的问题是我希望 LED 在 3 秒内完全消失,但我感觉它的消失速度更快。我还认为,它在高占空比上花费的时间比在低占空比上花费的时间更多。我的意思是,我看到 LED 亮了一会儿,然后它就消失了,但是当它变得暗淡时,它会非常快地移动到不同的工作周期,所以我想......

你对我所缺少的有什么想法吗?

代码可在此处获取:http://ideone.com/lUP5f

谢谢

I am trying to accomplish a simple PWM generation using delays on an AVR ATMega16

What i am trying to do is simply fade a led from 100% to 0% duty cycle, and later i will expand it to control two colors of a bicolor led to have a nice effect.

I have done this in mikroC with PICs before, but somehow it's not as easy in avr-gcc, or maybe i am missing something.

My problem here is that i want the led to fade completely in 3 seconds, but i feel its fading faster. I also think that it's spending more time on high duty cycles than on low ones. I mean that i see the led bright for a bit, then it fades, but when it becomes dim it moves to different duty cycles very fast or so i think..

Any ideas you might have on what i am missing ?

Code is available here : http://ideone.com/lUP5f

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的背包 2025-01-08 09:36:30

可能是保险丝设置有问题。时钟通常由熔丝控制,而不是由定义控制。然而,延迟功能使用#define F_CPU。可以贴一下你的保险丝设置吗?

您想要使用延迟而不是计时器来生成 PWM 是否有特定原因?如果您想了解计时器,我可以向您推荐这些教程:1)定时器教程 2)PWM

编辑:btw

 uint8_t b = 1000;

uint8_t 允许值从 0 到 255编译器通常会为此生成警告:

../test.c:16:警告:大整数隐式截断为无符号类型

It may be a problem with the fuse settings. The clock is usually controlled by fuses not by the define. However the delay function uses the #define F_CPU. Can you post your fuse settings?

Is there a specific reason that you want to use delay instead of a timer to generate the PWM? In case you want to have a look at timers I can recommend these Tutorials to you:1)Timer Tutorial 2)PWM

Edit: btw

 uint8_t b = 1000;

uint8_t allows values from 0 to 255 the compiler usually generates a warning for this:

../test.c:16: warning: large integer implicitly truncated to unsigned type

吻安 2025-01-08 09:36:30

问题是 LED 不是线性的。
因此,将它们打开(例如以 50% 将其亮度降低到 50%)是行不通的。

您可以使用预定义的表(例如此处:http://www.mikrocontroller.net/articles/LED-Fading#Das_Demoprogramm

您不需要能够阅读德语。只是研究这段代码..
有一些表,例如 16 位 PWM (pwmtable_16[])。

所以你可以写:

delay_us(pwmtable_16[a]);

而不是

delay_us(a);

The problem is, that LEDs are not linear.
So it doesn't work to turn them on for example with 50% to reduce their brightness to 50%..

You can use a predefinied table (for example here: http://www.mikrocontroller.net/articles/LED-Fading#Das_Demoprogramm)

You don't need to be able to read german. Just study this piece of code..
There are some tables for example for 16 bit PWM (pwmtable_16[]).

So you can write:

delay_us(pwmtable_16[a]);

instead of

delay_us(a);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文