confused for either getting right or worng output

Thread Starter

ect_09

Joined May 6, 2012
180
Code:
#include <P18F458.h>
void main(void)
  {           
    char mynum[]= {+1,-1,+2,-2,+3,-3,+4,-4};
    unsigned char z;
    TRISB = 0;           
    for(z=0;z<8;z++)
      PORTB = mynum[z];
    while(1);           
  }

The PIC Micro controller book by MAZIDI show the output=1= FFH, 2=FEH, 3=FDH, 4=FCH ..
but am confused for it. because according to me
1=10000000 its following RB0 to RB7
2=01000000
3=11000000

because when i sent only Z=10 to PORTB
i get the simulation in proteus as given below

10=01010000 RB0 to RB7

please correct me and explain me as well,
am beginner level student ..

Regards,
 

ericgibbs

Joined Jan 29, 2010
18,849
The PIC Micro controller book by MAZIDI show the output=1= FFH, 2=FEH, 3=FDH, 4=FCH
Is your book explaining SIGNED binary numbers.? Your program is using UNSIGNED binary numbers.

ie= FFH = -1, FEH =-2 ?

I use Assembler language, I cannot help with the 'C' language.
 

ErnieM

Joined Apr 24, 2011
8,377
I am not sure what you mean by:

because when i sent only Z=10 to PORTB
i get the simulation in proteus as given below

10=01010000 RB0 to RB7


I believe the author of that tutorial got a little fancy using an array to demonstrate the pitfalls of mixing types in a line. Types get mixed up here in the line "PORTB = mynum[z];" as PORTB is an unsigned char and mynum[] is a (signed) char.

Exactly what happens I am not too clear on as I avoid this situation. If you do this C has to change one form into another. When using a signed byte +1 is 00000001 and -1 is 11111111 as signed numbers are stored in 2's compliment form.
 
Top