boolean myflag1 = true; //ประกาศตัวแปรเป็นประเภทboolean
void setup() { // put your setup code here, to run once:
DDRB = 0xFF; // ให้ PORTB มีสถานะเป็น output
TCCR1A = 0; // ค่าเริ่มต้นเป็น timer1 (16 bits)
TCCR1B = 0; // ตั้งให้เป็น normal mode โดยดูจากตาราง
TCCR1B |= (1 << CS12); // ให้ prescaler เป็น 256
TCNT1 = 0x0; // รีเซ็ตการนับใหม่
TIMSK1 = 0; // ขัดการทำงาน
PORTB = 0xFF; //ให้ PORTB = 1111 1111
}
void loop() { // put your main code here, to run repeatedly:
if(TIFR1 & (1 << TOV1)){ // เช็ค timer1
TIFR1 = (1 << TOV1); // clear timer1 overflow flag
if(myflag1){
PORTB &= ~(1 <<PORTB7); // turn off PB7
}
else{
PORTB = (1 <<PORTB7); // turn on PB7
}
myflag1 =! myflag1; // สลับค่า flag
}
}