What is the best coding practice

Discussion in 'Programmer's Corner' started by t06afre, Jun 29, 2010.

  1. t06afre

    Thread Starter AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I am using HI-Tech PICC16. I am also quite new to C and microcontrollers only a few months. Then it comes to bit operations included testing on a variable. I found the whole thing somewhat awkward. As C is not very good at such operations. We can use macros but I do not like that concept. I think it obscure the code. So I came up with using UNION. To illustrate I have made two code examples. Both compile to the exact same machine code. But then it comes to coding practice which one is best or most easy to understand. If you saw the code for the first time.
    First the macro variant
    Code ( (Unknown Language)):
    1.  define _XTAL_FREQ 4000000
    2. #include <htc.h>
    3. #define bitset(var, bitno) ((var) |= 1UL << (bitno))
    4. #define bitclr(var, bitno) ((var) &= ~(1UL << (bitno)))
    5. #define toggle_bit(var,bitno) (var ^= (1UL << bitno))
    6. #define test_bit(var,bitno) (var & (1UL << bitno))
    7. #define  DISP_EN 0
    8. #define  DISP_RS 1
    9. #define  DISP_RW 2
    10. char unsigned test_var;
    11. char unsigned LCD;
    12. void main(void)
    13. {
    14.  LCD=0x00;
    15.  test_var=0x00;
    16.  TRISC=0;
    17.  while(1)
    18.   {
    19.    LCD=240;
    20.    test_var=240;
    21.    bitset(LCD, DISP_EN);
    22.    bitset(LCD, DISP_RW);
    23.    bitset(LCD, DISP_RS);
    24.     if (test_bit(LCD, DISP_RS))
    25.      {(bitclr(LCD, DISP_EN);}
    26.    PORTC=test_var;
    27.    PORTC=LCD;
    28.   }
    29. }
    30.  
    Then the UNION variant
    Code ( (Unknown Language)):
    1.  #define _XTAL_FREQ 4000000
    2. #include <htc.h>
    3. char unsigned test_var;
    4. union {
    5.    char unsigned data;
    6.   struct {
    7.   unsigned  DISP_EN:1;
    8.   unsigned  DISP_RS:1;
    9.   unsigned  DISP_RW:1;
    10.     }flags;
    11. }LCD;
    12. void main(void)
    13. {
    14.  LCD.data=0x00;
    15.  test_var=0x00;
    16.  TRISC=0;
    17.  while(1)
    18.   {
    19.    LCD.data=240;
    20.    test_var=240;
    21.    LCD.flags.DISP_EN=1;
    22.    LCD.flags.DISP_RW=1;
    23.    LCD.flags.DISP_RS=1;
    24.     if (LCD.flags.DISP_RS)
    25.      {LCD.flags.DISP_EN=0;}
    26.    PORTC=test_var;
    27.    PORTC=LCD.data;
    28.   }
    29. }
    30.  
     
  2. eblc1388

    Senior Member

    Nov 28, 2008
    1,542
    102
    Isn't the answer obvious?

    Some programmers have already been doing that all the times.

    Just so you wonder why someone would read the ADCH result into an integer and then shift it eight times before adding the ADCL result to get the full 10-bit result.

    UNION...UNION...UNION.
     
  3. t06afre

    Thread Starter AAC Fanatic!

    May 11, 2009
    5,939
    1,222

    UNION...UNION...UNION.[/QUOTE]
    Thank you for the answer. I could not find any note of it in the PICC manual, or the web. And I am quite new to C.
    By the way will this be the correct setup for parseing the ADC data into a unsigned int
    Code ( (Unknown Language)):
    1.  
    2. [LEFT]union { unsigned int RESULT;
    3.           unsigned char DATA[sizeof(unsignned int)];
    4.          } CONV;
    5.          //later in the code
    6.          CONV.DATA[0]=ADRESL;
    7.          CONV.DATA[1]=ADRESH;
    8.  
    9. [FONT=CourierNew][SIZE=1][FONT=CourierNew][SIZE=1][SIZE=2]         CONV.RESULT++;[/SIZE][/SIZE][/FONT][/SIZE][/FONT][/LEFT]
    10.  
     
Loading...