Created on: 20 May 2013
Part 14 of the ATtiny2313 Tutorial
Timer/counter1 of the ATtiny2313 is used for generating a PWM output wave that dims an LED. The PWM is set up in the C code for 10-bit resolution.
TC1 has two output pins that can be used for PWM output. For this example, an LED is connected to OC1A (PB3, pin 15) as shown in the circuit diagram.
OC1A and other TC1 pins can be seen on the ATtiny2313 outline in the diagram below. Only OC1A is enabled in this exercise. The other pins can be used as general purpose I/O.
The C code initializes timer/counter1 in 10-bit PWM mode and selects the system clock as the clock source for the PWM.
Every 2 seconds, a new value is written to the OCR1A register which changes the width of the PWM pulse on the OC1A pin.
This example works the same way as the TC0 PWM example in this tutorial, but this time with a 10-bit resolution PWM instead of 8-bit.
#define F_CPU 1000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRB |= (1 << PB3); // PWM output on PB3 // phase correct PWM, 10-bit TCCR1A = (1 << COM1A1)| (1 << WGM11) | (1 << WGM10); OCR1A = 0x10; // initial PWM pulse width // clock source = clock / 1 ; start PWM TCCR1B = (1 << CS10); while(1) { // change PWM pulse width every 2 seconds _delay_ms(2000); OCR1A = 0x10; _delay_ms(2000); OCR1A = 0x40; _delay_ms(2000); OCR1A = 0x100; _delay_ms(2000); OCR1A = 0x2FF; _delay_ms(2000); OCR1A = 0x3FF; // maximum 10-bit value } }
The above source code with Atmel Studio 6 project files and hex output file can be downloaded here:
TC1_PWM.zip (15.2kB)