View Single Post
Old 09-10-2008, 01:43 AM   #642 (permalink)
jmilk
EcoModding Lurker
 
Join Date: Aug 2008
Location: Home
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by dcb View Post
9/8/2008
updates for v0.71
sketch size: 13814 bytes, free mem 325, CPU% 52.42. We are for all practical purposes at the limit without a major re-write or some serious optimization and retest work, or an atmega328, or?
Without serious optimization, I was able to push the code size to 13494, freeing another 320 bytes of flash. Mind you, this isn't tested on hardware, but I don't see any glaring reasons why this shouldn't work. Feel free to test.

36 Bytes - Brightness Fix (inverted brightness values, removed (255-")
Code:
byte brightness[]={255,213,170,127}; //middle button cycles through these brightness settings
34 Bytes - LCD/CGRam programming, removed nested loop
Code:
    for(byte x=0;x<40;x++)
        LcdDataWrite(pgm_read_byte(&chars[x])); //write the character data to the character generator ram
14 Bytes - Remove default value for char * r in bigNum()
Code:
  char * r;// = "009.99"; //"009.99" "000.99" "000.09"
38 Bytes - Various LCD opts:
Code:
namespace LCD(
...
  void LcdCommandNibble(byte value, byte delay);
...
);
  ...
void LCD::init(){
  delay2(16);                    // wait for more than 15 msec
  LcdCommandNibble(B0011000, 5 );
  LcdCommandNibble(B0011000, 1 );
  LcdCommandNibble(B0011000, 1 );
  LcdCommandNibble(B0010000, 1 );
  ...
void LCD::LcdCommandWrite(byte value){       
  LcdCommandNibble(value,0);
  LcdCommandNibble(value<<4,5);
}       

void LCD::pushNibble(byte value){       
  digitalWrite(DB7Pin, value & 128);       
  value <<= 1;       
  digitalWrite(DB6Pin, value & 128);       
  value <<= 1;       
  digitalWrite(DB5Pin, value & 128);       
  value <<= 1;       
  digitalWrite(DB4Pin, value & 128);       
}     

void LCD::LcdDataWrite(byte value){       
  digitalWrite(DIPin, HIGH);       
  pushNibble(value);      
  tickleEnable();       
  pushNibble(value<<4);      
  tickleEnable();       
  delay2(5);       
}
20 Bytes - changed gotoXY to gotoYX with default parms:
Code:
  void gotoYX(byte y=0, byte x=0);
(then change all occurrences, swap params and remove trailing 0s; gotoXY(0,1) becomes gotoYX(1); gotoXY(0,0) becomes gotoYX()) and optimize gotoYX (line 3 offsets 0x54, which can be achieved using binary)
Code:
void LCD::gotoYX(byte y, byte x){      
  byte dr=x+0x80;      
  if (y&1)       
    dr += 0x40;      
  if (y&2)       
    dr += 0x14;      
  LCD::LcdCommandWrite(dr);        
}
60 Bytes - introduced format1000:
Code:
char* format1000(unsigned long num){
  return format(num*1000);
}
(then changed four occurrences of format(x*1000) to format1000(x) ) and optimized doDisplay7:
Code:
void doDisplay7(void){      
  LCD::gotoYX();LCD::print("C%");LCD::print(format1000(maxLoopLength*100/looptime));LCD::print(" T");LCD::print(format(tank.time()));     
  LCD::gotoYX(1);LCD::print("FREE MEM: ");LCD::print(format1000(memoryTest()));      
}    //display max cpu utilization and ram.
118 Bytes - fixed bigNum and compressed data
Code:
byte bignumchars[]={0x44,0x21,0x44, 0x21,0x44,0x25, 0x43,0x23,0x24, 0x21,0x23,0x44, 0x54,0x52,0x44,   
                    0x24,0x23,0x43, 0x44,0x23,0x43, 0x51,0x41,0x54, 0x44,0x23,0x44, 0x24,0x23,0x44};

void bigPrint(char c, byte line=0, char suffix=' '){
  byte i;
  byte v;
  for(i=0;i<3;i++){
    v = bignumchars[(c-'0')*3 + i];
    if(line){
      v>>4; // shift high nibble low for second line
    }
    v &= 0x0f; // or mask out high nibble for first line
    // above code could have been "else", but that seems to cost 10 bytes!
    
    if(v==5) v=32; // turn 5s into spaces
    LCD::LcdDataWrite(v); // and out
  }
  LCD::LcdDataWrite(suffix);
}

void bigNum (unsigned long t, char * txt1, char * txt2){      
  char  dp = 32; 
 
  char * r;// = "009.99"; //"009.99" "000.99" "000.09" 
  if(t<=99500){ 
    r=format(t/10); //009.86 
    dp=5; 
  }else if(t<=999500){ 
    r=format(t/100); //009.86 
  }   
 
  LCD::gotoYX(); 
  bigPrint(r[2]);
  bigPrint(r[4]);
  bigPrint(r[5]);
  LCD::print(txt1); 
 
  LCD::gotoYX(1); 
  bigPrint(r[2],1);
  bigPrint(r[4],1,dp);
  bigPrint(r[5],1);
  LCD::print(txt2); 
}
320 bytes savings
Available before: 522, now 842.

We could save another 28 bytes (and more), if LCD pins would map to 4,5,6,7...
Code:
void LCD::pushNibble(byte value){
  PORTD = ( PORTD & 0x0f ) | ( value & 0xf0 );
}
  Reply With Quote