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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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