질문게시판 > 답변 : atmega128a 스탑워치의 빠르기 정확히 제어

TODAY108 TOTAL2,582,187
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


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 정밀도를 가집니다.

 

  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기

댓글 0

조회수 398

등록된 댓글이 없습니다.

질문게시판HOME > 질문게시판 목록

MCU, AVR, 아두이노 등 전자공학에 관련된 질문을 무료회원가입 후 작성해주시면 전문가가 답변해드립니다.
ATMEGA128PWMLED초음파
아두이노AVR블루투스LCD
UART모터적외선ATMEGA
전체 스위치 센서
질문게시판 목록
제목 작성자 작성일 조회
공지 MCU, AVR, 아두이노 등 전자공학에 관련된 질문은 질문게시판에서만 작성 가능합니다. 스태프 19-01-15 17079
공지 사이트 이용 안내댓글[28] master 17-10-29 33544
질문 sd카드 이미지 전송댓글[1] 새글 던지기마스터 23-03-24 12
질문 초음파센서를 이용한 모터제어 질문있습니다.댓글[3] 이미지새글첨부파일 뉴비에오 23-03-24 22
질문 부품 관련 문의댓글[1] 새글 타코 23-03-23 17
질문 모터 작동 시, 일부 모듈이 꺼지는 현상에 대해 질문드립니다. 이미지 그렇찌 23-03-22 22
답변 답변글 답변 : 모터 작동 시, 일부 모듈이 꺼지는 현상에 대해 질문드립니다. 이미지새글 master 23-03-23 15
질문 ESP로 원격 모터제어를 할 수 있나용?댓글[1] 비차 23-03-22 18
질문 HC06을 마스터로 설정하면 HC06끼리만 통신되나요?댓글[4] 비차 23-03-21 27
질문 아두이노 제어하기댓글[3] 매드닥터 23-03-19 44
질문 atmega128 압력센서 관련 질문입니다댓글[1] 공린이 23-03-19 53
질문 아두이노 스텝모터 각도제어 질문댓글[3] master 23-03-17 58
질문 아두이노와 파이썬(중점좌표) 시리얼 통신 질문드립니다.댓글[1] 이미지첨부파일 아두이노어렵잖아 23-03-16 41
질문 아두이노 3LED 버튼 관련 질문댓글[1] 이츤 23-03-16 42
질문 ATmega328p로 pca9685를 이용하여 서보모터(mg996r, mg90s)를 pwm로 제어 중 떨림댓글[1] 이미지첨부파일 토비 23-03-15 46
질문 hm-10 다중연결 질문있습니다.댓글[1] 첨부파일 뉴비에오 23-03-14 39
질문 while loop 실행 속도댓글[1] 412904 23-03-12 62
질문 AND 연산 질문드립니다.댓글[1] 412904 23-03-11 67
질문 아두이노 테트리스 게임댓글[1] 첨부파일 지욱애 23-03-02 150
질문 아두이노우노 블루투스 2개 연결댓글[1] dsfadfs 23-02-28 103
질문 스텝모터 코딩 질문입니다..댓글[2] 무야더싱 23-02-25 127
질문 아두이노 나노33 IOT 의 BLE 연결 문제 이미지 가루밀 23-02-24 72
질문 220V AC를 직류로 바꾸면 건전지 5V 건전지 55개를 대체할 수 있는건가요?댓글[1] hghh 23-02-22 93
질문 HM-10 모듈 사용하는데 질문있습니다.댓글[2] 뉴비에오 23-02-21 128
질문 아두이노 블루투스 관련 질문입니다. 생물공학도 23-02-21 99
답변 답변글 답변 : 아두이노 블루투스 관련 질문입니다. master 23-02-21 99
질문 자율주행 로봇을 만드는데 질문있습니다.댓글[2] 뉴비에오 23-02-18 161
질문 블루투스에서 아두이노로 신호전달에 대하여(2)댓글[1] 이미지첨부파일 밍경 23-02-14 190
질문 블루투스에서 아두이노로 신호전달에 대하여 밍경 23-02-13 140
답변 답변글 답변 : 블루투스에서 아두이노로 신호전달에 대하여 master 23-02-14 115
게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기