encoder and tachometer class for EW3XX single board computer
Revision 5:58b13292286e, committed 2021-08-16
- Comitter:
- lddevrie
- Date:
- Mon Aug 16 11:12:19 2021 +0000
- Parent:
- 4:19ee7eb16605
- Commit message:
- update for digital pot motor control
Changed in this revision
EW305sbc.cpp | Show annotated file Show diff for this revision Revisions of this file |
EW305sbc.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 19ee7eb16605 -r 58b13292286e EW305sbc.cpp --- 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
diff -r 19ee7eb16605 -r 58b13292286e EW305sbc.h --- a/EW305sbc.h Wed Sep 25 16:05:11 2019 +0000 +++ b/EW305sbc.h Mon Aug 16 11:12:19 2021 +0000 @@ -43,9 +43,21 @@ #define NONE_REG 0x07 //No Register Selected +//============================================================================= +// AD5293 Command Set - Table 11 of Datasheet Command Operation Truth Table +//============================================================================= +#define MAX_SCALE 0.9f +#define NOP 0x0 // Do nothing +#define WRDAC 0x1 // Write the contects of the serial register to the RDAC (D9 - D0) +#define RRDAC 0x2 // Read the RDAC wiper setting from SDO in the next frame +#define RESET 0x4 // Refresh the RDAC with midscale code +#define WCON 0x6 // Write contents of serial reg to control reg (D2, D1) +#define RCON 0x7 // Read control register from SDO output in the next frame +#define PWDN 0x8 // Software power down (D0=0 normal mode, D0=1 shutdown mode) + /** - * Tach Interface. + * Tach and motor control Interface. */ class EW305sbc { @@ -93,7 +105,16 @@ */ void LS7366_reset_counter(int chan_num); + /** + * set input voltage to motor + */ + void analog_input(float inp); + /** + * initialize digital potentiometer + */ + void init_ad5293(void); + void reset_ad5293(void); private: @@ -108,6 +129,8 @@ SPI spi;//(PinName p5,PinName p6,PinName p7); DigitalOut ls7166_cs1;//(PinName p19,int tmp=1); //CS for LS7366-1 (U8) DigitalOut ls7166_cs2;//(PinName p20, int tmp=1); //CS for LS7366-2 (U9) + DigitalOut ad5293_sync; // (p12,1); //CS for Digital potentiometer AD5293 + DigitalIn ad5293_rdy; // (p27); // data ready input pin from AD5293 digital pot long count_; float speed_; @@ -135,6 +158,13 @@ * update current speed estimate */ void recalc(void); + + /** + * write data to digital potentiometer + */ + void write_ad5293(int cmd, int data); + + }; #endif /* TACH_H */ \ No newline at end of file