State Based Coding in C (Using MPLAB)

Discussion in 'The Projects Forum' started by Stackington21, May 24, 2016.

  1. Stackington21

    Thread Starter New Member

    Oct 16, 2015
    Hi all
    So i am doing my uni assignment and I am struggling with my debugging. I need to stress that this is a uni assignment and not the real thing, so clearly some of it will not be of logical sense (more so to show that it works for what we need to do). Basically my code is done and is in a sequence that is correct. The problem i am having with debugging is stepping through the states. At the moment it will step into case 0 perfectly fine, goes down as correct and should go to the state = 1; line.

    The problem after this is it does not go to the case 1, it jumps down to a random curly bracket line in case 3, jumps right down to the end of the code and repeats. Now my uni lecturer has told me that the problem is "You haven’t got a set of { } brackets encompassing all the code within each case:".

    The code i have included is what i have without trying to incorporate his comments, but i have tried different approaches with said missing brackets and i am still getting the same result. I am using MPLAB X IDE v3.25 for my compiling if that matters.

  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    MPLAB has a syntax highlighter you should be using. It is not a newbie thing but an advanced tool to show you mismatched brackets and the like. If you put your cursor on one bracket it highlights that and the matching bracket.

    I can't use that at the moment as I am on an iPad but it looks like you missed a bracket to close the encompassing while loop, but following that many brackets by eye is near impossible. Hence the syntax tool.

    I do note lots of do nothing code that can eventually cause trouble. I mean take any state: you test to see of the state should be advanced, that is good. But testing the state should not be advanced and then setting the state to the present state is a useless exercise, and while it may work now any extra code is a bad thing, especially if you make one modification and need to change something else later on.

    Also I don't like magic numbers used for states. Define using equates or enumerations the state numbers to state names. This way if you add a state you don't go crazy trying to fine say all the times 3 changes to 4. It also makes your code easier to read as it becomes more self documenting.

    Good luck with this, and nest time thing about using the micro controller forum, you will get better eyes on this problem.