Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $840

Donations Received

Top Donor: C.C. $100


ATtiny2313 10-bit PWM using TC1

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.

Circuit Diagram

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.

ATtiny2313 PWM on OC1A 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.

TC1 external pins on the ATtiny2313

C Source Code and Project Files

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);
        // change PWM pulse width every 2 seconds
        OCR1A  = 0x10;
        OCR1A  = 0x40;
        OCR1A  = 0x100;
        OCR1A  = 0x2FF;
        OCR1A  = 0x3FF;     // maximum 10-bit value

Atmel Studio Project Files

The above source code with Atmel Studio 6 project files and hex output file can be downloaded here: (15.2kB)

← Go back to Part 13Go to Part 15 →