8bit Timer/Counter
지적은 언제나 환영하며 정확한 이해를 위해 datasheet를 통해 공부하길 권장합니다.
Timer/Counter는 애초에 거의 유사한 logic을 가지고 동작하며 어떻게 사용하는가에 따라 그 용도가 결정됩니다.
Timer/Counter를 같이 배우는 의미는 거기서 나오게됩니다.
8-bit Timer/Counter는 Timer/Counter0, Timer/Counter2가 해당이됩니다.
The double buffered Output Compare Register (OCR0) is compared with the Timer/Counter value at all times. The result of the compare can be used by the waveform generator to generate a PWM or variable frequency output on the Output Compare Pin (OC0). See “Output Compare Unit” on page 94. for details. The compare match event will also set the compare flag (OCF0) which can be used to generate an output compare interrupt request.
항상 TCNT0와 OCR0의 값은 비교가 되며 OCF0가 compare match event 발생시 set되는데 interrupt가 발생되며 그에 따른 처리가 가능하다
Timer/Counter의 출력을 위한 pin을 다음과 같습니다.
Timer/Counter0는 외부 크리스탈사용이 가능한데
TOSC1,TOSC2는 datasheet에 따르면 외부 크리스탈(32.768khz)로 연결되서 Timer/Counter0에 asynchronous clocking 동작이 가능하게합니다.(사전에 ASSR의 AS0을 set시켜놔야합니다., datasheet에는 TOSC1이 input TOSC2가 output이라고 되어있는데 그냥 크리스탈을 두핀에 연결해서 사용하면 됩니다.)
PB7:4는 Timer/Counter관련기능으로 사용하게 되는데
사용하기위해서는 사전에 DDRB를 반드시 출력설정을 해야합니다.
해당핀들은 HW적인 interrupt가 걸리게 되어 SW적으로 interrupt routine을 설정할때 보다는 좀더 빠른 반응을 얻어낼수있습니다.
다만 SW interrupt방식을 사용했을때는 여러 I/O pin을 사용할수 있지만 HW interrupt는 핀수가 제한적이라 그러지 못한다는게 단점입니다.
8bit Timer/Counter0,2
해당 register는 TCCR0 register입니다.
WGM01:0 을 통해서 사용하고자 하는 mode를 결정
COM01:0 을 통해서 사용하는 mode에 따른 OC0 pin의 출력형태의 결정
CS02:0을 통한 분주비 결정
별도의 clock source를 설정하지 않을시 Timer/Counter기능을 사용하지 못합니다.
그외에도 몇가지 register가 있는데 아래의 data flow를 통해 알아보자
Normal Mode
The simplest mode of operation is the normal mode (WGM01:0 = 0). In this mode the counting direction is always up (incrementing), and no counter clear is performed. The counter simply overruns when it passes its maximum 8-bit value (TOP = 0xFF) and then restarts from the bottom (0x00). In normal operation the Timer/Counter overflow flag (TOV0) will be set in the same timer clock cycle as the TCNT0 becomes zero. The TOV0 flag in this case behaves like a ninth bit, except that it is only set, not cleared. However, combined with the timer overflow interrupt that automatically clears the TOV0 flag, the timer resolution can be increased by software. There are no special cases to consider in the normal mode, a new counter value can be written anytime. The output compare unit can be used to generate interrupts at some given time. Using the output compare to generate waveforms in normal mode is not recommended, since this will occupy too much of the CPU time.
normal mode에서는 TCNT0(이 register의 값을 매 clk_t마다 증가시키면서 OCR0와 비교한다.)이 계속 증가하다가 overflow가 발생하면 TOV0(TIFR)이 set되면서 다시 TCNT값이 bottom부터 증가하게 됩니다.
OCR0값을 바꾸지는 않는 mode이며 MAX와 Bottom을 반복하는 timer입니다.
TCNT0값은 언제든지 변경이 가능합니다.
compare match 시 interrupt를 사용하는 방법은 다음과 같습니다.
OCIE0(TIMSK)과 I(SREG)를 set시킵니다.
compare match event가 일어나게 되면 OCF0(TIFR)이 set되는데 interrupt가 수행되고나면 나동적으로 clear됩니다.
(이때 SW적으로 '1'을 넣어서 clear시켜줄수도 있습니다.)
CTC mode
An interrupt can be generated each time the counter value reaches the TOP value by using the OCF0 flag. If the interrupt is enabled, the interrupt handler routine can be used for updating the TOP value. However, changing the TOP to a value close to BOTTOM when the counter is running with none or a low prescaler value must be done with care since the CTC mode does not have the double buffering feature. If the new value written to OCR0 is lower than the current TCNTn OCn (Toggle) OCn Interrupt Flag Set Period 1 2 3 4 (COMn1:0 = 1) 98 2467X–AVR–06/11 ATmega128 value of TCNT0, the counter will miss the compare match. The counter will then have to count to its maximum value (0xFF) and wrap around starting at 0x00 before the compare match can occur.
좀더 능동적인 주파수 조절이 가능한 mode입니다.
interrupt사용시 OCR0와 TOP의 compare match가 발생하면 TOVn과 OCF0값이 set이되면서 TOP값의 업데이트가 가능해집니다.
(단, OCR0값은 TCNT값 이상의 값으로 하는게 좋습니다.)
Fast PWM mode
다른 PWM mode와 비교해서 single slope의 특징때문에 2배의 속도를 낼수있습니다.
The Timer/Counter overflow flag (TOV0) is set each time the counter reaches Max If the interrupt is enabled, the interrupt handler routine can be used for updating the compare value. In fast PWM mode, the compare unit allows generation of PWM waveforms on the OC0 pin. Setting the COM01:0 bits to 2 will produce a non-inverted PWM and an inverted PWM output can be generated by setting the COM01:0 to 3 (See Table 54 on page 104). The actual OC0 value will only be visible on the port pin if the data direction for the port pin is set as output. The PWM waveform is generated by setting (or clearing) the OC0 Register at the compare match between OCR0 and TCNT0, and clearing (or setting) the OC0 Register at the timer clock cycle the counter is cleared (changes from MAX to BOTTOM).
OCIE0가 set되어있는경우 TOV0값이 TOP가 도달할때마다 TOV0가 1이 되는 interrupt를 발생시킬수 있습니다.
마찬가지로 OCR0와 값을 계속해서 비교하며 compare match시 OCF0가 set되면서 interrupt발생시 OCR값을 변경할 수 있습니다.
COM01:0값을 수정해서 non-inverted PWM 혹은 inverted PWM을 OC0핀으로 출력시킬수 있습니다.
COM01:0 = 1로 설정하면 duty rate 50%의 PWM을 출력할수있습니다.
OCR0=0으로 설정시 f_ocn = f_clk_I/P / 2의 fast PWM mode의 최대값을 출력할수 있습니다.
Phase Correct PWM Mode
좌우대칭적인 특징탓에 모터제어에 사용이 가능합니다.
분해능은 8bit(최대값이 8bit까지 올라간다는 소리)로 고정이 되어있습니다.
BOTTOM 도달시 TOV0가 set이 되면서 interrupt가 발생
COM1:0을 변경해서 OCn pin을 통해 출력을 내보낼수 있습니다.