ATMega16 PWM (timer0)

Kali ini akan membahas mengenai Pulse With Modulator(PWM). Singkat cerita PWM bisa digunakan untuk mengendalikan nyala LED secara terang hingga redup, contoh penggunaan PWM lainnya adalah untuk mengatur kecepatan motor DC. Motor DC bisa diatur berputar cepat atau berputar lambat.

Gambar 1. PWM untuk mengatur kecerahan LED

program:

/*
 * servo.c
 * autor: avr.easyelektro.com
 * 2.000 MHz
 */
 
#include <io.h>
 
void main(void)
{
DDRD=0x00;
PORTD=0xff;
DDRB=0x08;
PORTB=0x08;
 
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 250,000 kHz
// Mode: Fast PWM top=0xFF
// OC0 output: Non-Inverted PWM
// Timer Period: 1,024 ms
// Output Pulse(s):
// OC0 Period: 1,024 ms Width: 0 us
TCCR0=(1<<WGM00) | (1<<COM01) | (0<<COM00) | (1<<WGM01) | (0<<CS02) | (1<<CS01) | (0<<CS00);
TCNT0=0x00;
OCR0=0x00;
 
while (1)
    {
    // Please write your application code here
    if (PIND.0==0){OCR0=0;}  //low-off
    if (PIND.1==0){OCR0=50;}
    if (PIND.2==0){OCR0=100;} 
    if (PIND.3==0){OCR0=140;}
    if (PIND.4==0){OCR0=170;}
    if (PIND.5==0){OCR0=200;}
    if (PIND.6==0){OCR0=220;}
    if (PIND.7==0){OCR0=255;} //high
    
    }
}