boolean myflag1 = true;
//กำหนด myflag1 ให้เก็บค่า1bit = true
void setup() {
// put your setup code here, to run once:
DDRB = 0xFF; // Set PORTB เป็น outputs
TCCR1A = 0; // initialize timer1
//TCCR1A=Timer/Counter 1 Control Register Aเริ่มต้น=0
TCCR1B = 0; // clear all bit
//TCCR1B Timer/Counter 1 Control Register Bเริ่มต้น=0
OCR1A = 7811.5; //กำหนดค่าความถี่8Hz Timer1
//จะนับจาก 0 จนถึง OCR1A ที่ค่าprescaler=256
TIMSK1 = 0; // disable all interrupts คำสั่งยกเลิกการinterrupts
TCNT1 = 0x0; // Reset Counter
TIFR1 = 0x6 ; // Clear interrupt flags ทั้งหมด จากB0-B6
TCCR1B |= (1 << WGM12); // set to CTC mode (look at Table 2-2)
TCCR1B |= (1 << CS12); // กำหนดค่าprescaler=256 (look at Table 2-1)
}
void loop() {
// put your main code here, to run repeatedly:
if(TIFR1 & (1 <<OCF1A)){ //เงื่อนไขตรวจสอบ interrupt & 1 <<OCF1A
//เมื่อTimer1 นับจนถึง OCR1Aที่ตั้งค่าไว้
TIFR1 = (1 << OCF1A); // ทำการ clear match interrupt
if(myflag1){//เมื่อ myflag1 = true ตามตามเงื่อนไข
PORTB &= ~(1 <<PORTB7); // turn off PB7
}//PORTB= PORTB7 & 1<<(เลื่อน1บิตไปทางซ้าย)
else{
PORTB = (1 <<PORTB7); // turn on PB7
}
myflag1 =! myflag1; // toggle flag
}
}