# 3 Digit binary to decimal conversion

Discussion in 'Embedded Systems and Microcontrollers' started by RRITESH KAKKAR, Dec 29, 2014.

1. ### RRITESH KAKKAR Thread Starter Senior Member

Jun 29, 2010
2,831
89
Hello,
I have done 2 digit counter successfully with E2PROm of PIC16F877a in H tech c code.
but i have problem with 3 digit after 99 something like that the middle display get suck and brightness is now also issue.
here is my code and i am scanning it as usual

please suggest if something is wrong in it...

Code (C):
1.  TRISB = 0X00 ;
2.     TRISC = 0B00000011 ;
3. unsigned int a=0;
4. unsigned int b=0;
5. unsigned int div;
6. unsigned int rem;
7. while(1){
9. PORTC=0b10000000;
10. rem = a%10;
11. PORTB=data[rem];
12. __delay_ms(5);
13. PORTC=0b01000000;
14. div=a/10;
15. PORTB=data[div];
16. __delay_ms(5);
17.
18. PORTC=0b00100000;
19. div=a/100;
20. PORTB=data[div];
21. __delay_ms(5);
22.
23.
24.
25.
26.
27.
28. if(RC0==1){
29. __delay_ms(100);
30. a=a+1;
31. // eeprom_write(0x00, a);
32. if(a>999){
33. a=999;
34. }}
35.   if(RC1==1){
36. __delay_ms(100);
37. //eeprom_write(0x00, a);
38. a=a-1;
39. if(a>999){
40. a=0;
41. }
42. }
43.     }
44.         }

2. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,434
1,625
Your basic idea is fine but you lost track of it past the units display.
When you do a modulus division by 10 you get the units digit, but the original variable remains unchanged. You would have to do the following:
Code (Text):
1.
2.     PORTC=0b10000000;
3.     rem = a%10;
5.     PORTB=data[rem];
6.     __delay_ms(5);
7.     PORTC=0b01000000;
8.     rem=a%10;        // was div
10.     PORTB=data[rem];    // was div
11.     __delay_ms(5);
12.     PORTC=0b00100000;
13.     // div=a/100;        // will ONLY work when a<1000
14.     // PORTB=data[div];    // will ONLY work when a<1000

3. ### RRITESH KAKKAR Thread Starter Senior Member

Jun 29, 2010
2,831
89
Hello,

I have updated the code as per your suggestion, but the problem is that the eeprom i think save data 8 bits i.e. 255 only in display so, i need the data should be at 999 how to move above 255 as i increment it it shows 000 please tell how to do this....

Code (C):
1. #include <htc.h>
2. #include <stdio.h>
3. __CONFIG(LVP_OFF& BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
4. #define _XTAL_FREQ 20000000
5.
6. char data[13]={  0b01111110,0b00010010,0b10111100,0b10110110,0b11010010,0b11100110,0b11101110, 0b00110010,0XFF,0b11110110};
7. main()
8.     {
9.     TRISB = 0X00 ;
10.     TRISC = 0B00000011 ;
11. unsigned int a=0;
12. unsigned int b=0;
13. unsigned int div;
14. unsigned int rem;
15. while(1){
16.
18.
19. PORTC=0b10000000;
20. rem = b%10;
21. b=b/10;
22. PORTB=data[rem];
23. __delay_ms(10);
24. PORTC=0b01000000;
25. rem=b%10;
26. b=b/10;
27. PORTB=data[rem];
28. __delay_ms(10);
29.
30. PORTC=0b00100000;
31. rem=b%10;
32.
33. PORTB=data[rem];
34. __delay_ms(10);
35.
36.
37. if(RC0==1){
38. __delay_ms(90);
39. a=a+1;
40.
41. eeprom_write(0x00, a);
42.
43. if(a>999){
44. a=999;
45. }}
46.
47.   if(RC1==1){
48. __delay_ms(90);
49. eeprom_write(0x00, a);
50.
51. a=a-1;
52. if(a>999){
53. a=0;
54. }
55. }
56.     }
57.         }
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.

Jun 29, 2010
2,831
89
5. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,434
1,625
Note the method you link to employs common anode displays with two sub sets of anodes, each connected to four elements. Also, the post concerns the display having a lack of brightness, so it is not a good source of information for making a bright display. Someone does make good points about not driving LEDs directly from port pins without resistors.
No comparison can be made to your work as your work is a mystery without a schematic being posted.