14 Replies - 2630 Views - Last Post: 08 May 2012 - 09:32 PM

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Stepper Motor Interfacing in MASM?

Posted 04 May 2012 - 01:37 PM

I'm looking for some kind of code written in MASM and using Visual Studio (or not written in Visual Studio, but that can be applied there) for stepper motor. If someone can give me some links or codes I would be glad. I made some search but I couldn't find anything. The only thing that I found was this code snippet below, and I think that it is not complete and won't run on Visual Studio.

        org 0H

stepper equ P1

main:
        mov stepper, #0CH
        acall delay
        mov stepper, #06H
        acall delay
        mov stepper, #03H
        acall delay
        mov stepper, #09H
        acall delay
        sjmp main

delay:
        mov r7,#4
wait2:
        mov r6,#0FFH
wait1:
        mov r5,#0FFH
wait:
        djnz r5,wait
        djnz r6,wait1
        djnz r7,wait2
        ret
        end



Is This A Good Question/Topic? 0
  • +

Replies To: Stepper Motor Interfacing in MASM?

#2 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,302
  • Joined: 28-March 11

Re: Stepper Motor Interfacing in MASM?

Posted 04 May 2012 - 05:04 PM

Of course it won't Assemble it isn't even x86 code it is ARM.

How is this motor connected to your computer or whatever? Why do you want to use Assembly vs C or some other HLL? What do you want this motor to do?

"People always want to build a building without even knowing how to do it or where to start!"
Was This Post Helpful? 1
  • +
  • -

#3 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Stepper Motor Interfacing in MASM?

Posted 05 May 2012 - 05:00 AM

Actually I don't need to program the motor, it is not connected to the computer, and even there isn't any motor. All I need is some well-documented MASM, x86 code for interfacing a stepper motor. It will be used just for illustration purposes.

I also found this one below. If someone can tell me in which Assembly it is written I would be glad. It uses a library from Microchip.

;        LIST    p=16F628 ; PIC16F628 is the target processor

        Processor       16F628
        Include         "P16F628.inc"

;                  +---------------+
;                  |               |
;     STEP_MODE  1-| RA2       RA1 |-18 HALFSTEPOUT
;                  |               |
;    HALFSTEPIN  2-| RA3       RA0 |-17 RESET
;                  |               |
;   CHIP_ENABLE  3-| RA4       OCS |-16--+------------+ n/c
;                  |               |     | Oscillator |
;           n/c  4-| ~MCLR     OCS |-15--+------------+ n/c
;                  |               |
;            0V  5-| Vss       Vdd |-14 +5V
;                  |               |
;          MOVE  6-| RB0       RB7 |-13 LSTOP
;                  |               |
;            RX  7-| RB1       RB6 |-12 RSTOP
;                  |               |
;            TX  8-| RB2       RB5 |-11 DIROUT
;                  |               |
;          CLOCK 9-| RB3       RB4 |-10 DIRIN
;                  +---------------+


;====================================================================
; Configuration Bits: 
;               Code Protection = OFF
;               WatchDog Timer  = OFF
;               Power-up Timer  = ON
;               Oscillator Type = INTERNAL
;			 Internal RC Oscillator = ON
;			 Low Voltage Program = OFF
;			 Master Clear = OFF
;			 Brown Out Detect = OFF
;====================================================================

        __Config _CP_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_CLKOUT & _LVP_OFF & _MCLRE_OFF & _BODEN_OFF



	CBLOCK	0X20			; start of internal memory
COUNTER0, COUNTER1			; define delay counters
STEPCOUNT
	ENDC			

#define	CHIP_ENABLE	PORTA,4		; define i/o pin labels
#define MOVE	PORTB,0
#define	RX	PORTB,1
#define	TX	PORTB,2
#define	CLOCK	PORTB,3
#define	DIRIN	PORTB,4
#define DIROUT	PORTB,5
#define LSTOP	PORTB,7
#define	RSTOP	PORTB,6
#define STEP_MODE	PORTA,2
#define	HALFSTEPIN	PORTA,3
#define HALFSTEPOUT	PORTA,1
#define	RESET	PORTA,0

_ResetVector    set     0x00	; reset vector

        org     _ResetVector	; pic starts code execution from here
        goto    START		
	nop			
	nop			

; *** Start of program code ***

START	
	
	movlw	0x07			; turn comparators off and
	movwf	CMCON		; enable pins for i/o functions
	
	bsf	STATUS,RP0		; select page 1
	movlw	B'00001101'	; i/o pin selection for port
	movwf	TRISA		; set port A
	movlw	B'11010011'	; i/o pin selection for port
	movwf	TRISB		; set port B

	bcf	OPTION_REG,7		; switch on portB pull up resistors

	bcf	STATUS,RP0		; return to page 0
	clrf	PORTA			; clear port A
	clrf	PORTB			; clear port B

MAIN	CLRWDT				; clear watchdog timer in case of latchup

	BTFSC	RESET		; check reset pin
	goto	RESET_MODE		; if 1 go to reset mode	

	BTFSS	MOVE			; check move pin
	goto	MAIN				; if 0 go back to main 

	BTFSC	HALFSTEPIN	; check half step pin
	goto	HALFSTEPMODE		; if 1 goto half step mode
	goto	FULLSTEPMODE		; if 0 goto full step mode

HALFSTEPMODE

	bsf	HALFSTEPOUT		; if 1 set high for half step mode
	goto	STEPMODE			; goto step mode

FULLSTEPMODE

	bcf	HALFSTEPOUT		; if 0 set low for full step mode
	goto	STEPMODE			; goto step mode

STEPMODE

	BTFSC	STEP_MODE		; check step mode pin
	goto	FREERUN			; if 1 go to free running mode
	goto	ONE_STEP			; if 0 go to one step mode

RESET_MODE

	BTFSS	LSTOP		; if lstop high then skip next line
	call	LEFT				; call left routine
	BTFSS	LSTOP		; if lstop high then skip next line
	goto	RESET_MODE		; go back to reset mode
	clrf	STEPCOUNT			; clear stepcount counter register
	call	RIGHT			; call right routine
	incf	STEPCOUNT,f		; increment step counter
	BTFSS	RSTOP		; if rstop high then skip next line
	goto	$-3				; go back 3 instructions
	bcf	STATUS,C			; clear complimentary bit
	rrf	STEPCOUNT,f		; move stepcount one bit right to divide by 2
	call	LEFT				; call left routine
	decfsz	STEPCOUNT,f	; decrement stepcount by one and skip next instruction on 0
	goto	$-2				; go back 2 instructions
	goto	MAIN				; go back to main

FREERUN

	BTFSC	DIRIN		; if dirin pin low then skip next line
	goto	TRYRIGHT			; go to tryright routine
 	goto	TRYLEFT			; go to tryleft routine
	
ONE_STEP
			
	BTFSC	DIRIN		; if dirin pin low then skip next line
	goto	STEPRIGHT			; go to stepright routine
	goto	STEPLEFT			; go to stepleft routine

STEPLEFT
	BTFSS	LSTOP		; if lstop high then skip next line
	call	LEFT				; call left routine
	btfsc	MOVE			; if move pin low then skip next line
	goto	$-1				; go back to last instruction
	goto	MAIN				; go back to main 

STEPRIGHT	
	BTFSS	RSTOP		; if rstop high then skip next line
	call	RIGHT			; call right routine
	btfsc	MOVE			; if move pin low then skip next line
	goto	$-1				; go back one instruction
	goto	MAIN				; go back to main

TRYRIGHT
	BTFSS	RSTOP		; if rstop high then skip next line
	call	RIGHT			; call right routine
	goto	MAIN				; go back to main

TRYLEFT	
	BTFSS	LSTOP		; if lstop high then skip next line
	call	LEFT				; call left routine
	goto	MAIN				; go back to main

RIGHT

	

	bcf	CHIP_ENABLE		; set chip_enable pin low
	bcf	DIROUT			; set direction out pin low to go right
	bsf	CLOCK			; set clock pin high
	call	DELAY			; go to delay routine
	bsf	CHIP_ENABLE		; set chip enable pin high
	bcf	CLOCK			; set clock pin low
	call	DELAY			; call delay routine
	return				; return

LEFT

	bcf	CHIP_ENABLE		; set chip_enable pin low
	bsf	DIROUT			; set direction out pin high to go left
	bsf	CLOCK			; set clock pin high
	call	DELAY			; go to delay routine
	bsf	CHIP_ENABLE		; set chip enable pin high
	bcf	CLOCK			; set clock pin low
	call	DELAY			; call delay routine
	return				; return

DELAY

	movlw	.10			; delay is equal to 10 milliseconds by moving .10 into w register
	movwf	COUNTER0		; move w register into COUNTER0	
FLOOP	nop				; no operation
	nop					; no operation
	movlw	.248			; inner part of delay loop  equal to 1 millisecond by moving .248 into w register
	movwf	COUNTER1		; move w register into COUNTER1
FILOOP	nop				; no operation
	decfsz	COUNTER1,f	; decrement counter1 by 1
	goto	FILOOP			; goto filoop
	decfsz	COUNTER0,F	; decrement counter 0 by 1
	goto	FLOOP			; goto floop
	return				; return
	END					; end of program

; *** End of program ***


This post has been edited by erkant: 05 May 2012 - 07:32 AM

Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Stepper Motor Interfacing in MASM?

Posted 05 May 2012 - 03:52 PM

PIC 16 series 8-bit microcontroller.
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,302
  • Joined: 28-March 11

Re: Stepper Motor Interfacing in MASM?

Posted 05 May 2012 - 03:55 PM

As turboscrew mentioned, it is for PIC, but you could tell that from the first line of the code:

Quote

; LIST p=16F628 ; PIC16F628 is the target processor


First you must learn Assembly if you are going to write about it or create illustrations. Why don't you start with a simple calculator?
Was This Post Helpful? 0
  • +
  • -

#6 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Stepper Motor Interfacing in MASM?

Posted 05 May 2012 - 03:58 PM

This may help...
Was This Post Helpful? 0
  • +
  • -

#7 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Stepper Motor Interfacing in MASM?

Posted 06 May 2012 - 02:49 AM

Thanks to both of you! And GunnerInc, where can I find MASM code for a calculator or for anything similar?

This post has been edited by erkant: 06 May 2012 - 03:44 AM

Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,302
  • Joined: 28-March 11

Re: Stepper Motor Interfacing in MASM?

Posted 06 May 2012 - 08:52 AM

I mention a calculator as a first project since it only has a handful of controls and it gives you a chance to learn to use ADD, SUB, IMUL/MUL, IDIV/DIV and or Shifting to multiply or divide. It could be anything simple like an alarm clock.

I see in your profile you list C, so you have experience with the Win32 API? x86 Windows Assembly is NOT much different than C. You also list Assembly, what dialect?

I have written quite a few MASM tutorials to get folks started, they are in our tutorial section

If you want to learn MASM, you could download the MASM32 SDK which contains MASM and its related tools, libs and includes for MANY Windows APIs, sample programs with source, a nice library of functions complete with source, and a few other goodies.

Quote

where can I find MASM code for a calculator or for anything similar?

Let's see, I just googled "calculator MASM" and got back many pages with sample code and programs.
Was This Post Helpful? 0
  • +
  • -

#9 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Stepper Motor Interfacing in MASM?

Posted 06 May 2012 - 09:15 AM

Quote

I see in your profile you list C, so you have experience with the Win32 API? x86 Windows Assembly is NOT much different than C.


It has said that C is high level assembly. :lol:
Was This Post Helpful? 0
  • +
  • -

#10 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Stepper Motor Interfacing in MASM?

Posted 08 May 2012 - 05:00 AM

Well I actually need some code about stepper motor in MASM, it don't have to be the full interfacing of the motor. Only for example how can motor turn in MASM, maybe to use some timer and other stuff, anything simple is highly welcome.

Edit: I have knowledge in C, C++, parallel programming languages and extensions, and I just started with the Assembly. But I really need this stepper motor code until tomorrow or the day after tomorrow. If anyone can help I would be glad.

This post has been edited by erkant: 08 May 2012 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#11 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Stepper Motor Interfacing in MASM?

Posted 08 May 2012 - 05:20 AM

How is ste motor connected to the x86?
In the picture, it was connected to PIC's pins.
(At least I think the picture shows PIC-chip with the pins used as I/O and the code programmed inside - no external memory.)

That can't be done with x86, and if it's some x86-based microcontroller, the pins must be quite different.

Also the kind of motor is unknown.
Was This Post Helpful? 0
  • +
  • -

#12 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Stepper Motor Interfacing in MASM?

Posted 08 May 2012 - 07:46 AM

As I wrote there is no stepper motor nor any microcontroller to which it is connected. It can also be something about digital interface with 8255. I just need a simple MASM code that will show approximately how it must perform, rotate, use timer and stuff like that. Just an example.

I found this one, what's your opinion:

CODE SEGMENT
	ASSUME	CS:CODE, DS:CODE, ES:CODE, SS:CODE
	;
PPIC_C		        EQU		1FH
PPIC			EQU		1DH
PPIB			EQU		1BH
PPIA			EQU		19H

	;
	ORG		1000H
	;
	;
	MOV		AL, 10000000B
	OUT		PPIC_C, AL
	;
	MOV		AL, 11111111B
	OUT		PPTA, AL
	MOV		AL, 00000000B
	OUT		PPIC, AL
	
L1:	MOV		SI, OFFSET ROT
L2:	MOV		AL, BYTE	PRT CS:[SI]
	CMP		AL, 0FFH
	JZ		L1
	OUT		PPIB, AL
	INC		SI
	CALL		TIMER
	JMP		L2
	;
TIMER:	MOV		CX, 1
TIMER2:	PUSH		CX
        MOV		CX, 0
TIMER1:	NOP
	NOP
	NOP
	NOP
	LOOP		TIMER1
	POP		CX
	LOOP		TIMER2
	RET
		
ROT:	DB		11101110B
	DB		10111011B
	DB		11011101B
	DB		01110111B
	DB		0FFH
	;
CODE	ENDS
	END		


This post has been edited by erkant: 08 May 2012 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#13 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Stepper Motor Interfacing in MASM?

Posted 08 May 2012 - 12:56 PM

I don't know x86 assembly too well. Maybe someone else could comment that, but mentioning 8255 makes things much clearer.

I recall (vaguely) that 4 lines (port pins) are enough to drive a typical stepper motor.
I also recall that the lines are brought up and down in order (order defines the direction) and pinpairs (kinda differential) are driven with about 90 degree phase shift.

pinpairs are attached to the ends of a coil (at least you can think like that).

That's what I recall, but you better check.

If A and B are connected to one coil and B and C to the other:

Forward:

ABCD
1001
1010
0110
0101

backward:
ABCD
1001
0101
0110
1010

A bit like the mouse, but signal goes backwards.

This post has been edited by turboscrew: 08 May 2012 - 12:57 PM

Was This Post Helpful? 1
  • +
  • -

#14 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,302
  • Joined: 28-March 11

Re: Stepper Motor Interfacing in MASM?

Posted 08 May 2012 - 04:14 PM

Let me see if I can understand you. You don't have a motor, you don't have a program, you don't have an OS, you don't have a way to connect the motor, you know nothing about the language you want the code in, you find code on the net and want US to tell you what it does you you can pass your class or exam?

You CANNOT write code for a motor you know nothing about (what model, what voltage, how is it connected to the computer - USB, COM, Serial, Parallel) What is the wiring diagram for the motor? You CANNOT write code to interface with this imaginary motor for an OS we don't know. The code above WILL NOT work in user land for anything higher and including VISTA.

I will teach you x86 to the best of my ability, but one thing I won't do is hand feed someone, that gets you no where and you learn nothing.

You should just write code for it in C or something that you know.

This post has been edited by GunnerInc: 08 May 2012 - 04:15 PM

Was This Post Helpful? 1
  • +
  • -

#15 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Stepper Motor Interfacing in MASM?

Posted 08 May 2012 - 09:32 PM

Yep, in C it would be much easier.
Also, assembly doesn't let you off the hook with driver interface of windows or linux.

Which should you illustrate, asm or stepper motor driving?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1