BASIC4MCU | 질문게시판 | 답변 : atmega128a 스탑워치의 빠르기 정확히 제어
페이지 정보
작성자 master 작성일2022-12-01 19:57 조회398회 댓글0건본문
안녕하세요. 저번에 큰 도움 주셔서 정말 감사합니다.
atmega128a 로 이것저것 해보고있는 학생인데 코딩외에도 공부 해야될게 꽤나 있네요...특히나 data sheet 를 보고 인터럽트를 제어하는게 어렵네요...
이번에 질문드릴 내용은 시간의 정확한 제어 입니다.
현재 목표는 파일 1의 LCD에 스톱워치를 구현하는게 목표입니다.
현재 인터럽트를 건들지 않고 코드의 진하게 표시된 부분의 숫자(10과8) 야매로 건드려서 최대한 정확한 시간에 맞게 구현해 보았습니다...
하지만 절대 정확한 시간은 아니지요...
도대체 어디를 보고 계산을 해야할 지 모르겠습니다.
정확한 계산까지 바라진 않습니다. 어떻게 하는지만 좀 알고싶습니다ㅠㅠ (인터럽트를 제어해야하는건지 함수를 고치면 되는지)
작성한 코드들과 atmega128a의 데이타시트를 올려놨습니다.
항상 감사합니다.
//
//stopwatch.c 입니다.
#include "Stopwatch.h"
//
uint8_t StopWatchState; //글로별 변수는 .c 파일에다 넣는다 .h 말고
uint16_t StopWatchCounter;
uint16_t StopWatchMsecCounter;
//
uint16_t StopWatch_GetMsec(){ return StopWatchMsecCounter; }
//
void StopWatch_IncMsec(){
if(StopWatchMsecCounter<50)FND_ColonOn(); else FND_ColonOff();
StopWatchMsecCounter++;
}
//
void StopWatch_ResetMsec(){ StopWatchMsecCounter = 0; }
//
uint16_t StopWatch_GetCounter(){ return StopWatchCounter; }
//
void StopWatch_IncCounter(){ //0.1sec counter
if(StopWatch_GetMsec()<10) return;
StopWatch_ResetMsec();
StopWatchCounter=(StopWatchCounter+8)%10000;
//FND_ColonOn(); _delay_ms(50); FND_ColonOff(); _delay_ms(50);
}
//
void StopWatch_ResetCounter(){
StopWatchCounter=0;
StopWatch_ResetMsec();
}
//
void StopWatch_StopCounter(){}
//
void StopWatch_SetState(uint8_t state){ StopWatchState=state; } //set , get 세트로 활용 (외우기)
//
uint8_t StopWatch_GetState(){ return StopWatchState; }
//
void StopWatch_Init(){
Button_Init();
FND_Init();
TIM_Init();
StopWatch_SetState(STOP); //시작상태를 stop상태로 놓는다.
StopWatch_ResetCounter;
StopWatch_ResetMsec();
}
//
void StopWatch_Excute(){
StopWatch_EventProcess();
StopWatch_StateProcess();
}
//
void StopWatch_EventProcess(){ //FSM에서 밖으로 나가는 화살표를 실행하는 코드
switch(StopWatch_GetState()){
case STOP: StopWatch_StopStateEventProcess(); break;
case RUN: StopWatch_RunStateEventProcess(); break;
case RESET: StopWatch_ResetStateEventProcess(); break;
}
}
//
void StopWatch_StopStateEventProcess(){
if (Button_GetState1()==ACT_PUSH){ StopWatch_SetState(RUN); }
else if(Button_GetState2()==ACT_PUSH){ StopWatch_SetState(RESET); }
}
//
void StopWatch_RunStateEventProcess(){
if(Button_GetState1()==ACT_PUSH){ StopWatch_SetState(STOP); }
}
//
void StopWatch_ResetStateEventProcess(){ StopWatch_SetState(STOP); }
//
void StopWatch_StateProcess(){ //상태에 대한 실행코드
switch(StopWatch_GetState()){
case STOP: StopWatch_StopStateExcute(); break;
case RUN: StopWatch_RunStateExcute(); break;
case RESET: StopWatch_ResetStateExcute(); break;
}
}
//
void StopWatch_StopStateExcute(){
char buff[30];
StopWatch_StopCounter();
FND_Setnumber(StopWatch_GetCounter());
sprintf(buff,"%2d:%2d:%2d:%2d ",StopWatch_GetCounter()/100/60/60%60,StopWatch_GetCounter()/100/60%60,StopWatch_GetCounter()/100%60,StopWatch_GetCounter()%100);
LCD_WriteStringXY(0,0,"StopWatch stop");
LCD_WriteStringXY(1,0,buff);
}
//
void StopWatch_RunStateExcute(){
char buff[30];
StopWatch_IncCounter();
// StopWatchCounter=(StopWatchCounter+1)%10000;
FND_Setnumber(StopWatch_GetCounter());
sprintf(buff,"%2d:%2d:%2d:%2d ",
StopWatch_GetCounter()/100/60/60%60, //시
StopWatch_GetCounter()/100/60%60, //분
StopWatch_GetCounter()/100%60, //초
StopWatch_GetCounter()%100); //10ms
LCD_WriteStringXY(0,0,"StopWatch run ");
LCD_WriteStringXY(1,0,buff);
}
//
void StopWatch_ResetStateExcute(){
char buff[30];
StopWatch_ResetCounter();
FND_Setnumber(StopWatch_GetCounter());
sprintf(buff,"%2d:%2d:%2d:%2d ",StopWatch_GetCounter()/100/60/60%60,StopWatch_GetCounter()/100/60%60,StopWatch_GetCounter()/100%60,StopWatch_GetCounter()%100);
LCD_WriteStringXY(1,0,buff);
}
변수명이 너무 길어서 별 것 아닌데 굉장히 복잡해보입니다.
불필요한 연산이 너무 많습니다.
PC 데스크탑 정도라면 128보다 적어도 백만배 정도는 빠를테니 문제가 전혀 없지만 128은 고속의 시스템이 아닙니다.
프로젝트에 따라선 us단위의 코드 실행 시간도 계측해야 하기도 하죠
https://cafe.naver.com/circuitsmanual/30422
이 예제에서 인터럽트만 가져와보죠
//==================================================================
interrupt [TIM1_COMPA] void timer1_compa_isr(void){ //10msec 주기
//시계
mSec++;
if(mSec>99){ mSec=0; Sec++; }
if(Sec >59){ Sec=0; Min++; }
if(Min >59){ Min=0; Hour++; }
if(Hour>23)Hour=0;
//스톱워치 RUN
if(Smode==Start){
S_mSec++;
if(S_mSec>99){ S_mSec=0; S_Sec++; }
if(S_Sec >59){ S_Sec=0; S_Min++; }
if(S_Min >59)S_Min=0;
}
}
CTC비교매치 인터럽트는 누적오차가 없으며
X-TAL 정밀도를 가집니다.
댓글 0
조회수 398등록된 댓글이 없습니다.