Create Pin array in HI TECH C

Microcontroller Topics
Post Reply
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Create Pin array in HI TECH C

Post by Nandika » Sat Sep 14, 2013 7:02 pm

Hello Friends,
I want to create pin array in HI TECH C for PIC16F like Arduino.

Code: Select all

int ledPins[] = {2, 7, 4, 6, 5, 3 };
Assume I have PORTB.RB0,RB1....RB7 are my pins now.
I coded like this,

Code: Select all

#define D0 RB0
#define D1 RB1
#define D2 RB2
#define D3 RB3
#define D4 RB4
#define D5 RB5
#define D6 RB6
#define D7 RB7

bit pins_b[]={D0,D1,D2,D3,D4,D5,D6,D7};
Not Worked...
compiler gives following errors...
error.PNG
error.PNG (37.66 KiB) Viewed 5174 times
bit data type not allowed for array implementation. :(
How can I implement an array with PORTB pins?



Thank in advance
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Create Pin array in HI TECH C

Post by Neo » Sun Sep 15, 2013 3:11 pm

Nandika, after long time. Welcome back.

As per the error says, RB0.... RB8 are not defined. Make sure that you have includes pic.h

Also, I'm wondering why you need such as array to assign to the port. As far as I see it, your purpose is to avoid nested ifs and use the port as a lookup table.

PORTB itself can be used for this purpose using boolean operations (bitwise). So rather than addressing it as pins_b[x], you would address the port as below.

Code: Select all

void writePin(index, value){

     PORTB = (PORTB & ~(1 << index)) | (value << index);
}
Reading the pin can be written as follows,

Code: Select all

int readPin(index){

     return ((PORTB >> index) & 1);
}
You can also use them as preprocessor defines as follows,

Code: Select all

#define WRITE_PIN(index, value)     PORTB = (PORTB & ~(1 << index)) | (value << index)
#deifne READ_PIN(index)      ((PORTB >> index) & 1)
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Create Pin array in HI TECH C

Post by SemiconductorCat » Sun Sep 15, 2013 11:35 pm

>> Also, I'm wondering why you need such as array to assign to the port. As far as I see it, your purpose is to avoid nested ifs and use the port as a lookup table.

I think he need to use that array in a for loop. If so use that index method provided by neo.
as said , you could not create arrays of bits.

And your defines are wrong.
from the sdcc project includes in \device\non-free\include\pic14\pic16f84a.h include header file,
it defines like this.

Code: Select all

#define RA0                  PORTAbits.RA0                  /* bit 0 */
#define RA1                  PORTAbits.RA1                  /* bit 1 */
#define RA2                  PORTAbits.RA2                  /* bit 2 */
#define RA3                  PORTAbits.RA3                  /* bit 3 */
#define RA4                  PORTAbits.RA4                  /* bit 4 */

#define RB0                  PORTBbits.RB0                  /* bit 0 */
#define RB1                  PORTBbits.RB1                  /* bit 1 */
#define RB2                  PORTBbits.RB2                  /* bit 2 */
#define RB3                  PORTBbits.RB3                  /* bit 3 */
#define RB4                  PORTBbits.RB4                  /* bit 4 */
#define RB5                  PORTBbits.RB5                  /* bit 5 */
#define RB6                  PORTBbits.RB6                  /* bit 6 */
#define RB7                  PORTBbits.RB7                  /* bit 7 */

Don't worry to read the header files and if you don't understand what's it, then refer back to the manual of the
compiler and see what's that?
http://www.mikroe.com/download/eng/docu ... l_bits.htm

Every source code file was written by a man for another man first and compiler to the second.


In the case you don't understand what the bellow code ":" , it simply used to limit the number of bits used in
that datatype. So when you need a variable with 3-bits , you could define it inside a structure. Note that
this feature is only avaliable to be used inside 'structures' and classes.

Code: Select all

// ----- PORTB bits --------------------
typedef union {
  struct {
    unsigned char RB0:1;
    unsigned char RB1:1;
    unsigned char RB2:1;
    unsigned char RB3:1;
    unsigned char RB4:1;
    unsigned char RB5:1;
    unsigned char RB6:1;
    unsigned char RB7:1;
  };
} __PORTBbits_t;
extern volatile __PORTBbits_t __at(PORTB_ADDR) PORTBbits;
I think whole the code is clear to you now. Crystal clear? Then nice code reviewing.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Create Pin array in HI TECH C

Post by Nandika » Tue Sep 17, 2013 3:13 pm

PORTB = (PORTB & ~(1 << index)) | (value << index);
return ((PORTB >> index) & 1);
Nice cording Neo... :biggrin: I got some time to understand it.
Thanks..

SemiconductorCat,
Your coding is hard.I cant understand. because,I couldn't study about some part of your code..Soon I will understand it. :)
Post Reply

Return to “Microcontrollers”