Searching \ for '[PIC] Hitec C looping output pins' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: massmind.org/techref/microchip/ios.htm?key=output
Search entire site for: 'Hitec C looping output pins'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Hitec C looping output pins'
2012\06\13@052102 by Goran Hosinsky

flavicon
face
I am working on a 16f887 project in Hitec C which will use many output pins. I

    #define LINE1    RA0
    #define LINE2    RA1

etc which gives me a readable code. The program is basically

    if ...
    LINE1 = ON;
    if ...
    LINE2 = ON;

With the few output connections that I am starting with  I just repeat the  code for each output line, but I am amplifying to 24 lines, which makes for a rather massive code. Also, the "if" part will grow with time as I add more sensors
Is there a way to handle the RA0 RA1...  bits in C with a loop? This would simplify the code for me.

Goran
Canary Island

2012\06\13@081314 by Isaac Marino Bavaresco

flavicon
face
Em 13/6/2012 06:20, Goran Hosinsky escreveu:
{Quote hidden}

A simple approach is to create a function to manipulate the pins,
passing a sequential pin number (you assign these number at your will)
and the value.
Inside that function it is much to like what you have now, but a
'switch' is probably more efficient than the 'if' chain.

Although this approach doesn't simplify the overall code, your main
function becomes more readable.


A more elaborate solution is to create a table with bit-masks and
pointers to the PORT registers. For instance:


typedef struct
   {
   unsigned char    *Port;
   unsigned char    BitMask;
   } portpin_t;

portpin_t IOPins[2]    = {{ &PORTA, 0x01 }, { &PORTA, 0x02 }};

....

void SetPin( unsigned char PinNumber )
   {
   *IOPins.Port[PinNumber]    |= IOPins.BitMask[PinNumber];
   }

void ClearPin( unsigned char PinNumber )
   {
   *IOPins.Port[PinNumber]    &= ~IOPins.BitMask[PinNumber];
   }


You can use macros also:

#define    SETPIN(p)    do{ *IOPins.Port[(p)]|=
IOPins.BitMask[(p)];}while(0)
#define    CLEARPIN(p)    do{ *IOPins.Port[(p)]&=
~IOPins.BitMask[(p)];}while(0)


If all your pins reside in the same port then you don't need to use a
struct (the '*port' member is not necessary) and if besides this all
your pins follow a numeric sequence then you don't need the masks, you
can create them ad-hoc: "PORTA |= 0x01 << PinNumber;" / "PORTA &= ~(0x01
<< PinNumber);"

This solution is good when your I/O pins are all spread over several
ports without a logic sequence. It can be much less efficient than the
'switch' with several cases (but can also be much more efficient,
depending on the number of pins, compiler and target architecture).


Isaac

More... (looser matching)
- Last day of these posts
- In 2012 , 2013 only
- Today
- New search...