I am programming for a microcontroller using CCS compiler (which based on C language)
My code is pressing button to select a specified time then using that time to switch on and off a lamp, for e.g
The value I got when press button is an int, with 0x31 stand for '1', 0x32 stand for '2' ... these number '1', '2' ... will appear on screen and they are also stored in an array.
Now I want to change a number "12....." to an int, I am using atoi() (I don't sure, I declare my array[] type int but I think it is 8 bit like char) and test the result on hardware but see no result, I think my code is wrong, could you help me with this, thanks !!!
Here is the code

int array[16]={'1','4','7','<','2','5','8','0','3','6','9','>','C','&','E','M'};
   int keyPressed;
   int timeResult;
// declare functions ...
void main()
{
   // TODO: USER CODE!!
   InitPic();
   InitLcd();
   timeResult=Keypad();
   delay_ms(500);
   do
   {
   output_high(PIN_C0);
   delay_ms(timeResult);
   output_low(PIN_C0);
   delay_ms(timeResult);
   }
   while(1);
}
signed int OnRelease()   // this function work well, it returns exactly the member in array[]
{
   int i,j,k;
   int value[4]={0x07,0x0B,0x0D,0x0E};
   set_tris_a(0x0f);
   k=0;
   for(i=0;i<4;i++)
   {
      output_d(value[i]);
      delay_us(100);
      for(j=0;j<4;j++)
      {
         if(!input(40+j))
         {
            do
            {
               delay_ms(50);
            }
            while(!input(40+j));
            return k;
         }
         else
            k++;
      }
   }
   return -1;
}
//*************************
int Keypad()
{
   char number[];
   int iNum=0;
   int numResult;
   timeResult=0;
   do
   {
   keyPressed=OnRelease();
   if ((array[keyPressed] > 0x2F) && (array[keyPressed] < 0x3A))   // for numbers 0-9 only
   {
      WrDat2Lcd(array[keyPressed]);
      number[iNum]=array[keyPressed];
      iNum++;
   }
   if (array[keyPressed] == 'E')   // press Enter key to exit function
   {
      number[iNum]='\0';
      numResult=atoi(number);
      return numResult;
   }
   }
   while(1);
}

Thanks :)

Recommended Answers

All 2 Replies

line 51: That is an ILLEGAL way to declare an array. You must specify its size.


lines 57 and 58: What happens if OnRelease() returns -1, as it will in line 46? Answer: crash your program.

Where (line number please) do you want to use atol() ? If you are getting the letters from the keyboard one character at a time than atol() may not be necessary.

int key = 0;
char k;
// convert letters to format an integer.
while( (k = getKeybord()) > 0)
{
    key = (key * 10) + k - '0';
}

Hi ! Thanks for your hint, maybe it is enough, no complex but the hardware work well !!!
Thanks so much !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.