gnupic: Re: [gnupic] Re: PIC18F47J53
Subject:
Re: [gnupic] Re: PIC18F47J53
From:
Bob Jacobsen ####@####.####
Date:
17 Aug 2013 17:39:43 -0000
Message-Id: <031AB147-7117-46CE-B87A-4C395647F875@lbl.gov>
On Aug 17, 2013, at 7:06 PM, Luis de Arquer ####@####.#### wrote:
>
> First case -> works great:
> --------------------------------
>
> void sleep_a_while(byte c)
> {
> byte i,j;
>
> for(i=0; i<200; i++)
> for(j=0; j<c; j++) ;
> }
>
>
> **********************************************************
> **********************************************************
>
> Second case: delay independent on input parameter c
> ---------------------------------------------------------------------
>
> void sleep_a_while(byte c)
> {
> int i,j; /* This is the only change! */
>
> for(i=0; i<200; i++)
> for(j=0; j<c; j++) ;
> }
>
> In both cases, the function is called like
>
> MOVLW 0xd0 ;whatever parameter
> MOVWF POSTDEC1
> CALL _sleep_a_while
>
Decimal 208 is the integer 0x00D0. In C, int is a signed type.
C does down-conversion by truncation. If you then convert that 208 to a byte type, it's 0xD0, which is a _negative_ number.
When j is an int, the comparison is signed, so c is sign-extended to an int and will immediately be less than j, hence fall through.
Moral: If you want unsigned counters, tell C to use unsigned types.
Bob
--
Bob Jacobsen, LBNL Physics Division
####@####.#### +1-510-486-7355 AIM, Skype JacobsenRG