Attiny2313 and PWM led example

In this example we connect an LED to PB2 (Pin 14) of an Attiny 2313. The PWM output is obtained from the OC0A output of timer/counter0.

 

Schematic

attiny2313 pwm led

attiny2313 pwm led

Code

Avr studio code example

 

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
 
int main(void)
{
DDRB |= (1 << PB2); // PWM output on PB2
TCCR0A = (1 << COM0A1) | (1 << WGM00); // phase correct PWM mode
OCR0A = 0x10; // initial PWM pulse width
 
TCCR0B = (1 << CS01); // clock source = CLK/8, start PWM
 
while(1)
{
// change PWM pulse width every 2 seconds
_delay_ms(2000);
OCR0A = 0x5;
_delay_ms(2000);
OCR0A = 0x10;
_delay_ms(2000);
OCR0A = 0x30;
_delay_ms(2000);
OCR0A = 0x50;
_delay_ms(2000);
OCR0A = 0xA0;
_delay_ms(2000);
OCR0A = 0xFF;
}
}

 

Links

5 PCS of ATTINY2313A

Attiny2313 flashing LED examnple

This was one of the first Attint chips that I looked at, so I decided to create a simple DIY development board (more on that later) and create some basic examples to play around with

Not a very exciting example, just the usual flashing LED example with a couple of LEDs connected to our attiny, lets look at the schematic.

Schematic

attiny2313 and leds

attiny2313 and leds

Code

We wrote this example in Atmel Studio. Fairly basic stuff here, set PORT D as outputs, switch on the LED connected to PD0, then the PD1 LED on, both LEDS on, then switch them off.

 

#define F_CPU 1000000UL // 1 MHz
 
#include <avr/io.h>
#include <util/delay.h>
 
//
 
int main(void)
{
// LEDs are on portD 0 and 1
DDRD = 0xff;
 
while(1)
{
PORTD = 0x01;
_delay_ms(500);
PORTD = 0x02;
_delay_ms(500);
PORTD = 0x03;
_delay_ms(500);
PORTD = 0x00;
_delay_ms(500);
}
}

 

Links

5PCS ATTINY2313A

10 PCS ATTINY2313A 8-bit Microcontroller

Attiny85 – Flash an LED

The hello world of examples this time for an ATTINY85, although this should work with similar chips as well. We will connect the LED to pin 2 (PB3) and simply flash it on and off.

Here is our schematic

Schematic

attiny85 Basic LED

attiny85 Basic LED

 

Code

The code was written in Atmel Studio

 

  1. #include <avr/io.h>
  2. #define F_CPU 1000000UL
  3. #include <util/delay.h>
  4. // Define the I/O port to be used for the LED.
  5. #define LED_PORT PB3
  6.  
  7. int main(void) {
  8.  
  9. // Set the LED port number as output.
  10. DDRB |= (1 << LED_PORT);
  11.  
  12. // Start infinite loop.
  13. while (1)
  14. {
  15.  
  16. // Set the LED bit to "1" - LED will be "on".
  17. PORTB |= (1 << LED_PORT);
  18. // The delay function simply does N-number of "empty" loops.
  19. _delay_ms(200);
  20. // Set the LED bit to "0" - LED will be "off".
  21. PORTB &= ~(1 << LED_PORT);
  22. _delay_ms(400);
  23. }
  24.  
  25. return (0);
  26. }

 

And another way to do the same task

 

  1. #include <avr/io.h>
  2. #define F_CPU 1000000UL
  3. #include <util/delay.h>
  4.  
  5. // Define the I/O port to be used for the LED.
  6. #define LED_PORT PB3
  7.  
  8. int main(void) {
  9.  
  10. // Set the LED port number as output.
  11. DDRB |= (1 << LED_PORT);
  12.  
  13. // Start infinite loop.
  14. while (1)
  15. {
  16.  
  17. // Set the LED bit to "1" - LED will be "on".
  18. PORTB |= (1 << LED_PORT);
  19. // The delay function simply does N-number of "empty" loops.
  20. _delay_ms(200);
  21. // Set the LED bit to "0" - LED will be "off".
  22. PORTB &= ~(1 << LED_PORT);
  23. _delay_ms(400);
  24. }
  25.  
  26. return (0);
  27. }

Links

 

Free shipping 10PCS ATTINY85