Nama : Wahyu Pratama NIM : 13116053 Tugas : Sistem Mikroprosesor Soal 1.Buat program mode normal timer-conter1 dengan konfigurasi masing-masing indunidu berbeda! 2.Buat program fast PWM dengan konfigurasi masing-masing indunidu berbeda! 3.Buat program output compare match (mode CTC) dengan konfigurasi masing-masing indunidu berbeda! Program No.1 timer-counter1 (mode Normal) #include <mega8535.h> #include <stdio.h> void main(void) { // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: kHz // Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge TCCR1A=0x00; TCCR1B=0x04; ------------------> prescaler 256 TCNT1H=0xC2; ------------------> nilai didapat dari rumus ...... TCNT1L=0xF7; ------------------> ....agar Timer 1 bernilai 1 detik
NO.2 Fast PWM #include <mega8535.h> #include <delay.h> void main(void){ unsigned int temp; PORTC=0x00; DDRC=0x00; PORTD=0x00; DDRD=0x30; // PD4 (OC1B) dan PD5 (OC1A) sebagai output TCCR1A=0x82; // PWM, Phase correct TCCR1B=0x12; // Clock
TCNT1=0x0000; ICR1H=0xFF; // Resolusi phase correct PWM 16 bit ICR1L=0xFF; OCR1A=0x0000; temp=0x0FF0; // Nilai awal PWM while (1){ if(PINC.1==0){ delay_ms(10); temp+=0xFF; if(temp==0x00FE) temp=0xFFFF;//mode fast pwm } OCR1A=temp; }; }
NO.3 Mode CTC (Output Compare Match) #include
void init_tim(void); void Delay(void); int main(void) { DDRA=0xFF; PORTA = 0x00; init_tim(); while (1) { PORTA = 0x0F; Delay(); PORTA = 0xF0; Delay(); } return 0; } void init_tim(void) { //mode operasi CTC, sumber clock prescaller 256 TCCR1A = 0; TCCR1B = 0b00001100; //overflow interrupt disable TIMSK|=(0<
{ //overflow interrupt enable TIMSK|=_BV(TOIE1);//(1<