control microcontroller outputs with array

Discussion in 'Programmer's Corner' started by ecka333, Apr 11, 2012.

  1. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    It is possible to control pic microcontroller outputs easy with array? For example, i want to control RA0, RA1 and RA2 outputs only and set RA0 to 0, RA1 to 0 and RA2 to 1. I will create variable:

    unsingned short int PORT[3]={PORTA.B0, PORTA.B1, PORTA.2}.

    Is any way simply to set desired RA0, RA1 and RA2 low or high, like:

    PORT[3]={0, 0, 1}.

    or i must to set each output separately, like:

    PORT[0]=0;
    PORT[1]=0;
    PORT[2]=1;
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You can use bitwise operations in C. To clear a bit you and the corresponding bit with "0" and to set a bit. The bit you want unchanged you use the value "1". You or the corresponding bit you want to set with "1", and use the value "0" for the rest. As an example your problem
    Code ( (Unknown Language)):
    1.  
    2. PORTA=PORTA & 0b11111100 //clear bit 0 and 1
    3. PORTA=PORTA | 0b0000100   //set bit 2
    4.  
    With two operations you can set and clear any bit you want in any integer type value
    You can read more about here http://www.vipan.com/htdocs/bitwisehelp.html
    If the changes need to appear at the same time. You will have to read the port into a dummy value. Then make the changes on the dummy value. And write the dummy value back to port
     
  3. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    Yes, i know things about what you talking. But my question was about possibility simply to control microcontroller outputs using ARRAY.
     
Loading...