Debug Code?

Status
Not open for further replies.

doo20000

New Member
#include <htc.h>
#include <pic.h>

__CONFIG(RC & WDTDIS & PWRTEN & BOREN & LVPEN & WRTEN & DEBUGEN & UNPROTECT);

int x, y;

delay(int xvalue,int yvalue)
{

for(x = 0; x < xvalue; x++)
for(y = 0; y < yvalue; y++);

return;
}

main()
{
PORTA = 0x00; //Clear port A
PORTB = 0x00; //Clear port B
PCFG2 = 1;
PCFG1 = 1;
TRISA = 0xff; //Set port A as input
TRISB = 0; //set port B as output

while(1) // Loop forever
{
if(RA0 == 1)
{
while(RA0 == 1) // If push button 0 is pressed
{

RB0 = 1; // Pulse high
delay(2,279); // 2 ms
RB0 = 0; // Low
delay(19,243); // 18 ms
}
}

else if (RA1 == 1)

{
while(RA1 == 1)
{

RB0 = 1; // Pulse high
delay(2,127); // 1 ms
RB0 = 0; // Low
delay(19,271); // 18 ms
}
}
}
}


Please someone could help me debug this code, i would like to be able to control my servo motor (TowrPro MG995) Using pic16f877a. As you can see i am using port A as input and port B as output. when i press RA0 i want it to go left and RA1 i want it to go right. thank you
 
I have found one bug so far. I have been working on this code so hard. I only send one pulse everytime i press button so frequency donest change. so how should i make it to where it increment one step at the time when i still holding the RA0 or RA1? please help me out. thank you
 
What happens if you do the following?

Code:
main()
{
    PORTA = 0x00; //Clear port A
    PORTB = 0x00; //Clear port B
    PCFG2 = 1;
    PCFG1 = 1;
    TRISA = 0xff; //Set port A as input
    TRISB = 0; //set port B as output

    while(1) // Loop forever
    {
        if(RA0 == 1)
        {
            RB0 = 1; // Pulse high
            delay(2,279); // 2 ms 
            RB0 = 0; // Low
        }
        else if (RA1 == 1)
        {
            RB0 = 1; // Pulse high
            delay(2,127); // 1 ms 
            RB0 = 0; // Low
        }
        else
        {
            RB0 = 1; // Pulse high
            delay(2,200); // 1.5 ms 
            RB0 = 0; // Low    
        }
        delay(19,271); // 18 ms
    }
}

BTW, if you put
Code:
 and [ /code] without the space it will keep the code indentations.

Mike.
 
Hi, Mike after i tested the code and send to the pic, the servo only giggle a little (shacking) and froze there. why is that?
 
What value resistor and capacitor are you using for your RC oscillator?

Can you look at the output with a scope?

Mike.
 
That board has either a 20MHz crystal or a much slower RC oscillator. Your delay code looks like it is written for the crystal oscillator.

Try switching the onboard switch to xtal and change the config line to,

__CONFIG(HS & WDTDIS & PWRTEN & BOREN & LVPEN & WRTEN & DEBUGEN & UNPROTECT);

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…