MPlabX and MCC, variable declarations and general annoyances

Thread Starter

Travm

Joined Aug 16, 2016
363
Learning about microcontrollers, and electronics in general, this forum has been awesome at pointing out things I misunderstand.

My problem (its not really a problem, just annoying), is that i'm trying to use microchips MCC(Microchip Code Configurator) with MPlabX, both of which i like for the GUI. When MCC generates files, it makes a ratsnest of files; each peripheral gets its own header file and .C file, in addition to the main.c file.
when im declaring variables that I need to use in multiple files, i cant put my variable declarations in consistent places. if they are in multiple places I have to put them in the peripheral header file, otherwise the compiler hits them as undeclared.

What i'm hung up on is that MCC puts the interrupt routines inside the peripheral .c files, and there i almost always need to use a variable.

Should i move the interrupt routines to the main.c file? or am i doing something stupid and wrong with my variable declarations?

Every variable declaration i can i have put directly in front of my while(1) loop, so I have a handy reference, and "known" place to put them. it screws me up, if i have an interrupt that depends on a variable, i have that variable defined in that specific peripherals header file, which has my variable declarations somewhat hard to find. Not impossible, but its killing my OCD here.

or

Do I just need to get over my love of GUI's and just configure it all by hand? I really hope that isnt the answer, I love MCC.
 

nsaspook

Joined Aug 27, 2009
13,272
Learning about microcontrollers, and electronics in general, this forum has been awesome at pointing out things I misunderstand.

My problem (its not really a problem, just annoying), is that i'm trying to use microchips MCC(Microchip Code Configurator) with MPlabX, both of which i like for the GUI. When MCC generates files, it makes a ratsnest of files; each peripheral gets its own header file and .C file, in addition to the main.c file.
when im declaring variables that I need to use in multiple files, i cant put my variable declarations in consistent places. if they are in multiple places I have to put them in the peripheral header file, otherwise the compiler hits them as undeclared.

What i'm hung up on is that MCC puts the interrupt routines inside the peripheral .c files, and there i almost always need to use a variable.

Should i move the interrupt routines to the main.c file? or am i doing something stupid and wrong with my variable declarations?

Every variable declaration i can i have put directly in front of my while(1) loop, so I have a handy reference, and "known" place to put them. it screws me up, if i have an interrupt that depends on a variable, i have that variable defined in that specific peripherals header file, which has my variable declarations somewhat hard to find. Not impossible, but its killing my OCD here.

or

Do I just need to get over my love of GUI's and just configure it all by hand? I really hope that isnt the answer, I love MCC.
I don't use the MCC for simple controllers because it does make a bit of a mess with files. My personal preference (from being a old fart) would be to not use it to actually build applications if you're serious about learning the low-level language implementation details of 8 bit microcontrollers and have little or no previous experience.

One thing you can easily do to keep track of variables is to create structures that contain all the variables you need to pass between routines in one header file and then create variables using those structures in main program file so they can be used as external variables elsewhere.

A simple non-MCC example: https://github.com/nsaspook/fanmon_xc8
C:
// header file declares a V structure
typedef struct V_data { // ISR data structure
uint16_t blink, blink_out, blink_alt, spin_count1, spin_count2, spurious_int;
uint8_t valid : 1;
uint8_t comm : 1;
uint8_t comm_state;
uint8_t spinning : 1;
uint8_t fan1_spinning : 1;
uint8_t fan2_spinning : 1;
uint8_t boot_code : 1;
uint8_t rx_data, tx_data;
uint8_t led_pwm[8], led_pwm_set[8];
} V_data;

// main code file creates V
volatile struct V_data V;

// routine header file give information about V
extern volatile struct V_data V;

// routine code file using V
void Blink_Init(void)
{
LEDS.out_byte = 0xff;
V.blink = 0;
V.blink_out = 0;
V.blink_alt = 0;
// High priority interrupt in ISR for Blink timer
}
 

JohnInTX

Joined Jun 26, 2012
4,787
My personal preference (from being a old fart) would be to not use it to actually build applications if you're serious about learning the low-level language implementation details of 8 bit microcontrollers and have little or no previous experience.
+1 You kind of have to decide what you want. If you just want to drag and drop to make a quick application and leave the details to MCC, that's one thing. It's not necessarily a bad thing but like any high level code generator, you have to let it do things its way (and hope they are right). But, as another old fart, if you really want to get a handle on how things work, drill down into the manuals, learn how the various peripherals work and write the code yourself. It takes longer but you'll understand it and have a fighting chance to resolve problems when they occur.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
I do actually understand everything it is doing, just love how easy it is to set it up in the GUI, then click - Generate Files.
MCC has been an amazing help (crutch) as ive been learning.
Guess I'm just outgrowing it.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
I don't use the MCC for simple controllers because it does make a bit of a mess with files. My personal preference (from being a old fart) would be to not use it to actually build applications if you're serious about learning the low-level language implementation details of 8 bit microcontrollers and have little or no previous experience.

One thing you can easily do to keep track of variables is to create structures that contain all the variables you need to pass between routines in one header file and then create variables using those structures in main program file so they can be used as external variables elsewhere.

A simple non-MCC example: https://github.com/nsaspook/fanmon_xc8
C:
// header file declares a V structure
typedef struct V_data { // ISR data structure
uint16_t blink, blink_out, blink_alt, spin_count1, spin_count2, spurious_int;
uint8_t valid : 1;
uint8_t comm : 1;
uint8_t comm_state;
uint8_t spinning : 1;
uint8_t fan1_spinning : 1;
uint8_t fan2_spinning : 1;
uint8_t boot_code : 1;
uint8_t rx_data, tx_data;
uint8_t led_pwm[8], led_pwm_set[8];
} V_data;

// main code file creates V
volatile struct V_data V;

// routine header file give information about V
extern volatile struct V_data V;

// routine code file using V
void Blink_Init(void)
{
LEDS.out_byte = 0xff;
V.blink = 0;
V.blink_out = 0;
V.blink_alt = 0;
// High priority interrupt in ISR for Blink timer
}
I havent found a reason to need to learn structures yet. But fundamentally this appears to be the same as declaring all the variables in a header file.
I dont like that one, so far as they variables are always in another file. I want to just scroll up and see my variables in a logical place.

Programming is new to me, and i havent bothered with anything more complicated than simple 8-bit pics. So my world is pretty small if you will.
 

nsaspook

Joined Aug 27, 2009
13,272
I havent found a reason to need to learn structures yet. But fundamentally this appears to be the same as declaring all the variables in a header file.
I dont like that one, so far as they variables are always in another file. I want to just scroll up and see my variables in a logical place.

Programming is new to me, and i havent bothered with anything more complicated than simple 8-bit pics. So my world is pretty small if you will.
It's time to learn about data structures then. :D
This is all on a PIC18F1320 8-bit micro so even on the smallest chips you can use most C modular programming concepts.
http://www.embedded.com/design/prototyping-and-development/4023876/Modular-Programming-in-C

If you use structures with the correct header files then the IDE will track all the structure variable data components and show them in a selection box on the current source file being edited even is the structure is declared elsewhere.
Example: Use the variable V.comm from the V structure.

64-bit Linux MPLABX 3.60

Type 'V.' selection menu box opens

Select variable and click.

Done.
 
Last edited:

Thread Starter

Travm

Joined Aug 16, 2016
363
It's time to learn about data structures then. :D
This is all on a PIC18F1320 8-bit micro so even on the smallest chips you can use most C modular programming concepts.
http://www.embedded.com/design/prototyping-and-development/4023876/Modular-Programming-in-C

If you use structures with the correct header files then the IDE will track all the structure variable data components and show them in a selection box on the current source file being edited even is the structure is declared elsewhere.
Example: Use the variable V.comm from the V structure.

64-bit Linux MPLABX 3.60

Type 'V.' selection menu box opens

Select variable and click.

Done.
I haven't gotten into this in detail yet, but thanks for the tip. I expect I will benefit from this.
 
Top