Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Rotary encoder to controller Stepper motor

Status
Not open for further replies.

ssnickson

New Member
Hi All,
I have project which use Rotary encoder as input to PIC16F676 and the output will go to L293D Driver Motor that in turn will control my Stepper motor. My circuit is working in the sense i can run the stepper motor CW and CCW. But i need to run the Motor according to the input from Encoder.

Mean if the encoder moves CW then the motor hav to mov CW and vice versa. for every single step of encoder the motor have to move 1 loop
0001
0010
0100
1000

I have attached my program for your reference. My problem is everything works properly.But when i power on my PIC it immediately start to rotate 1 whole rotation CW and CCW and then only it rotate according to input which is Port A. I need the motor not to rotate until it get the input. Sorry if i have bad programming sense
\
list p=16F676 ; list directive to define processor
#include <p16F676.inc> ; processor specific variable definitions

errorlevel -302 ; suppress message 302 from list file

__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;call Delay

;***** VARIABLE DEFINITIONS (examples)
CBLOCK 0x20

CounterH
CounterL
ENDC


;w_temp RES 1 ; variable used for context saving
;status_temp RES 1 ; variable used for context saving
;endc

;***** All the Define in here

#define C0 PORTC, 0
#define C1 PORTC, 1
#define C2 PORTC, 2
#define C3 PORTC, 3
#define C4 PORTC, 4
#define C5 PORTC, 5

#define A0 PORTA, 0
#define A1 PORTA, 1
#define A2 PORTA, 2
#define A3 PORTA, 3
#define A4 PORTA, 4
#define A5 PORTA, 5


;**********************************************************************
;PROGRAM MEMORY *
;**********************************************************************
ORG 0X000 ;RESET Vector
nop
goto main

;**********************************************************************
;Main program Starts *
;**********************************************************************

main
;**********************************************************************
;Initializing
;************************************************************************
clrf INTCON ;Write 0 in f, INTCON = 0

bsf STATUS, RP0 ;Select Bank1

clrf PIE1 ;Write 0 in f, PIE1 = 0

;call 0x3ff
;movwf OSCCAL ;Copy W to f, OSCAL=0x3ff

movlw 0x8f ;Write constant in W register, W=0x8f
movwf OPTION_REG ;PortA Pull-ups are disabled, Prescaler is assigned to WDT

clrf ANSEL ;All pins are assigned to Digital IO(PortA & PortC)
clrf WPUA ;Weak pull-up resisters disabled(PortA)
clrf IOCA ;Interrupt-on-change disabled(PortA)
clrf VRCON ;CVREF circuit powered down, no IDD drain

bcf STATUS,RP0 ;Select Bank 0

movlw 0x07 ;Write constant in W register, W=0x07
movwf CMCON ;Comparator Off(PortA)
clrf ADCON0 ;ADC disabled


;*****************************************************************************
;Program starting from here
;*****************************************************************************
; remaining code goes here
clrf PORTC
clrf PORTA
clrf ROT
clrw
; clrf FLAGS

;Intial
; bsf STATUS, RP0 ;Select Bank1
; movlw B'11111111' ;Set Port A as Input
; movwf TRISA

BCF STATUS,RP0 ;Bank 0
; CLRF PORTA ;Init PORTA
MOVLW B'11111111' ;Set RA<5:0> to
MOVWF CMCON ;digital I/O
BSF STATUS,RP0 ;Bank 1
; CLRF ANSEL ;digital I/O
MOVLW B'11111111' ;Set PORTA as inputs
; MOVLW B'00000000'
MOVWF TRISA
;BCF STATUS,RP0 ;Bank 0
movlw B'00000000' ;set Port C as output
movwf TRISC
bcf STATUS,RP0 ;Select Bank 0
; retlw 0


PDATA equ 30h
TDATA equ 31h
ROT equ 32h
;Intial Value of PDATA (Previous Data)

MOVLW B'000000000'
MOVWF PDATA
; MOVWF PORTC
; MOVLW B'00000101'
; MOVWF PORTA
MOVLW 0xFF
;MOVWF CounterH
MOVWF CounterL
MOVLW 0xFF
MOVWF CounterH


RCheck
MOVF PORTA,0 ;Moving Input from PortA to W register
MOVWF TDATA ;Temp store of input into a register (TDATA)
SUBWF PDATA ,0 ;Substrate W register value from PDATA(Previous stored data) i.e PDATA-W and store the value in W
; MOVFW PDATA ;Move the PDATA into W register
BTFSS STATUS,C ;Skip if no Carry (STATUS C=1)
;nop
;goto RCheck
Call loopCW ;Call Anti Clockwise Loop
call loopACW ;Call Clockwise Loop

loopCW
MOVWF ROT ;Move W register into ROT
COMF ROT ;Taking Complement of ROT
MOVFW TDATA ;Move TDATA to W register
MOVWF PDATA ;Move W register into PDATA
MOVFW ROT ;Move ROT to W register
ADDLW B'00000010' ;ADD 2 to W register
MOVWF ROT ;Move W to ROT

;STEPPER MOTOR ROTATION LOOP
CWTurn
DECFSZ ROT ;Decrease the value of ROT until it reach zero then skip a step
GOTO Rotate ;If not zero goto to Rotate
GOTO RCheck ;IF zero goto to Rcheck

Rotate MOVLW B'00000001'
MOVWF PORTC
call Delay
MOVLW B'00000010'
MOVWF PORTC
call Delay
MOVLW B'00000100'
MOVWF PORTC
call Delay
MOVLW B'00001000'
MOVWF PORTC
call Delay
goto CWTurn

loopACW
ADDLW B'00000001' ;ADD 1 to W register
MOVWF ROT ;Move W register into ROT
MOVFW TDATA ;Move TDATA to W register
MOVWF PDATA ;Move W register into PDATA
;STEPPER MOTOR ROTATION LOOP
ACWTurn
DECFSZ ROT ;Decrease the value of ROT until it reach zero then skip a step
GOTO Rotate1 ;If not zero goto to Rotate1
GOTO RCheck ;IF zero goto to Rcheck

Rotate1 MOVLW B'00001000'
MOVWF PORTC
call Delay
MOVLW B'00000100'
MOVWF PORTC
call Delay
MOVLW B'00000010'
MOVWF PORTC
call Delay
MOVLW B'00000001'
MOVWF PORTC
call Delay
goto ACWTurn
; goto Check

Delay

DECFSZ CounterH
goto Delay
DECFSZ CounterL
goto Delay
return
 
hi
Tried your program in Oshonsoft Sim.

I get a STACK overflow message.

You seem to be Calling 'loopCW' from the ReCheck subr without any Return, so as you keep looping you eventually over fill the STACK.

Also you should really define the regs

Message[305] C:\00000000.ASM 143 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 152 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 177 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 198 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 200 : Using default destination of 1 (file).
 
Last edited:
hi
Tried your program in Oshonsoft Sim.

I get a STACK overflow message.

You seem to be Calling 'loopCW' from the ReCheck subr without any Return, so as you keep looping you eventually over fill the STACK.

Also you should really define the regs

Message[305] C:\00000000.ASM 143 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 152 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 177 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 198 : Using default destination of 1 (file).
Message[305] C:\00000000.ASM 200 : Using default destination of 1 (file).

Ok i understand that i need to define all my register even if it SFR. But can elaborate ur 1st point about return i m just beginner in Assembly language sry for that
 
Ok i understand that i need to define all my register even if it SFR. But can elaborate ur 1st point about return i m just beginner in Assembly language sry for that

hi,

You dont have to redefine ALL the PIC's registers, the '16F676.inc' takes care of the SFR's.

I mean when you DEC register, say a counter , you have two options of where the decrement value will be placed, either in the 'W' reg or the 'F' [user reg]

Look at this image, its a collection of STACK information from the 16F676 datasheet.

Stack1..gif
 
Status
Not open for further replies.

Latest threads

Back
Top