Help with basic PIC programming on the X8 compiler

Discussion in 'Embedded Systems and Microcontrollers' started by dom11990, Jan 12, 2013.

  1. dom11990

    Thread Starter New Member

    Jan 12, 2013
    2
    0
    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: Jan 12, 2013
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,389
    1,605
    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!
     
  3. dom11990

    Thread Starter New Member

    Jan 12, 2013
    2
    0
    Thank you for your response. I just found this solution as well. Either way, it is not natively supported by C which is unfortunate...
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,389
    1,605
    I have never found that to be the case.
     
  5. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    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
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,389
    1,605
    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:

    Code ( (Unknown Language)):
    1. // When in 4-bit interface define if the data is in the upper
    2. // or lower nibble.  For lower nibble, comment the #define UPPER
    3. #define UPPER
    4.  
    5. // DATA_PORT defines the port to which the LCD data lines are connected
    6. #define DATA_PORT      PORTD
    7. #define TRIS_DATA_PORT TRISD
    8.  
    9. // CTRL_PORT defines the port where the control lines are connected.
    10. // adjusted to the port pins used in this app.
    11. #define RW_PIN   LATDbits.LATD2             // PORT for RW  
    12. #define TRIS_RW  TRISDbits.TRISD2           // TRIS for RW  
    13. #define RS_PIN   LATDbits.LATD3             // PORT for RS  
    14. #define TRIS_RS  TRISDbits.TRISD3           // TRIS for RS  
    15. #define E_PIN    LATCbits.LATC6             // PORT for E  
    16. #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.
     
  7. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    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! =]
     
  8. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
Loading...