Port writing????

Help
sansenan
2008-05-05
2013-03-12
  • sansenan
    sansenan
    2008-05-05

    Hello,

        How do you access individual bits for PIC 18F4550 in SDCC,

    for Example

    One can write to any Port By using
    PORTB = 0xFF;

    But if you want to set a particular bit in PORT B

    you cant use PORTB.5=1;

    Is there any alternate of doing this??

    Ie I have different Port bits configured as input and output...
    Say for example I have PORTB 6th and 4th pin as input and  rest(i.e 7,5,3,2,1) is output.
    So how do i individually set high or low at these PINS

    thanks again
    sans

     
    • Raphael Neider
      Raphael Neider
      2008-05-05

      Try ANSI C'ish bit-operations:

      PORTB |= 0x01;      // set bit 0
      PORTB &= ~0x40;     // clear bit 6
      PORTB ^= (1 << 3);  // toggle bit 3

      SDCC will generate BSF, BCF, and (maybe) BTG instructions iff the literal is (a) a literal or constant expression and (b) has exactly one bit to operate on.

      Regards,
      Raphael

       
      • sansenan
        sansenan
        2008-05-08

        Thanks Raphael ,

              one more query,how do check whether a bit is set ie high or low..

        For example if we have to check like

        if(PORTB.3 == 1)
        {
        ...;
        }
        How to do this in SDCC

         
        • Raphael Neider
          Raphael Neider
          2008-05-08

          For single bit tests use

          if (PORTB & (1 << 3)) {
            // do something if PORTB.3 is set/high
          }

          if (!(PORTB & (1 << 5))) {
            // do something if PORTB.5 is clear/low
          }

          Do NOT use

          if (0 != (PORTB & (1 << 3))) {
            // EXPENSIVE TEST!
            // do something if PORTB.3 is set/high
          }

          if (0 == (PORTB & (1 << 5))) {
            // EXPENSIVE TEST!
            // do something if PORTB.5 is clear/low
          }

          as these produce rather largish code! These variants are OK for multi-bit tests like (0 == (PORTB & 0x42)) {none of bits 1 and 6 set?}, (0 != (PORTB & 0x0F)) {any of bits 0--3 set?}, or (0x42 == (PORTB & 0x42)) {all of bits 1 and 6 set?}.

          Regards,
          Raphael