- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在 ATMega164PA 上使用带有 Timer0 的 PWM 来增加 LED 的亮度。在下面运行我的代码后,LED 保持发光状态并且不会改变其亮度。
请看一下我的代码,如果我做错了什么请告诉我:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
int dutycycle = 0; // Variable for dutycycle
/********************************************** MAIN ****************************************************/
int main(void)
{
DDRB |= (1 << PB3); // Make pins output and OC0A pin for PWM
TCCR0A |= (1 << COM0A1) | (1<<WGM01) | (1<<WGM00); // Clear OC0A on comare match and set OC0A at BOTTOM
TIMSK0 |= (1<<TOIE0); // Overflow Interrupt Enabled
TCNT0 = 0; // Set Counter Value Register for comparison with OCR0A
OCR0A = (dutycycle / 100) * 255; // Set duty cycle ON period
sei(); // Enable global interrupts
TCCR0B |= (1 << CS00); // Prescale of 1 - start timer
while (1)
{
_delay_ms(500);
dutycycle += 10; // increase duty cycle by 10% every 500ms
if (dutycycle > 100) // if duty cycle is greater than 100% set to 0
{
dutycycle = 0;
}
}
}
ISR(TIMER0_OVF_vect)
{
OCR0A = (dutycycle / 100) * 255; // Set duty cycle ON period
}
最佳答案
我不确定你的方法的逻辑,但我可以看到一个明显的问题给你带来了困难。
整数除法不产生分数。相反,它将结果向下舍入到最接近的整数。这意味着 dutycycle / 100
几乎总是 0,因为你确保 dutycycle <= 100
.所以OCR0A
几乎总是 0。一个异常(exception)是 dutycycle
正好是 100,它设置了 OCR0A
到 255。
解决此问题的一种方法是使用 OCR0A = dutycycle * 255 / 100;
反而。我不知道这是否会解决所有问题,只是我看到的第一个问题。
关于c - 带 ATMega164PA 的脉宽调制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42274642/
我想执行 BPSK 调制并用 Java 绘制调制信号。但我无法弄清楚。 我已经成功地对信号进行了编码,但是当我将载波和基带信号相乘然后绘制它时,我得到了一个尴尬的信号。 出于绘图目的,我使用了 JFr
我在 Octave 中使用以下代码来实现正弦消息和方波载波的频率调制。 %script to make a squarewave carrier modulated with a sinusoidal
我如何调制 Web Audio API 中的任何 AudioParams,例如使用低频振荡器的 GainNode 的增益值? 最佳答案 https://coderwall.com/p/h1jnmg v
我目前正在创建一个卫星地面站, future 几个月将用于控制我们的立方体卫星。使用的调制方案是 GFSK,波特率为 9600。在尝试通过直接连接流程图中的 tx 和 rx block 与卫星通信之前
我是一名优秀的程序员,十分优秀!