Ricardo Binas
/
SPI_EncoderPanTilt_AMT203-V
Camera Pan Tilt SPI Encoder
Revision 1:ba4ae56b4895, committed 2019-02-24
- Comitter:
- Rbinas
- Date:
- Sun Feb 24 03:07:12 2019 +0000
- Parent:
- 0:32af259f9f2a
- Commit message:
- Camera Pan Tilt Encoder
Changed in this revision
SPIEncoder.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 |
diff -r 32af259f9f2a -r ba4ae56b4895 SPIEncoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SPIEncoder.h Sun Feb 24 03:07:12 2019 +0000 @@ -0,0 +1,120 @@ +#ifndef SPIENCODER_H_ +#define SPIENCODER_H_ + +#include "mbed.h" + +#ifdef __cplusplus +extern "C" { +#endif + +SPI EncoderSpi(p5, p6, p7);//Mosi Miso Sclk +DigitalOut cs(p8); //Chip select +DigitalOut cs1(p9); //Chip select + +uint16_t EncoderByteData = 0; +uint16_t Encoderposition_last = 0; +uint8_t temp[2]; +uint16_t Steps; + +uint16_t EncoderByteData1 = 0; +uint16_t Encoderposition_last1 = 0; +uint8_t temp1[2]; +uint16_t Steps1; + +void wait_ms(int us); + +void InitializeEncoder() +{ + EncoderSpi.format(8,0); + EncoderSpi.frequency(8000000); +} +//------------------------------------------EncoderA--------------------------------------------------- +uint8_t SPI_T (uint8_t SPITransmit)//Repetive SPI transmit sequence +{ + uint8_t SPI_temp = 0; //vairable to hold recieved data + cs=0; //select spi device + SPI_temp = EncoderSpi.write(SPITransmit);//send and recieve + cs=1;//deselect spi device + return(SPI_temp); //return recieved byte +} + +void start() +{ + uint8_t recieved = 0xA5;//just a temp vairable + EncoderByteData = 0;//reset position vairable + cs=0; + SPI_T(0x10);//issue read command + wait_ms(50);//give time to read. Timmig is critical + + while (recieved != 0x10) { //loop while encoder is not ready to send + recieved = SPI_T(0x00); //cleck again if encoder is still working + wait_ms(1); //again,give time to read. Timmig is critical + } + + temp[0] = SPI_T(0x00); //Recieve MSB + temp[1] = SPI_T(0x00); // recieve LSB + cs=1; + + temp[0] &=~ 0xF0;//mask out the first 4 bits + EncoderByteData = temp[0] << 8; //shift MSB to correct EncoderByteData in EncoderByteData message + EncoderByteData += temp[1]; // add LSB to EncoderByteData message to complete message + wait_ms(1);//again,give time to read. Timmig is critical +} + + +void EncoderA() +{ + if (EncoderByteData != Encoderposition_last) { //if nothing has changed dont wast time sending position + Encoderposition_last = EncoderByteData ; //set last position to current position + } else { + start(); //if something has changed in position, catch it + } + Steps = EncoderByteData/16; +} +//-----------------------------------------------EncoderB--------------------------------------------------------------- +uint8_t SPI_TT (uint8_t SPITransmit1)//Repetive SPI transmit sequence +{ + uint8_t SPI_temp1 = 0; //vairable to hold recieved data + cs1=0; //select spi device + SPI_temp1 = EncoderSpi.write(SPITransmit1);//send and recieve + cs1=1;//deselect spi device + return(SPI_temp1); //return recieved byte +} + +void start1() +{ + uint8_t recieved1 = 0xA5;//just a temp vairable + EncoderByteData1 = 0;//reset position vairable + cs1=0; + SPI_TT(0x10);//issue read command + wait_ms(50);//give time to read. Timmig is critical + + while (recieved1 != 0x10) { //loop while encoder is not ready to send + recieved1 = SPI_TT(0x00); //cleck again if encoder is still working + wait_ms(1); //again,give time to read. Timmig is critical + } + + temp1[0] = SPI_TT(0x00); //Recieve MSB + temp1[1] = SPI_TT(0x00); // recieve LSB + cs1=1; + + temp1[0] &=~ 0xF0;//mask out the first 4 bits + EncoderByteData1 = temp1[0] << 8; //shift MSB to correct EncoderByteData in EncoderByteData message + EncoderByteData1 += temp1[1]; // add LSB to EncoderByteData message to complete message + wait_ms(1);//again,give time to read. Timmig is critical +} +void EncoderB() +{ + if (EncoderByteData1 != Encoderposition_last1) { //if nothing has changed dont wast time sending position + Encoderposition_last1 = EncoderByteData1 ; //set last position to current position + } else { + start1(); //if something has changed in position, catch it + } + Steps1 = EncoderByteData1/16; +} + +#ifdef __cplusplus +} +#endif + +#endif // #ifndef SPIENCODER_H_ \ No newline at end of file
diff -r 32af259f9f2a -r ba4ae56b4895 main.cpp --- a/main.cpp Sun Feb 17 00:30:29 2019 +0000 +++ b/main.cpp Sun Feb 24 03:07:12 2019 +0000 @@ -1,70 +1,21 @@ // program used to read SPI 12 bit Encoder AMT203-V in steps of 16 #include "mbed.h" +#include "SPIEncoder.h" Serial pc(USBTX, USBRX); // tx, rx -SPI EncoderSpi(p5, p6, p7);//Mosi Miso Sclk -DigitalOut cs(p8); //Chip select - -uint16_t EncoderByteData = 0; -uint16_t Encoderposition_last = 0; -uint8_t temp[2]; -uint16_t Steps; -void wait_ms(int us); - - -uint8_t SPI_T (uint8_t SPITransmit)//Repetive SPI transmit sequence -{ - uint8_t SPI_temp = 0; //vairable to hold recieved data - cs=0; //select spi device - SPI_temp = EncoderSpi.write(SPITransmit);//send and recieve - cs=1;//deselect spi device - return(SPI_temp); //return recieved byte -} - -void start() -{ - uint8_t recieved = 0xA5;//just a temp vairable - EncoderByteData = 0;//reset position vairable - cs=0; - SPI_T(0x10);//issue read command - wait_ms(50);//give time to read. Timmig is critical - - while (recieved != 0x10) //loop while encoder is not ready to send - { - recieved = SPI_T(0x00); //cleck again if encoder is still working - wait_ms(1); //again,give time to read. Timmig is critical - } - - temp[0] = SPI_T(0x00); //Recieve MSB - temp[1] = SPI_T(0x00); // recieve LSB - cs=1; - - temp[0] &=~ 0xF0;//mask out the first 4 bits - EncoderByteData = temp[0] << 8; //shift MSB to correct EncoderByteData in EncoderByteData message - EncoderByteData += temp[1]; // add LSB to EncoderByteData message to complete message - wait_ms(1);//again,give time to read. Timmig is critical -} int main() { - EncoderSpi.format(8,0); - EncoderSpi.frequency(8000000); + + InitializeEncoder(); while(true) { - - if (EncoderByteData != Encoderposition_last) //if nothing has changed dont wast time sending position - { - Encoderposition_last = EncoderByteData ; //set last position to current position - } - else - { - start(); //if something has changed in position, catch it - } - Steps = EncoderByteData; - //pc.printf("Data = %d \n", EncoderByteData ); //actual byte reading - pc.printf("Data = %03d \n", Steps/16 ); + EncoderA(); + EncoderB(); + pc.printf("Data = %03d%03d \n", Steps,Steps1); + } } \ No newline at end of file