• AVR Freaks

Hot!Skipping code or avoiding the undefined identifier error

Author
ArakelTheDragon
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
2021/03/01 16:43:46 (permalink)
0

Skipping code or avoiding the undefined identifier error

Hi!
 
I am sorry for asking this question, which probably has a very simple solution, unfortunately the lack of experience in these situations causes me problems, where there shouldn't be any. I am familiar with C programming and MCU architecture, systems and devices.
 
Problem:
I am trying to build my own function for gaining experience and to use in later programs. I intend to build the code piece by piece and transfer them to libraries, so the programming can become small and easy to read. The function I am making will set a pin to input/output and high/low. However on some processors the TRIS and PORT names are not the same, for example GPIO on PIC10F200 or PORTA/ PORTB on others. I need to use this library for more than 1 processor, but when I compiler for PIC10F200, it gives me the error undefined identifier. How to avoid this error and be able to use the same function for a lot of processors(baseline and mid-range for now). The function itself:
 

// The function is located in a separate file "Library_2_Functions.h"
// In the main.c file there is an #include "Library_2_Functions.h" directive
void SetPin (int iTRISName, int iInputOutput, int iPIN, int iState)    // The function sets a pin to input or output and sets
                                    //it high or low.
{
                                    // Switch between TRIS and PORT A, B, C, D or E.
    switch (iTRISName)
    {
        case 'A':                        // If we have TRIS and PORT A
            TRISA | (iInputOutput << iPIN);            // OR TRISA with input(1) or output(0) shifted by iPIN
            PORTA | (iState << iPIN);            // OR PORTA with low(0) or high(1) shifted by iPIN
            break;
        case 'B':
            TRISB | (iInputOutput << iPIN);
            PORTB | (iState << iPIN);
            break;
    }
}

#1

3 Replies Related Threads

    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Skipping code or avoiding the undefined identifier error 2021/03/01 16:47:58 (permalink)
    +2 (2)
    Good luck, but I really don't think this is a useful exercise.
    I guess you are trying to make the PIC more "Arduino like".
    This way ensures your port control code is as inefficient as possible to be. It takes a lot of code to do a variable shift left in PIC assembly, and totally stops the code from using simple BCF and BSF instructions to control the pin.
     
    It also tries to use the same port for input and output, which is NOT a good idea on all the newer devices that have LATx registers.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    du00000001
    Just Some Member
    • Total Posts : 4144
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Skipping code or avoiding the undefined identifier error 2021/03/01 17:19:17 (permalink)
    +2 (2)
    @ArakelTheDragon
    Basically this can be done - - - - - with a lot of conditional compilation. Whether it's worth the effort? I don't think so.
    You seem to think that a library is a library of object code modules. Historically correct, but not the only conceivable solution.
    Arduino libraries are called libraries but come as source code, not object code. (This is the only way how such libs can be made available for differing MCU architecures.) These Arduino libraries are laden with conditional compilation, text replacement and alike. A nice thing to learn coding techniques - - - once you are familiar with the basic techniques used to implement the portability targeted. If you're not familiar with these, the code sometimes is short from unreadable. Not to mention "understandable".

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Skipping code or avoiding the undefined identifier error 2021/03/01 18:38:45 (permalink)
    +2 (2)
    Hi,
    As mentioned above, it may be done by conditional compilation using preprocessor directives,
    something like this:
    #include <xc.h>
     
    void  SetPin (...)
    {
        switch (iTRISName)
        {    case 'A':
    #if defined  LATA
                TRISA |= (iInputOutput << iPIN);            // OR TRISA with input(1) or output(0) shifted by iPIN
                LATA |= (iState << iPIN);            // OR PORTA with low(0) or high(1) shifted by iPIN
    #elif defined PORTA
                TRISA |= (iInputOutput << iPIN);            // OR TRISA with input(1) or output(0) shifted by iPIN
                PORTA |= (iState << iPIN);            // OR PORTA with low(0) or high(1) shifted by iPIN
    #elif defined GPIO
                TRISA |= (iInputOutput << iPIN);            // OR TRISA with input(1) or output(0) shifted by iPIN
                GPIO |= (iState << iPIN);            // OR PORTA with low(0) or high(1) shifted by iPIN
    #else
      #warning  "Nothing such!"
    #endif
                break;
            case 'B':
            ...
        } 

    This way will still be limited by hardware dependencies between Port and Pin number within each Port register.
     
    Another way that resolve the dependencies upon hardware port registers,
    is used by MCC, where it is possible to define Custom names for individual pins,
    and then there are Macro functions generated in: pin_manager.h
    such that expressions like this may be available:
        LED_Red_SetHigh();
        LED_Red_SetDigitalOutput();
        LED_Red_SetDigitalMode();  

    Then it will be possible to write application code that may be configured to run on different hardware,
    where  LED_Red is connected to pin RA0  on one device, and to pin RC3 on another.
     
        Mysil
           
    post edited by Mysil - 2021/03/01 18:54:44
    #4
    Jump to:
    © 2021 APG vNext Commercial Version 4.5