How to compile for ATtiny45 and other AVR ?

Hi guys,

I've made Arduino as ISP and with avrdude I'm programing standalone AVRs. Everything works fine, however there are some small issues.

First I tried to program ATmega328p-pu with Arduino UNO. I load there just Blick example just to see how it goes. I works pretty well, but for example in function delay I had 1000 which is ok if I test it in Arduino, but on AVR this time duration was much more longer, (about 10seconds).
I think it will be because I compiled it for Arduino UNO, but I have no idea where I can select which Board or AVR I want program. My Arduino IDE shows only Arduino boards.

Do you know where I can find AVRs in Arduino IDE ?

Thank you! :wink:

PS: Or maybe some other compiler where I can compile exactly same code and libaries as in Arduino IDE and get .hex from it

wolfking:
AVR this time duration was much more longer, (about 10seconds).

It's probably because you have the clock speed on the ATmega328p set differently than the Uno which is 16MHz.

wolfking:
Do you know where I can find AVRs in Arduino IDE ?

First of all, Arduino boards like the Uno use an AVR microcontroller so that's not a very accurate way to explain what you're looking for but I understand. If your hardware is configured the same as a certain Arduino board then you can just use an equivalent board from the menu. If your hardware is different then you will need to install support for it. The easiest way to do this is by using Boards Manager. Here is a list of Boards Manager install URLs for a bunch of different hardware: Unofficial list of 3rd party boards support urls · arduino/Arduino Wiki · GitHub You will probably be interested in the ATtiny options for your ATtiny45. For the ATmega328p there is https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard, optiboot, or the Barebones ATmega Chips (no bootloader) entry on the Boards Manager URLs page

Thank you for your response.

As for this clock speed, it really make sence. Just I'm not sure how to fix it as I'm using same 16Mhz crystal. It must be propably something with setup or code than...

Yes, my hardware is just ATmega (or ATtiny) conected via ISP and programmed without bootloader - just .hex

I followed steps in .pdf in attachment, and despite it's really well writen. I didn't found any source there, where I can download extra boards or AVRs there.

Also, maybe you can help me with another thing, I load simple Switch program into ATmega328p (button on - led on) and AVR is somehow so sensitive, than even hand around it makes led shine or blick ... do you know how to fix this problem ? Thank you again :wink:

ArduinoAsProgrammer2015(1).pdf (894 KB)

wolfking:
It must be propably something with setup or code than...

No, the fuses. Did you burn the bootloader yourself?

No, I just uploaded .hex file with my program into brand new ATmega328p. I didn't do anything with bootloader or fuses.

wolfking:
As for this clock speed, it really make sence. Just I'm not sure how to fix it as I'm using same 16Mhz crystal.

You need to set the fuses, that is how the clock speed on your AVR is set. When you do Tools > Burn Bootloader in the Arduino IDE it will set the fuses to the settings specified in the boards.txt file for the board you have selected in the Tools > Board menu. It will also upload the bootloader to your board if there is a bootloader specified for that board but then when you Upload Using Programmer the bootloader will be removed. Upload Using Programmer doesn't set the fuses so you have to Burn Bootloader whenever you want to change the fuse settings.

wolfking:
where I can download extra boards or AVRs there.

Read my previous post I gave you multiple links.

wolfking:
I load simple Switch program into ATmega328p (button on - led on) and AVR is somehow so sensitive, than even hand around it makes led shine or blick ... do you know how to fix this problem

You need to set the pullups on the input pin

wolfking:
Hi guys,

I've made Arduino as ISP and with avrdude I'm programing standalone AVRs. Everything works fine, however there are some small issues.

First I tried to program ATmega328p-pu with Arduino UNO. I load there just Blick example just to see how it goes. I works pretty well, but for example in function delay I had 1000 which is ok if I test it in Arduino, but on AVR this time duration was much more longer, (about 10seconds).
I think it will be because I compiled it for Arduino UNO, but I have no idea where I can select which Board or AVR I want program. My Arduino IDE shows only Arduino boards.

Do you know where I can find AVRs in Arduino IDE ?

Thank you! :wink:

PS: Or maybe some other compiler where I can compile exactly same code and libaries as in Arduino IDE and get .hex from it

I use this for programming the ATtiny 25/45/85 chips.

ADD TO BOARDS.TXT FILE:

t25.name=ATtiny25 (8.0)
t25.upload.using=avrispmkii
t25.upload.maximum_size=2048
t25.upload.speed=115200
t25.build.mcu=attiny25
t25.build.f_cpu=8000000UL
t25.build.core=arduino
t25.build.variant=attiny_x5

For the tiny85, set the maximum size to 8192, and for the tiny45, set it to 4096.

Then, in your [b]arduino-x.x.x/hardware/arduino/variants[/b] directory, create a new directory named "[b]attiny_x5[/b]" and create a file named "[b]pins_arduino.h[/b]" containing this text:

/////////////////////////////////////////////////////////////////////////////
//
//  pins_arduino.h - Pin definition functions for Arduino
//  Part of Arduino - http://www.arduino.cc/
//
//  Copyright (c) 2015 Roger A. Krupski
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef Pins_Arduino_h
#define Pins_Arduino_h

#if defined ( __AVR_ATtiny25__ ) || ( __AVR_ATtiny45__ ) || ( __AVR_ATtiny85__ )

#define NUM_DIGITAL_PINS  6
#define NUM_ANALOG_INPUTS 4
#define PORT_B_ID         1
#define NOT_A_PIN         0
#define NOT_A_PORT        0
#define NOT_ON_TIMER      0

#define digitalPinHasPWM(p)((p)==5||(p)==6)

static const uint8_t MOSI = 5;
static const uint8_t MISO = 6;
static const uint8_t SCK  = 7;

static const uint8_t SDA = 5;
static const uint8_t SCL = 7;

static const uint8_t A0 = 1;
static const uint8_t A1 = 7;
static const uint8_t A2 = 3;
static const uint8_t A3 = 2;

#define digitalPinToInterrupt(p)(((p)==7)?1:NOT_AN_INTERRUPT)
#define digitalPinToPCICR(p)(((p)>=0&&(p)<=5)?(&GIMSK):((uint8_t *)NULL))
#define digitalPinToPCICRbit(p)(PCIE)
#define digitalPinToPCMSK(p)(((p)>=0&&(p)<=5)?(&PCMSK):((uint8_t *)NULL))
#define digitalPinToPCMSKbit(p)(p)

#ifdef ARDUINO_MAIN

//////////////////////////////////////////////////////
//            ATTINY 25/45/85 TOP VIEW              //
//                    --------                      //
//    RESET ADC0 PB5 |1*     8| VCC                 //
//    XTAL1 ADC3 PB3 |2      7| PB2 ADC1 SCK INT0   //
//    XTAL2 ADC2 PB4 |3      6| PB1 AIN1 MISO       //
//               GND |4      5| PB0 AIN0 MOSI AREF  //
//                    --------                      //
//////////////////////////////////////////////////////

// PB5 (PIN 1):___________________________________________________________________
//     RESET   Reset Pin
//     dW      debugWIRE I/O
//     ADC0    ADC Input Channel 0
//     PCINT5  Pin Change Interrupt, Source 5
//
// PB4 (PIN 3):___________________________________________________________________
//     XTAL2   Crystal Oscillator Output
//     CLKO    System Clock Output
//     ADC2    ADC Input Channel 2
//     OC1B    Timer/Counter1 Compare Match B Output
//     PCINT4  Pin Change Interrupt 0, Source 4
//
// PB3 (PIN 2):___________________________________________________________________
//     XTAL1   Crystal Oscillator Input
//     CLKI    External Clock Input
//     ADC3    ADC Input Channel 3
//     OC1B    Complementary Timer/Counter1 Compare Match B Output
//     PCINT3  Pin Change Interrupt 0, Source 3
//
// PB2 (PIN 7):___________________________________________________________________
//     SCK     Serial Clock Input
//     ADC1    ADC Input Channel 1
//     T0      Timer/Counter0 Clock Source
//     USCK    USI Clock (Three Wire Mode)
//     SCL     USI Clock (Two Wire Mode)
//     INT0    External Interrupt 0 Input
//     PCINT2  Pin Change Interrupt 0, Source 2
//
// PB1 (PIN 6):___________________________________________________________________
//     MISO    SPI Master Data Input/Slave Data Output
//     AIN1    Analog Comparator, Negative Input
//     OC0B    Timer/Counter0 Compare Match B Output
//     OC1A    Timer/Counter1 Compare Match A Output (PWM1)
//     DO      USI Data Output (Three Wire Mode)
//     PCINT1  Pin Change Interrupt 0, Source 1
//
// PB0 (PIN 5):___________________________________________________________________
//     MOSI    SPI Master Data Output/Slave Data Input
//     AIN0    Analog Comparator, Positive Input
//     OC0A    Timer/Counter0 Compare Match A output (PWM0)
//     OC1A    Complementary Timer/Counter1 Compare Match A Output
//     DI      USI Data Input (Three Wire Mode)
//     SDA     USI Data Input (Two Wire Mode)
//     AREF    External Analog Reference
//     PCINT0  Pin Change Interrupt 0, Source 0

const uint16_t PROGMEM port_to_mode_PGM[] = {
	NOT_A_PORT,
	(uint16_t) &DDRB,
};

const uint16_t PROGMEM port_to_output_PGM[] = {
	NOT_A_PORT,
	(uint16_t) &PORTB,
};

const uint16_t PROGMEM port_to_input_PGM[] = {
	NOT_A_PORT,
	(uint16_t) &PINB,
};

const uint8_t PROGMEM digital_pin_to_port_PGM[] =
{
	NOT_A_PORT,
	PORT_B_ID,
	PORT_B_ID,
	PORT_B_ID,
	NOT_A_PORT,
	PORT_B_ID,
	PORT_B_ID,
	PORT_B_ID,
	NOT_A_PORT,
};

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] =
{
	NOT_A_PIN,
	_BV(5),
	_BV(3),
	_BV(4),
	NOT_A_PIN,
	_BV(0),
	_BV(1),
	_BV(2),
	NOT_A_PIN,
};

const uint8_t PROGMEM digital_pin_to_timer_PGM[] =
{
	NOT_ON_TIMER,
	NOT_ON_TIMER,
	NOT_ON_TIMER,
	NOT_ON_TIMER,
	NOT_ON_TIMER,
	TIMER0A, // OC0A
	TIMER1A, // OC1A
	NOT_ON_TIMER,
	NOT_ON_TIMER,
};

#endif // ARDUINO_MAIN
#endif // ( __AVR_ATtiny25/45/85__ )
#endif // Pins_Arduino_h

I don't bother to support a bootloader since I use the ATtiny24 (only 2K of flash). Why suck up 1/4 of my flash when I can burn it directly?

Hope this helps.

Guys, Thank you so much!!! You really helped me a lot. I'm new in programing and Arduino aswell, but so many possibilities, what it offers really not allow me to sleep :wink:

I've user your advice @Krupski and it works perfectly! Such a pitty that ATtiny45 don't support libaries what I need, I should check it before... anyway, do you know if there is a chance how to compile these extra libaries(and functions) into ATtiny ? Or is there some different MCU between ATtiny and ATmega with will have same(or almost same) support with libaries and functions as ATmega328p ?

I also followed your advices @pert and now ATmega works as I wanted! I downloaded third-party libaries and used them without problems. Also this button problem was nicely solved with pullup function.
Just I still having problems with this "slowness" of MCU. I founded out, that when I set internal clock to 1Mhz, everything works just fine (I'm using just simple code), but it seems that problem is only with function delay().
I don't want to burn bootloader because I will make MCU slow to power ON, but I hope there must be some way how to deal with it. In worst case use something else instead delay() function.

wolfking:
I don't want to burn bootloader because I will make MCU slow to power ON, but I hope there must be some way how to deal with it. In worst case use something else instead delay() function.

Read my post again. I explained that you need to do the Burn Bootloader just to set the fuses so that your clock will run at the right speed. Then when you Upload Using Programmer the bootloader will be erased so it won't cause a delay. I'm excited to play with my Arduino also but I'm taking my valuable time to help you out, the least you can do is to read what I wrote.

Ah, okay, I missunderstanded it propably. I'm sorry
So when I use burn bootloader function in Adruino IDE, it will not start burning bootloader in my Ardutino UNO, but it will start to do it in externally connected ATmega yes ? Just to be sure ... I was little afraid of that ...
Thank you

wolfking:
So when I use burn bootloader function in Adruino IDE, it will not start burning bootloader in my Ardutino UNO, but it will start to do it in externally connected ATmega yes ?

If you're using the UNO as your Arduino as ISP then yes, it will burn the bootloader to the board that is connected to the UNO via SPI and it will not do anything to the bootloader on the UNO. The UNO is just being used as the programmer device to burn the bootloader(and set fuses) on the externally connected board.

wolfking:
Or is there some different MCU between ATtiny and ATmega

The Attiny of ATmega is the MCU so yeay, there is a huge difference. Depending on the library you can try to figure out what stops the library from working on a ATtiny. Mayby it's simple or maybe is because of the timers etc. Like the ATtiny45 does not have a 16-bit timer...

wolfking:
I don't want to burn bootloader because I will make MCU slow to power ON

Burning a ATmega328 with the bootloader in the current versions of the IDE (I thought all the 1+ versions) does not the ATmega slow to turn on. On power on the bootloader is skipped. Only with a reset the ATmega with bootloader is slow because it will wait in the bootloader. But it only does this on reset, not on power on.

Yes, I meant some MCU in size range between ATtiny(8pin) and ATmega(28pin) what support wide range of libraries ... but you give me an answer anyway :wink: Just need to learn more about programing to find courage to dig into libraries

wolfking:
I meant some MCU in size range between ATtiny(8pin) and ATmega(28pin)

ATtiny84 is available in 14 pin DIP, 4313 and 861 are available in 20 pin DIP package these have hardware cores made for them already. There are some others in surface mount. But...

wolfking:
what support wide range of libraries ...

You'll never find as many supporting libraries as you will with the ATmega328P.

Ah, okay, I see now
Also I've tried this procedure with bootloader and everything works just fine! Now even delay() timing is as it's suppose to be:)
Do you know if there is any difference between 8Mhz internal clock instead of 16Mhz external ? Only speed ?
I would rather use internal, as it's require less external components, but I'm not sure if behaviour of program (despite compilation for internal) will be the same. So far everything works fine. Or it's also depends on libaries ? (I'm using virtualwire and IR remote)

The problem with internal clock is that it's like +/- 10% - very inaccurate. So doing any sort of timing with it, that may not be acceptable. Most frequent problem is serial communication, which requires the clock to be within a couple percent, otherwise you get garbage. You can "tune" the internal clock by setting OSCCAL (there are a few approaches to this); I recently had a 'quick and dirty' idea for tuning (tune it off the blink sketch from an arduino with a crystal), but when I tried to test it, I couldn't find any ATTiny's that didn't work for serial (using both software serial, and the soft serial included in the core). By using a crystal, you ensure that the clock is accurate.

If you like playing with attiny's my cores (see sig for board manager links, or github pages GitHub - SpenceKonde/arduino-tiny-841: Arduino core for ATtny841, 828, 1634 and 441 GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8 for more info), between them, support just about every attiny worth using. The 841, 1634 are particularly interesting, as they both have two hardware uarts. And the 841 is only like 3 cents more than the 84. Sadly, they don't come in DIP packages...

Yep, only speed. But that can lead to different programs because you need different setting. And it depends on the library if it can run @ 8Mhz.

Because if a library uses timers they have to be set different to give the same result @ a lower clock speed. But not all libraries do that and are only written for the normal 16Mhz of the Uno. But like you have seen with delay, that library is written in a way it can work with a whole range of clock speeds. So it really depends on the library.

As an aside, the ATtiny x5 series and x61 series have a PLL Clock option, that will let you run them at 16mhz off the internal oscillator using the PLL. Of course, since the internal oscillator is still inaccurate, you've still got that issue.