0
ATMega164PAのTimer0でPWMを使用してLEDの輝度を上げようとしています。下のコードを実行した後、LEDは発光したままで、輝度は変わりません。ATMega164PAを使用したPWM
私のコードを見て、私に教えてください私が間違っているのものがある場合:
#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
}
デューティサイクルを100%に達するまで10%増やそうとしています。これが起こると、私はデューティサイクルの値を0に戻してリピートしたいと思います。 私はコード内の整数値を変更すると出力を得るだけですが、メイン内部の計算は無視され、OCR0Aはデューティサイクルの整数に割り当てられているようです。 あなたが提案した変更を加えたが、まだ運がない:( – LoneCoder