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

