robinfawell Posted November 18, 2006 Report Share Posted November 18, 2006 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 Quote Link to comment Share on other sites More sharing options...
jackchaos Posted November 18, 2006 Report Share Posted November 18, 2006 declare the variable 1st, then assign a value like so:unsigned char button_state;button_state = 17; Quote Link to comment Share on other sites More sharing options...
robinfawell Posted November 18, 2006 Author Report Share Posted November 18, 2006 Thanks jackchaos,I've already tried that. In my program the value 17 is picked and the value stays at 17. I need to initialise the value outside the function somehow then allow the function to change the value from (say) 17 to 16 during the operation.Robin Quote Link to comment Share on other sites More sharing options...
audiocommander Posted November 18, 2006 Report Share Posted November 18, 2006 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 exportNow 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.cvolatile 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,Michaelps: As I'm an autodidact, I always welcome any corrections or better explanations :) Quote Link to comment Share on other sites More sharing options...
robinfawell Posted November 18, 2006 Author Report Share Posted November 18, 2006 Thanks MichaelHere 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 Quote Link to comment Share on other sites More sharing options...
TK. Posted November 18, 2006 Report Share Posted November 18, 2006 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:http://sdcc.svn.sourceforge.net/viewvc/sdcc/trunk/sdcc/device/lib/pic16/startup/crt0i.c?revision=3714&view=markupbut I never tried it out.An easier (and sometimes less code consuming) way is to initialize global variables inside the Init() Hook of MIOSBest Regards, Thorsten. Quote Link to comment Share on other sites More sharing options...
robinfawell Posted December 1, 2006 Author Report Share Posted December 1, 2006 Dear ThorstenI 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) functionplay_rec_group_stored_pin = 7;It worked !Many Thanks again. Robin Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.