C-14.Timer 1 - Normal mode

Timer1 mode 0 (Normal)

สร้างสัญญาณ สี่เหลี่ยมออกทางพอร์ท B

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

.plot V(PB0)

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

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

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

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

// Interrupt handler example for INT0

//

ISR(TIMER1_OVF_vect)

{

PORTB = ~PORTB; //Complement PortB

TCNT1 = 60000; //Preload Counter

}

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

// Main program

//

int main (void)

{

DDRB = 0b11111111; // All outputs

TCCR1A = 0; // initialize timer1

TCCR1B = 0; // mode 0

TCCR1B |= (1 << CS10); // no prescaler

TCNT1 = 60000; // preload timer 60000

TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt

PORTB = 0xff; // Set port B =1111 1111

sei(); // Global Interrupt

while(1)

{

}

}