This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

unsigned long to digits for LCD display

Hi,First of all, Merry Christmas! I got a problem with the little function below: I just try to display an unsigned long variable on a 6 digits LCD (I know, I m not going to be able to display the whole number)Everything works fine from 0 to 69999 but there is a problem from 70000 to 100000 and then it works fine again. I would understand if the problem happens after 65535 but...no... Thank you for any idea, sorry for my English an , again, Merry Christmas!

 unsigned long count;

unsigned char tab[8];


void compt_to_digit_4b(unsigned long count_4b, unsigned char *tab_compt)
{
unsigned char centmillier;
unsigned char dimillier;
unsigned char millier;
unsigned char centaine;
unsigned char dizaine;
unsigned char unite;
unsigned char dixieme;
unsigned char centieme;

//unsigned char incr_x;

/*for(incr_x=0; incr_x<8;incr_x++)
{
tab_compt[incr_x]= 0x00;
}
*/
centmillier=0;
dimillier=0;
millier=0;
centaine=0;
dizaine=0;
 unite=0;
 dixieme=0;
 centieme=0;

//if(ind==1)
//{
centmillier=(unsigned char)(count_4b/10000000);
tab_compt[0]= centmillier;

count_4b=(count_4b - (unsigned long)(centmillier*10000000));
dimillier=(unsigned char)(count_4b/1000000);
tab_compt[1]= dimillier;

count_4b=(count_4b - (unsigned long)(dimillier*1000000));
//}
millier=(unsigned char)(count_4b/100000);
tab_compt[2]=millier;

count_4b=(count_4b - (unsigned long)(millier*100000));
centaine=(unsigned char)(count_4b/10000);
tab_compt[3]= centaine;

count_4b=(count_4b - (unsigned long)(centaine*10000));
dizaine= (unsigned char)(count_4b/1000);
tab_compt[4]=dizaine;

count_4b = (count_4b - (unsigned long)(dizaine*1000));
unite= (unsigned char)(count_4b/100);
tab_compt[5]= unite;


return;
}


void main(void)
{
compt_to_digit_4b(70000,tab);
}
Arnaud Stoumont

  • The following code works just fine:

    #define LCD_LEN 8
    unsigned char tab[LCD_LEN];
    
    void compt_to_digit_4b (
      unsigned long count_4b,
      unsigned char *tab_compt)
    {
    unsigned char i;
    
    for (count_4b, i=LCD_LEN; i>0; i--, count_4b/=10L)
      {
      tab_compt[i-1] = count_4b%10L;
      }
    }
    
    void main(void)
    {
    compt_to_digit_4b(70000L,tab);
    compt_to_digit_4b(12345678L,tab);
    compt_to_digit_4b(98765L,tab);
    while (1);
    }
    

    Jon

  • Wow wow wow wow!
    Thank you very much Jon!!

    Everything works fine now.

    I just wonder why my code was wrong. I guess it's just not the right way to do this.
    If somebody got some time to tell me why, I would be really happy!

    Anyway, I wish you a merry Christmas! Mine is going to be better due to you! Thanks again!

    Arnaud Stoumont