Embedded Programming (AVR)/Atmega 128A (실습)
궁금증 정리 노트 - Atmega128A
유로 청년
2024. 5. 25. 22:13
※ 정리 할 내용이 추가 될 수 있음.
- Q) 왜 #define F_CPU 16000000UL를 작성해야 하며, 이는 어떤 의미를 갖는가?
- define 전처리기를 이용하여 16000000UL를 F_CPU로 정의한 이유는
avr studio에 내장된 delay 함수를 사용하기 위해 클럭을 재정의하는 것이다.
#define F_CPU 16000000UL
#include <util/delay.h>
- delay.h 헤더파일에서 클럭의 기본 설정은 아래와 같이 클럭을 1000000UL, 1MHz로 설정된 상태이다.
#ifndef F_CPU
/* prevent compiler error by supplying a default */
# warning "F_CPU not defined for <util/delay.h>"
/** \ingroup util_delay
\def F_CPU
\brief CPU frequency in Hz
The macro F_CPU specifies the CPU frequency to be considered by
the delay macros. This macro is normally supplied by the
environment (e.g. from within a project header, or the project's
Makefile). The value 1 MHz here is only provided as a "vanilla"
fallback if no such user-provided definition could be found.
In terms of the delay functions, the CPU frequency can be given as
a floating-point constant (e.g. 3.6864E6 for 3.6864 MHz).
However, the macros in <util/setbaud.h> require it to be an
integer value.
*/
# define F_CPU 1000000UL
#endif
- 위 util/delay.h 헤더파일의 일부분이며, 기본적으로 F_CPU가 1000000UL로 치환된 것을 확인 가능
- 16000000UL : 16MHz, 1000000UL : 1MHz
- Q) 클럭을 재정의 하지 않는다면 어떤 현상이 발생하는가?
즉, 클럭 값을 1MHz으로 사용했을 경우와 16MHz으로 사용했을 때의 차이점은 무엇인가?
- Atmega128A은 16MHz의 클럭으로 동작시킨다.
- #define F_CPU 16000000UL으로 치환하면 _delay_ms(1000)은 1000ms, 1초가 소요
- #define F_CPU 1000000UL으로 치환하면 _delay_ms(1000)은 1000ms / 16 = 62ms, 0.062초가 소요
- 따라서 delay 함수 이용시 우리가 원하는 시간에 정확하게 동작하게 설계하기 위해서
MCU의 동작 클럭을 확인하고, 그에 맞게 F_CPU를 재치환할 필요가 있다.