array initialization through header file

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!

I have the following problem to solve:
In my system I have a main unit "with brain", and variable amount of input, and output cards (containing just latches, and transistors).

There is no way to detect automatically the amount of connected cards, so this information has to be configured in the program, compiled and loaded to microcontroller. Currently I store the information about the available cards in a global array(see below).

I would like to gather all these kind of initialization settings in one single file, preferably a header file (let's call it "USER_settings.h" ), but the global variable array which contains the info about the cards can't be initialized in header file, because header files can just contain declarations, and no definitions. (correct me if I'm wrong)

volatile uint8_t output_cards_available[12]=
{	
	1,	// Output_Card_1 is ON
	0,	// Output_Card_2 is NOT AVAILABLE
	0,	// Output_Card_3 is NOT AVAILABLE
	0,	// Output_Card_4 is NOT AVAILABLE
	0,	// Output_Card_5 is NOT AVAILABLE
	0,	// Output_Card_6 is NOT AVAILABLE
	0,	// Output_Card_7 is NOT AVAILABLE
	0,	// Output_Card_8 is NOT AVAILABLE
	0,	// Output_Card_9 is NOT AVAILABLE
	0,	// Output_Card_10 is NOT AVAILABLE
	0,	// Output_Card_11 is NOT AVAILABLE
	0	// Output_Card_12 is NOT AVAILABLE
};

I have found a solution, not really nice though: I define constants in my "USER_settings.h"

/* Set constant to one if that card is plugged in the system */
#define OUTPUT_CARD_1 1
#define OUTPUT_CARD_2 0
#define OUTPUT_CARD_3 0
...
etc.

And then in the source file I can write:

#include "USER_settings.h"

volatile uint8_t output_cards_available[12]=
{	
	OUTPUT_CARD_1,	
	OUTPUT_CARD_2,	
	OUTPUT_CARD_3,	
	OUTPUT_CARD_4,	
	...	
	..	
	
};

Is there a better way to do this?

Thanks for the help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Is there a better way to do this?

That scheme seems fine but is there no way you can arrange for the electronics to somehow be detectable at run time - then the code would just probe each possible output in turn and just fill in a 1 for those it finds present.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You will have to compile separately for each 'Slave' board.

You may find life easier by just doing a self-diagnosis. e.g. test each line / card for (in)activity.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
You may find life easier by just doing a self-diagnosis. e.g. test each line / card for (in)activity.

The output, and input cards are just a bunch of latches (HC574), drivers (HC245) and multiplexers to select the latch or driver IC, so there is simply no way to read whether they are present or not.

Quote:
You will have to compile separately for each 'Slave' board.

nope, just for the main board, but I will have to compile every time I want to remove or add a new i/o board (which hopefully won't happen just once a year or so)
And because I want to give the whole system to the university, it will be their job to maintain the software, and hardware, so that is why I would like to make their job as easy as it can be.

For the next generation I will definitely add a small microcontroller to the I/O cards. But for now I need to find neat solution for the initialization of the I/O cards related data.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can put a const static array in a header file.
If the data will be optimized out, you needn't worry about SRAM usage.

Also, there is nothing about a header file that makes it intrinsically easier for your target audience to edit.
If the data actually needs to be stored, you might as well let them edit a .c file.

Why is output_cards_available volatile?
My suspicion is that there is some other bug in your code that volatile appears to cure.

"Demons after money.
Whatever happened to the still beating heart of a virgin?
No one has any standards anymore." -- Giles

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why recompile at all if only the hardware configuration changes ?

Store configuration data in flash or eeprom, then read the data on startup.

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ChaunceyGardiner wrote:
Why recompile at all if only the hardware configuration changes ?

Store configuration data in flash or eeprom, then read the data on startup.

EEPROM could work, but considering that newbies will work on the software, I would rather not use EEPROM. Problems like separately erase EEPROM in AVRStudio, or preserve EEPROM while chip erase, program only EEPROM etc. makes the idea more vulnerable to user mistakes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lammelm wrote:
ChaunceyGardiner wrote:
Why recompile at all if only the hardware configuration changes ?

Store configuration data in flash or eeprom, then read the data on startup.

EEPROM could work, but considering that newbies will work on the software, I would rather not use EEPROM. Problems like separately erase EEPROM in AVRStudio, or preserve EEPROM while chip erase, program only EEPROM etc. makes the idea more vulnerable to user mistakes.

You want untrusted newbies to use AVRStudio?
output_cards_available.c:
#include 
#include "output_cards_available.h"

// comments here

// After editing this file, click on output_cards_available_install.bat

volatile uint8_t output_cards_available[12] EEPROM = {
    // Change all the x's
    // Change only the x's
    // 0==AVAILABLE
    // 1==NOT AVAILABLE
    // Other values are sinful
    [ 0]=x,  // Output_Card_1
    [ 1]=x,  // Output_Card_2
    [ 2]=x,  // Output_Card_3
    [ 3]=x,  // Output_Card_4
    [ 4]=x,  // Output_Card_5
    [ 5]=x,  // Output_Card_6
    [ 6]=x,  // Output_Card_7
    [ 7]=x,  // Output_Card_8
    [ 8]=x,  // Output_Card_9
    [ 9]=x,  // Output_Card_10
    [10]=x,  // Output_Card_11
    [11]=x,  // Output_Card_12
};

output_cards_available_install.bat:

python output_cards_available_syntax_checker.py
make output_cards_available.install

The content of Makefile is left as an exercise for the reader.
Instead of python, it might be better to use something that comes with winAVR.
It could probably be done in sed,
but getting newbie-understandable error messages might be a major exercise for the reader.
If awk comes with winAVR, I expect that awk could do the job.

"Demons after money.
Whatever happened to the still beating heart of a virgin?
No one has any standards anymore." -- Giles

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

If awk comes with winAVR, I expect that awk could do the job

It has gawk.exe