ATmega328P Timer0 Explorer

ATmega328P / Timer/Counter0 Explorer
8-bit timer / CTC / PWM / Normal / Fast PWM / 16 MHz
Click blocks to explore
Block diagram
Click any block to see its description and have the associated register highlighted on the right panel.
Timing diagram
--OC0A freq
--Period
--Duty cycle
Timing diagram updates live as you change registers on the right. Adjust WGM mode, prescaler, and OCR0A/B values to see the effect on the waveforms.
User guide
1. Block diagram

Click any coloured block in the block diagram to highlight it and read its description. The corresponding register on the right panel scrolls into view and is outlined in blue.

2. Register panel

Each register shows all 8 bits with their names and current values. Use the dropdown menus below each register to change bit fields. The hex value updates instantly.

3. WGM / Timer mode

WGM0[2:0] spans two registers. Set WGM0[1:0] in TCCR0A and WGM02 in TCCR0B together to pick the mode:

WGM[2:0]ModeTOP
000Normal0xFF
001Phase-correct PWM0xFF
010CTCOCR0A
011Fast PWM0xFF
101Phase-correct PWMOCR0A
111Fast PWMOCR0A
4. Prescaler (CS0[2:0])

Controls how fast the counter ticks relative to clkIO (16 MHz). A higher prescaler gives a lower output frequency and longer period.

CS0[2:0]DivisorTick period
001/162.5 ns
010/8500 ns
011/644 us
100/25616 us
101/102464 us
5. OCR0A and OCR0B

These 8-bit registers set the compare thresholds. In Fast PWM and Phase-correct PWM modes, the duty cycle of OC0A is OCR0A/TOP and OC0B is OCR0B/TOP. In CTC mode, OCR0A defines the TOP value and therefore the output frequency.

6. COM0A / COM0B

Controls what happens to the OC0A (PD6) and OC0B (PD5) output pins on a compare match. Set to 00 to disconnect, 10 for non-inverting PWM (clear on match, set at BOTTOM), or 11 for inverting PWM.

7. Interrupts (TIMSK0)

Enable up to three interrupt sources. TOIE0 fires on counter overflow (TOV0 flag). OCIE0A fires when TCNT0 matches OCR0A (OCF0A flag). OCIE0B fires when TCNT0 matches OCR0B (OCF0B flag). Flags are in TIFR0 and cleared by writing 1.

8. Timing diagram channels

TCNT0 shows the raw counter ramp. Dashed lines mark the OCR0A and OCR0B compare levels. OC0A and OC0B show the waveform output pins. TOVn pulses high at each overflow event.

9. Frequency formula

Fast PWM:     f = clkIO / (N x (TOP + 1))
CTC:          f = clkIO / (2 x N x (OCR0A + 1))
Phase-correct: f = clkIO / (2 x N x TOP)
where N = prescaler divisor, clkIO = 16 MHz

Register Configuration
TCCR0A 0x44 (I/O: 0x24)
Hex: 0x00
COM0A[1:0] / OC0A output mode
COM0B[1:0] / OC0B output mode
WGM0[1:0] / Waveform gen. mode (bits 1:0)
TCCR0B 0x45 (I/O: 0x25)
Hex: 0x00
WGM02 / Waveform gen. mode (bit 2)
CS0[2:0] / Clock select / prescaler
FOC0A / FOC0B / Force output compare
TCNT0 0x46 (I/O: 0x26)
Hex: 0x00
TCNT0[7:0] / Counter value (read/write)
OCR0A 0x47 (I/O: 0x27)
Hex: 0x00
OCR0A[7:0] / Output compare register A
OCR0B 0x48 (I/O: 0x28)
Hex: 0x00
OCR0B[7:0] / Output compare register B
TIMSK0 0x6E
Hex: 0x00
Interrupt enables
TIFR0 0x35 (I/O: 0x15)
Hex: 0x00
Interrupt flags (write 1 to clear)

Post a Comment

Previous Post Next Post