(AVR) 1 Nút Nhấn Nhiều Chức Năng - CodeVisionAVR

Trong bài viết này mình sẽ viết chương trình sử dụng 1 nút nhấn 2 chúc năng với ATMEGA16

Sơ đồ mô phỏng

Sơ đồ giải thuật

Code

/*******************************************************
Chip type : ATmega16 Program type : Application AVR Core Clock frequency: 8.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *******************************************************/#include <mega16.h> #define BT PINA.0 #define LED1 PORTB.0 #define LED2 PORTB.1 unsigned char count_T0,count;interrupt [TIM0_OVF] void timer0_ovf_isr(void){ TCNT0 = 130; // luu gia tri dem count_T0++;}void main(void){DDRA = 0x00;PORTA = 0xff;DDRB = 0xff;PORTB = 0x00;TCCR0 = (1<<CS01)|(1<<CS00); //prescaler = 64 TCNT0 = 130; // x= 0.001(1ms) * 8000000 / 64 = 125 // TCNT0 = 255 - 125 = 130 TIMSK |= (1<<TOIE0); // mode Normal, cho phép ngat #asm ("sei") while (1) { if (!BT) { count = 0; do { if (count_T0 > 200) { count++; count_T0 = 0; } } while ((count < 5) && (!BT)); if (count == 5) { LED1 = ~LED1; while(!BT); } else { LED2 = ~LED2; } } }}

Demo