# Is my C code to control stepper motor ok?

#### Guinness

Joined Dec 31, 2009
81
Hi, this is the first real program I have done that isn't just a copy of someone else's. As such I think I have got everything in there I need, but as I only started learning C just over a week ago, I expect a few things to be wrong. If you could have a look and point out any errors I have made please.

The hardware is ok, as I had a different thread to check that out, so I have only included it as refference to check with the C code. I only mention that so it doesn't get confussed as a duplicate thread.

Rich (BB code):
/* The program is designed to make a 200 step Unipolar Stepper Motor turn
360 degrees once every second*/

#include<pic.h>
__CONFIG(XT & WDTDIS & UNPROTECT & MCLREN);
void pause (unsigned short usvalue);
void msecbase (void);
void step (unsigned char phase);
main()
{
unsigned char counter = 0;

PORTB = 0x00;
TRISB = 0x00;

while(1==1)
{
while(counter<=200)
{
step;          //Calls Step Function
pause(5);      //Calls pause Function which pauses for 5 milli Sec
counter++;     //increment counter by 1

}

counter = 0;      //reset counter so while loop can start again

}
}
void pause (unsigned short usvalue)//Calls the 1mS delay function usvalue times
{
unsigned short x;

for(x=0; x<=usvalue; x++)
{
msecbase();
}

}
void msecbase (void)     //This function delays for about 1mS
{
OPTION = 0x00000001;//sets the prescaler to 1:4
TMR0 = 0xD;         //presets TMR0 overflow
while(!T0IF);       //stay untill TMR0 overflow flag equals 1
T0IF = 0;           //clears the TMR0 overflow flag
}

/* The step function is designed to activate step 1 when it is first called.
When it is called the second time, the phase variable would have been
incremented to 2, which activates step 2. The same for step 3, but at the
end of step 4, the phase variable resets to 1, starting the proccess over.*/
void step (unsigned char phase)
{
unsigned char phase = 1;

switch (phase)
{
case 1:
PORTB = 160;//RB7 + RB5 go high
phase++;    //increments phase to 2
break;
case 2:
PORTB = 96;//RB6 + RB5 go high
phase++;   //increments phase to 3
break;
case 3:
PORTB = 80;//RB6 + RB4 go high
phase++;   //increments phase to 4
break;
case 4:
PORTB = 144;//RB7 + RB4 go high
phase = 1;  //resets phase to 1
break;

}

}

#### Attachments

• 50.4 KB Views: 102

#### rjenkins

Joined Nov 6, 2005
1,013
Can you please post what compiler you are using, as each has it's own extensions and none-standard instructions / functions for the internal peripherals.

The only bit of the code that stands out on a quick glance is the function prototype declarations:

Rich (BB code):
void pause (unsigned short usvalue);
void msecbase (void);
void step (unsigned char phase);
The declarations should not have the actual variables included, just the variable types, like
Rich (BB code):
void pause (unsigned short);
void msecbase (void);
void step (unsigned char);

The big question is what happens when you try to compile it?
Happy programming!

#### Guinness

Joined Dec 31, 2009
81
Can you please post what compiler you are using, as each has it's own extensions and none-standard instructions / functions for the internal peripherals.
Its the Hi-Tech lite Compiler that I got with the pic Kit 2 package.

The big question is what happens when you try to compile it?
It wouldn't compile at first, but I done what you said and deleted the variable names from the prototype functions. That got rid of the old failed message, so cheers for that one

I did get a new failed messege about the MCLREN in the CONFIG line at the beginning, I deleted that and it compiled fine then.

But does that mean I wont be able to use the MCLR pin on the PIC to reset it?
Or is it turned on by default, and the option is to leave it on or put MCLRDIS to turn it off?

#### rjenkins

Joined Nov 6, 2005
1,013
Does the 16F84 have any alternate function on the MCLR pin?
It won't need that option unless MCLR can be configures as an I/O pin.

If you have the PICKIT 2 then you would be better off using a chip that supports the ICD interface, you can then single step through the program and watch the memory & registers to see what is happening from within MPLab.

It's a brilliant system for debugging.

#### Guinness

Joined Dec 31, 2009
81
Does the 16F84 have any alternate function on the MCLR pin?
It won't need that option unless MCLR can be configures as an I/O pin.
Oh right, will remember that as its only a MCLR pin. I done my first few little programs on the 16F690, so just assumed all PICs had to have it configured.

If you have the PICKIT 2 then you would be better off using a chip that supports the ICD interface, you can then single step through the program and watch the memory & registers to see what is happening from within MPLab.

It's a brilliant system for debugging.
I got the starter Kit instead of the Debugging Kit. Is it just a case of making a demo board with different connections, or is there special software that needs to be used with MPLAB to use the debugging system?

It does sound very usefull, been able to see what the program is doing before putting it in practice and finding out there is a mistake.

edit: never mind my questions about the degugging kit, I have looked into it now and understand what its all about and with what PIC's I can and can't use it with. Cheers for the info, just have a information overload now in regards to what PIC to get!!!

Last edited: