Author Topic: Tidying up MPLAB program  (Read 1830 times)

0 Members and 1 Guest are viewing this topic.

Offline yashrkTopic starter

  • Frequent Contributor
  • **
  • Posts: 274
  • Country: in
  • A MAKER, AN ENGINEER, A HOBBYIST FOR LIFE
    • My Personal Blog
Tidying up MPLAB program
« on: November 30, 2016, 11:30:38 am »
Hey guys,
               I have written a program in MPLAB X using xc8 compiler which is working fine and dandy its just that due to many function in the program, the program has become very lengthy, now I am trying to tidy it up but I don't know how to write a header file for the program, I tried searching same on the internet but no luck. I'm looking to make my main code in to just includes, global variable and main function. Do tell how you do it or links to videos or blogs will be great to.
 
Thanks,
YashRK  :-+
Find me and things I'm working on - https://www.yashkudale.com/
 

Offline DTJ

  • Frequent Contributor
  • **
  • Posts: 997
  • Country: au
Re: Tidying up MPLAB program
« Reply #1 on: November 30, 2016, 11:41:25 am »
Near the top of your main.c program put:

#include "header.h"

Depending on the header file  location #include <header.h>
I can't remember the details...

Create the header.h file and add it to the project.

Then cut & paste the declarations and definitions into the header.h file.


Hopefully I've got this correct, if not others feel free to correct me.
Perhaps do it line at a time so if things go astray you'll have an idea what the problem might be.


 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: Tidying up MPLAB program
« Reply #2 on: November 30, 2016, 11:55:09 am »
have no idea how polished it can get, not knowing your program.

on how to write an include file and what to put or not put, you should read up on basic C programming.
One resource i usually use whenever i don't remember something: https://www.tutorialspoint.com/cprogramming/

Anyway, on an include file you will put all the declarations so
 - Other includes, if needed
 - defines
 - type definitions
 - function prototypes
do NOT put variables or actual code as it will be compiled every time the include file is used.
I usually have a "main.h" file that has all the includes and global defines and typedefs and in each C file i only include the main.h

if let's say i had 5 .c files and i had defined an array of 1024 8bit cells in the main.h file.. the compiler would allocate 5k of data memory for that array instead of one, because each .c file when compiled will allocate 1k for himself. (then the linker would complain and maybe fail the compilation)
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12856
Re: Tidying up MPLAB program
« Reply #3 on: November 30, 2016, 11:57:51 am »
XC8 is an ANSI C89 compliant C compiler (with some C99 extensions), so you just break up your program into separate modules (.c files) and headers as you would for any other ANSI C compiler.  See http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798 for the details.

All modules should #include <xc.h> and also #include whichever standard headers and project specific headers you create, that they need.  All project specific headers must be added to the MPLAB project.  In each #include, use <> for standard headers and "" for project specific ones.
 

Offline yashrkTopic starter

  • Frequent Contributor
  • **
  • Posts: 274
  • Country: in
  • A MAKER, AN ENGINEER, A HOBBYIST FOR LIFE
    • My Personal Blog
Re: Tidying up MPLAB program
« Reply #4 on: December 01, 2016, 07:21:59 am »
Thank you guys! it worked thanks for the links  :-+
Find me and things I'm working on - https://www.yashkudale.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf