# confused for either getting right or worng output

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, Sep 18, 2014.

1. ### ect_09 Thread Starter Member

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

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,

2. ### ericgibbs AAC Fanatic!

Jan 29, 2010
3,234
565
hi ect,
Its reversed, just like Hundreds, Tens, Units
think D7,D6..... D0, so decimal 10 is 00001010
E

3. ### ect_09 Thread Starter Member

May 6, 2012
180
1
yup its exactly right for z=10,
but the code i share with you show strange output... what is the reason

4. ### ericgibbs AAC Fanatic!

Jan 29, 2010
3,234
565
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.

5. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,906
1,789
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.