Arduino chip as Stepper Controller

Hi, I am experimenting with the code you posted ( Reply #15 on: January 15, 2012, 05:19:57 PM ) with 4 IRF530's, an Arduino Uno, a small unipolar stepper and solder-less board and It works really well with EMC2 linux. My goal is to drive a homebrew CNC router table with the above components. I have very little (NO) experience with port manipulation and am somewhat apprehensive about adding another axis to the code, as I recall reading that I could bugger up the TX RX ports? does the UNO have enough ports available to control 3 axes? I would still be happy with 2! In any case, great work on this elegant code so far!

I am using the ATtiny2313 for this application. It has a few extra pins, It turns out I need 1 interrupt pin, 1 input and 4 outputs. I would also like to add 1 more input as an enable and possibly a Ready output. The problem with trying to run more than one stepper in this application is the Interrupt inputs. Because of priority of interrupts the second interrupt would get missed if it occurred at the time as the first.

Look into some ATtiny2313 chips and use the Arduino as the programmer. That circuit is abut as inexpensive and simple as an Arduino circuit can be. If yuwant more info to use that approach I can point you to the few pieces you need to put together to do that.

That would be great. I was wondering about the ability to program with the arduino and swap the IC out to a PCB, so that sounds like it could work for me. Any resources you can point me to would be greatly appreciated.

To program a chip I use Arduino-0.22 to load the ArduinoISP sketch on my Arduino Uno board. I made a small board that has 2 sockets, a 20 pin that could be used to program 20 and 8 pin chips and a 28 pin socket that I use to program 328's (like the Uno). Search for Arduino as ISP as there is a lot of info out there that exlains the process and the wiring. My little board has 6 wires between the boards to program the chip and a 120 Ohm resistor between the Reset and +5 on the Arduino Uno. To program an Arduino sketch using the ArduinoISP there is a selection on the ArduinoIDE File menu to write the sketch usin Arduino as ISP.

I use the Arduino-0.22 IDE as there was a change in something in Arduino-1.0 and the ArduinoISP only works at 9600 baud instead of 19200 which causes a problem.

Here's a picture (poor) of my ISP board to use with the Uno.

I got it working -

Figured out what was lacking in my assembly code and now it works.

.NoList
.INCLUDE "tn2313Adef.inc" ; Header for ATTINY2313A
.List
; ============================================
;   R E G I S T E R   D E F I N I T I O N S
; ============================================
.DEF Conf = R16 ; Multipurpose register
.DEF StepCt = R17
.DEF StepMask = R18
.DEF Direction = R19
.DEF PortBHold = R20
.DEF PortDHold = R21
.DEF PortDMask8 = R22
.DEF SeqBase = R24
; ============================================
;       S R A M   D E F I N I T I O N S
; ============================================
.DSEG
.ORG  0X0060
; ============================================
;   R E S E T   A N D   I N T   V E C T O R S
; ============================================
.CSEG
.ORG $0000
	rjmp Main ; Reset vector
	rjmp Step ; Int vector 1
	reti ; Int vector 2
	reti ; Int vector 3
	reti ; Int vector 4
	reti ; Int vector 5
	reti ; Int vector 6
	reti ; Int vector 7
; ============================================
;     I N T E R R U P T   S E R V I C E S
; ============================================
Step:   in  PortDHold,PIND
; look at only PD#, the Direction input
        and PortDHold,PortDMask8
; if PD3 High, Add Direct to StepCt, if PD3 Low Subtract Direction
        breq SinNeg
        add StepCt,Direction
        rjmp Next
SinNeg: sub StepCt,Direction
; limit StepCt to range from 0 to 7 (-1 becomes 7)
Next:   and StepCt,StepMask
; use Z as a pointer to the StepSequence table
        mov ZL,SeqBase
; StepCt is teh offset into the table
	add ZL,StepCt
; transfer the next bit pattern from ProgMem to PortBHold, R20 
	Lpm PortBHold,Z
	out PortB,PortBHold
	reti
; ============================================
;     M A I N    P R O G R A M    I N I T
; ============================================
Main:
; Init stack
	ldi Conf, LOW(RAMEND) ; Init LSB stack
	out SPL,Conf
; Init Port A
	ldi Conf,0 ; Direction Port A
	out DDRA,Conf
; Init Port B
	ldi Conf,(1<<DDB0)|(1<<DDB1)|(1<<DDB2)|(1<<DDB3) ; Direction Port B
	out DDRB,Conf
; Set ISC00 to 0 and ISC01 to 1, Falling Edge INT0 Interrupt
	ldi Conf,(0<<ISC00)|(1<<ISC01) 
	out MCUCR,Conf
; Set INTO , Bit 6 of GIMSK to Enable INT0
	ldi Conf,(1<<int0)
	out GIMSK, Conf
; Initialize StepCt, R17
	ldi StepCt,0
; Initailize StepMask, R18
	ldi StepMask,7
;Initialize Direction, R19
	ldi Direction,1
; Initialize SeqBase to point to 8 bit address of StepSeq
	ldi SeqBase, StepSeq
	add SeqBase,SeqBase
; Initialize PortDMask8, R22, to mask only PD3 of port D
        ldi PortDMask8,8
        ldi ZH,0    
 	sei
; ============================================
;         P R O G R A M    L O O P
; ============================================
Loop:  rjmp loop ; go back to loop
; bit patterns for output to the Stepper Drive Bits.
StepSeq: .DB 1,3,2,6,4,12,8,9,0,0
; End of source code

This was one of the best reads on Arduino stepper logic thata I have seen. You are to be commended and I thank you very much.

It's a pity that it seemed to drop so abruptly,

Do you have any final specs on the speed of your assembly code version?

Got into a few other projects so I have not paid much attention to it for a while. I was able to get hold of a Hardinge UM milling machine and that took some time to get cleaned and moved into my shop. I have a Harbor Freight Mini Mill that I want to convert to CNC along with a 10:1 gearbox that I want to use as an indexer on that mill. Living down here in South Texas my year is backwards from folks that live farther north. Here it get too hot to work in an un-airconditioned garage so most of my projects out there take place from September to May whereas when I lived up in Indiana the projects took place from April to October when it was warm enough to work out in the shop.

I have mounted 2 2313 on a perf board along with the 8 MOSFETs and actually had it running - ~140 rpm using my Arduino as the step generator. As that was written in the Arduino IDE I suspect that I can get quite a bit more speed using MACH3 or GRBL.

What's the timing for 12 instructions and an interrupt on an ATtiny running at 8Mhz? According to how I interpret the info from AVRStudio the interrupt shold process in ~2.25mSec.

gharryh:
Now i am confused. Do you use the dedicated chip to drive the stepper or the Arduino.
If you use the PC to send Direction and Step commands what does the Arduino do?

yeah very interesting...

I used the Arduino to test my idea and then migrated the code over to the ATtiny2313. A stepper driver takes the problem of controlling the motor away from your main processor - be it an Arduino or a PC running Mach3. The chip is programmed so it has 2 inputs - Step & Direction, and 4 outputs - Pahses 1 - 4. Those outputs drive 4 MOSFETs which are then connected to the 4 phases of the stepper. Very similar to some other stepper controllers that are available - the PIC Stepper is one that I chose to emulate. Instead of the PICStepper I have the AVRStepper...

And by using a stepper driver my Arduino can control 4 steppers instead of just 2. Or I can hook those steppers to my PC and control them using Mach3, or to some other device.

4 is impressive! With my code I can control 3 at once.

Distributed processing. And by using the 2313's for controllers they can be placed away from the main arduino meaning things can be mounted more conveniently.

Sort of a bump to the thread.

I have some perforated circuit board material coming - Vectorboard #169P44WEC1 which is copper clad on 1 side with .042 holes spaced every 0.1". Saves having to drill my sample boards. I have generated a board layout using AutoCAD and will be spending the next couple days drawing the resist pattern on the board and then etching it with Radio Shack Ferric Chloride etchant. Have spent that last couple weeks going over the design and that helped me spot an error in the schematic. I sort of had a schematic, but this was a project where the schematic followed the breadboard build and I found out I had drawn it with the signals to the wrong pins... Oh well, slide them over and the schematic matches the working parts...

Salvaged some terminals and now I just have to get the materials in hand to try it out.

Is the Vectorboard #169P44WEC1 copper "all continuous," i.e. no precut pads on them? It's really just a whole plane of copper with holes, and you can still etch on them?

How did it work out for you?

I'm looking for something similar, but there aren't many of these so when I read the product description, I'm not sure it's what I think it is.

Thanks!

patternp:
Is the Vectorboard #169P44WEC1 copper "all continuous," i.e. no precut pads on them? It's really just a whole plane of copper with holes, and you can still etch on them?

How did it work out for you?

I'm looking for something similar, but there aren't many of these so when I read the product description, I'm not sure it's what I think it is.

Thanks!

This version of the board has copper on just 1 side. There is also a version that ends with C2 that has copper on both sides.

I have a couple pictures on my phone of the boards I made. I drew the resist pattern with a Sharpie and it worked well. I make 2 boards with a minimum of copper removed with the bulk of the board being a ground plane. The other 2 I removed any unneeded copper. Both versions seem to work just fine. Will try to upload the pictures tonight.

Thanks for info on C2 version of board.

Looking forward to seeing the photos, if you get a chance to post them :slight_smile:

Here's a top view of the assembled board.

The 5 pins across the top are, from right to left, Motor 1, Motor2, Motor Common, Motor 3, Motor 4.

The 7 down the side, from top to bottom, Motor+, Motor-, Logic+, Logic-, Logic Common, Direction, Step.

This one doesn't zoom too well, but it shows the backside, one with minimal copper removed.

Here are a couple boards I made that have minimal copper left.

Here's a couple of board layouts from the copper side. They don't zoom well either.

Stepper Driver Board.bmp (3.52 MB)