encoder and tachometer class for EW3XX single board computer

Revision:
5:58b13292286e
Parent:
4:19ee7eb16605
--- a/EW305sbc.cpp	Wed Sep 25 16:05:11 2019 +0000
+++ b/EW305sbc.cpp	Mon Aug 16 11:12:19 2021 +0000
@@ -3,7 +3,7 @@
 
 
 EW305sbc::EW305sbc(int ch, int pulsesPerRev) 
-    : ch_(ch), pulsesPerRev_(pulsesPerRev), spi(p5, p6, p7), ls7166_cs1(p19,1), ls7166_cs2(p20,1)
+    : ch_(ch), pulsesPerRev_(pulsesPerRev), spi(p5, p6, p7), ls7166_cs1(p19,1), ls7166_cs2(p20,1), ad5293_sync(p12,1), ad5293_rdy(p27)
 {
     init();
 }
@@ -210,4 +210,61 @@
     } else {
         ls7166_cs2 = 1;
     }
+}
+
+// Write command and data - cmd[B13:B10], Data[B9:B0]
+void EW305sbc::write_ad5293(int cmd, int data)
+{
+    spi.frequency(10000000); // Datasheet states up to 50MHz operation
+    spi.format(16, 1);       // 16 data bits, CPOL0, and CPHA1 (datasheet page 8)
+    uint16_t dataw = (cmd << 10) | data;  // set data write bits (command and data)
+    ad5293_sync = 0;
+    spi.write(dataw); // SPI transmit of 16 bits
+
+    int timeout = 0;
+    while(!ad5293_rdy) { // takes about 250ns to update
+        timeout++;
+        if(timeout >1000) {
+            //pc.printf("Timeout waiting for RDY wiper update! timout=%d\r\n\r\n", timeout);
+            wait(2);
+        }
+    }
+    ad5293_sync =1;
+    spi.format(8,0);// return to 'normal' SPI format
+}
+
+
+//Motor control routine for digital potentiometer analog driver board
+// inp is analog voltage output (+/-1.0)
+void EW305sbc::analog_input(float inp)
+{
+    if(inp>MAX_SCALE)
+        inp=MAX_SCALE;
+    if(inp<-MAX_SCALE)
+        inp=-MAX_SCALE;
+    
+    write_ad5293(WCON,0x3FF); // Write to Control Register, C2=1 (normal mode), C1=1 (allows update of wiper)
+    write_ad5293(WRDAC,512 + inp*512);
+}
+
+void EW305sbc::reset_ad5293()
+{
+    write_ad5293(RESET,0x3FF);
+    wait(2.5);
+    LS7366_reset_counter(1);
+    LS7366_quad_mode_x4(1);
+    LS7366_write_DTR(1,0);
+}
+
+void EW305sbc::init_ad5293()
+{
+    write_ad5293(WCON,0x3FF); // Write to Control Register, C2=1 (normal mode), C1=1 (allows update of wiper)
+    write_ad5293(RESET,0x3FF);
+    wait(.4);    // allow time for motor to settle if the mbed was just reset while motor was moving
+    LS7366_reset_counter(1);
+    LS7366_quad_mode_x4(1);
+    LS7366_write_DTR(1,0);
+
+    write_ad5293(WCON,0x3FF);   // Write to Control Register, C2=1 (normal mode), C1=1 (allows update of wiper)
+    wait(1);
 }
\ No newline at end of file