C-11. Timer 0 - PWM mode

สร้างสัญญาณ PWM ที่มี Duty cycle 20% ออกทาง OC0A (PD6)

1. สร้างโปรเจคใหม่ และแก้ไขโมเด็ลของ MICRO เพื่อให้จำลองการทำงาน TIMER 0 ของ ATmega168 microcontroller ได้ ให้เปลี่ยน

จาก .MICRO "ATmega168" เป็น .MICRO "ATmega168b"

และเพื่อตรวจจับสัญญาณที่ขา PD6 ให้เพิ่มคำสั่งจำลองการทำงานของออสซิลโลสโคป ด้วยคำสั่ง

.plot V(PD6)

2. เขียนโปรแกรม

#include <avr\io.h> // Most basic include files

#include <avr\interrupt.h> // Add the necessary ones

#include <avr\signal.h> // here

// ***********************************************************

// Main program

//

int main (void)

{

DDRD = 0b11111111; // All outputs

TCCR0A = 0b10000001; // Mode = PWM

TCCR0B = 0b00000011; // Clock/64, Normal phase

OCR0A = 51; // Duty cycle 20%

PORTD = 0xff; //PWM Output on PD6 = OC0A

while(1)

{

}

}