XC8 Calculate Thousands, Ten Thousands, Hundred Thousands & Millions place - Page 1
Forum section: electronics / general

author: Your_Boy_Yuriy
author_rank: Newbie
timestamp: October 11, 2021, 07:35:42 pm
content: Hello,I am having trouble how to thousands, ten thousand, hundred thousands & millions place. Let me know if more explanation is needed?- PIC18F4520- XC8- Passing in unsigned int into the loadvoid __interrupt() isr(void){// 32MHZ CLOCK DIVIED BY 4 = FOSC 8HMZ// PRESCALE IS SET TO 256 THUS 256/8MHZ IS 32us PER A SINGAL TMR0 COUNTER// TIMER0 IS SET TO 16 BIT REGISGER: 65536 COUNTER. THUS 32us * 65536 = 2.097152 SECONDS// WITH THE TIMER0 COUNTER SET TO 32768 WHICH IS 65536/2. 32us * 32768 = 1.04 SECONDS PER INTERRUPT// TIMER0 = 31249 for one secondstatic int temp = 17000;if (temp < 1000000)temp= temp+1000;elsetemp = 0;/*counterN1 = temp / 100; // Finds the hundredth placecounterN2 = temp % 100 / 10; // Finds the tenth placecounterN3 = temp % 10; // Finds the Single place*/// CCPR2L = 100;load(n3[temp % 1000 /100 ]|n2[temp % 100 / 10]|n1[temp % 10],n4|n5|n6);//TMR0 = 32768; // PRESET THE 16 BIT TIMER0 COUNTER TO HALF ROUGLY 1 SECTMR0 = 40000; // doesn't matter right nowINTCONbits.TMR0IF = 0;// CCPR2L = 0; // leave alone for now}

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 11, 2021, 09:03:22 pm
content: Not sure what your question is exactly, but one remark already:XC8's default size for 'int' is most likely 16-bit. It may be modifiable through compiler options, but I wouldn't mess with that.So as it is, your code won't work.As XC8 supports at least C99, just use stdint types instead, if you want to make sure a given integer has a specific size.If you need integers in the order of at least 1 million, you can use the 'int32_t' type. (You need to #include <stdint.h>)As far as I've read, XC8 was also supporting 24-bit integers natively (int24_t for instance), which would probably be enough here, but apparently they removed support for them. While they probably took less memory space, they were not necessarily a lot more efficient in compiled code. You can try and see for yourself though, in case XC8 still supports them.

author: rstofer
author_rank: Super Contributor
timestamp: October 12, 2021, 01:03:29 am
content: After you get numeric values for each digit, are you going to convert the value to ASCII and print it?If so, maybe you could consider the itoa() function (integer to ASCII). I use the version from "The C Programming Language" book by Kernighan and Ritchie but this one looks very much the same.https://www.geeksforgeeks.org/implement-itoa/There might already be a library function.If you're just going to display the values in LEDs or something then conversion to ASCII probably isn't required.

author: JPortici
author_rank: Super Contributor
timestamp: October 12, 2021, 03:15:48 am
content: Quote from: SiliconWizard on October 11, 2021, 09:03:22 pmit isQuoteit is notQuoteit does (__int24 / __uint24 in C99 mode, everything is in the compiler manual amyway)QuoteThey did in XC8 1.40-something, the first release with C99, but there was an uproar and in less than a week an update was available that restored support for 24bit types, this was a few years ago and in 2.32 i am happiy using __int24.Quotei can assure you that there is a noticeable gain in code speed, even with PIC18division and multiplication really benefit from using the 24bit type

author: Your_Boy_Yuriy
author_rank: Newbie
timestamp: October 12, 2021, 04:40:16 pm
content: Quote from: SiliconWizard on October 11, 2021, 09:03:22 pmThanks, that was my issue. Just used __uint24 and it works as it should. Happy that XC8 support larger intQuote from: rstofer on October 12, 2021, 01:03:29 amNo ASCII conversion going on here. n1-6[] are basically data for shift regesiter.Here is the working code:for(;; ){static __uint24 temp = 312512;if (temp < 1000000)temp++;elsetemp = 0;load(n1[temp % 10]|n2[(temp/10)%10]|n3[(temp/100)%10],n4[(temp/1000)%10]|n5[(temp/10000)%10]|n6[(temp/100000)%10]);__delay_ms(500);}So, n1| n2|n3, n4|n5|n6. I actually didn't need to calculate the million's place. 24-bit integer will work for the million's.