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.
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