Categories

Program your attiny2313 with an Arduino

This is the second part of setting up an Arduino to program an Attiny microcontroller, in the previous parts we added support for the variety of microcontrollers. For reference purposes this is what the Attiny2313 will be like when you do this, the pins and the corresponding Arduino pins are shown

attiny2313-pinout

attiny2313-pinout

Now lets crack on with the setup

Setup attiny2313 connection with Arduino board

Connect up your Attiny2313 to your Arduino as follows

1) attiny2313 pin 1 to arduino pin 10 reset
2) attiny 2313 pin 17 to arduino pin 11 MOSI
3) attiny2313 pin 18 to arduino pin 12 MISO
4) attiny2313 pin 19 to arduino pin 13 SCK
5) attiny2313 pin 10 to arduino GND
6) attiny2313 pin 20 to arduino VCC

Here is a layout

Arduino to atttiny2313

Arduino to atttiny2313

Open the ArduinoISP program and upload the ArduinoISP program to the Arduino board to turn it into an ISP programmer. Now, open up the BLINK sketch, and upload the BLINK sketch to Attiny2313 via “Upload Using Programmer” option on the Arduino IDE as follows

1) Open ArduinoISP program.
File>Examples>ArduinoISP

2) Select the serial port.
Tools>Port>COM5 (yours may differ)

2) Select the required Arduino board, in this case we are using an Arduino Uno.
Tools>Board>Arduino Uno

3) Set the programmer type as AVRISP MKII.
Tools>Programmer>AVRISP MKII

4) Press the upload button.

5) Now your Arduino Uno is setup as the ISP programmer. Next open up the LED Blink sketch.
File>Examples>Basics>Blink

6) Select the AVR target for the Arduino ISP to program to.
Tools>Board>ATtinyx313

7) Change the Board for the Arduino ISP to program to.
Tools>Chip>ATtiny2313

8) Make sure the Clock for the Arduino ISP is set to 8Mhz.
Tools>Clock>8mhz(internal)

You can see these settings in the screenshot below

attinyx313

attinyx313

9) Set the programmer mode.
Tools>Programmer>Arduino as ISP

10) Finally, start the sketch upload to the Attiny2313 microcontroller
File>Upload

 

If you connect an LED to pin 16 of your Attiny2313 you will see an LED flash on and off

 

 

Attiny2313 PWM flashing LED example

A basic PWM example using an Attiny2313

Schematic

attiny2313 pwm led

attiny2313 pwm led

Code


#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
for(int i = 0;i<=255;i++)
{
_delay_ms(200);
OCR0A  = i;
}
}
}

Links

 

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 and 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 and flashing LED example

This was another Attiny micro that I had in the tool box, 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