Programming an ATtiny85 Microcontroller with the AVRISP mkII

The ATtiny85 is an 8 pin microcontroller, complete with 8KB of flash, and 512 bytes of EEPROM and SRAM. It also has some hardware support for I2C (otherwise known as TWI), which makes it ideal for my current project. Here it is hooked up to Atmel’s AVRISP mkII programmer (the blue box on the top left):

The rest of the post details the steps required to get it up and running with a simple program (mostly for my benefit, as no doubt I’ll forget in a few months). Here’s what I did:

  1. Install AVR Studio 4
    Download AVR Studio 4, the latest service pack, and the AVR Toolchain Installer from Atmel: http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725. Make sure that you don’t install it under c:\Program Files (x86)\ (or any path with a bracket in the name) as you’ll get this obscure error when you attempt to build: 

    make: Interrupt/Exception caught (code = 0xc00000fd, addr = 0x4217b3)
  2. Identify the pins in the AVRISP cable
    The cable pinout for the AVRISP mkII is:Pin1 is on the same side of the connector as the red stripe, and opposite where the cable enters the connector body (ie in the diagram above, the cable would be entering from the right).
  3. Wire up the microcontroller
    Wiring up the microcontroller to the programmer is pretty straightforward. Here’s the pinout for the ATtiny 85/45/25:
    Some gotchas: 

    • You will need to power the circuit with an external power supply, as  the programmer doesn’t supply power to the microcontroller.
    • You should set the microcontroller’s reset pin high. Make sure you don’t use anything less than a 4.7K resistor or the programmer may not be able to set it low to put the microcontroller into programming mode.
    • Make sure you connect VTG to your VCC. Without this, the programmer assumes the circuit isn’t powered.

    Here’s a closeup of the breadboard:

  4. Create a project in AVR Studio 4
    Creating a new project is easy: 

    • Hit the New Project button (or choose Project Wizard from the Project menu).
    • Choose ‘AVR GCC’ and give the project a name.
    • Choose ‘AVR Simulator 2′ and your microcontroller type.
    • Write your program.

    Here’s the program I used (it flashes an LED on pin 2):

    #define F_CPU 1000000UL
    #include <avr/io.h>
    #include <util/delay_basic.h>
    #define LED PB3
    void delay_ms(uint16_t ms){
      while (ms > 0) {
        _delay_loop_2(F_CPU/4000);
        ms--;
      }
    }
    int main(void) {
      // Set Pin 2 (PB3) as an output pin.
      DDRB |= 1 << LED;
      while(1) {
    
        // Set pin 2 high.
        PORTB |= 1 << LED;
        delay_ms(1000);
        // Set pin 2 low.
        PORTB &= ~(1 << LED);
        delay_ms(1000);
      }
      return 0;
    }
  5. Flash the Program to the Microcontroller
    The last step. Plug in your programmer, hit the  button on the toolbar, chose AVRISP mkII and all going well you’ll have two green LEDs lit in your programmer. If not, have a look at the AVRISP mkII programmer, page 25 for some troubleshooting hints.To flash the program to your micrcontroller, ensure you’ve built the program, switch to the Program tab in the dialog box, hit the ‘Program’ button in the ‘Flash’ panel, select your .hex file (it should be in the ‘default’ directory inside the directory which contains your .c file), and you should be done! 

About these ads
This entry was posted in Electronics, Microcontrollers and tagged , , , . Bookmark the permalink.

3 Responses to Programming an ATtiny85 Microcontroller with the AVRISP mkII

  1. Thank you this was helpful! I have an ATtiny85 and was having trouble with code size. I found out it was my delay routine, and yours shrunk my code by 4kbytes!!

  2. Pingback: def __init__(self): » iCufflinks on ATTiny85

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s