This won't debug!

Thread Starter

jdraughn

Joined Jan 30, 2009
33
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:

Rich (BB code):
#include <pic18.h>
TurnOnLed(char *Led,int brightness);
void main(void)
{
   PORTB = 0b00000000;
   while (1)
   {
   TurnOnLed(&TRISB,100); 
   }
 
}
TurnOnLed(char *Led,int brightness)
{
  *Led = 0b00000000; // turn it on
  for(int b=brightness;b=0;b--) // wait brightness cycles
  { }
  *Led = 0b11111111; // turn it off
  for(int b=100-brightness;b=0;b--) // wait 100 - brightness cycles
  { }
}
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.
 

RiJoRI

Joined Aug 15, 2007
536
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.

--Rich
 

Thread Starter

jdraughn

Joined Jan 30, 2009
33
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.
 

Vaughanabe13

Joined May 4, 2009
102
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.
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.
 
Top