driver

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
dhaldane
Date:
Sat Mar 05 00:09:45 2016 +0000
Commit message:
Driver codes;

Changed in this revision

enc.cpp Show annotated file Show diff for this revision Revisions of this file
enc.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enc.cpp	Sat Mar 05 00:09:45 2016 +0000
@@ -0,0 +1,105 @@
+#include "enc.h"
+
+enc::enc(PinName mosi, PinName miso, PinName sck, PinName cs) : m_spi(mosi, miso, sck), csl(cs){
+    ushort zpos = 2228; // est. 8/6/2015 1773
+    ushort zhold;
+    m_spi.format(16, 1);
+    m_spi.frequency(50000);
+    csl=1;
+    wait(0.05);
+
+
+    write_spi(0x0019, 0); //SETTINGS2
+    write_spi(0x001D, 0);
+    write_spi(0x0018, 0); //SETTINGS1
+    write_spi(0x0018, 0);
+    
+    zhold = zpos >> 6; //8MSB of zero position
+    write_spi(0x0016, 0);
+    write_spi(zhold, 0);
+    
+    zhold = zpos & 0x003F; //6 LSB of zero position
+    write_spi(0x0017, 0);
+    write_spi(zhold, 0);   
+    
+    enc_pos.pos = 0;
+    enc_pos.oticks = 0;
+    enc_pos.offset = 0;
+    enc_pos.calibPos = 0;
+    }
+    
+enc::~enc(){
+ 
+}
+
+unsigned int enc::cal_state(){
+    return enc_pos.pos;
+}
+
+void enc::set_offset(){
+    enc_pos.offset = ams_read();
+}
+
+ushort enc::write_spi(ushort reg, u8 rw){
+    ushort data;
+    reg = reg | rw << 14;
+    reg = reg | par(reg);
+    csl=0;
+    data = m_spi.write(reg);
+    csl=1;
+    wait(0.00001);
+    return data;
+    
+}
+
+ushort enc::read_spi(ushort reg){
+    ushort data;
+    reg = reg | 1 << 14;
+    reg = reg | par(reg);
+    write_spi(reg, 1);
+    data = write_spi(0x0000, 1);
+    return data;  
+}
+
+/*!
+*****************************************************************************
+* Calculate even parity of a 16 bit unsigned integer
+*
+* This function is used by the SPI interface to calculate the even parity
+* of the data which will be sent via SPI to the encoder.
+*
+* \param[in] value : 16 bit unsigned integer whose parity shall be calculated
+*
+* \return : Even parity
+*
+*****************************************************************************
+*/
+ushort enc::par(ushort value)
+{
+    u8 cnt = 0;
+    u8 i;
+ 
+    for (i = 0; i < 16; i++)
+    {
+        if (value & 0x1)
+        {
+            cnt++;
+        }
+        value >>= 1;
+    }
+    cnt = cnt & 0x1;
+    return cnt << 15;
+}
+
+void enc::update_pos(){
+    enc_pos.pos = ams_read();
+}
+
+unsigned int enc::ams_read() {
+    unsigned int enc_data;
+
+    enc_data = read_spi(0x3FFE);
+    read_spi(0x0001);
+        
+    return enc_data & 0x3FFF;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enc.h	Sat Mar 05 00:09:45 2016 +0000
@@ -0,0 +1,39 @@
+#include "mbed.h"
+#define u8 unsigned char
+#define u16 unsigned int
+#define ubyte unsigned char
+#define ushort unsigned short int
+#define bit unsigned char
+typedef struct {
+    unsigned int pos; // raw reading from sensor 14 bits
+    long oticks;  // revolution counter
+    unsigned int calibPos;  // 0 to 2pi, converted to 16 bits
+    unsigned int offset; // initial reading on setup - relative zero position
+} EncObj; 
+
+class enc
+{
+public:
+    //Connect over i2c
+    enc(PinName mosi, PinName miso, PinName sck, PinName cs);
+    //Destroy instance
+    ~enc();
+    unsigned int  ams_read();
+    void update_pos();
+    void set_offset();
+    unsigned int cal_state(); //Calibrated stat
+    static ushort par(ushort value);
+    ushort write_spi(ushort reg, u8 rw);
+    ushort read_spi(ushort reg);
+    EncObj enc_pos;
+    
+private:
+    SPI m_spi;
+    DigitalOut csl;
+
+};
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Mar 05 00:09:45 2016 +0000
@@ -0,0 +1,23 @@
+#include "mbed.h"
+#include "enc.h"
+
+Ticker RTI;
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX); // tx, rx
+enc crankPos(p5,p6,p7,p8);
+
+
+void get_state(){
+    crankPos.update_pos();
+}
+
+int main() {    
+    RTI.attach(&get_state, 0.01);
+    while(1) {
+        myled = 1;
+        wait(0.05);
+        pc.printf("Encoder: %u                             \r\n", crankPos.cal_state());
+        myled = 0;
+        wait(0.05);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Mar 05 00:09:45 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file