atoi function for a string array

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

atoi function for a string array

1,189 Views
cmha184
Contributor I

I´m working on a calculator but my atoi function isn´t working, its just converting the first digit as I enter it to my array. Any ideas ?

 

 

#include <hidef.h>      // for EnableInterrupts macro
#include "derivative.h" // include peripheral declarations
#include "lcd.h"        // lcd utility functions
#include <stdlib.h>
//#include <extras_stdlib.h>

char texto[5];
char keypressed,noper;
char nBuffer[6];
int  a,b,tst,result=0;

char keyPadMatrix[13] =
{
    '1','2','3',
    '4','5','6',
    '7','8','9',
    '*','0','#',
    0x00
  
};

  

void main(void) {
  char i;
  LCDinit();
   //MCU_init(); /* call Device Initialization */

  /* ### Init_GPIO init code */
  /* PTFDD: PTFDD3=1,PTFDD2=1,PTFDD1=1,PTFDD0=1 */
  PTADD |= (unsigned char)0x0F;                             
  /* ### Init_GPIO init code */
  /* PTBPE: PTBPE7=1,PTBPE6=1,PTBPE5=1,PTBPE4=1 */
  PTCPE |= (unsigned char)0xF0;                             
  /* PTBDD: PTBDD7=0,PTBDD6=0,PTBDD5=0,PTBDD4=0 */
  PTCDD &= (unsigned char)~0x00;                   
  /* ### */
 
  /* include your code here */
  // inicializo mi buffer
  nBuffer[0] = ' ';   
  nBuffer[1] = ' ';
  nBuffer[2] = ' '; 
  nBuffer[3] = ' '; 
  nBuffer[4] = ' ';
  nBuffer[5] = 0;  /*AQUI*/
 

  for(;:smileywink: {   //ciclo infinito
 
      

//* este IF solo ocurre cuando se presiona *, basicamente es un clr :smileyhappy: borra el array, las variables y el display */
    if (keypressed == '*'){   //borrar            
      LCDClearScreen();
      nBuffer[0] = ' ';   
      nBuffer[1] = ' ';
      nBuffer[2] = ' '; 
      nBuffer[3] = ' '; 
      nBuffer[4] = ' ';

      a = 0;
      b = 0;
      result = 0;
 
    }
    keypressed = ScanKeyMatrix();
    if (keypressed != 0x00){
      
        for (i = 0; i < 4; i++){
          nBuffer[i] = nBuffer[i+1];
      
        }
    
     nBuffer[4] = keypressed;
     //a = atoi(nBuffer);  //capturo el valor del buffer = al valor en pantalla
     
     
    
            LCDClearScreen();
            LCDputchar(nBuffer[0]);
            LCDputchar(nBuffer[1]);
            LCDputchar(nBuffer[2]);
            LCDputchar(nBuffer[3]);
            LCDputchar(nBuffer[4]);
        
           
     
     
    }
 
  
    //aqui debo revisar si alguna operacion matematica ha sido escogida


/*Si se presiona push button este realiza una operación según el Case NOPER*/
    if (!(PTAD_PTAD4)){
      a = atoi(nBuffer);  //capturo el valor del buffer = al valor en pantalla
      LCDClearScreen();   //limpio pantalla
      tst = 1;
      noper = 1;   //suma
      nBuffer[0] = ' ';                //
      nBuffer[1] = ' ';                //* el buffer almacena los teclasos de forma FIFO son 5 posiciones porque 16 bits = $FF = 65535 = 5 numeros */
      nBuffer[2] = ' '; 
      nBuffer[3] = ' '; 
      nBuffer[4] = ' ';
     
      while ( !(PTAD_PTAD4) ){
      }
    }

Labels (1)
0 Kudos
6 Replies

715 Views
AirDragon
Contributor III

I didn't see any function in your infinite for loop that checks the keypad... nor any de-bouncing.

 

Edit:

 

I see it now, down at the bottom. I... don't quite understand it though. What kind of keypad are you using?

0 Kudos

715 Views
cmha184
Contributor I

its a 7 pin matrix keypad

0 Kudos

715 Views
AirDragon
Contributor III

Ok, do you have the "char ScanKeyMatrix( void )" function defined?

 

For a matrix keypad, you usually have a driver function that "scans" each key that powers (or grounds) a single row  and then proceeds to check the coloums for a pressed key. In the C language, the driver can be made like so:

 

// PA[7:4] = Ouput (rows)// PA[3]   = Unused// PA[2:0] = Input (columns)PORTA = 0x10;    // Power row 1 onlychar key_pressed = ( PORTA & 0x0FU );  // load temporary buffer with only the lower 4 bits of PORTAswitch (key_pressed){  case 0x01U:    // Row 1, Column 1 key pressed  case 0x02U:    // Row 1, Column 2 key pressed  case 0x04U:    // Row 1, Column 3 key pressed}PORTA = 0x20;    // Power row 2 only// etc...

 This simple function (or at least the algorithm) works fairly well for single key presses, but if you want to do fancy stuff like Shift+A, you'll have to come up with something creative.

0 Kudos

715 Views
cmha184
Contributor I

My problem goes in this point

 if (keypressed != 0x00)
        {
            //for (i = 0; i < 4; i++)
              //  {
                  //nBuffer[i] = nBuffer[i+1];  
                    nBuffer[i] = ((keypressed));
                    nBuffer[i+1] ='\0';
                    //nBuffer[i] = nBuffer[i+1];
                //}
            
               nBuffer[4] = keypressed;
               //a = atoi(nBuffer);  //capturo el valor del buffer = al valor en pantalla
              
                LCDClearScreen();
                LCDputchar(nBuffer[0]);
                LCDputchar(nBuffer[1]);
                LCDputchar(nBuffer[2]);
                LCDputchar(nBuffer[3]);
                LCDputchar(nBuffer[4]);
      
        }

 

If I press a button from my keyboard it starts filling an array and when I go to the point of pressing a button apart from my keyboard which in this case is PTAD_PTAD4, the ITOA function only converts one the first of the characters entered from my array

 

if (!(PTAD_PTAD4)){
      a = atoi(nBuffer); 
      //a = 35;
      //LCDClearScreen();  
      tst = 1;
      noper = 1;   //suma
        /*
        nBuffer[0] = '\0';   
        nBuffer[1] = '\0';
        nBuffer[2] = '\0'; 
        nBuffer[3] = '\0'; 
        nBuffer[4] = '\0';
        */
      while ( !(PTAD_PTAD4) ){
      }
     
      itoa(a);
      //LCDputchar(prueba);
      LCDputchar(texto[0]);
   LCDputchar(texto[1]);
   LCDputchar(texto[2]);
   LCDputchar(texto[3]);
   LCDputchar(texto[4]);
    }

0 Kudos

715 Views
cmha184
Contributor I

Working basic operations calculator for HSC08GB60

Thanks to http://www.roboticsguy.com/blog/1/entry-23-writing-an-itoa-function/ 

 

 

0 Kudos

715 Views
bigmac
Specialist III

Hello,


cmha184 wrote:

Working basic operations calculator for HSC08GB60

Thanks to http://www.roboticsguy.com/blog/1/entry-23-writing-an-itoa-function/ 

  


The thread found here also addressed the atoi requirement in a similar manner, but without the need to separately determine the number of digits prior to conversion.

 

Regards,

Mac

0 Kudos