Anda di halaman 1dari 1

#include <mega8535.

h>
#include <delay.h>

void main (void)


{
unsigned int temp;
PORTC=0x00;
DDRC=0x00; //PORTC sebagai input
PORTD=0x00;
DDRD=0x30; //PD4 (OC1B) dan PD5 (OC1A) sebagai output

//inisialisasi Timer/Counter 1
//Clock source : System Clock/8
//Clock value: 500khz
//mode: phase correct PWM. TOP value = ICR1 value
//TCCR1A=0x82 -> Non-Inverted PWM mode
// OC1A output: clear OC1A on compare match when up counting
//set OC1A on compare match when down-counting
//OC1B otput: Disconnected (normal)

TCCR1A=0x82; //PWM, phase correct


TCCR1B=0x12; //Clock =(4Mhz/8)
TCNT1=0x0000;
ICR1H=0xFF; //resolusi phase correct PWM 16-bit
ICR1L=0xFF; //resolusi phase correct PWM={log(TOP+1)}/{log(2)}
// ={log(65535+1)}/{log(2)}
// =4.8165/0.301
// =16 bit
OCR1A=0x0000;

temp=0x0FF0; //nilai awal PWM


while (1)

{
if (PINC.0==0) //jika tombol terhubung dengan PC0
{ //ditekan maka motor semakin lambat
delay_ms(100); //berputar sampai berhenti
temp-=0xFF;
if (temp==0xFF01) temp=0x00;
}
if (PINC.1==0) //jika tombol yang terhubung dengan PC1
{ // ditekan maka motor semakin cepat
delay_ms(100); //berputar semapai berputar terus
temp+=0xFF;
if (temp==0x00FE) temp=0xFFFF;
}
OCR1A=temp;
};
}

Anda mungkin juga menyukai