CNCzone.com-The Largest Machinist Community on the net!



Home Page Mark Forums Read Today's Posts My Replies Classifieds Reviews Photo Gallery Web Links Share Files Advertise With Us Ad List
Go Back   CNCzone.com-The Largest Machinist Community on the net! > Electronics > PIC Programing / Design


PIC Programing / Design Discuss programing of PIC chips here and design of electronics using PIC chips.


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Ban this user!
Old 06-19-2007, 01:46 PM
 
Join Date: Aug 2006
Location: Ireland
Posts: 4
The7thGuest is on a distinguished road
PICnc controller, yeah, its another one...

I heard about PIC-Step and I decided to try my hand at designing my own stepper controller. At the minute I am just working on the step sequence etc... and an algorithm that will allow me to compute 1, 1/2, 1/4, 1/8 etc step modes without the need for a large step table, I just thought I would share my progress so far. If anyone has any input I'd be more than happy to listen to it.

Also, I was considering using TIP-121 NPN Darlington transistors with a max Ic of 6A as my motors run at around 3A each @ 12v so there would be loads or leeway there...

All code is written for a 16F684 and is written in Hi-Tech C Lite.

StepperController.c

Code:
/*
// LEGAL JIBBER JABBER	----------------------------------------------------------------

	PICNC FIRMWARE - PIC STEPPER MOTOR CONTROLLER
	Copyright (C) 2007 STEVEN MOUGHAN <STEVEN@THEOBSOLETE.COM>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// WIRING CONNECTIONS	----------------------------------------------------------------

OUTPUTS
RA0		A1
RA1		B1
RA2		A2
RA3		B2
RA4		ERROR STATE (E-STOP)
RA5		OPERATIONAL (LIVE)

INPUTS
RC0		STEP MODE, 0 == FULL, 1 == HALF
RC1		ENABLE
RC2		DIRECTION (1 == FORWARDS)
RC3		STEP
RC4		RESET
RC5		E-STOP

*/

#include <pic.h>							//STANDARD PIC HEADER FOR DEFINITIONS
#include "stepfunctions.h"					//CUSTOM FUNCTIONS FOR STEPPER CONTROL

// CUSTOM DEFINITIONS	-----------------------------------------------------------------
#define _STEP_DELAY 1

#define _MODE RC1
#define _ENABLE RC1
#define _DIRECTION RC2
#define _STEP RC3
#define _RESET RC4
#define _ESTOP RC5

#define _A1 RA0
#define _B1 RA1
#define _A2 RA2
#define _B2 RA3

#define _ERROR RA4
#define _OPERATIONAL RA5


//         1       2        3        4          5         6         7         8
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);

//	1:	INTERNAL OSC
//	2:	DISABLE WATCH DOG TIMER
//	3:	70mS TIME UP DELAY
// 	4:	DISABLE MCLR
//	5:	DISABLE CODE PROTECTION
//	6:	BROWN OUT DETECT DISABLED
//	7:	INTERNAL EXTERNAL SWITCHOVER MODE DISABLED
//	8:	FAIL-SAFE CLOCK

//	VARIABLE DECLERATIONS	--------------------------------------------------------------


//	FUNCTIONS	--------------------------------------------------------------------------
void step_forwards(void) {
	if(!_MODE) {
		fullstep_forwards();
	} else {
		halfstep_forwards();
	}
	delayMS(_STEP_DELAY);
}	

void step_backwards(void) {
	if(!_MODE) {
		fullstep_backwards();
	} else {
		halfstep_backwards();
	}
	delayMS(_STEP_DELAY);
}

void idle(void) {							//THIS WILL STOP THE PIC
	_ERROR = 1;								//SET ERROR LED
	//_OPERATIONAL = 0;						//CLEAR OPERATIONAL LED
	while(1 == 1) {							//IN CASE OF EMERGENCY
		if(_RESET) {						//CHECK FOR RESET
			_ERROR = 0;						//CLEAR ERROR LED
			//_OPERATIONAL = 1;				//SET OPERATIONAL LED
			return;							//RETURN TO MAIN
		} //END IF
	} //END WHILE
} //END IDLE

void main(void) {
	CMCON0 = 7; 							//TURN OFF COMPARATORS
	ANSEL = 0; 								//TURN OFF ADC
	PORTA = 0x01;		 					//INITIALISE PORTA TO 0x01
	PORTC = 0x00;							//INITIALISE PORTC TO 0x00
	TRISA = 0b000000;						//MAKE PORTA OUTPUT
	TRISC = 0b111111;						//MAKE PORTC INPUT

	//_OPERATIONAL = 1;						//SET OPERATIONAL LED

	while(1 == 1) {							//MAIN PROGRAM LOOP
		if(RC5 == 1) {						//IS ESTOP ACTIVATED
			idle();							//IDLE UNTIL RESET
		}

		if(_STEP && _ENABLE) {
			if(!_DIRECTION) {
				step_backwards();
			} else {
				step_forwards();
			}
		}

	} //END INFINATE LOOP
} //END MAIN
stepfunctions.h

Code:
/*
// LEGAL JIBBER JABBER	----------------------------------------------------------------

	PICNC FIRMWARE - PIC STEPPER MOTOR CONTROLLER
	Copyright (C) 2007 STEVEN MOUGHAN <STEVEN@THEOBSOLETE.COM>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

int x,i,j;									//VARIABLES FOR TIMERS

void delayMS(int time) {					//CREATE A DELAY LASTING 'TIME' NUMBER OF mS
	for(x=0;x<=time;x++) {					//THIS LOOP SHOULD TAKE A TOTAL OF 1uS
		for(i=0;i<=1;i++);					//AND THESE LOOPS CONTROL IT
		for(j=0;j<=60;j++);
	} //END PERMUTATIONS LOOP
} //END DELAYMS	- THIS IS FOR INTERNAL OSC AT 8MHz, IT PROVIDES 1.002mS DELAY PER LOOP

void fullstep_forwards(void) {
	switch(PORTA) {	//  2121
		case(8):
			PORTA = 0b0001;
			break;
		default:
			PORTA = PORTA * 2;
			break;
	}
}

void halfstep_forwards(void) {
	switch(PORTA) {
		case(9):
			PORTA = 0b0001;
			break;
		default:
			PORTA++;
			break;
	}
}

void fullstep_backwards(void) {
	switch(PORTA) {
		case(1):
			PORTA = 0b1000;
			break;
		default:
			PORTA = PORTA / 2;
			break;
	}
}

void halfstep_backwards(void) {
	switch(PORTA) {
		case(1):
			PORTA = 0b1001;
			break;
		default:
			PORTA--;
			break;
	}
}
Tweet this Post!Share on Facebook
Reply With Quote

  #2   Ban this user!
Old 06-26-2007, 12:54 PM
 
Join Date: Aug 2005
Location: Canada
Posts: 36
niclatrique is on a distinguished road

As I see, you haven't implemented the microstep mode yet.

Your program is easy to read that's nice. I'd be interested in your microstepping code once you've programmed it. Let us know.

Thanks a lot for sharing your work

Nic
Tweet this Post!Share on Facebook
Reply With Quote

  #3   Ban this user!
Old 06-26-2007, 01:16 PM
 
Join Date: Apr 2004
Location: Martinez, CA
Posts: 40
Ryobiguy is on a distinguished road
Lightbulb

Interesting...
It's weird to have code and data in your .h files.

Something you might want to do is to read some timer/counter and keep track of how much time expires instead of getting the CPU stuck in an idle delay loop. It's also trivial to implement debouncing of the step signal.

Something like this maybe should do the trick:

int stepDebounce, lastStep, thisStep;
thisStep = _STEP;
if (stepDebounce) {
// Read timer (if you already havn't on this loop through.)
// Set stepDebounce to zero once enough time has elapsed.
} else if (_ENABLE && thisStep && !lastStep) {
// We just detected a rising edge on step, and enable is on.
stepDebounce = 1; // elapsed time counter and/or just a boolean.
// Enable step pin(s) output
}
lastStep = thisStep;

Last edited by Ryobiguy; 06-26-2007 at 09:17 PM.
Tweet this Post!Share on Facebook
Reply With Quote

  #4   Ban this user!
Old 06-27-2007, 03:27 PM
 
Join Date: Aug 2006
Location: Ireland
Posts: 4
The7thGuest is on a distinguished road

Code:
/*
// LEGAL JIBBER JABBER	----------------------------------------------------------------

	PICNC FIRMWARE - PIC STEPPER MOTOR CONTROLLER
	Copyright (C) 2007 STEVEN MOUGHAN <STEVEN@THEOBSOLETE.COM>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// WIRING CONNECTIONS	----------------------------------------------------------------

OUTPUTS
RA0		A1
RA1		B1
RA2		A2
RA3		B2
RA4		ERROR STATE (E-STOP)
RA5		OPERATIONAL (LIVE)

INPUTS
RC0		STEP MODE, 0 == FULL, 1 == HALF
RC1		ENABLE
RC2		DIRECTION (1 == FORWARDS)
RC3		STEP
RC4		RESET
RC5		E-STOP

*/

#include <pic.h>							//STANDARD PIC HEADER FOR DEFINITIONS
#include "stepfunctions.h"					//CUSTOM FUNCTIONS FOR STEPPER CONTROL

// CUSTOM DEFINITIONS	-----------------------------------------------------------------
#define _STEP_DELAY 1

#define _MODE RC1
#define _ENABLE RC1
#define _DIRECTION RC2
#define _STEP RC3
#define _RESET RC4
#define _ESTOP RC5

#define _A1 RA0
#define _B1 RA1
#define _A2 RA2
#define _B2 RA3

#define _ERROR RA4
#define _OPERATIONAL RA5


//         1       2        3        4          5         6         7         8
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);

//	1:	INTERNAL OSC
//	2:	DISABLE WATCH DOG TIMER
//	3:	70mS TIME UP DELAY
// 	4:	DISABLE MCLR
//	5:	DISABLE CODE PROTECTION
//	6:	BROWN OUT DETECT DISABLED
//	7:	INTERNAL EXTERNAL SWITCHOVER MODE DISABLED
//	8:	FAIL-SAFE CLOCK

//	VARIABLE DECLERATIONS	--------------------------------------------------------------
int stepDebounce, thisStep, lastStep;

//	CONSTANT DECLERATIONS	--------------------------------------------------------------
const int Twentyms = 1150;				//DECLARE A CONSTANT FOR 20mS DELAY

//	FUNCTIONS	--------------------------------------------------------------------------
void step_forwards(void) {
	if(!_MODE) {
		fullstep_forwards();
	} else {
		halfstep_forwards();
	}
	delayMS(_STEP_DELAY);
}	

void step_backwards(void) {
	if(!_MODE) {
		fullstep_backwards();
	} else {
		halfstep_backwards();
	}
	delayMS(_STEP_DELAY);
}

void idle(void) {							//THIS WILL STOP THE PIC
	_ERROR = 1;								//SET ERROR LED
	//_OPERATIONAL = 0;						//CLEAR OPERATIONAL LED
	while(1 == 1) {							//IN CASE OF EMERGENCY
		if(_RESET) {						//CHECK FOR RESET
			_ERROR = 0;						//CLEAR ERROR LED
			//_OPERATIONAL = 1;				//SET OPERATIONAL LED
			return;							//RETURN TO MAIN
		} //END IF
	} //END WHILE
} //END IDLE

void main(void) {
	CMCON0 = 7; 							//TURN OFF COMPARATORS
	ANSEL = 0; 								//TURN OFF ADC
	PORTA = 0x01;		 					//INITIALISE PORTA TO 0x01
	PORTC = 0x00;							//INITIALISE PORTC TO 0x00
	TRISA = 0b000000;						//MAKE PORTA OUTPUT
	TRISC = 0b111111;						//MAKE PORTC INPUT

	//_OPERATIONAL = 1;						//SET OPERATIONAL LED

	while(1 == 1) {							//MAIN PROGRAM LOOP
		if(_ESTOP == 1) {						//IS ESTOP ACTIVATED
			idle();							//IDLE UNTIL RESET
		}

		thisStep = _STEP;
		if(!stepDebounce) {
			delayMS(20);
			stepDebounce = 1;				//SET
		} else if(_ENABLE && thisStep && !lastStep) {
			stepDebounce = 0;				//RESET
				if(!_DIRECTION) {
					step_backwards();
				} else {
					step_forwards();
				}//END IF
		}//END ELSE IF
		lastStep=thisStep;

	} //END INFINATE LOOP
} //END MAIN
Hows about this? Not sure if I have it 100%, but it does compile. I haven't sim'd yet...
Tweet this Post!Share on Facebook
Reply With Quote

  #5   Ban this user!
Old 07-12-2007, 11:44 PM
 
Join Date: Aug 2006
Location: Ireland
Posts: 4
The7thGuest is on a distinguished road

I have created a Source Forge project for the controller. It is available here and the project website is available here.

__________________
http://www.sf.net/projects/picnc/ - Open source PIC based stepper motor controller.
Tweet this Post!Share on Facebook
Reply With Quote

Sponsored Links
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DeskCNC 2nd. gen controller AND HobbyCNC Pro Controller maycom DeskCNC Controller Board 1 06-02-2007 02:33 PM
What controller can I use. erkiwi Stepper Motors and Drives 11 04-18-2007 02:10 AM
Another PIC controller spalm PIC Programing / Design 71 04-15-2007 09:32 AM
Controller or cam? Halfnutz Mach Software (ArtSoft software) 2 04-19-2005 08:21 AM
CNC controller justCNCit General Electronics Discussion 2 03-23-2004 02:32 PM




All times are GMT -5. The time now is 10:29 AM.





Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO
Template-Modifications by TMS

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353