This won't debug!

Discussion in 'Embedded Systems and Microcontrollers' started by jdraughn, Jul 15, 2009.

  1. jdraughn

    Thread Starter Active Member

    Jan 30, 2009
    So very frusturated with MPLab. Anyway, I am just starting out learning how to program microcontrollers, and im pretty new to C and assembly too. I am trying to make an LED dim via PWM and while I know if I look on the web I can find a super great effecient way of doing it, I like to try and figure things out for myself first. So I wrote the following block of code to try and get things going:

    Code ( (Unknown Language)):
    3. #include <pic18.h>
    4. TurnOnLed(char *Led,int brightness);
    5. void main(void)
    6. {
    7.    PORTB = 0b00000000;
    8.    while (1)
    9.    {
    10.    TurnOnLed(&TRISB,100);
    11.    }
    13. }
    14. TurnOnLed(char *Led,int brightness)
    15. {
    16.   *Led = 0b00000000; // turn it on
    17.   for(int b=brightness;b=0;b--) // wait brightness cycles
    18.   { }
    19.   *Led = 0b11111111; // turn it off
    20.   for(int b=100-brightness;b=0;b--) // wait 100 - brightness cycles
    21.   { }
    22. }
    Well the problem is that I will build the project, then I will start debugging by stepping into the code. After about 4 clicks or so the green arrow will hit the TurnOnLed(&TRISB,100); line of code, and the next time I click "step into", the green arrow dissapears for a split second, but reappears on the same line! It will NOT actually proceed into the function itself.

    I have tried rebooting my computer, rebuilding it from scratch with a new project, changing the code around a tiny bit, but no matter what execution will not proceed past the point where I call the function.

    Does anyone have any ideas? I don't even know if that function would work to dim an LED, it seems like it would when I worked the logic out in my head but I have a tendency to not get very simple logical things, letalone create them myself.
  2. RiJoRI

    Well-Known Member

    Aug 15, 2007
    Chances are your for loops are being optimized away -- they aren't doing anything, so why test?

    for(int b=brightness;b=0;b--) will never work. The test portion "b=0" assigns 0 to b, and I believe it will always return TRUE. Also, your test is inverted. It should be "b != 0".

    Also, TRISB is NOT the port, it is the TRIState controller for Port B. I believe you are switching Port B between INPUT and OUTPUT.

  3. jdraughn

    Thread Starter Active Member

    Jan 30, 2009
    Ahh thanks, said I was new to c. I thought the first part what the b variable starts with, the middle part what causes the loop to end, the third part how it increments/decrements the b variable. I tried changing the port, lat and tris values and discovered that changing trisb to 0 would make the light light up. I will do some more research.

    Fixing the loop will probably make my circuit work, but not the way it should by changing the port from input to output.
  4. Vaughanabe13

    Active Member

    May 4, 2009
    Not to be rude but there are a LOT of things wrong with what you just said.

    The problem with your loop is you are using a single equals sign, which is the "assignment" operator. In other words, every time you say b = 0 you are setting b equal to 0. To CHECK and see if it is zero you need to use the double equals operator (==). That is basic C code right there. The most common for loop will look like this:

    int x;
    for(x = 0; x < 10; x++) {
    //do code here;

    Note that I declared the variable outside of the loop, which is good programming practice depending on what compiler you are using. The first part of the for loop sets the initial condition of the loop variable, so x is set to 0. The next part checks to see if the loop is done or not, so if x is less than 10 it will keep looping but when x is equal to 10 it will stop. The last part will decide how to change the loop variable, so in this case I am using a post increment.

    The reason it lights up when you set TRISB equal to 0 is because that means you set the pin as an output and the output happened to be set to high (logic level 1). TRISB controls the "direction" of port B, so if you set a bit to a 0 it is an output (and can drive an LED) and if you set it to a 1 it is an input (will not light LED).

    So if you want to light up an LED you first need to set the corresponding TRIS bit to a 0 for output, and then set the PORT bit to a 1 for high, which will light up your LED. Make sense? If not you should do a lot more research because there is tons of information about this online and from reading the datasheets.