Go Down

Topic: Arduino IDE wont compile a simple Attiny13 program (Read 209 times) previous topic - next topic

lfreddi

Hi!
I'm trying to make a simple program (using an Attiny13A) that can read an analog value on pin ADC2 and then turn on a led depending on the analog value.

The problem is that when I add the line "PORTB = (1<<PORTB3); // Enciende el LED", the program crashes and it goes from this nice compile:

Sketch uses 192 bytes (18%) of program storage space. Maximum is 1024 bytes.
Global variables use 4 bytes (6%) of dynamic memory, leaving 60 bytes for local variables. Maximum is 64 bytes.


to this failing compile:


Sketch uses 1374 bytes (134%) of program storage space. Maximum is 1024 bytes.text section exceeds available space in board

Global variables use 4 bytes (6%) of dynamic memory, leaving 60 bytes for local variables. Maximum is 64 bytes.
Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.
Error compiling for board ATtiny13.




I really don't know what the problem is, I tried using MicroCore and DIY Attiny, but the problem stays on.

Someone with experience with the Attiny13A can help?
Thanks in advance!

I attach the code here:

Code: [Select]

const float Vreferencia = 0.905;  // Tension de referencia del circuito que se usará para calibrar este Attiny13A

float raw = 0.0000;
float tension = 0.0000;

void setup() {
  DDRB = (1<<DDB3);     // Establece el pin PB3 como output (pin fisico 2)
  PORTB = (0<<PORTB3);  // Apaga el LED
}

void loop(){
   raw = leerADC2();  // Lee el ADC2 (pin fisico 3)
   tension = Vreferencia * (1024 / raw);
   if (tension < 1.00) {
   PORTB = (1<<PORTB3); // Enciende el LED
   }
}


int leerADC2(){
  byte i;                 // contador de ciclos
  byte numReadings = 10;  // numero de ciclos de medicion
  float value = 0.0000;   // variable para cargar el valor final
 
  // REF0 = 1, Referencia de tensión Vbg 1.1V
  // ADLAR = 0, alineamiento del resultado a la izquierda
  // MUX1 = 1 y MUX0 = 0, se leerá por el pin ADC2
  ADMUX = (0<<REFS0) | (0<<ADLAR) | (1<<MUX1) | (0<<MUX0);
 
  for (i = 0; i < numReadings; i++){ 
  ADCSRA |= _BV( ADSC );                   // comienza una conversion
  while( ( (ADCSRA & (1<<ADSC)) != 0 ) );  // y espera a que se complete
  value = value + ADC;
  delay(1);                      }     // 1ms pause adds more stability between reads.
  value = value / numReadings;
  return value;
}

lfreddi

I've added the library <avr/io.h>, and corrected some sentences, but the sketch has the same problem, I can't understand what I am missing.


Code: [Select]

#include <avr/io.h>           // to tell the chip where all the ports & pins are located

const float Vreferencia = 0.905;  // Reference value

float raw = 0.0000;
float tension = 0.0000;

void setup() {  
  DDRB |= (1 << DDB3);      // sets pin PB3 as output
  PORTB &= ~(1 << PORTB3);  // turns off the LED
}

void loop(){
   raw = leerADC2();  // Reads ADC2
   tension = Vreferencia * (1024 / raw);
   if (tension < 1.00) {
   PORTB |= (1<<PORTB3) ;  // lights the LED
   }
}


int leerADC2(){
  byte i;                 // cycle counter
  byte numReadings = 10;  
  float value = 0.0000;   // accumulation variable
  
  // REF0 = 1
  // ADLAR = 0
  // MUX1 = 1 & MUX0 = 0
  ADMUX = (0<<REFS0) | (0<<ADLAR) | (1<<MUX1) | (0<<MUX0);
 
  for (i = 0; i < numReadings; i++){  
  ADCSRA |= _BV( ADSC );                   // starts conversion
  while( ( (ADCSRA & (1<<ADSC)) != 0 ) );  // waits for complete conversion
  value = value + ADC;
  delay(1);                      }     // 1ms pause adds more stability between reads.
  value = value / numReadings;
  return value;
}

westfw

A tiny13 doesn't have enough memory to do floating point.

lfreddi

Hi westfw, thank you for your reply!
So, it is impossible in processors like Attiny13A to for example read an ADC value and then introduce that value in a 2nd or 3rd polinomic? That was what I was planning to do.
I mean, if the ADC value is stored in "A" variable, to then calculate for example:
Code: [Select]

float output = 3.6585 * A*A*A - 2.7165 *A*A + 26.5465 *A - 13.7568;  


I mean, if 3.6585, 2.7165, 26.5465 and 13.7568 were integers it would be much easier and maybe it could be done without float variables. But it is not the case, i need to use similar values in the equation and then get the output value.

There is any way I could implement float without using heavy libraries in order to get all the code in 1KB?

Lucas

westfw

Quote
So, it is impossible in processors like Attiny13A to for example read an ADC value and then introduce that value in a 2nd or 3rd polinomic?
Pretty much.
I mean, with a significant amount of effort, you could implement a lookup table instead - you have 1024 possible input values, many of which are probably outside the "interesting" range.  And presumably not that many meaningful output values.
Or you can change you equation to operate on integer millivolts instead of volts, and use several digits worth of integer precision in your constants:
Code: [Select]
uint32t output = 366*A*A*A - 272*A*A + 2655*A-1375;
Or use a chip with more memory.  (I don't quite understand why the tiny13 is so popular.  It's got nearly zero features, a pitiful amount of memory, and it's not even CHEAP compared to newer, better, alternatives (attiny402, for instance.)


lfreddi

I'm thinking a way to calculate in milivolts using a custom made fixed point math on the program, it should be a way to do this with this few resources from the Attiny13A.
You are right, it is disgusting to work with such a poor microcontroller, but in the third world countries like mine it is difficult to find what you need in electronics, and maybe if you find it, it is so damn expensive. I only have two choices: Attiny13A or Attiny85, I have chosen de 13A to lean a little more, but if I fail I will buy the Attiny85 although is so expensive here.
There is another problem too, if you work with the Attiny85 and tomorrow by any chance your goverment decides to suspend import of goods (belive me, this can happen because of the lack of dollars in the country) maybe you are in a trouble if you can't find the Attiny85. If you can make it with the 13A, you can migrate to the 13A if it is findable.
Sometimes one is pushed to do things that don't make sense, but in the end it is because of the context.
Thank you for your reply and your time, I will put effort in working around the problem.

Lucas

Go Up