Jump to content

Initialising a Variable


robinfawell
 Share

Recommended Posts

I have a problem where I have variable "record_button_state" that is not behaving as I want.

I would like it to have an initial value of 17(dec).

I have tried to initialise it by the global declaration :-

unsigned char record_button_state = 17;

However in testing the result, using Midi-ox I find that the inital value is 0 not 17.

The variable picks up another value in operation.  It should have  values of 16 and 17 during the operation of the system but not 0.

Please help.

Robin

Link to comment
Share on other sites

Hi Robin,

I'm using variables this way:

variable declaration (or) declaration and initialisation

This is done in the .c-file at the top, outside any function:

[tt]unsigned char record_button_state;[/tt] (or)

[tt]unsigned char record_button_state = 17;[/tt]

The additional keyword "volatile" is used to tell the compiler that this variable may be changed in other places. So it's a good idea to add this for globals:

[tt]volatile unsigned char record_button_state;[/tt] (or)

[tt]volatile unsigned char record_button_state = 17;[/tt]

global export

Now we need to export the variable as global.

This is normally done in the header-file or above the declaration.

The global export may not initialize the value!

[tt]extern volatile unsigned char record_button_state;    // in myfile.h or on top of myfile.c

volatile unsigned char record_button_state = 17;      // in myfile.c[/tt]

note that this is not only valid for variables, but also for functions.

I hope this helps :)

best regards,

Michael

ps: As I'm an autodidact, I always welcome any corrections or better explanations :)

Link to comment
Share on other sites

Thanks Michael

Here is the top of my revised applicaton c file.

      #include "cmios.h"

#include "pic18f452.h"

extern volatile unsigned char record_button_state;

void SendNote(unsigned char chn, unsigned char note, unsigned int velocity);

void SendTremelo();

void SendDump(unsigned char dump_index);

void SendPedalNotes(unsigned char pin, unsigned char pin_value);

//void WriteMem ( unsigned char q, unsigned char pin);

  // void Memory_IO(unsigned int start_addr);

unsigned char SetLed_NoDump(unsigned char low_pin, unsigned char high_pin, unsigned char canc_pin, unsigned char stored_pin, unsigned char pin, unsigned char pin_value );

unsigned char SetLed_SendDump(unsigned char low_pin, unsigned char high_pin, unsigned char canc_pin, unsigned char stored_pin, unsigned char pin, unsigned char pin_value ); //void RecordSetup ();

//void Set_Mem_Led_Rec_1 ( unsigned char value, unsigned int flag_addr, unsigned char pin, unsigned char pin_value);//used when Record is set

unsigned char dump_index; //triggers sysex messages

//unsigned char memory_group_rec_1 [6];

volatile unsigned char record_button_state = 17;//Record Pin 16

unsigned char k;// used to "count" flag memory locations

unsigned char q;

unsigned int addr;

I have tried to follow your suggestions but the record_button_state remains at 0.

Please note that there are two entries at line 3 and the 15th line.  Is this what you intended?

Thanks for spending your time in helping me.

Robin

Link to comment
Share on other sites

I think, that the initialisation of RAM variables outside a function won't work, because this requires a special SDCC specific copy routine, which copies the value from flash memory into RAM.

It's located here:

but I never tried it out.

An easier (and sometimes less code consuming) way is to initialize global variables inside the Init() Hook of MIOS

Best Regards, Thorsten.

Link to comment
Share on other sites

  • 2 weeks later...

Dear Thorsten

I am nearing the end of the organ project ( I hope!) and found that there was a problem in starting the organ in a common sense way.  In fact to select any organ stops it was necessary to push either the "Record" or "Playback" button first.

This was not logical.

I decided to see if I could initialise the variable to the  "non Record" "non Playback" state (7).  I knew that if I could achieve this, the organ would start by selecting an organ stop.

and made the following entry in the void Init(void) function

play_rec_group_stored_pin = 7;

It worked !

Many Thanks again.  Robin

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...