I would appreciate some help with a function. I have been successful with some simple ones.
Here is the code, provded by Thorsten, that I want to convert into a function. There are multiple cases only 2 are shown. The lengthy code works. When I replace it with the function it fails.
switch (group)
////Memory Group//////uses pin 0 - 5
{case(1) :
{
memory_group_selected_stored_pin = stored_pin
if (pin_value == 0)// reacts only when pressed, momentary switch action
//SetLed_NoDump ( 0, 5, 9, (stored_pin));/// ******* see A below
//if button already selected, turn off LED's
if(memory_group_selected_stored_pin == pin)//Is current button same as stored pin? ?
{
memory_group_selected_stored_pin = 9;// if same, set to pin 9. This is the cancel pin
}else
memory_group_selected_stored_pin = pin ;//save the current pin in memory_group_selected_stored_pin variable
for(i=0; i<= 5; i++)//All LED's should be cleared
MIOS_DOUT_PinSet0(i);
if(memory_group_selected_stored_pin != 9)
MIOS_DOUT_PinSet1(memory_group_selected_stored_pin);//set the LED for the current pin
}
break;
//////////Record (Used with Memory Group) /////// uses pin 6
{case (2) :{
record_button_state = stored_pin
if (pin_value == 0)
//SetLed_NoDump ( 6, 6, 7, (stored_pin));///!!!!!!! see B below
if(record_button_state == pin)
etc
A) The 4th line, that is commented out, is the function sending statement. B) This is a similar line to A) in Case (2) with the change of the last parameter. Here is my attempt at the function.
Can you provide some more details about what this code is supposed to do and what (if anything) the function actually ends up doing? I’m pretty good with C, but I’ve not done any work with the C applications around here yet. Error messages from the compiler?
if(pin == (stored_pin) )
why do you have stored_pin in parantheses? It prolly won’t affect the program at all, but this shouldn’t be needed.
Without getting out pen and paper to check your logic seems to be correct. One last thing: you are using “i” as a counter variable (i for integer), but you are declaring it as a character. While this may still work, you should use the type “int” here. Since you know you don’t need to count negative numbers the “unsigned int” is slightly more efficient.
While this may still work, you should use the type “int” here. Since you know you don’t need to count negative numbers the “unsigned int” is slightly more efficient.
No he shouldn’t. He should use “unsigned char” as loang as he don’t need neagtive numbers (then he should use “(signed) char”) or values > 255 (>127, <128).
to use a 16bit with SDCC and PIC is a very bad idea! This could increase the code to more than doubled size! (the pic is only 8bit and has only 8bit registers)
No he shouldn’t. He should use “unsigned char” as loang as he don’t need neagtive numbers (then he should use “(signed) char”) or values > 255 (>127, <128).
Yes, that is definatly correct :-[ Java has ruined me
I’m still not seeing why were keeping stored_pin in parantheses… reason?
Thomas, I found the error with the 5th parameter. This should have been removed. It shows as a compiler error.
I have (stored_pin) declared just under VoidNotifyDin Toggle. Hence it is outside the function.
Moogah, you were right about == versus =.
The application is setting a “Radio” group of momentary contact pin/led. Only one led should light at a time. The second press of the same button should cause the led to be off.
In this case you are passing the function “MIOS_DOUT_PinSet1” the variable “stored_pin” which you symbolize by putting the variable in parantheses right after the function:
MIOS_DOUT_PinSet1(stored_pin)
notice that there is no space between the function name and the variable in parantheses. Unless there is some other detail with the SDCC this is the correct way.
As for the issue with needed two presses for a button to begin working correctly, consider this:
Since sending this post I have done some further reading and realise what Moogah was referring to. I need to prototype my function for unsigned char and write the function as an unsigned char. ie No Void. I’m not there yet.
The function will change the variables. I need to return values.
I forgot to include the variable pin in the function. I’m getting there!
I made an error in thinking that my problem was over. I have spent a few hours trying to find the error but no luck! There are no Led’s on and nothing works in case1 and 2! The rest of the code is pre Led function and works well.
I cannot find any faults in my logic but its wrong. The pre function version works fine.
Here are the relevant parts of the code expanded for completeness. Sorry about the length. I will try to underline the relevant lines.
The above type passes values to the function but receives no parameters back.
Function where values are passed back to the calling function
Prototype
In my “difficult” case I needed to send back a value to the calling function. This involves a different prototype statement at the start of the program
Note the 3rd, 4th and 5th lines. These refer to the functions where no values are returned.
Note the 6th line. This is the prototype statement where the function is preceded by the data type of the variable being returned. The function title follows.
Then the parameters or variables are listed (6 are shown).
Calling function
I have shown two cases out of about 12 . You will see the lines that include SetLedNoDump. These lines are the important ones; they send the values to the function.Note tha they are preceded by stored_pin. This is the variable that the function returns values to.
switch (group)
{case(1) :
{
stored_pin = memory_group_selected_stored_pin;// change value of stored pin to the group stored value (in this case the memory group.
stored_pin = SetLed_NoDump(0, 5, 9,stored_pin, pin, pin_value); // for the function, set pin_low to 0, pin_high to 5, canc_pin to 9
memory_group_selected_stored_pin = stored_pin;//resets the variable for this group to the current stored pin after the function above.
Hey, I’m too busy with work and school this week to have taken a serious look into what your doing to offer insight, but, it looks like you’ve got the right idea and know how to find the relavant documents to help you along, keep studying and keep trying (and erroring) new things and I’m sure you’ll do fine ;D
I noticed you are making no use of curly brackets within if-statements. As this is no error, your function will work fine without, but it may increase readability of the code, if you’d use some brackets and indentation; the logic of the blocks gets a lot clearer…
Moreover I found a problem with your for()-loop. As there are also no curly brackets set, I think this might not work as inteded? See the nested version with curly brackets set here; this is probably how the compiler interprets your code:
I have revised the code as you suggest. I agree that it is good practice to always use brackets. I have avoided them when there is only one statement following that is relevant.
As far as the logic is concerned I agree about the return statement. I suppose that I was lucky. In my case the results are the same. However the compiler, I think, will warn you if the return value is not sent
However I will indent the lines and insert brackets in future.