Help with basic PIC programming on the X8 compiler

Thread Starter

dom11990

Joined Jan 12, 2013
5
I wanted to cycle several output ports through a certain bit sequence defined in an array. Ideally what I want is to have a virtual register, call it PORT_CUSTOM and have it contain references to the specific ports i chose (i.e. {PORTB1, PORTA2, PORTB0...} any arbitrary sequence of ports). Can this be easily accomplished? I've looked at the code that declares PORTB and PORTA but it is not intuitive to me how I would modify this to work the way I want. Any help would be greatly appreciated. I'm using Microchip's X8 compiler. Thanks

cheers,
Dom
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
You could do that by making a function such as:

void PORT_CUSTOM (int NewSetting)

Where the routine extracts portions of the NewSetting variable and sends them on their way to whatever port pins you require.

Cheers!
 

chrisw1990

Joined Oct 22, 2011
551
now, iv tried to do something like this before.. surely it would be able to typedef something? so that say myLCDPort is actually { PORTBbits.RB4 etc..}? that way if you assigned a value to the lcdport it would assign values in order to the bits in the grouping... ?
just a thought, for the beginning project its probably not a great answer, from me anyway.. im sure ill be told off for this =D
 

ErnieM

Joined Apr 24, 2011
8,377
now, iv tried to do something like this before.. surely it would be able to typedef something? so that say myLCDPort is actually { PORTBbits.RB4 etc..}? that way if you assigned a value to the lcdport it would assign values in order to the bits in the grouping... ?
just a thought, for the beginning project its probably not a great answer, from me anyway.. im sure ill be told off for this =D
Some things you can do, some things you can't do. When coding for an LCD I use a heavily modified version of the XLCD routines downloaded off Embedded Code Source. Yes, I use #define to change the symbols to send information to the proper pins, like so:

Rich (BB code):
// When in 4-bit interface define if the data is in the upper
// or lower nibble.  For lower nibble, comment the #define UPPER
#define UPPER 

// DATA_PORT defines the port to which the LCD data lines are connected 
#define DATA_PORT      PORTD
#define TRIS_DATA_PORT TRISD

// CTRL_PORT defines the port where the control lines are connected.
// adjusted to the port pins used in this app.
#define RW_PIN   LATDbits.LATD2             // PORT for RW  
#define TRIS_RW  TRISDbits.TRISD2           // TRIS for RW  
#define RS_PIN   LATDbits.LATD3             // PORT for RS  
#define TRIS_RS  TRISDbits.TRISD3           // TRIS for RS  
#define E_PIN    LATCbits.LATC6             // PORT for E   
#define TRIS_E   TRISCbits.TRISC6           // TRIS for E
Each definition defines a single destination, no where does any line say "take x and send one part to y and another part to z."

What is *not* being done here is saying that there exists some structure foo where part of foo resides at one address and another part resides at another.

There is no problem defining such a structure where the intent of the information contained is to be sent multiple places, but it takes code to unwind that data and send it to these different places.
 

chrisw1990

Joined Oct 22, 2011
551
yeh, i had a think, and a read online, and the theory seems a little wrong anyway.. the pipelining of the system and addressing wouldnt allow two parts of the data to be simultaneously written to one location.. hence the need for either a compiler function to do it or a user function to write to those pins/ports specifically! =]
 

takao21203

Joined Apr 28, 2012
3,702
All you need is suitable bitmasks.

You set with OR, and you clear with AND.

You can also use the FSR registers and load them with the value for PORT or LAT register.

You can define bitmasks in an array which set or reset more than one bit.
You can use the inout bit pattern as the index, load the value from the array, and then write to the INDF register.
 
Top