Strage behavour, shouldn't be hard to figure out...

Discussion in 'Embedded Systems and Microcontrollers' started by dannybeckett, Mar 8, 2011.

  1. dannybeckett

    Thread Starter Active Member

    Dec 9, 2009
    163
    0
    Hi guys -

    I am writing some code that involved bit shifting, but I am getting strange outputs from my microcontroller. Save me explain whats going on, I will share my test code and results with you.

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #define _XTAL_FREQ 8000000
    4. __CONFIG (FOSC_XT & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF);
    5.  
    6. void initDigitalOut() {
    7.     PORTA = PORTB = PORTC = PORTD = 0;
    8.     TRISA = TRISB = TRISC = TRISD = 0;
    9.     ANSEL = ANSELH = 0;
    10. }
    11.  
    12. test0() {
    13.     int temp;
    14.     int y = 0xFF;                // 11111111
    15.     PORTB = y;
    16.     __delay_ms(1000);
    17.     for (int x = 0; x < 8; x++) {
    18.         y = y >> 1;
    19.         PORTB = y;
    20.         __delay_ms(1000);
    21.     }
    22. }
    23.  
    24. test1() {
    25.     int y = 0x88;                // 10001000
    26.     PORTB = y;
    27.     __delay_ms(1000);
    28.     for (int x = 0; x < 8; x++) {
    29.         y = y >> 1;
    30.         PORTB = y;
    31.         __delay_ms(1000);
    32.     }
    33. }
    34.  
    35. test2() {
    36.     int y = 0x8888;                // 1000100010001000
    37.     PORTB = y;
    38.     __delay_ms(500);
    39.     for (int x = 0; x < 16; x++) {
    40.         y = y >> 1;
    41.         PORTB = y;
    42.         __delay_ms(500);
    43.     }
    44. }
    45.  
    46. test3() {
    47.     int y = 0x7888;                // 0100100010001000
    48.     PORTB = y;
    49.     __delay_ms(500);
    50.     for (int x = 0; x < 16; x++) {
    51.         y = y >> 1;
    52.         PORTB = y;
    53.         __delay_ms(500);
    54.     }
    55. }
    56.  
    57. test4() {
    58.     unsigned int y = 0x7888U;    // 0100100010001000
    59.     PORTB = y;
    60.     __delay_ms(500);
    61.     for (int x = 0; x < 16; x++) {
    62.         y = y >> 1;
    63.         PORTB = y;
    64.         __delay_ms(500);
    65.     }
    66. }
    67.  
    68. test5() {
    69.     unsigned int y = 0x8888;    // 1000100010001000
    70.     PORTB = y;
    71.     __delay_ms(500);
    72.     for (int x = 0; x < 16; x++) {
    73.         y = y >> 1;
    74.         PORTB = y;
    75.         __delay_ms(500);
    76.     }
    77. }
    78.  
    79. void main() {
    80.     initDigitalOut();
    81. //    test0();    // Worked fine.
    82. //    test1();    // This worked fine. Bits shifted along nicely and the last loop outputted a fully-off PORTB.
    83. //    test2();    // This worked fine until the most significant 1 appeared... It left a trail of 1s behind it!
    84. //    test3();    // This produced a pattern of 0111100010001000, traversing PORTB, the trailing LEDs were all off this time though.
    85. //    test4();    // Same as test3. Any combination of U and unsigned didn't address this issue.
    86.     test5();    // Worked as expected. U suffix didn't seem to make a difference, unsigned did though. Tried all combinations of both.    
    87. }
    88.  
    I found the answer to the 'trailing' LEDs problem (unsigned variable) but I cant seem to figure out test4()'s behaviour. Anyone shed any light to the matter?

    Thanks

    Dan
     
    Last edited: Mar 8, 2011
  2. dannybeckett

    Thread Starter Active Member

    Dec 9, 2009
    163
    0
    Please ignore this thread, everything works as planned.
     
Loading...