5 Replies - 32619 Views - Last Post: 05 February 2009 - 06:08 AM Rate Topic: -----

#1 umair54  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-June 07

Programming a Stepper Motor in C using PIC16F684

Post icon  Posted 02 June 2007 - 07:02 PM

 #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...1202F-print.pdf [/link]
i'd applicate you help

Is This A Good Question/Topic? 0
  • +

Replies To: Programming a Stepper Motor in C using PIC16F684

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Programming a Stepper Motor in C using PIC16F684

Posted 03 June 2007 - 04:48 AM

View Postumair54, on 2 Jun, 2007 - 07:02 PM, said:

 #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...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
Was This Post Helpful? 0
  • +
  • -

#3 umair54  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-June 07

Re: Programming a Stepper Motor in C using PIC16F684

Posted 03 June 2007 - 08:28 AM

[link] http://www.flickr.co...@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...
Was This Post Helpful? 0
  • +
  • -

#4 NumairaObaid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-June 07

Re: Programming a Stepper Motor in C using PIC16F684

Posted 10 June 2007 - 01:19 PM

Good Job, Umair! -numaira
Was This Post Helpful? 0
  • +
  • -

#5 notics  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 11-October 08

Re: Programming a Stepper Motor in C using PIC16F684

Posted 11 October 2008 - 07:48 PM

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 image(s)

  • Attached Image

Was This Post Helpful? 1
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Programming a Stepper Motor in C using PIC16F684

Posted 05 February 2009 - 06:08 AM

a basic timer for the PIC16 would look something like this

volatile int tenMillSecCounter=0;			   // incremented every 10mSec
static volatile int tenMillSecCounter2=0;	   // incremented every 10mSec - used for delays
volatile int tenthsSecond=0;					// incremented every tenth of a second
volatile int second=0;						  // incremeneted every second

// Timer 0 is programmed to interrupt every 10mSeconds
//  T0 clock is FOSC/4 = 200000
//  with 256 prescaler decrements 2000000/256 times / second
//  to get an interrupt every T mSec the calculation for TMR0 overflow from 0xFF to 0 is
//	count = 255 - T * 2000/256
//	i.e. for 10mSec counter is
#define tenMillSec 177
static void interrupt isr(void)
{
	if (T0IF)						   // A TMR0 interupt occurred
	{
		TMR0=tenMillSec;				// increment counters every 10 mSec
		tenMillSecCounter2++;
		revolutionTimer++;
		if((++tenMillSecCounter % 10)==0) tenthsSecond++;
		if((tenMillSecCounter % 100)==0) { second++; tenMillSecCounter=0; }
	}
	T0IF=0;							 // clear TMR0 interrupt flag
}

// delay for count tenths of a second
void tenthsSecondDelay(int count)
{
	tenMillSecCounter2=0;						 // zero counter
	while( tenMillSecCounter2  < (count*10));	// wait
}
		
// delay for count seconds
void secondDelay(int count)
{
	tenMillSecCounter2=0;						 // zero counter
	while( tenMillSecCounter2  < (count*100));   // wait
}
		
// initalise clock, set up timer 0, etc
void systemInitialise(void)
{
		OSCCON=0x70;			 // Fosc use oscillator 8MHz
	   TMR0=tenMillSec;		 // initialise timer 0 counter
		T0IE = 1;				// enable interrupt on TMR0 overflow from 0xFF to 0
		GIE = 1;				 // Global interrupt enable
 }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1