Toggle Output Pin
Timer 0:
- Normal Mode and CTC Mode can be used to control an output pin OC0
connected to Timer 0 (PORTB.3).
- Use COM01 and COM00 to control output mode (sub mode).
- Sub mode will occur after overflow for Normal mode and after compare
match for CTC mode
- Toggle sub mode is used to generate a pulse waveform (usually with
CTC)
- COM01 and COM00 bit can be found in TCCR0
COM01 COM00 Mode
0 0 OC0 Disconnected
0 1 Toggling OC0
1 0 Clear OC0
1 1 Set OC0
Example: Write a micro-c program for ATmega16 MC to use timer0 to toggle
OC0 (PB3) to generate a pulse waveform with frequency of 500 Hz. Assume
ATmega16 MC is running at 1M Hz.
Solution:
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝑜𝑓 𝑢𝑐
𝑂𝐶𝑅0 =
2 × 𝑆𝑖𝑔𝑛𝑎𝑙 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 × 𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟
If Fuc = 1 MHz and Fsignal = 500 Hz
Then Prescaler = 8 and OCR0 = 125
TA: Abdalla Tawfik
void main () {
// Configure Port B as O/P
DDRB = 0xFF;
// Set the timer0 mode (CTC mode), its output mode (Toggle OC0) and
the prescaler (8) to generate a 500 Hz signal
TCCR0 = 0x1A;
// Set the OCR0 level to be 125 to generate a 500 Hz signal
OCR0 = 125;
// Infinite loop to force the microcontroller not to Halt and continue
executing the program as long as it’s connected to a power source
while (1);
}
TA: Abdalla Tawfik
Timer 1:
- Normal Mode and CTC Mode can be used to control output pins OC1A
and OC1B connected to Timer 1 (PORTD.5 and PORTD.4 respectively).
- Use COM1A1/COM1B1 and COM1A0/COM1B0 to control output mode
(sub mode).
- Sub mode will occur after overflow for Normal mode and after compare
match for CTC mode
- Toggle sub mode is used to generate a pulse waveform (usually with
CTC)
- COM1A1/COM1B1 and COM1A0/COM1B0 bit can be found in TCCR0
- Channel A is usually used in this mode.
COM1A1/COM1B1 COM1A0/COM1B0 Mode
0 0 OC1A/OC1B Disconnected
0 1 Toggling OC1A/OC1B
1 0 Clear OC1A/OC1B
1 1 Set OC1A/OC1B
Example: Write a micro-c program for ATmega16 MC to use timer1 to toggle
OC1A (PD5) to generate a pulse waveform with frequency of 500 Hz. Assume
ATmega16 MC is running at 1M Hz.
Solution:
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝑜𝑓 𝑢𝑐
𝑂𝐶𝑅1𝐴 𝑜𝑟 𝑂𝐶𝑅1𝐵 =
2 × 𝑆𝑖𝑔𝑛𝑎𝑙 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 × 𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟
If Fuc = 1 MHz and Fsignal = 500 Hz
Then Prescaler = 8 and OCR1A = 125
TA: Abdalla Tawfik
void main () {
// Configure Port D as O/P
DDRD = 0xFF;
// Set the timer1 mode (CTC mode), its output mode (Toggle OC1A) and
the prescaler (8) to generate a 500 Hz signal
TCCR1A = 0x40;
TCCR1B = 0x0A;
// Set the OCR1A level to be 125 to generate a 500 Hz signal
OCR1A = 125;
// Infinite loop to force the microcontroller not to Halt and continue
executing the program as long as it’s connected to a power source
while (1);
}
TA: Abdalla Tawfik
PWM Modes
- Generates a PWM signal with a specific frequency according to the
prescaler value and a specific duty cycle according to OCR value
Timer 0:
WGM00 WGM01 Mode
0 0 Normal
0 1 CTC
1 0 PWM, Phase Correct
1 1 Fast PWM
- PWM Phase Correct Mode:
• Sub modes:
COM01 COM00 Mode
0 0 OC0 Disconnected
0 1 Reserved
1 0 Clear OC0 on compare match when up-counting. Set OC0
on compare match when downcounting.
1 1 Set OC0 on compare match when up-counting. Clear OC0
on compare match when downcounting.
Example: Write a micro-C program for the ATmega16 MC to use timer0 to
generate a PWM signal with frequency around 245 Hz and with 70% duty cycle.
Assume ATmega16 MC is running at 1M Hz using PWM Phase Correct Mode.
Solution:
𝑂𝐶𝑅0 = (1 − 𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒) × 255
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝑜𝑓 𝑢𝑐
𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 =
𝑆𝑖𝑔𝑛𝑎𝑙 𝐹𝑟𝑒𝑞𝑢𝑛𝑐𝑦 × 510
If Duty Cycle = 70% and Fuc = 1 MHz and Fsignal = 245 Hz
Then OCR0 = 76 and Prescaler = 8
TA: Abdalla Tawfik
void main () {
// Configure Port B as O/P
DDRB = 0xFF;
// Set the timer0 mode (PWM Phase Correct mode), its output mode
(set in up count, clear in down count) and the prescaler (8) to generate a 245
Hz signal
TCCR0 = 0x72;
// Set the OCR0 level to be 0.3 of its maximum value to generate a signal
with 70% Duty Cycle
OCR0 = 76;
// Infinite loop to force the microcontroller not to Halt and continue
executing the program as long as it’s connected to a power source
while (1);
}
TA: Abdalla Tawfik
- Fast PWM:
• Sub modes:
COM01 COM00 Mode
0 0 OC0 Disconnected
0 1 Reserved
1 0 Clear OC0 on compare match, set OC0 at TOP
1 1 Set OC0 on compare match, clear OC0 at TOP
Example: Write a micro-C program for the ATmega16 MC to use timer0 to
generate a PWM signal with frequency around 490 Hz and with 70% duty cycle.
Assume ATmega16 MC is running at 1M Hz using Fast PWM Mode.
Solution:
𝑂𝐶𝑅0 = (1 − 𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒) × 256
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝑜𝑓 𝑢𝑐
𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 =
𝑆𝑖𝑔𝑛𝑎𝑙 𝐹𝑟𝑒𝑞𝑢𝑛𝑐𝑦 × 256
If Duty Cycle = 70% and Fuc = 1 MHz and Fsignal = 490 Hz
Then OCR0 = 76 and Prescaler = 8
void main () {
// Configure Port B as O/P
DDRB = 0xFF;
// Set the timer0 mode (Fast PWM mode), its output mode (Set OC0 on
compare match, clear OC0 at TOP) and the prescaler (8) to generate a 490 Hz
signal
TCCR0 = 0x7A;
// Set the OCR0 level to be 0.3 of its maximum value to generate a signal
with 70% Duty Cycle
OCR0 = 76;
// Infinite loop to force the microcontroller not to Halt and continue
executing the program as long as it’s connected to a power source
while (1);
}
TA: Abdalla Tawfik
Timer 1:
WGM13 WGM12 WGM11 WGM10 Mode
0 0 0 0 Normal
0 1 0 0 CTC
1 0 1 0 PWM, Phase Correct
1 1 1 0 Fast PWM
- PWM Phase Correct Mode:
• Sub modes:
COM1A1/COM1B1 COM1A0/COM1B0 Mode
0 0 OC1A/OC1B Disconnected
0 1 OC1A/OC1B Disconnected
1 0 Clear OC1A/OC1B on compare match when up-
counting. Set OC1A/OC1B on compare match
when downcounting.
1 1 Set OC1A/OC1B on compare match when
upcounting. Clear OC1A/OC1B on compare
match when downcounting.
Example: Write a micro-C program for the ATmega16 MC to use timer1 to
generate a PWM signal with frequency around 50 Hz and with 70% duty cycle.
Assume ATmega16 MC is running at 1M Hz using PWM Phase Correct Mode.
Solution:
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝑜𝑓 𝑢𝑐
𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 =
𝑆𝑖𝑔𝑛𝑎𝑙 𝐹𝑟𝑒𝑞𝑢𝑛𝑐𝑦 × 2 × 𝐼𝐶𝑅1
𝑂𝐶𝑅1A or OCR1B = (1 − 𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒) × ICR1
Let prescaler = 256 and Fuc = 1 MHz and Fsignal = 50 Hz
Then ICR1= 39
If Duty Cycle = 70% and ICR1 = 39
Then OCR1A = 11
TA: Abdalla Tawfik
void main () {
// Configure Port D as O/P
DDRD = 0xFF;
// Set the timer1 mode (PWM Phase Correct mode), its output mode
(set in up count, clear in down count) and the prescaler (256) to generate a 50
Hz signal
TCCR1A = 0xC2;
TCCR1B = 0x14;
// Set the ICR1 level to be 39 to generate a 50 Hz signal
ICR1 = 39;
// Set the OCR1A level to be 0.3 of ICR1 value to generate a signal with
70% Duty Cycle
OCR1A = 11;
// Infinite loop to force the microcontroller not to Halt and continue
executing the program as long as it’s connected to a power source
while (1);
}
TA: Abdalla Tawfik
- Fast PWM:
• Sub modes:
COM1A1/COM1B1 COM1A0/COM1B0 Mode
0 0 OC1A/OC1B Disconnected
0 1 OC1A/OC1B Disconnected
1 0 Clear OC1A/OC1B on compare match, set
OC1A/OC1B at TOP
1 1 Set OC1A/OC1B on compare match, clear
OC1A/OC1B at TOP
Example: Write a micro-C program for the ATmega16 MC to use timer1 to
generate a PWM signal with frequency around 50 Hz and with 70% duty cycle.
Assume ATmega16 MC is running at 1M Hz using Fast PWM Mode.
Solution:
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝑜𝑓 𝑢𝑐
𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 =
𝑆𝑖𝑔𝑛𝑎𝑙 𝐹𝑟𝑒𝑞𝑢𝑛𝑐𝑦 × 𝐼𝐶𝑅1
𝑂𝐶𝑅0 = (1 − 𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒) × 𝐼𝐶𝑅1
Let prescaler = 256 and Fuc = 1 MHz and Fsignal = 50 Hz
Then ICR1= 78
If Duty Cycle = 70% and ICR1 = 39
Then OCR1A = 22
TA: Abdalla Tawfik
void main () {
// Configure Port D as O/P
DDRD = 0xFF;
// Set the timer1mode (Fast PWM mode), its output mode (set in up
count, clear at Top) and the prescaler (256) to generate a 50 Hz signal
TCCR1A = 0xC2;
TCCR1B = 0x1C;
// Set the ICR1 level to be 78 to generate a 50 Hz signal
ICR1 = 78;
// Set the OCR1A level to be 0.3 of ICR1 value to generate a signal with
70% Duty Cycle
OCR1A = 22;
while (1);
}
TA: Abdalla Tawfik