StartUSB based on PIC18f2550, MicroC PRO 7.6 and arithmetic expression - issue or feature ?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

StartUSB based on PIC18f2550, MicroC PRO 7.6 and arithmetic expression - issue or feature ?

#1 Post by cpservicespb » 21 Oct 2022 21:01

There is expression:

Code: Select all

unsigned int tmr0 = 65536U - (10 * 1000U * 48 / (4 * 2));
TMR0H=Hi(tmr0);
TMR0L=Low(tmr0);
It has to be

Code: Select all

TMR0H=0x15
TMR0L=0xA0
But actually is

Code: Select all

TMR0H=0xf5
TMR0L=0xA0
The same expression:

Code: Select all

unsigned int tmr0 = 65536U - 10 * (1000U * 48 / (4 * 2));
TMR0H=Hi(tmr0);
TMR0L=Low(tmr0);
Attention - first bracket is moved before 1000 !!

And, bingo !
There is:

Code: Select all

TMR0H=0x15
TMR0L=0xA0

Such is observed not always.
For

Code: Select all

unsigned int tmr0 = 65536U - (1 * 1000U * 48 / (4 * 1));
or

Code: Select all

unsigned int tmr0 = 65536U - 1 * (1000U * 48 / (4 * 1));
is allright.

Why so ?????

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: StartUSB based on PIC18f2550, MicroC PRO 7.6 and arithmetic expression - issue or feature ?

#2 Post by hexreader » 21 Oct 2022 21:38

Short answer - overflow

Long answer:

Code: Select all

unsigned int tmr0 = 65536U - (10 * 1000U * 48 / (4 * 2));
Those values are unsigned integers

Maximum value for an unsigned integer is 65535

10 * 1000 * 48 = 480000

480000 is much larger than the maximum value that unsigned int can be, so it will get truncated from hexadecimal 0x75300 to 0x5300 (decimal 21248)

65536 is too big for unsigned int and will truncate to 0

If you want to do maths on large numbers then use unsigned long values:

Code: Select all

unsigned int tmr0 = 65536UL - (10 * 1000UL * 48 / (4 * 2));
Start every day with a smile...... (get it over with) :)

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: StartUSB based on PIC18f2550, MicroC PRO 7.6 and arithmetic expression - issue or feature ?

#3 Post by cpservicespb » 21 Oct 2022 22:30

Where did you see 480000 and as following overflow ?!
Let' s start to calculate:

Code: Select all

1. 4*2 = 8;
2. 48/8=6;
3. 10*1000*6=60000;
4. 65536-60000=5536;
Or other way:

Code: Select all

1. 10*1000*48=480000;
2. 4*2=8;
3. 480000/8=60000;
4. 65536-60000=553
6;

That is at both cases: 5536=0x15A0.

Where is overflow ??????????

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: StartUSB based on PIC18f2550, MicroC PRO 7.6 and arithmetic expression - issue or feature ?

#4 Post by hexreader » 21 Oct 2022 22:40

Read the help file section "Operators Precedence and Associativity"

.... or any good C language resource ...
Start every day with a smile...... (get it over with) :)

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: StartUSB based on PIC18f2550, MicroC PRO 7.6 and arithmetic expression - issue or feature ?

#5 Post by cpservicespb » 21 Oct 2022 23:09

I think I suppose what you want to say.

I will read ...

Thanks.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: StartUSB based on PIC18f2550, MicroC PRO 7.6 and arithmetic expression - issue or feature ?

#6 Post by hexreader » 22 Oct 2022 00:38

Ah.... I think that I see the flaw in your thinking....

Hopefully your reading will have shown that multiply and divide have equal precedence, and that calculations are performed left to right

This means that your second calculation is in the correct order, not the first one

1. 10 * 1000 * 48 = 480000; // all numbers are unsigned integers - an overflow has already occurred in line 1 causing the result to be 21248, not 480000
2. 4 * 2 = 8;
3. 21248/8 = 2656;
4. 0 - 2656 = 62880; // 65536 overflows to 0 so sum is 0 - 2656 = 62880 (negative numbers impossible for unsigned int, so -2656 underflows to 62880 (hex 0xF5A0)

It is not just the final result that is limited to 0-65535, it is all stages of calculation that are limited to 0-65535
By default numbers are signed integers
Addition of the U after one or more numbers causes the calculations to be unsigned int
Start every day with a smile...... (get it over with) :)

Post Reply

Return to “mikroC PRO for PIC General”