Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,132 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,793 people online right now. Registration is fast and FREE... Join Now!




Programming a Stepper Motor in C using PIC16F684

 
Reply to this topicStart new topic

Programming a Stepper Motor in C using PIC16F684, Made in MPLAB IDE

umair54
2 Jun, 2007 - 06:02 PM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2007
Posts: 2


My Contributions
CODE
#include <pic.h>



__CONFIG (INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);

int i;
int j;
unsigned char OutputVal = 1 << 5;
int i, j, k, n, Flag;

const int ones = 225;
const int fiftyms = 2790;
const int twentyms = 1000;
const int fourms   = 118;


main ()

{

PORTA = 0x3F;
PORTC = 0x3F;
CMCON0 = 7;
ANSEL = 0;
TRISA = 0b001100;
TRISC0 = 0;

while (1==1)

{
INTCON = 0;            

        Flag = 0;                           //  Flag Pressed Button
        for (i = 0; i < twentyms; )              //  Debounce Button Press
            for (i = 0; (i < twentyms) && (0 == RA2); i++)
                if (0 == RA3)
                    Flag = 1;

        PORTA = 0x00;                       //  Turn off LEDs
        PORTC = 0x00;

        for (i = 0; i < twentyms; )             //  Debounce Button Release
            for (i = 0; (i < twentyms) && (1 == RA2); i++)
                if (0 == RA3)
                    Flag = 1;

        for (i = 0; (i < ones) && (0 == Flag); i++)      //  1s Dlay
            for (j = 0; j < ones; j++)
                if (0 == RA3)
                    Flag = 1;

        k = TMR0;               //  Get Start Random Value
        for (i = 0; (i < k) && (0 == Flag); i++)     //  Random Dlay
            for (j = 0; j < fourms; j++)
                if (0 == RA3)
                    Flag = 1;

for (i = 0; i < 255; i++)
for (j = 0; j < 129; j++);

OutputVal = (OutputVal & 0x3c) >> 1;
if ((1 << 1) == OutputVal)
OutputVal = 1 << 5;
PORTC = OutputVal;


INTCON = 0;                             //  Disable/Reset ALL Interrupts

        Flag = 0;                           //  Flag Pressed Button
        for (i = 0; i < twentyms; )              //  Debounce Button Press
            for (i = 0; (i < twentyms) && (0 == RA3); i++)
                if (0 == RA2)
                    Flag = 1;

        PORTA = 0x00;                       //  Turn off LEDs
        PORTC = 0x00;

        for (i = 0; i < twentyms; )              //  Debounce Button Release
            for (i = 0; (i < twentyms) && (1 == RA3); i++)
                if (0 == RA2)
                    Flag = 1;

        for (i = 0; (i < ones) && (0 == Flag); i++)      //  1s Dlay
            for (j = 0; j < ones; j++)
                if (0 == RA2)
                    Flag = 1;

        k = TMR0;                           //  Get Start Random Value
        for (i = 0; (i < k) && (0 == Flag); i++)     //  Random Dlay
            for (j = 0; j < fourms; j++)
                if (0 == RA2)
                    Flag = 1;

for (i = 0; i < 255; i++)
for (j = 0; j < 129; j++);

OutputVal = (OutputVal & 0x3c) << 1;
if ((1 << 6) == OutputVal)
OutputVal = 1 << 2;
PORTC = OutputVal;


}
    
}



hi guys..my name is Umair..and i have a very minor experience in programming on the C language...and i need some help (would really appreciate it)... the above program is supposed to make the stepper motor turn for a certain time (counter clockwise); when RA2 is pressed and its supposed to make the motor turn for the same time (clockwise); when RA3 is pressed..although it builds properly during the simulation on MPLABIDE but it wouldn't run when we wire it on (on a breadboard)
could you help me and show where my problem is..? and if possible i would also like some help on button command...as you can see mine are really really long...and some simplicity in this program would be of some great help...
here is a link to the PIC16F648 data sheet [link] http://ww1.microchip.com/downloads/en/Devi...1202F-print.pdf [/link]
i'd applicate you help
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Programming A Stepper Motor In C Using PIC16F684
3 Jun, 2007 - 03:48 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(umair54 @ 2 Jun, 2007 - 07:02 PM) *

CODE
#include <pic.h>



__CONFIG (INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);

int i;
int j;
unsigned char OutputVal = 1 << 5;
int i, j, k, n, Flag;

const int ones = 225;
const int fiftyms = 2790;
const int twentyms = 1000;
const int fourms   = 118;


main ()

{

PORTA = 0x3F;
PORTC = 0x3F;
CMCON0 = 7;
ANSEL = 0;
TRISA = 0b001100;
TRISC0 = 0;

while (1==1)

{
INTCON = 0;            

        Flag = 0;                           //  Flag Pressed Button
        for (i = 0; i < twentyms; )              //  Debounce Button Press
            for (i = 0; (i < twentyms) && (0 == RA2); i++)
                if (0 == RA3)
                    Flag = 1;

        PORTA = 0x00;                       //  Turn off LEDs
        PORTC = 0x00;

        for (i = 0; i < twentyms; )             //  Debounce Button Release
            for (i = 0; (i < twentyms) && (1 == RA2); i++)
                if (0 == RA3)
                    Flag = 1;

        for (i = 0; (i < ones) && (0 == Flag); i++)      //  1s Dlay
            for (j = 0; j < ones; j++)
                if (0 == RA3)
                    Flag = 1;

        k = TMR0;               //  Get Start Random Value
        for (i = 0; (i < k) && (0 == Flag); i++)     //  Random Dlay
            for (j = 0; j < fourms; j++)
                if (0 == RA3)
                    Flag = 1;

for (i = 0; i < 255; i++)
for (j = 0; j < 129; j++);

OutputVal = (OutputVal & 0x3c) >> 1;
if ((1 << 1) == OutputVal)
OutputVal = 1 << 5;
PORTC = OutputVal;


INTCON = 0;                             //  Disable/Reset ALL Interrupts

        Flag = 0;                           //  Flag Pressed Button
        for (i = 0; i < twentyms; )              //  Debounce Button Press
            for (i = 0; (i < twentyms) && (0 == RA3); i++)
                if (0 == RA2)
                    Flag = 1;

        PORTA = 0x00;                       //  Turn off LEDs
        PORTC = 0x00;

        for (i = 0; i < twentyms; )              //  Debounce Button Release
            for (i = 0; (i < twentyms) && (1 == RA3); i++)
                if (0 == RA2)
                    Flag = 1;

        for (i = 0; (i < ones) && (0 == Flag); i++)      //  1s Dlay
            for (j = 0; j < ones; j++)
                if (0 == RA2)
                    Flag = 1;

        k = TMR0;                           //  Get Start Random Value
        for (i = 0; (i < k) && (0 == Flag); i++)     //  Random Dlay
            for (j = 0; j < fourms; j++)
                if (0 == RA2)
                    Flag = 1;

for (i = 0; i < 255; i++)
for (j = 0; j < 129; j++);

OutputVal = (OutputVal & 0x3c) << 1;
if ((1 << 6) == OutputVal)
OutputVal = 1 << 2;
PORTC = OutputVal;


}
    
}



hi guys..my name is Umair..and i have a very minor experience in programming on the C language...and i need some help (would really appreciate it)... the above program is supposed to make the stepper motor turn for a certain time (counter clockwise); when RA2 is pressed and its supposed to make the motor turn for the same time (clockwise); when RA3 is pressed..although it builds properly during the simulation on MPLABIDE but it wouldn't run when we wire it on (on a breadboard)
could you help me and show where my problem is..? and if possible i would also like some help on button command...as you can see mine are really really long...and some simplicity in this program would be of some great help...
here is a link to the PIC16F648 data sheet [link] http://ww1.microchip.com/downloads/en/Devi...1202F-print.pdf [/link]
i'd applicate you help


Hi

Given that you are using the C language, why not do a little decomposition to extract common code blocks into functions. That will reduce your code considerably.

Now, as to your actual problem, without a schematic (i have about 30 years electronics experience) its not possible to guess the purpose of your code.

Can you make a schematic available for your hardware so we can see how its connected to the PIC?

regards


Greg

User is offlineProfile CardPM
+Quote Post

umair54
RE: Programming A Stepper Motor In C Using PIC16F684
3 Jun, 2007 - 07:28 AM
Post #3

New D.I.C Head
*

Joined: 1 Jun, 2007
Posts: 2


My Contributions
[link] http://www.flickr.com/photos/8666079@N04/527805066/ [/link]

here is link to the picture of the schematic although there are some changes....1)on RA3 (pin4) instead of a 10k pot there is a push button 2) there is also a push button on RA2 (pin 11)

what im trying to make is draw bridge using that motor...which would raise and lower 90 degrees on the push of a button even at this point im not certain that the program made above will make it go 90 degrees or not...
User is offlineProfile CardPM
+Quote Post

NumairaObaid
RE: Programming A Stepper Motor In C Using PIC16F684
10 Jun, 2007 - 12:19 PM
Post #4

New D.I.C Head
*

Joined: 10 Jun, 2007
Posts: 1


My Contributions
Good Job, Umair! -numaira
User is offlineProfile CardPM
+Quote Post

notics
RE: Programming A Stepper Motor In C Using PIC16F684
11 Oct, 2008 - 06:48 PM
Post #5

New D.I.C Head
*

Joined: 11 Oct, 2008
Posts: 1

hi umair,
nice to meet u..
can u help me about c program..
im new in c++..

how to create timer for this program
this program that i have just for ccw/cw only

void cw() //clockwise rotation
{
portb=0b00000001; //1 the sequence for stepper motor clockwise
delay_ms(100);
portb=0b00000011; //3
delay_ms(100);
portb=0b00000010; //2
delay_ms(100);
portb=0b00000110; //6
delay_ms(100);
portb=0b00000100; //4
delay_ms(100);
portb=0b00001100; //12
delay_ms(100);
portb=0b00001000; //8
delay_ms(100);
portb=0b00001001; //9
delay_ms(100);
}

void ccw() // counter clockwise rotation
{
portb=0b00001001; //9
delay_ms(100);
portb=0b00001000; //8
delay_ms(100);
portb=0b00001100; //12
delay_ms(100);
portb=0b00000100; //4
delay_ms(100);
portb=0b00000110; //6
delay_ms(100);
portb=0b00000010; //2
delay_ms(100);
portb=0b00000011; //3
delay_ms(100);
portb=0b00000001; //1
delay_ms(100);
}
int i;

void main()
{
trisb=0;
portb=0;

while(1)
{

for(i=0;i<10;i++)
{
cw();
}

for(i=0;i<10;i++)
{
ccw();
}

}
}


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month