hctl 2032 encoder for stm32

Dependencies:   FastIO

Files at this revision

API Documentation at this revision

Comitter:
c128
Date:
Fri Sep 05 04:51:48 2014 +0000
Child:
1:6cab31aea0b8
Commit message:
hctl 2032 encoder with stm32

Changed in this revision

EncoderCounter.cpp Show annotated file Show diff for this revision Revisions of this file
EncoderCounter.h Show annotated file Show diff for this revision Revisions of this file
FastIO.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EncoderCounter.cpp	Fri Sep 05 04:51:48 2014 +0000
@@ -0,0 +1,206 @@
+
+
+#include "EncoderCounter.h"
+
+#include "FastIO.h"
+
+
+      FastOut<PA_0> HCTL_PIN_XY;
+      FastOut<PA_1> HCTL_PIN_OE; // Active LOW
+      FastOut<PA_4> HCTL_PIN_EN1;
+      FastOut<PB_3> HCTL_PIN_EN2;
+      FastOut<PC_10> HCTL_PIN_SEL1;
+      FastOut<PC_11> HCTL_PIN_SEL2;
+      FastOut<PB_6> HCTL_PIN_RSTX; // Active LOW
+      FastOut<PB_7> HCTL_PIN_RSTY; // Active LOW
+      
+  
+      FastIn<PC_0> bus0;
+      FastIn<PC_1> bus1;    
+      FastIn<PC_2> bus2;
+      FastIn<PC_3> bus3; 
+      FastIn<PC_4> bus4;
+      FastIn<PB_9> bus5; 
+      FastIn<PC_6> bus6;
+      FastIn<PC_7> bus7;       
+      //PortIn HCTL_DataBus(PortC, 0x00FF);
+      
+
+
+ENCODER::ENCODER(unsigned char countMode)
+
+{
+
+
+   switchCountMode( countMode );
+
+
+ 
+   XAxisReset();
+   YAxisReset();
+   
+   HCTL_PIN_RSTX = 1;  // Active LOW
+   HCTL_PIN_RSTY = 1;  // Active LOW   
+   HCTL_PIN_OE = 1;
+   wait_ms(1);
+   
+
+}
+
+   
+    
+// Communicates with a HCTL-2032 IC to get reset the X encoder count
+// see Avago/Agilent/HP HCTL-2032 PDF for details
+void ENCODER::XAxisReset()
+{
+   HCTL_PIN_RSTX = 0;
+   wait_ms(0);
+   HCTL_PIN_RSTX = 1;
+   wait_ms(1);
+}
+
+void ENCODER::YAxisReset()
+{
+   HCTL_PIN_RSTX = 0;
+   wait_ms(0);
+   HCTL_PIN_RSTX = 1;
+   wait_ms(1);
+}
+
+// Communicates with a HCTL-2032 IC to get the X Axis encoder count via an 8bit parallel bus
+// see Avago/Agilent/HP HCTL-2032 Datasheet PDF for details
+ int ENCODER::XAxisGetCount( )
+{
+
+   HCTL_PIN_XY = 0;  //0
+   HCTL_PIN_OE = 0;
+   wait_ms(5);
+   
+
+   //read MSB
+   HCTL_PIN_SEL1 = 0;  //0
+   HCTL_PIN_SEL2 = 1;  //1
+   wait_ms(5);
+   busByteMSB = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByteMSB;
+   countEnc <<= 8 ;
+
+ 
+  
+   //read 2nd byte
+   HCTL_PIN_SEL1 = 1;
+   HCTL_PIN_SEL2 = 1;    
+   wait_ms(5);
+   busByte = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByte + countEnc;
+   countEnc <<= 8 ;
+
+
+      
+   //read 3rd byte
+   HCTL_PIN_SEL1 = 0;
+   HCTL_PIN_SEL2 = 0;
+   HCTL_PIN_OE = 0;
+   wait_ms(5);
+   busByte = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByte + countEnc;
+   countEnc <<= 8;
+
+      
+   //read LSB
+   HCTL_PIN_SEL1 = 1;
+   HCTL_PIN_SEL2 = 0;
+   wait_ms(5);
+   busByte = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByte + countEnc;
+ 
+   
+   HCTL_PIN_OE = 1; //1
+
+
+   return countEnc;
+}
+
+int ENCODER::YAxisGetCount( )
+{
+
+   HCTL_PIN_XY = 0;  //0
+   HCTL_PIN_OE = 0;
+   wait_ms(5);
+   
+
+   //read MSB
+   HCTL_PIN_SEL1 = 0;  //0
+   HCTL_PIN_SEL2 = 1;  //1
+   wait_ms(5);
+   busByteMSB = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByteMSB;
+   countEnc <<= 8 ;
+
+ 
+  
+   //read 2nd byte
+   HCTL_PIN_SEL1 = 1;
+   HCTL_PIN_SEL2 = 1;    
+   wait_ms(5);
+   busByte = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByte + countEnc;
+   countEnc <<= 8 ;
+
+
+      
+   //read 3rd byte
+   HCTL_PIN_SEL1 = 0;
+   HCTL_PIN_SEL2 = 0;
+   HCTL_PIN_OE = 0;
+   wait_ms(5);
+   busByte = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByte + countEnc;
+   countEnc <<= 8;
+
+      
+   //read LSB
+   HCTL_PIN_SEL1 = 1;
+   HCTL_PIN_SEL2 = 0;
+   wait_ms(5);
+   busByte = (bus0 | (bus1 << 1) | (bus2 << 2) | (bus3 << 3) | (bus4 << 4) | (bus5 << 5) | (bus6 << 6) | (bus7 << 7) );
+   countEnc = busByte + countEnc;
+ 
+   
+   HCTL_PIN_OE = 1; //1
+
+
+   return countEnc;
+}
+
+void ENCODER::switchCountMode( unsigned char countMode )
+{
+   // Count Mode Illegal Mode EN1 LOW  EN2 LOW
+   // Count Mode   4X         EN1 HIGH EN2 LOW
+   // Count Mode   2X         EN1 LOW  EN2 HIGH
+   // Count Mode   1X         EN1 HIGH EN2 HIGH
+   switch(countMode)
+   {
+      case 1: // 1X Count Mode
+         HCTL_PIN_EN1 = 1;
+         HCTL_PIN_EN2 = 1;
+         break;
+      case 2: // 2X Count Mode
+         HCTL_PIN_EN1 = 0;
+         HCTL_PIN_EN2 = 1;
+         break;
+      case 4: // 4X Count Mode is the default
+         HCTL_PIN_EN1 = 1;
+         HCTL_PIN_EN2 = 0;
+         break;
+      default:
+         HCTL_PIN_EN1 = 1;
+         HCTL_PIN_EN2 = 0;
+         break;
+
+                 
+   }
+   wait_ms(1);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EncoderCounter.h	Fri Sep 05 04:51:48 2014 +0000
@@ -0,0 +1,40 @@
+/* porting stm32 http://www.robogaia.com/two-axis-encoder-counter-mega-shield-version-2.html
+/* by Paolo Della Vedova
+/* ther is more problem to convert 5v to 3v
+
+#ifndef HCTL_2032_DRIVER
+#define HCTL_2032_DRIVER
+
+#include "mbed.h"
+
+
+//*** arduino compatibility
+#define HIGH 1
+#define LOW 0
+
+#define digitalWrite(x,y)   x=y
+
+
+
+      
+class ENCODER
+{
+   public:
+      ENCODER( unsigned char countMode=4 );
+
+      void  XAxisReset( );
+      int   XAxisGetCount( );
+      void  YAxisReset( );
+      int   YAxisGetCount( );
+
+      void          switchCountMode( unsigned char countMode );
+
+   private:
+      signed int countEnc;
+      unsigned char busByte;
+      signed char busByteMSB;
+};
+
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FastIO.lib	Fri Sep 05 04:51:48 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/c128/code/FastIO/#6e0f24f71081