BLDC control for jumping robot.
Dependencies: CRC MODSERIAL mbed-dev mbed-rtos
Fork of mbed_BLDC_driver_KL25Z by
Diff: enc.cpp
- Revision:
- 1:d68c51a0a706
- Parent:
- 0:00c56a9c6dee
--- a/enc.cpp Sat Mar 05 00:09:45 2016 +0000 +++ b/enc.cpp Wed Sep 28 16:59:46 2016 +0000 @@ -1,27 +1,17 @@ #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); + set_offset(2134); // est. 9/13/2016 csl=1; + m_spi.format(8, 1); + m_spi.frequency(1000000); 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; @@ -36,18 +26,30 @@ return enc_pos.pos; } -void enc::set_offset(){ - enc_pos.offset = ams_read(); +void enc::set_offset(ushort zpos){ + ushort zhold; + 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); } ushort enc::write_spi(ushort reg, u8 rw){ ushort data; reg = reg | rw << 14; reg = reg | par(reg); + char d1 = reg >> 8; + char d2 = reg & 0x00FF; csl=0; - data = m_spi.write(reg); + d1 = m_spi.write(d1); + d2 = m_spi.write(d2); + csl=1; - wait(0.00001); + wait(0.0001); + data = (d1 << 8) | (d2 & 0xFF); return data; } @@ -102,4 +104,4 @@ read_spi(0x0001); return enc_data & 0x3FFF; -} \ No newline at end of file +}