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 and LCD example

This is a very similar example to our ATMEGA16 and LCD example. We simply connect our 16×2 LCD display to our Attiny2313 PORTB. Again for simplicity the code is written in MikroC PRO for AVR.

We actually added an LCD breakout to our Attiny2313 development/experimenting board

 

Schematic

attiny2313 and lcd

attiny2313 and lcd

Code

 

sbit LCD_RS at PORTB4_bit;
sbit LCD_EN at PORTB5_bit;
sbit LCD_D4 at PORTB0_bit;
sbit LCD_D5 at PORTB1_bit;
sbit LCD_D6 at PORTB2_bit;
sbit LCD_D7 at PORTB3_bit;
 
sbit LCD_RS_Direction at DDB4_bit;
sbit LCD_EN_Direction at DDB5_bit;
sbit LCD_D4_Direction at DDB0_bit;
sbit LCD_D5_Direction at DDB1_bit;
sbit LCD_D6_Direction at DDB2_bit;
sbit LCD_D7_Direction at DDB3_bit;
 
void main()
{
 
PORTB = 0;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"Hello world"); //Write the word mikroC on the LCD
Delay_ms(300);
} // end main

 

 

Links
5PCS ATTINY2313A

5pcs LCD 16×2 Yellow Green Screen Backlight

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