Dynamic port bits

Discussion in 'Embedded Systems and Microcontrollers' started by varunme, Dec 19, 2011.

  1. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    How can we access port bits using loops like this in mikroC ?

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #define _TOTAL_LIGHTS 5
    4.  
    5. for(i=0; i<_TOTAL_LIGHTS; i++)
    6.              {
    7.                      if (PORTD.[i] == 1)
    8.                         {
    9.                                  PORTC.[i] = 1;
    10.                         }
    11.                      else
    12.                        {
    13.                                  PORTC.[i] = 0;
    14.                        }
    15.             }
    16. [/i][/i][/i]
     
    Last edited by a moderator: Dec 19, 2011
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    Use a switch/case statement.
     
  3. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    no way to access by for loop like this ?
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    No. The part after the dot is a member of a structure. It is not a variable.
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    I don't use MicroC so I don't know if there is some simple MicroC specific way to do it but the following method will work in any version of C.
    Code ( (Unknown Language)):
    1.  
    2. #define set_bit(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
    3. #define clear_bit(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
    4. #define toggle_bit(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
    5. #define test_bit(ADDRESS,BIT) (ADDRESS & (1<<BIT))
    6.  
    7. #define _TOTAL_LIGHTS 5
    8.  
    9. void some_function(void)
    10. {
    11.     for(i = 0; i<_TOTAL_LIGHTS; i++)
    12.     {
    13.         if (test_bit(PORTD, i))
    14.         {
    15.             set_bit(PORTC, i);
    16.         }
    17.         else
    18.         {
    19.             clear_bit(PORTC, i);
    20.         }
    21.     }
    22. }
    23.  
     
    varunme likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    As long as you insist on using a loop you will be using more code and a longer time to execute then just doing a direct assignment:

    Code ( (Unknown Language)):
    1.  
    2.         LATC.B0 = PORTD.B0;
    3.         LATC.B1 = PORTD.B1;
    4.         LATC.B2 = PORTD.B2;
    5.         LATC.B3 = PORTD.B3;
    6.         LATC.B4 = PORTD.B4;
    7.  
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,001
    I have often wanted to pass a port or latch in a function call myself. I have yet to figure out a way to do it.


    pseudo code example

    void foo(PortType port)
    {

    port = 1;

    }


    I realize you can do it with #defines but it would be nice to be able to pass a port or latch as a argument in a function.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You can always pass the port by reference, ie, by passing the address of the port:

    Code ( (Unknown Language)):
    1.  
    2. void ClrPort(near unsigned char* Port);
    3.  
    4. void main (void)
    5. {
    6.   ClrPort(&LATA);
    7. }
    8.  
    9. void ClrPort(near unsigned char* Port)
    10. {
    11.   *Port = 0;
    12. }  
    13.  
     
  9. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    I am using 16F , so i cant use the LATCH
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    They you use PORT.


     
  11. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    nice , hahhha
     
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Just declare all of your variables globally. :D Buy a PIC with enough RAM first.
     
Loading...