Using an ATMEGA48V-10PI

I have written my code.

compiled it comes to <4kb.

looking at the atmegas the ATMEGA48 looks to be suitable to hold that.

I am planning to use this for serial communication

and http://ntsdt.net/2009/08/07/programming-an-atmega8-using-arduino/ for programming.

how do I go about building the simplest circuit for it (i.e what differences from here http://www.arduino.cc/en/Main/StandaloneAssembly )

Also how do I set fuses(etc) and to what?

compiled it comes to <4kb

What about SRAM? How much does your Sketch need?

ATMEGA48V-10PI

Does the Sketch work correctly at 8 MHz?

Quote:
compiled it comes to <4kb

What about SRAM? How much does your Sketch need?

Quote:
ATMEGA48V-10PI

Does the Sketch work correctly at 8 MHz?

I assume sram is the memory for variables.

(globals)
2 byte
3 Boolean
2 char

setup():
1 byte

analogToSerial(1 byte):
1 byte
1 int

Motor(2 byte):
2 boolean

serialCommand(1 byte):
2 byte

AlignFrame()
1 byte
1 int

loop()
5 byte
1 int

Program flow:

loop calls
1)serialCommand which calls
a)analogToSerial
b)Motor
2)AlignFrame(which calls motor)

I do not know if it runs at 8MHz. the only timing issue would be the Serial communication.

Full Code:

/*

                              
      Digital                  Analog      
      0      Serial *            0      Current1
      1      Serial *            1      Current2
      2      serial select            2      Led1
      3      Input Selection(Done)      3      Led2
      4      In1A                    4      Postion
      5      In2A                    5      
      6      In1B                  
      7      In2B                  
      8            
      9      Stby(done)                  
      10      Pwm(Done)                  
      11      PosA                  
      12      PosB                  
      13      PosC                  


*/



byte mtrSpeed=128;

boolean standBy=false;
boolean Stall[2]={false,false};

#define StartChar  'f'
static char address = '0';
boolean align=false;

void setup() {
  Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
  
  pinMode(2, OUTPUT); //serial mode select
  
  
  pinMode(3, OUTPUT); //set LDR selector to ouput mode
  
  
  pinMode(4, OUTPUT); //motor directional control
  pinMode(5, OUTPUT); //motor directional control
  pinMode(6, OUTPUT); //motor directional control
  pinMode(7, OUTPUT); //motor directional control
  
  pinMode(9, OUTPUT); //set standby to ouput mode
  pinMode(10, OUTPUT); //set Motor PWM selector to ouput mode
  
  pinMode(11, INPUT); //AddressA
  pinMode(12, INPUT); //AddressB
  pinMode(13, INPUT); //AddressC
  
  byte mod=0;
   
  for(byte i=13;i>=11;i--){
    mod = mod * 2;
    if( digitalRead(i)){
      mod = mod  + 1;
    }
  }
/*
  byte mod = digitalRead(11) ;
  
  if(digitalRead(12)){ mod += 2; }
  if(digitalRead(13)){ mod += 4;}
  */
  
  
  address = mod + address;
  
}

void analogToSerial(byte number){
  byte NewNum= number -48;
  //0-currentA (0)
  //1-currentB (1)
  //2-PostionA (2)
  //3-PostionB (3)
  //4-Ldr1 (4)(pin D13 low)
  //5-Ldr2 (5)(pin D13 low)
  //6-Ldr3 (4)(pin D13 High)
  //7-Ldr4 (5)(pin D13 High)
  //8- MotorSpeed
  
  int inpute;
    
  if(NewNum ==8){
     inpute = mtrSpeed;
     
  }else{
        
      if(NewNum > 5){
        
        digitalWrite(3,HIGH);
        NewNum = NewNum - 2;
      }else if(NewNum > 3 ){
        digitalWrite(3,LOW);  
      }
    
      
    inpute = analogRead(NewNum)/4;
    
     
    if(NewNum < 2){
      if(Stall[number]){
         inpute = 255; 
      }
    }
    
  
  }
  byte chrOut = inpute;
  
  
  digitalWrite(2,HIGH);    
  Serial.print(StartChar);
  Serial.print(0);
  Serial.print(inpute,BYTE);
  Serial.print(StartChar);
  digitalWrite(2,LOW);  

}

void Motor(byte motor,byte clockwise){
if(Stall[motor]){
 delay(10); 
}
  Stall[motor] = false;   
  
   digitalWrite(9,standBy);      
  analogWrite(10,mtrSpeed); //set speed control
      
  boolean DirA=false;
  boolean DirB=false;
  
  if(clockwise == 0){ DirA=true; }
  if(clockwise == 1){ DirB=true; }
  
  if(DirA){
    digitalWrite(4+motor*2,HIGH); 
  }else{
    digitalWrite(4+motor*2,LOW);    
  }
  
  if(DirB){
    digitalWrite(4+motor*2,HIGH); 
  }else{
    digitalWrite(4+motor*2,LOW);    
  }
  
  if(analogRead(motor) > 100){ //Stall Sensor
      Stall[motor] = true;
      digitalWrite(4+motor*2,LOW); 
      digitalWrite(4+motor*2,LOW);    
  } 
  
  
}

void serialCommand(byte command){
  if (command >= '0' && command <= '8'){    
      analogToSerial(command);
  }else if (command >= 'A' && command <= 'B'){    
    byte dir = command-65;
    byte motor=0;
    if(dir > 3){
      motor =1;
      dir=dir-3; 
    }
      Motor(motor,dir);  
    
    
  }else{
  switch(command){
    case 'd':
       if(mtrSpeed > 5){      mtrSpeed -= 5;   }else  {      mtrSpeed =0;   }       
     break; 
    case 'u':
       if(mtrSpeed < 250){      mtrSpeed += 5;   }else  {      mtrSpeed =255;   }       
     break; 
     
    case 'h': //halt
        standBy=false;
        digitalWrite(9,standBy);
        break;
    case 's': //go
        standBy=true;
        
    case 'a':
        align = true;
    case 'b':
        align = false;
  }
    
  }
}

void AlignFrame(){
    
  for(byte i=0;i<2;i++){
    digitalWrite(3,i);    
    int dif = abs(analogRead(5)-analogRead(4));
                
        
    dif = dif/dif+1;
    
    Motor(i,dif);      
  }
}

void loop() {
  
int  i=0;

byte srByte[4] = {0,0,0,0};      // for incoming serial data

      // send data only when you receive data:
      if (Serial.available() > 0) {
            // read the incoming byte:
      

            for(byte  j=0;j<=3;j++){
              
                 srByte[j] = Serial.read();
                  while (Serial.available() == 0 || i>1000 ) { 
                      i++; 
                  }
            }

           if(srByte[0]==StartChar){
           if(srByte[1]==address ){
           if(srByte[3]==StartChar ){              
            serialCommand(srByte[2]);
           }
           }
           }
          
Serial.flush();

      }
if(align){
AlignFrame();
}
}

After eye-balling your code...

I do not know if it runs at 8MHz. the only timing issue would be the Serial communication.

That's all I can see that would be affected. I mention "8 MHz" because the processor you've chosen is only rated to run up to 10 MHz. (that's the "-10" in the part number")

    dif = dif/dif+1;

After that expression, dif will always be 2. Is that your intention?

I can't see anything that would prevent your Sketch from running on a '48 processor.

dif = dif/dif+1;
After that expression, dif will always be 2. Is that your intention?

No. [smiley=embarassed.gif] Thanks.

It is mean to change -,0,+ to 0,1,2(CW,break,CCW)

Yikes! My apologies. I failed to consider negative values. But, it still won't work...

-2 / -2 = 1
-1 / -1 = 1
0 / 0 = ?
+1 / +1 = 1
+2 / +2 = 1

You'd be much better off using a series of "if" statements. Division is rather expensive on AVR processors and the "if"s will make it crystal clear what you're trying to accomplish.

Yikes! My apologies. I failed to consider negative values.

But, while we're on the subject, you'd be better off using a series of "if" statements. Division is rather expensive on AVR processors.

-1/-1 still equals 1.

the single / is 2 bytes less then

    if(dif>0){
     dif=0;
    }else if(dif>0){
     dif=1;      
    }else{
     dif=2;
    }

(3958 total)

    if(dif>0){
     dif=0;
    }else if(dif>0){
     dif=2;      
    }else{
     dif=1;
    }

(3952 total)

but that one works

[edit]And the working code:

    if(dif<0){
     dif=0;
    }else if(dif>0){
     dif=2;      
    }else{
     dif=1;
    }

(3968)[/edit]

Try changing this...

   inpute = analogRead(NewNum)/4;

...to this...

   inpute = analogRead(NewNum) >> 2;

I doubt it will make a difference but it's still worth trying.

Back to your original question...

how do I go about building the simplest circuit for it (i.e what differences from here http://www.arduino.cc/en/Main/StandaloneAssembly )

Will you be using an external crystal / resonator or the internal oscillator?

Also how do I set fuses(etc) and to what?

Spend a few minutes here...
http://www.engbedded.com/fusecalc/

If you have specific questions, report back.

I doubt it will make a difference but it's still worth trying.

well no difference in compile size.

Will you be using an external crystal / resonator or the internal oscillator?

I do not know.

What are the advantages of one over the other?

If you have specific questions, report back.

Thanks.

Bolded where I need help

CKDIV8 - don't know what this mean
CKOUT -I am not driving anything so un-programmed
SUT1 - un-programmed
SUT0 - programmed (to give me 65ms time to use ICSP? )
CKSEL3
CKSEL2
CKSEL1
CKSEL0-will decide on what clock source is used (when I decide what I am using )

RSTDISBL un-programmed
DWEN un-programmed
SPIEN programmed(so ICSP)
WDTON What is the Watchdog Timer
EESAVE -un-programmed not using EEPROM
BODLEVEL2
BODLEVEL1
BODLEVEL0-000 Not using the Brown-out

SELFPRGEN -I do not know this one

Will you be using an external crystal / resonator or the internal oscillator?

What are the advantages of one over the other?

Crystal --> Most accurate. Requires three external components. Uses two pins on the chip. Most expensive (but not much more than a resonator). Available for virtually any frequency.

Resonantor --> Accurate but not as accurate as a crystal. Only one external component. Uses two pins on the chip. Available for virtually any frequency.

Internal Oscillator --> Least accurate. Factory guarantees ±10%. Can be tuned to ±1%. No external components. No pins used. Can used at 1 MHz or 8 MHz.

CKDIV8 - don't know what this mean

The processor runs at either the clock frequency or the clock frequency divided by 8. This setting is typically used to run the processor at 1 MHz from the internal 8 MHz oscillator.

SUT1, SUT0

The "start up time" is how long the clock takes before it is reliable. Basically, when the processor is starting up, it is held in reset until this time has elapsed. It gives the clock time to stabilize. I always use the longest start-up time. Someone else will have to advice you about using shorter times.

WDTON What is the Watchdog Timer

It's a timer. :smiley:

When the watchdog timer elapses, the processor is reset (or an interrupt is generated). There is an instruction that resets the timer. The theory is that if a Sketch takes too long to "pet the dog" (reset the watchdog timer) then the Sketch has stopped working correctly and the watchdog takes control (resets the processor). By resetting, the Sketch has an opportunity to try to bring the situation under control. Until you have a full understanding of how it works, leave this option at WDTON=0.

EESAVE -un-programmed not using EEPROM

I suggest programming this option. When using an ICSP, the EEPROM erase is skipped which should make uploading a bit faster. In addition, in my experience, erasing the EEPROM is not desired.

SELFPRGEN -I do not know this one

I believe this option is necessary for a bootloader to work.

Thanks.

I think I will try the internal one and see how it goes from there.