Jump to content

Patch for reversed encoders


Recommended Posts

Hello everybody,

since I have some encoders (but not all) in my MBSEQ changing values in the opposite direction, I thought the best way is to tell somewhere in the software which encoder is reversed (has its pins swapped) and which is not.

So I've made a little patch to the MIOS code making it able to read a flag in the encoder table and give a negated (2-complement) incrementer to the user application if that flag is set.

Here's the patch against MIOS v1.9f:

diff -Naur mios_v1_9f_src.orig/src/mios_enc.inc mios_v1_9f_src/src/mios_enc.inc
--- mios_v1_9f_src.orig/src/mios_enc.inc	2008-01-20 23:39:02.000000000 +0100
+++ mios_v1_9f_src/src/mios_enc.inc	2008-02-22 19:43:43.000000000 +0100
@@ -16,6 +16,7 @@
 MIOS_ENC_MODE_NON_DETENTED	EQU	0x00
 MIOS_ENC_MODE_DETENTED		EQU	0x80
 MIOS_ENC_MODE_DETENTED2		EQU	0x81
+MIOS_ENC_MODE_REVERSED      EQU 0x40

 ENC_STAT_ACT_A          EQU 0	; Current status of channel A
 ENC_STAT_ACT_B          EQU 1	; Current status of channel B
@@ -801,6 +802,10 @@
 	addlw	1
 	addwf	FSR0L, F

+	;; now TABLAT contains encoder flags, so it's a dummy read
+	;; if incrementer is zero or we can read "reverse" flag otherwise.
+	tblrd*+
+
 	;; temporary disable the IRQs
 	IRQ_DISABLE

@@ -814,6 +819,12 @@
 	;; enable IRQs again
 	IRQ_ENABLE

+	;; makes incrementer negative if encoder is reversed mounted
+	BRA_IFCLR TABLAT, 6, ACCESS, MIOS_ENC_Dont_Negate
+	negf	WREG
+MIOS_ENC_Dont_Negate
+	
+
 	;; save incrementer in MIOS_PARAMETER2
 	movwf	MIOS_PARAMETER2

@@ -830,7 +841,6 @@

 MIOS_ENC_Handler_Loop_Next
 	IRQ_ENABLE
-	tblrd*+			; dummy read
 	incf	MIOS_TMP_CTR, F, BANKED
 	BRA_IFSET MIOS_TMP_CTR, 6, BANKED, MIOS_ENC_Handler_Loop_End
 	rgoto	MIOS_ENC_Handler_Loop
That's the same content of the attached file. To use it on your application, just add this row in mios.h of the app's source:
MIOS_ENC_MODE_REVERSED		EQU 0x40
and set your reversed encoders this way:
	ENC_ENTRY  2,  6,  MIOS_ENC_MODE_DETENTED2 | MIOS_ENC_MODE_REVERSED	; this is a reversed encoder

	ENC_ENTRY  2,  4,  MIOS_ENC_MODE_DETENTED2	; this is a normal encoder

I hope it gets useful for somebody.

Greetings, Enrico

revenc.diff.txt

revenc.diff.txt

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...