Thread: Defining a number that will be used throughout an entire program.

  1. #1
    Registered User
    Join Date
    Jul 2021
    Posts
    20

    Defining a number that will be used throughout an entire program.

    Hi

    I am going wrong somewhere with something that i feel should be quite simple.

    I have a header file called MCU_Config, In there SYS_FREQ is defined.

    Code:
    #ifndef _MCU_CONFIG_H        
    #define _MCU_CONFIG_H
    
    
    #define SYS_FREQ 80000000
    In another header file i want to use the definition SYS_FREQ but i get an error telling me that i have an undeclared first use for SYS_FREQ

    Code:
    #ifndef _DELAY_H        
    #define _DELAY_H    
    
    
    #include <xc.h>
    
    
    //#define SYS_FREQ 80000000                  // RUNNING AT 80MHz
    
    
    /////////////////////////
    // FUNCTION PROTOTYPES //
    /////////////////////////
    void Delay_mS(int msec);                   //Initialise UART1 Communication
    
    
    #endif
    This is the .c file where SYS_FREQ is called.

    Code:
    
    #include "Delay.h"
    
    
    
    
    void Delay_mS(int msec)
    {
        unsigned int tWait;
        
        tWait=(SYS_FREQ / 2000)*msec;       // Calculate clock cycles required
        _CP0_SET_COUNT(0);                  // Set Core Timer count to 0
        while (_CP0_GET_COUNT()<tWait);     // Wait until Core Timer count reaches the number we calculated earlier 
    }
    If i uncomment SYS_FREQ in the header file then the program works as expected.

    If i include MCU_Config.h then i get multiple errors about multiple definitions.

    SYS_FREQ is used in a couple of places in my program. On 3 different UARTS Delay and probably will be used elsewhere as it develops.

    I dont want to be changing all these header files should i ever change the frequency.
    So is it possible to have this defined in one place and allow all files to use it?


    This is my main()

    Code:
    // Section: Included Files 
    #include "MCU_Config.h"
    #include "UART1_Comms.h"
    #include "VT100.h"
    #include "Delay.h"
    
    
    #include <xc.h>
    #include <stdio.h>
    #include <stdbool.h>
    #include <proc/p32mx575f256l.h>
    
    #define LED1 LATBbits.LATB7
    
    void main()
    {   
    
        // Main loop    while(1) 
        {
            LED1 = 0;
            Delay_mS(1000);
            LED1 = 1; 
            Delay_mS(1000);        
        }
        
    }
    Last edited by Kisen; 07-16-2021 at 05:32 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You should include MCU_Config.h and fix the errors. A properly written header file will not result in multiple definition errors when included, even if it is included multiple times.

    Quote Originally Posted by Kisen
    So is it possible to have this defined in one place and allow all files to use it?
    Yes, by writing a header file and including the header where those constants are needed.
    Last edited by laserlight; 07-16-2021 at 05:50 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    I have added to the Delay.h

    Code:
    #include "MCU_Config.h"
    This is the full MCU_Config file.

    Code:
    #ifndef _MCU_CONFIG_H		
    #define _MCU_CONFIG_H
    
    
    #define SYS_FREQ 80000000
    
    
    
    
    // DEVCFG3
    
    
    #pragma config USERID = 0xFFFF          // Enter Hexadecimal value (Enter Hexadecimal value)
    #pragma config FSRSSEL = PRIORITY_0     // SRS Select (SRS Priority 0)
    #pragma config FCANIO = OFF             // CAN I/O Pin Select (Alternate CAN I/O)
    #pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by Port Function)
    #pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by Port Function)
    
    
    // DEVCFG2
    #pragma config FPLLIDIV = DIV_2         // PLL Input Divider (2x Divider)
    #pragma config FPLLMUL = MUL_20         // PLL Multiplier (20x Multiplier)
    #pragma config UPLLIDIV = DIV_1         // USB PLL Input Divider (1x Divider)
    #pragma config UPLLEN = ON              // USB PLL Enable (Enabled)
    #pragma config FPLLODIV = DIV_1         // System PLL Output Clock Divider (PLL Divide by 1)
    
    
    // DEVCFG1
    #pragma config FNOSC = PRIPLL           // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
    #pragma config FSOSCEN = OFF            // Secondary Oscillator Enable (Disabled)
    #pragma config IESO = OFF               // Internal/External Switch Over (Disabled)
    #pragma config POSCMOD = HS             // Primary Oscillator Configuration (HS osc mode)
    #pragma config OSCIOFNC = ON            // CLKO Output Signal Active on the OSCO Pin (Enabled)
    #pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
    #pragma config FCKSM = CSECME           // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Enabled)
    #pragma config WDTPS = PS1048576        // Watchdog Timer Postscaler (1:1048576)
    #pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
    
    
    // DEVCFG0
    #pragma config DEBUG = OFF              // Background Debugger Enable (Debugger is disabled)
    #pragma config ICESEL = ICS_PGx1        // ICE/ICD Comm Channel Select (ICE EMUC1/EMUD1 pins shared with PGC1/PGD1)
    #pragma config PWP = OFF                // Program Flash Write Protect (Disable)
    #pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
    #pragma config CP = OFF                 // Code Protect (Protection Disabled)
    
    
    
    
    #endif
    When i add this include, i get these errors

    Code:
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: build/default/production/Delays.o:(.config_BFC02FFC+0x0): multiple definition of `__config_BFC02FFC'; build/default/production/main.o:(.config_BFC02FFC+0x0): first defined here
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: build/default/production/Delays.o:(.config_BFC02FF8+0x0): multiple definition of `__config_BFC02FF8'; build/default/production/main.o:(.config_BFC02FF8+0x0): first defined here
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: build/default/production/Delays.o:(.config_BFC02FF4+0x0): multiple definition of `__config_BFC02FF4'; build/default/production/main.o:(.config_BFC02FF4+0x0): first defined here
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: build/default/production/Delays.o:(.config_BFC02FF0+0x0): multiple definition of `__config_BFC02FF0'; build/default/production/main.o:(.config_BFC02FF0+0x0): first defined here
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: dist/default/production/Q10_Development.X.production.elf section `.config_BFC02FF0' will not fit in region `config3'
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: dist/default/production/Q10_Development.X.production.elf section `.config_BFC02FF4' will not fit in region `config2'
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: dist/default/production/Q10_Development.X.production.elf section `.config_BFC02FF8' will not fit in region `config1'
    c:\program files\microchip\xc32\v3.01\bin\bin\gcc\pic32mx\8.3.1\..\..\..\..\bin/pic32m-ld.exe: dist/default/production/Q10_Development.X.production.elf section `.config_BFC02FFC' will not fit in region `config0'
    Having been using MPLAB and coding for just under a week, i dont know how to go about fixing this. Largely because i really dont know why the error is occurring in the first place.

  4. #4
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    So, I think i have fixed this. Although i dont know how or why it is fixed exactly.

    so i changed the Header to contain just the defines...

    Code:
    #ifndef MCU_CONFIG_H        
    #define MCU_CONFIG_H
    
    
    #define SYS_FREQ 80000000           //MAIN SYSTEM CLOCK FREQUENCY - 80MHz
    #define PBCLK (SYS_FREQ/1)          //PERIPHERAL CLOCK FREQUENCY / 1 = 80MHz
    
    
    #endif
    I then moved all the pragma code to a MCU_Config.c file and included MCU_Config.h

    This now compiles and works as i wanted it too.
    Including the MCU_Config.h in my Delay files allows the use of SYS_FREQ.

    I can only assume at this point, because i cant find an answer, that the pragma code in the header file can be called multiple times, but putting it into the .c file it cant??

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Kisen View Post
    So, I think i have fixed this. Although i dont know how or why it is fixed exactly.

    so i changed the Header to contain just the defines...

    Code:
    #ifndef MCU_CONFIG_H        
    #define MCU_CONFIG_H
    
    
    #define SYS_FREQ 80000000           //MAIN SYSTEM CLOCK FREQUENCY - 80MHz
    #define PBCLK (SYS_FREQ/1)          //PERIPHERAL CLOCK FREQUENCY / 1 = 80MHz
    
    
    #endif
    I then moved all the pragma code to a MCU_Config.c file and included MCU_Config.h

    This now compiles and works as i wanted it too.
    Including the MCU_Config.h in my Delay files allows the use of SYS_FREQ.

    I can only assume at this point, because i cant find an answer, that the pragma code in the header file can be called multiple times, but putting it into the .c file it cant??
    The meaning of Compiler pragmas are different for each compiler.
    based on the errors you got I am guessing some of the pragmas generate binary code. Most binary code cannot be in headers.

    Also, macros with starting underline and uppercase letters "_MCU_CONFIG_H" are not really safe to use; I think they are reserved for compiler writers.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop entire C program ll Please Help
    By Johny2000 in forum C Programming
    Replies: 3
    Last Post: 06-08-2011, 04:00 AM
  2. Defining a Prime Number
    By Dulus in forum C Programming
    Replies: 8
    Last Post: 11-18-2010, 12:49 PM
  3. Loop Entire Program?
    By seanminator in forum C Programming
    Replies: 25
    Last Post: 07-22-2009, 01:23 PM
  4. Replies: 3
    Last Post: 10-25-2007, 09:41 AM
  5. I'm not ask for ENTIRE program, only 1 Question !
    By Th3-SeA in forum C Programming
    Replies: 10
    Last Post: 10-01-2003, 12:33 PM

Tags for this Thread