decoding quadrature encoders


Results 1 to 40 of 94

Threaded View

  1. #26
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Below is one part of the interrupt assembly code.
    (I have tested the code with the MPLAB simulator).

    This is not the end of the difficulties.

    I can help (the forum can help) if you do the following:

    Write a PicBasic program that displays on the LCD the
    rotary encoder count. The program will use an
    interrupt-onchange in assembly for the encoder.
    (Please write a new well formatted program).

    The assembly code I have posted today is only one
    part of the code you will need in the interrupt routine.
    BEFORE my code you will have to save the processor context.
    AFTER my code you will have to restore the processor context.
    See PicBasic manual '9.3. Interrupts in Assembler' for
    everything else you will need in order to use an interrupt
    in assembly.

    For now do not modify my code. Add everything, also the
    two comment lines. (Cut & Paste).
    (;====== BEGINNING OF THE ROTARY ENCODER CODE ========)
    (;========= END OF THE ROTARY ENCODER CODE ===========)

    If you like you can add the code for the two LEDs CCW and CW.
    See comments in the code for the LEDs.

    In my code I use 3 variables that you will have to declare
    in your PicBasic program. These variables have new names.
    (Not the same names you have used in your code).
    I don't like names like 'new' because 'new' can be a
    reserved word for some compiler.

    The 3 variables are:

    enc_new VAR BYTE
    enc_old VAR BYTE
    enc_counter VAR WORD

    Set to 0 (zero) the variable enc_old when you power up
    the PIC, before you enter the main loop of your PicBasic
    program. This is not necessary but it will help you to
    understand how this variable is used.


    * * *

    From your post of the 6th May 2005, 12:06

    ....im using a 16F873A,which has a 4K flash,meaning 4 banks...
    according to the PBP manual, i have to define wsave,wsave1,wsave2,wsave3...
    it takes in wsave and wsave1...but when i declare wsave2 and wsave3..it gives a RAM_END 255 error


    From the PicBasic manual
    2.2. Your First Program

    If you don’t tell it otherwise, the PicBasic Pro Compiler defaults to
    creating code for the PIC16F84. To compile code for PICmicro MCUs
    other than the PIC16F84, simply use the -P command line option
    described later in the manual to specify a different target processor. For
    example, if you intend to run the above program, BLINK.BAS, on a
    PIC16F877, compile it using the command:

    PBP -p16f877 blink

    So verify what you need for the 16F873A.

    Luciano

    Code:
    	;====== BEGINNING OF THE ROTARY ENCODER CODE ========
    	;The Rotary Encoder is connected to PORTB  
    	;The A signal of the encoder connected to the PIN portB.7
    	;The B signal of the encoder connected to the PIN portB.6
    	;
    	;The 3 variables used are declared in the PicBasic code.
    	;
    	;	enc_new VAR BYTE
    	;	enc_old VAR BYTE
    	;	enc_counter VAR WORD
    	;
    	;================================================
    		
    	;Read latest input from PORTB & put the value in _enc_new.
    
         	movf    PORTB,W
         	movwf  _enc_new
    
         	;Strip off all but the 2 MSBs in _enc_new.
    
         	movlw	B'11000000'	;Create bit mask (bits 7 & 6).
         	andwf   _enc_new,F     	;Zero bits 5 thru 0.
    
         	;Determine the direction of the Rotary encoder.  
    
         	rlf     _enc_old,F     	;left shift it into _enc_old to align bit 6 of 
                                    ;_enc_old with bit 7 of _enc_new.
    
         	movf    _enc_new,W     	;Move the contents of _enc_new to W in order to XOR.
         	xorwf   _enc_old,F    	;XOR previous inputs (in _enc_old) with latest
                                    ;inputs (in W) to determine CW or CCW.
     
          	btfsc   _enc_old,7     	;Test bit 7 of result (in _enc_old).  Skip next line
                             	;if it is 0 (direction is CCW).
         	goto    Up        	;Bit is 1 (direction is CW).  Go around Down
                             	;and increment counter.
    
    
    Down
         	;Decrements _enc_counter because the rotary encoder moved CCW.
    	;Decrements _enc_counter (16 bit value), sets Z on exit.
    	     	 
            decf    _enc_counter,F      ; Decrement low byte
            incfsz  _enc_counter,W      ; Check for underflow
            incf    _enc_counter+1,F    ; Update
            decf    _enc_counter+1,F    ; Fixup
            movf    _enc_counter,W
            iorwf   _enc_counter+1,W    ; Set Z bit
    		
    	;Add here code for the CCW LED if needed.
         	
         	goto    Continue  	    ;Branch around UP.
    
    Up
         	;Increments _enc_counter because the rotary encoder moved CW.
    	;Increments _enc_counter (16 bit value), sets Z on exit.
    
            incfsz  _enc_counter,W      ; Add one to low byte
            decf    _enc_counter+1,F    ; No carry (negates next step)
            incf    _enc_counter+1,F    ; Add one to high byte
            movwf   _enc_counter        ; Store updated low byte back.
            iorwf   _enc_counter+1,W    ; Set Z flag
    		
    	;Add here code for the CW LED if needed.
    	
    Continue 
         	
         	;Assign the latest encoder inputs (in _enc_new) to _enc_old.
    
         	movf 	_enc_new,W
         	movwf   _enc_old
    
    	;============ END OF THE ROTARY ENCODER CODE =====
    Last edited by Luciano; - 12th May 2005 at 22:20.

Similar Threads

  1. Quick thoughts: DT Ints with encoders
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 7th January 2010, 01:01
  2. PMDC SERVO MOTOR WITH quadrature encoder DRIVE ?
    By phoenix_1 in forum Schematics
    Replies: 37
    Last Post: - 22nd November 2009, 19:45
  3. 32-bit Quadrature Counter With Serial Interface
    By precision in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 10th June 2008, 02:49
  4. quad encoders
    By cpayne in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2007, 17:49
  5. "momentary" IR decoding
    By sporker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th June 2005, 01:53

Members who have read this thread : 4

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts