I am using pic16f887 microcontroller and using Hi-tech C compiler. I want to implement port pin's Array in Hi-tech but it is not allowing me to do so. What is the correct way to access pin address with indexing, like we do in arduino.

i want array like

   bit * lcd[4]={RA0,RA1,RB0,RC4};

it gives following errors

  • arrays of bits or pointers to bit are illegal
  • constant expression required
  • illegal conversion of integer to pointer

It clearly means I cannot declare Arrays of Bit, what is the other way to access pin numbers via indexing in standard C style or any other compiler related trick.

In header files pins are declared in following manners

    // Register: TRISB
    volatile unsigned char           TRISB               @ 0x086;
// bit and bitfield definitions
volatile bit TRISB0              @ ((unsigned)&TRISB*8)+0;
volatile bit TRISB1              @ ((unsigned)&TRISB*8)+1;
volatile bit TRISB2              @ ((unsigned)&TRISB*8)+2;
volatile bit TRISB3              @ ((unsigned)&TRISB*8)+3;
volatile bit TRISB4              @ ((unsigned)&TRISB*8)+4;
volatile bit TRISB5              @ ((unsigned)&TRISB*8)+5;
volatile bit TRISB6              @ ((unsigned)&TRISB*8)+6;

My problem is that, I do not want to name the port I want to index them so that I could call them with their numbers like we do in arduino style

  • 1
    \$\begingroup\$ You need to write a function that takes a pin number and produces a port name and pin number (or equivalent register and bit). Look at the source code of digitalWrite(). \$\endgroup\$
    – uint128_t
    Jul 12, 2016 at 0:40
  • \$\begingroup\$ In AVR the individual pin is not accessible, mean AVR C is not bit adressable, but the PIC is. Once I got the port name (let say, PORTA); and i got the pin number 1, then I have to read and write that port via logical and or OR. I want some way of bit adressing. Is there any way in Hi-tech or I have to tackle this problem with writing a bit in byte with logical opertions? \$\endgroup\$ Jul 12, 2016 at 13:32
  • \$\begingroup\$ The individual pins are accessible, using the port and pin number and logical operators. Again, the source code of digitalWrite() has all of this, and it should be trivial to port this to Hi-tech C. \$\endgroup\$
    – uint128_t
    Jul 12, 2016 at 13:39


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.