Out of memory Attiny2313

I must say that in some way I disappointed because the low flash memory of this chip, I really don't check the amount of memory before to buy it (my bad), I just count the 18 pins and all the ports and the 4 pmw pins that it has.

but I have learned some stuff about the memory and how to keep it low, I could reduce a 2202 bytes sketch to 2000 bytes just below the 2048 of the attiny2313.

after a rest and go to hear the message, thinking about other tings....
I couldn't understand how a micro with a "lot of pins" just has 2k of flash or at last 2k must be enough to handle all the pins , the ting is that I'm believe that the arduino's IDE is keeping more memory that it must in other way for example the simplest sketch keeps 310 bytes, and adding one pinMode(); isntruction it reaches 508 bytes.

void setup(){}
void loop(){}

please some advice in front of this matter

  1. 2k is actually "a lot" of program memory by some standards. The smallest microcontrollers I own have about a quarter of that.
  2. The Arduino environment isn't really designed for such small microcontrollers; the initial Arduino used a chip with 8k.
  3. The program space used by the arduino libraries is not linear with the "number of pins" you use, or even how much code you actually put in your sketch. For example, adding "pinMode" for one pin may add 200 bytes to a sketch that doesn't use pinMode, but when you use it on a 2nd pin, you WON'T add another 200 bytes. You've already got the big piece of code, and using on more than one pin only adds a little bit.

I agree with Bill that you shouldn't use the Arduino wiring library. Write your code in C, compile with avr-gcc, and download it to the 2313 with avrdude. You can do a lot with 2K of memory without the Arduino overhead.
And if you want to squeeze out every last byte, read an article I wrote a couple weeks ago:

I can see it more clear thanks to you.
also I can see that this sketch only get 96 bytes

int main(void){

while(1)
{
//loop code
}
}

Ah, but in assembly language, that could be a very short program indeed! :slight_smile:

The AVR series is pretty deep. You can get a lot of combinations to support complex programs and/or high I/O requirements. The 2313 may be good for simple automated tasks that don't have much programmatic complexity, but need to control or sense several things. Think of it like a more typical 8-pin ATtiny with a built-in I/O expander! Although, the one thing missing is a micro that has lots of computing resources (like the ATmega 1284P) but with a small form factor.

In the end, keep in mind these microcontrollers are like mini computers, but they're also not exactly like mini computers. When you get to the point where you're using graphics, sound, networking, and so on so forth, you're really pushing beyond the intended purpose of a microcontroller.

Write your code in C, compile with avr-gcc, and download it to the 2313 with avrdude.

You don't have to go so far. Go ahead and use the Arduino IDE; just avoid most of the arduino libraries.
Like Other ways to Blink ! - Frequently-Asked Questions - Arduino Forum

westfw:

Write your code in C, compile with avr-gcc, and download it to the 2313 with avrdude.

You don't have to go so far. Go ahead and use the Arduino IDE; just avoid most of the arduino libraries.
Like Other ways to Blink ! - Frequently-Asked Questions - Arduino Forum

That may do the trick, but the core (mostly the millis() timer) still takes up some code. This really basic sketch compiles to 364 bytes:

void setup(){}
void loop()
{
  PORTB=0;
}

But here I initialize PORTB to zero with only 6 bytes of code:

public@server1:~/code/avr/flashfiles> make bareSketch
avr-gcc -mmcu=attiny85 -Os -fno-inline-small-functions -mrelax -nostartfiles crt1.o    bareSketch.c   -o bareSketch
/opt/cross/avr/lib/gcc/avr/4.8.2/../../../../avr/bin/ld: warning: -z relro ignored.
public@server1:~/code/avr/flashfiles> avr-size bareSketch
   text    data     bss     dec     hex filename
      6       0       0       6       6 bareSketch
public@server1:~/code/avr/flashfiles> cat bareSketch.c
#include <avr/io.h>

void main()
{
 PORTB=0;
}

But here I initialize PORTB to zero with only 6 bytes of code

But you used "magic": "-fno-inline-small-functions -mrelax -nostartfiles crt1.o"
(dangerous magic too, considering what -nostartfiles leaves out, and depending on what crt1.o you have!)

You only have to optimize enough that your code fits; if you can avoid learning a whole new toolset, it MIGHT be a reasonable idea.
(OTOH, learning more is good for you!)

I'm not sure what the attraction of the larger ATtiny chips is, considering that you can get ATmega8s for about $1, and be well within "supported" territory. (I guess there is the whole "physically smaller but still a DIP" thing...)

westfw:
I'm not sure what the attraction of the larger ATtiny chips is, considering that you can get ATmega8s for about $1, and be well within "supported" territory. (I guess there is the whole "physically smaller but still a DIP" thing...)

For production use, I saw one person posting that they were using Tiny85's for high-speed PWM. You can run the PWM off the internal 64Mhz (8x8Mhz) PLL.

For hobbyists that aren't etching their own boards, the DIP packages are nice. Tayda sells the Tiny85 DIPs for 115c, and shipping is ~$2, so I think it's still cheaper than a Mega8 DIP when you're only getting a few of them. With the 8-pin DIPs you can bend the leads and solder the +5 and Gnd to the bottom of a CR2032 battery holder. Solder an 8-pin dip socket piggyback style for plugging in wires and for programming, and now you've got a tiny battery-powered AVR.

Overall I agree there's not much point to them now. I only started using the Tiny AVRs in the last year, and had I known how cheap the ATMega328 arduino compatible boards would be I wouldn't have bothered with with the ATTinys.

ralphd:

westfw:
I'm not sure what the attraction of the larger ATtiny chips is, considering that you can get ATmega8s for about $1, and be well within "supported" territory. (I guess there is the whole "physically smaller but still a DIP" thing...)

For production use, I saw one person posting that they were using Tiny85's for high-speed PWM. You can run the PWM off the internal 64Mhz (8x8Mhz) PLL.

For hobbyists that aren't etching their own boards, the DIP packages are nice. Tayda sells the Tiny85 DIPs for 115c, and shipping is ~$2, so I think it's still cheaper than a Mega8 DIP when you're only getting a few of them. With the 8-pin DIPs you can bend the leads and solder the +5 and Gnd to the bottom of a CR2032 battery holder. Solder an 8-pin dip socket piggyback style for plugging in wires and for programming, and now you've got a tiny battery-powered AVR.

Overall I agree there's not much point to them now. I only started using the Tiny AVRs in the last year, and had I known how cheap the ATMega328 arduino compatible boards would be I wouldn't have bothered with with the ATTinys.

I must agree with you guys.
I buy two attiny2313v-10p at my electronic local store $48MXN each (3.8USD) It seems to be a good deal but compared with an arduino compatible board (3.30usd) with all the circuitry using the mega328p just missing the FTDI is not so good.

edit ($2.95) the price of the compatible board is lower.

However I steel wanted to use them like a CHALLENGE.
my project, read DHT11 (temperature and humidity) and send wireless using 433MHz antena.
what you think, must it?

westfw:
I'm not sure what the attraction of the larger ATtiny chips is, considering that you can get ATmega8s for about $1, and be well within "supported" territory. (I guess there is the whole "physically smaller but still a DIP" thing...)

For me, it's a compulsive need to right-size the components. I guess I could order 1000 ATmega1284Ps and use those for everything, but I would feel bad that there's so much hardware that never ever gets used....

well I don't know if you know that there is a competition called js1k is about to make a javascript that fits in 1Kbyte.
I have seen a chess in 1k
I don't know what is the difference but it must bee suficient space in 2kbytes for a good sketch, 2048 words!

Reminds me of those old 1990s PC demos, where they would hand-code an executable to do as much impressive stuff as they could in 4096 bytes. Lots of morphing, 3D rendering (back when all you had was 2D hardware), fractals, all kinds of neat stuff.

There are Pro Mini boards for sale on Ebay that include the 6 pin ISP programming. These are great boards because they bypass the bootloader but can be programed via the regular FTDI serial interface as well as serial through the same device. This is a link that is a good price. A "Better Solution. 5 devices for $16.99 + 1.99 shipping. Personally I prefer these boards because they also work from 3V3 to 5V into the 5V pin or 6 to 12 V through the onboard 250 mA linear regulator. they are really great on strip-board or Veroboard.
http://www.ebay.com/itm/5pcs-Pro-Mini-ATMEGA328P-5V-16M-Replace-ATmega128-for-Arduino-Compatible-Nano-/271349693369?pt=LH_DefaultDomain_0&hash=item3f2db3bbb9

Doc

Docedison:
There are Pro Mini boards for sale on Ebay that include the 6 pin ISP programming. These are great boards because they bypass the bootloader but can be programed via the regular FTDI serial interface as well as serial through the same device. This is a link that is a good price. A "Better Solution. 5 devices for $16.99 + 1.99 shipping. Personally I prefer these boards because they also work from 3V3 to 5V into the 5V pin or 6 to 12 V through the onboard 250 mA linear regulator. they are really great on strip-board or Veroboard.
http://www.ebay.com/itm/5pcs-Pro-Mini-ATMEGA328P-5V-16M-Replace-ATmega128-for-Arduino-Compatible-Nano-/271349693369?pt=LH_DefaultDomain_0&hash=item3f2db3bbb9

Doc

I already have one of this

SirNickity:
Reminds me of those old 1990s PC demos, where they would hand-code an executable to do as much impressive stuff as they could in 4096 bytes. Lots of morphing, 3D rendering (back when all you had was 2D hardware), fractals, all kinds of neat stuff.

It must be an Open Challenge for Attiny develop design; certainly many people could learn a lot from that.

Docedison:
There are Pro Mini boards for sale on Ebay ...

3.65 in qty 1 with a crystal (more accurate than a ceramic resonator):
http://www.aliexpress.com/item/Pro-Mini-Module-Atmega328-5V-16M-For-Arduino-Compatible-With-Nano/1503533345.html
Qty 10 price: $27

Not nearly the deal it might appear to be...
5 pieces @ Ebay$ 3.398 Ea., 5 pieces (minimum bulk order price) on Aliexpress $3.36 Ea.... No ISP Programmer port in the Aliexpress part.
A plain vanilla Pro-Mini... I buy the Ebay Pro-Mini's especially because I can solder a 6 pin ISP header to the Pro Mini board... instead of having to break it out on the strip-board/Veroboard that I like to use to connect that board... It does help to look at the pictures Very carefully...
My mention of the Ebay Pro Mini specifically was about the convenience of the "On Board" ISP connector, Not to sell Pro-Mini's..

Doc
Edited for clarity

My mention of the Ebay Pro Mini specifically was about the convenience of the "On Board" ISP connector, Not to sell Pro-Mini's..

For those that have FTDI (or other) serial programming via the bootloader, these appear to be a better choice, both price and free shipping. They also ship from Hong Kong which in my experience is generally faster than the mainland. Over $4 in savings on qty 5.

I have used these successfully at 3.3V and 5.0V without issue. Your results may vary.

Ray

Docedison:
No ISP Programmer port in the Aliexpress part.

I use jumper aka dupont wires for most of my arduino hacking. Just connect Gnd, Vcc, MOSI, MISO, SCK & RST on the programmer with the pins on the pro mini... no need for soldering an ICSP header, and no need for an adapter from 10-pin to 6-pin ICSP (I have a USBASP with a 10-pin connector).

I'm sorry, I only buy programmers that fit my boards, much easier than trying to find an adapter when i need to program any Arduino products, I've already been through that nonsense once or twice in a 40 year history...
Using Wire-wrap wire is great.. If you only need to program a part once or twice but the use of 30 Ga solid wire does have it's pitfalls too, Especially at 3 am when being sharp is an impossibility... Much better to design properly so as to eliminate as much operator error as possible. Makes the important work much easier to do.. The longer I worked the more I found that shortcuts were invariably a source of error, If one works a little more at proper design one has to work much less to complete a task.
Although I am now happily retired the old work habits persist and because my sight is failing a little each year I can't afford the things I might have done 20 years ago... too many places to make foolish mistakes. IMNSHO...

Doc