View previous topic :: View next topic |
Author |
Message |
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
Strange push stack problem [SOLVED] |
Posted: Mon Dec 05, 2022 2:50 am |
|
|
Hello i have very strange problem if i use both "push stack" in same time.
If only one of these stack is in use there is no problem, but if they are used in same time only one works..
Code: |
//user defines:
#define TEMPELEMENTS 24 //24h- memory (outdoor)
#define TEMPELEMENTSIN 24 //24h- memory (indoor)
#define PRINTTMPPUSHSTACK HW_UART2
//outdoor globals
float temp_stackbuffer[TEMPELEMENTS]={0.0};
//functions:
void temperature_push_stack(float val);
void print_temperature_push_stack(void);
void clear_temperature_push_stack(void);
//indoor globals
float temp_stackbuffer_indoor[TEMPELEMENTSIN]={0.0};
//functions:
void temperature_push_stack_indoor(float val_in);
void print_temperature_push_stack_indoor(void);
void clear_temperature_push_stack_indoor(void);
//----------------------------------------------------------
//indoor push data to stack
//----------------------------------------------------------
void temperature_push_stack_indoor(float val_in)
{
for(byte cc_=TEMPELEMENTSIN;cc_>0;cc_--)
temp_stackbuffer_indoor[cc_]=temp_stackbuffer_indoor[cc_-1];
temp_stackbuffer_indoor[0]=val_in;
}
//----------------------------------------------------------
//indoor print it out
//----------------------------------------------------------
void print_temperature_push_stack_indoor(void)
{
byte tmp__=1;
fprintf(PRINTDSTEMP,"\n\rIndoor temperatures (24h) %3ldd:%02d:%02d:%02d",totaldays,totalhours,totalmins,seconds);
for(byte ii_=0;ii_<TEMPELEMENTSIN;ii_++)
fprintf(PRINTDSTEMP,"\n\r(-%uh):%.1fc",tmp__++,temp_stackbuffer_indoor[ii_]);
}
//----------------------------------------------------------
//indoor clear it
//----------------------------------------------------------
void clear_temperature_push_stack_indoor(void)
{
memset(temp_stackbuffer_indoor, 0.0, TEMPELEMENTSIN);
}
//outdoor
//----------------------------------------------------------
void temperature_push_stack(float val)
{
for(byte c_=TEMPELEMENTS;c_>0;c_--)
temp_stackbuffer[c_]=temp_stackbuffer[c_-1];
temp_stackbuffer[0]=val;
}
//outdoor
//----------------------------------------------------------
void print_temperature_push_stack(void)
{
byte tmp_=1;
fprintf(PRINTDSTEMP,"\n\rOutdoor temperatures (24h) %3ldd:%02d:%02d:%02d",totaldays,totalhours,totalmins,seconds);
for(byte ii=0;ii<TEMPELEMENTS;ii++)
fprintf(PRINTDSTEMP,"\n\r(-%uh):%.1fc",tmp_++,temp_stackbuffer[ii]);
}
//outdoor
//----------------------------------------------------------
void clear_temperature_push_stack(void)
{
memset(temp_stackbuffer, 0.0, TEMPELEMENTS);
}
|
PCWHD 5.092 + MPLAB IDE 8.92 + plugin for MPLAB IDE
OS WIN10/64bit
Can somebody help, because i cannot locate where the problem is?
Last edited by -Terppa- on Mon Dec 05, 2022 1:08 pm; edited 1 time in total |
|
|
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
|
Posted: Mon Dec 05, 2022 4:00 am |
|
|
Hmmm.. it seems to work if i add "dummy" line
Code: |
float temp_stackbuffer_indoor[TEMPELEMENTSIN]={0.0};
float dummy[1]={0.0}; //added dummy line
float temp_stackbuffer[TEMPELEMENTS]={0.0};
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Mon Dec 05, 2022 6:01 am |
|
|
Does CCS C consider both 'push' and 'stack' to be reserved words and cannot be used ,in your case as variables?
I know BASIC always had an appendix that listed 'reserved words' that you could not use
I'm thinking that when the compiler attempts to parse a line it sees push, stack and says 'error', however when it's got =0.0; on the end, it now KNOWS that the item on the left side is a variable.
I'm sure one of the C experts here know HOW the compiler figures this stuff out and can explain it |
|
|
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
|
Posted: Mon Dec 05, 2022 6:42 am |
|
|
Thank you for your reply Mr.Temtronic.
I always try to avoid using reserved words.
I'm little bit confusing because now i have same functions and that added dummy float and now both buffers seems to work
I think it is not the proper way to do that.. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Dec 05, 2022 7:23 am |
|
|
I suspect it does not like having an 'array' initialisation with only one element.
The dummy, may be tricking it into accepting this. |
|
|
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
|
Posted: Mon Dec 05, 2022 7:35 am |
|
|
Thank you Mr.Ttelmah
I tested in this way:
Code: |
float temp_stackbuffer_indoor[TEMPELEMENTSIN]={};
float temp_stackbuffer[TEMPELEMENTS]={};
|
and it stop working again. (only temp_stackbuffer gets values)
but when it is this way it works:
Code: |
float temp_stackbuffer_indoor[TEMPELEMENTSIN]={};
float dummy__[1]={0.0};
float temp_stackbuffer[TEMPELEMENTS]={};
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Dec 05, 2022 12:11 pm |
|
|
The problem is the code is overflowing the stack. Having the dummy
variable allows this to not overwrite the next variable,
For example:
Code: |
for(byte c_=TEMPELEMENTS;c_>0;c_--)
temp_stackbuffer[c_]=temp_stackbuffer[c_-1];
|
c_ is set to 24. So the first time round, this writes to element 24.
The array only has elements 0 to 23.
It looks as if possibly you have taken this code from a language that
has 1 as the first array element. |
|
|
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
|
Posted: Mon Dec 05, 2022 1:07 pm |
|
|
Thats is! Thank you very much! Or how finnish people says: Paljon kiitoksia |
|
|
|