[ATMEGA128] 키 입력하면 OCM

// 키를 눌렀을 때 500ms로 LED 깜빡이기
 
#include<avr/io.h>
#include<avr/interrupt.h>
#include<avr/signal.h>
 
#define CPU_CLOCK 16000000
#define TICKS_PER_SEC 1000
#define Prescaler 64
 
volatile unsigned int tic_time;
volatile unsigned char STATE_FLAG;
 
void init_device(void);
void init_exint(void);

int main(void){
        init_device();
        init_exint();
        sei(); // set enable interrupt
        for(;;){
                if(PINA!=0xFF){ // A포트 어느것에 입력이 들어오면...
                        if(STATE_FLAG==0){ // tic_time 0, LED반전
                                tic_time=0;
                                PORTF=~PORTF;
                        }
                        STATE_FLAG=1; // STATE_FLAG 1
                }
                if(STATE_FLAG==1){ // STATE_FLAG가 1일경우
                        if(tic_time>=500){
                                tic_time=0;
                                PORTF=~PORTF;
                        }
                }
        }
        return 1;
}
void init_device(){
        DDRF=0xFF; // F포트의 방향 설정 > 출력
        PORTF=0xFF; // LED 모두 off
        DDRA=0x00; // A포트 방향 설정 > 입력
}
void init_exint(){
        TCCR0=0x1C;     // Prescaler 설정과 OC0 토글 사용
        TCNT0 = 0;      // TCNT0 0으로 초기화
        OCR0 = CPU_CLOCK / TICKS_PER_SEC / Prescaler - 1;
        TIMSK=0x02;     // OCM 인터럽트 허용
}
SIGNAL(SIG_OUTPUT_COMPARE0){
 tic_time++;    // output compare인터럽트 발생시 카운팅
}

Posted by rCan

2008/09/17 21:30 2008/09/17 21:30
Response
No Trackback , No Comment
RSS :
http://rcan.net/rss/response/566

Trackback URL : http://rcan.net/trackback/566

Leave a comment
« Previous : 1 : ... 33 : 34 : 35 : 36 : 37 : 38 : 39 : 40 : 41 : ... 439 : Next »

블로그 이미지

- rCan

Calendar

«   2012/02   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Notices

  1. About Me

Site Stats

Total hits:
113576
Today:
7
Yesterday:
43