Timmer 0
1. สร้างโปรเจคใหม่ และเพิ่มคำสั่งจำลองการทำงานของ LED และ ออสซิลโลสโคป
D1 VDD D1_NODE
D2 VDD D2_NODE
D3 VDD D3_NODE
D4 VDD D4_NODE
D5 VDD D5_NODE
D6 VDD D6_NODE
D7 VDD D7_NODE
D8 VDD D8_NODE
R1 D1_NODE PB0 0.62K
R2 D2_NODE PB1 0.62K
R3 D3_NODE PB2 0.62K
R4 D4_NODE PB3 0.62K
R5 D5_NODE PB4 0.62K
R6 D6_NODE PB5 0.62K
R7 D7_NODE PB6 0.62K
R8 D8_NODE PB7 0.62K
.plot V(PD0)
2. เขียนโปรแกรม
#include <avr\io.h> // Most basic include files
#include <avr\interrupt.h> // Add the necessary ones
#include <avr\signal.h> // here
// Define here the global static variables
#define DataPort PORTB // Using PortC as our Dataport
#define DataDDR DDRB
#define PulsePort PORTD // Using PortC as our Dataport
#define PulseDDR DDRD
unsigned char temp,pulse;
// Interrupt handler example for INT0
//
SIGNAL(TIMER0_COMPA_vect) {
++temp;
TCNT0 = 0;
pulse=pulse^0x01;
}
// ***********************************************************
// Main program
//
int main(void) {
DataDDR = 0b11111111; // All outputs
PulseDDR = 0b11111111;
TIMSK0 = _BV(OCIE0A); // Enable Interrupt TimerCounter0 Compare Match A (SIG_OUTPUT_COMPARE0A)
TCCR0A = _BV(WGM01); // Mode = CTC
TCCR0B = _BV(CS02) | _BV(CS00); // Clock/1024, 0.001024 seconds per tick
OCR0A = 10; // 0.001024*244 ~= .25 SIG_OUTPUT_COMPARE0A will be
TCNT0 = 0;
sei();
temp = 0x01;
pulse = 0;
while(1) { // Infinite loop; define here the
DataPort = temp^0xff;
PulsePort = pulse;
}
}
ผลการทำงาน