[ATMega128] 타이머/카운터

200ms마다 LED상태를 반전 시키는 코드
두가지 방법으로 overflow 방법과 ocm방식으로 코딩을..
실제적인 테스트는 아직이라서 확실하진 않지만.

// Timer/Counter Overflow

#include<avr/io.h>
#include<avr/interrupt.h>
#include<avr/signal.h>

#define OVERFLOW 256
#define CPU_CLOCK 16000000
#define TICKS_PER_SEC 1000
#define Prescaler 64

volatile unsigned int tic_time;

void init(void);

int main(void){
        init();
        sei(); // set enable interrupt
        for(;;){
                if(tic_time==200){
                        tic_time=0;     // tic_time 초기화
                        PORTF=~PORTF;   //200ms PORTF 상태 반전
                }
        }
        return 1;
}

void init(){
// 초기화 작업
        DDRF=0xFF;      // DDRF 방향 설정
        PORTF=0xFF;     // PORTF LED 초기화
        TCCR0=0x04;     // Prescaler 설정
        TCNT0= OVERFLOW - (CPU_CLOCK / TICKS_PER_SEC / Prescaler);      // 오버플로우에 사용될 초기값
        TIMSK=0x01;     // 오버플로우 인터럽트 허용
}

SIGNAL(SIG_OVERFLOW0){
 tic_time++; // 1000ms마다 생기는 오버 플로우를 카운팅함.
 TCNT0 = OVERFLOW - (CPU_CLOCK / TICKS_PER_SEC / Prescaler);    // 오버 플로우 한후에 TCNT0값을 초기화 시켜줌.
}

// Timer/Counter Output Compare Match

#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;

void init(void);

int main(void){
        init();
        sei(); // set enable interrupt
        for(;;){
                if(tic_time==200){
                        tic_time=0;     // tic_time 초기화
                        PORTF=~PORTF;   // PORTF상태 반전
                }
        }
        return 1;
}

void init(){
        DDRF=0xFF;      // DDRF 방향 설정
        PORTF=0xFF;     // PORTF LED 초기화
        TCCR0=0x1C;     // Prescaler 설정과 OC0 토글 사용
        TIMSK=0x02;     // OCM으로 설정
        TCNT0 = 0;      // TCNT0 0으로 초기화
        OCR0 = CPU_CLOCK / TICKS_PER_SEC / Prescaler - 1;
}

SIGNAL(SIG_OUTPUT_COMPARE0){
 tic_time++;
}


Posted by rCan

2008/09/10 21:45 2008/09/10 21:45
Response
No Trackback , No Comment
RSS :
http://rcan.net/rss/response/560

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

Leave a comment
« Previous : 1 : ... 40 : 41 : 42 : 43 : 44 : 45 : 46 : 47 : 48 : ... 441 : Next »

블로그 이미지

- rCan

Calendar

«   2012/05   »
    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 30 31    

Notices

  1. About Me

Site Stats

Total hits:
118622
Today:
49
Yesterday:
120