Simple program to read X and Y data from AS5013 Hall sensor IC. It connects to the AMS5013 Eval Kit (https://www.digikey.com/product-detail/en/ams/AS5013-QF_EK_AB/AS5013-QF_EK_AB-ND/6568933)

Dependencies:   mbed

Committer:
bradstew
Date:
Fri Jul 28 18:44:56 2017 +0000
Revision:
0:cfe457d3bc29
Final version used in Digikey article;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bradstew 0:cfe457d3bc29 1 #include "mbed.h"
bradstew 0:cfe457d3bc29 2 #if defined (TARGET_KL05Z)
bradstew 0:cfe457d3bc29 3
bradstew 0:cfe457d3bc29 4 #else
bradstew 0:cfe457d3bc29 5 #error TARGET NOT DEFINED
bradstew 0:cfe457d3bc29 6 #endif
bradstew 0:cfe457d3bc29 7
bradstew 0:cfe457d3bc29 8 PinName const SDA = PTB4;
bradstew 0:cfe457d3bc29 9 PinName const SCL = PTB3;
bradstew 0:cfe457d3bc29 10
bradstew 0:cfe457d3bc29 11 I2C i2c(SDA, SCL); //construct I2c object
bradstew 0:cfe457d3bc29 12
bradstew 0:cfe457d3bc29 13 const int addr = 0x40<<1; //i2c address of AS5013
bradstew 0:cfe457d3bc29 14 Serial pc(USBTX, USBRX); //optional, but needed to set faster baud
bradstew 0:cfe457d3bc29 15 char data[7];
bradstew 0:cfe457d3bc29 16 //void init_AS5013(void);
bradstew 0:cfe457d3bc29 17
bradstew 0:cfe457d3bc29 18 void init_AS5013(){
bradstew 0:cfe457d3bc29 19 int8_t statusF=0;
bradstew 0:cfe457d3bc29 20 //i=0;
bradstew 0:cfe457d3bc29 21 wait(0.1);
bradstew 0:cfe457d3bc29 22 data[0]=0x0f;
bradstew 0:cfe457d3bc29 23 data[1]=0x02;
bradstew 0:cfe457d3bc29 24 i2c.write(addr,data,2); //reset part. write 2 to reg 0x0f
bradstew 0:cfe457d3bc29 25 wait(.01);
bradstew 0:cfe457d3bc29 26 //printf("two...");
bradstew 0:cfe457d3bc29 27 /*
bradstew 0:cfe457d3bc29 28 while(statusF != 0xf0) {
bradstew 0:cfe457d3bc29 29 data[0]= 0x0f;
bradstew 0:cfe457d3bc29 30 i2c.write(addr,data,1); //set up to read register 0x0f
bradstew 0:cfe457d3bc29 31 i2c.read(addr,data,1);
bradstew 0:cfe457d3bc29 32 statusF = data[0] & 0xfe;
bradstew 0:cfe457d3bc29 33 wait(0.01);
bradstew 0:cfe457d3bc29 34 printf("0x%x2 0x%x2...",statusF,data[0]);
bradstew 0:cfe457d3bc29 35 }
bradstew 0:cfe457d3bc29 36 */
bradstew 0:cfe457d3bc29 37 //printf("three...");
bradstew 0:cfe457d3bc29 38 //set up the gain factor
bradstew 0:cfe457d3bc29 39 data[0] = 0x2d; //set up write to register 0x2d with scalling factor
bradstew 0:cfe457d3bc29 40 data[1] = 0x04; //scaling factor
bradstew 0:cfe457d3bc29 41 i2c.write(addr,data,2);
bradstew 0:cfe457d3bc29 42
bradstew 0:cfe457d3bc29 43 }
bradstew 0:cfe457d3bc29 44
bradstew 0:cfe457d3bc29 45 int main() {
bradstew 0:cfe457d3bc29 46 int8_t x,y; //x y values to be printed to terminal
bradstew 0:cfe457d3bc29 47 uint8_t statusF; //status flag
bradstew 0:cfe457d3bc29 48 pc.baud (115200);
bradstew 0:cfe457d3bc29 49 //printf("start...");
bradstew 0:cfe457d3bc29 50 init_AS5013(); //reset and initialze the AS5013
bradstew 0:cfe457d3bc29 51 //printf("one...");
bradstew 0:cfe457d3bc29 52 while (1) {
bradstew 0:cfe457d3bc29 53 wait(0.1);
bradstew 0:cfe457d3bc29 54 statusF =0;
bradstew 0:cfe457d3bc29 55 data[0] = 0x0f;
bradstew 0:cfe457d3bc29 56 i2c.write(addr, data, 1); //set to read address 0x0f
bradstew 0:cfe457d3bc29 57 i2c.read(addr, data, 3); //read data at register 0x0f, 0x10, 0x11
bradstew 0:cfe457d3bc29 58 statusF = data[0]; //get status byte
bradstew 0:cfe457d3bc29 59 if(statusF & 0x01){
bradstew 0:cfe457d3bc29 60 x=data[1]; //get x
bradstew 0:cfe457d3bc29 61 y=data[2]; //get y
bradstew 0:cfe457d3bc29 62 printf(" %d %d", x,y); //output result
bradstew 0:cfe457d3bc29 63 printf("\n");
bradstew 0:cfe457d3bc29 64 }
bradstew 0:cfe457d3bc29 65 else{
bradstew 0:cfe457d3bc29 66 pc.printf("error 0x%x\n",statusF); //should not get here
bradstew 0:cfe457d3bc29 67 }
bradstew 0:cfe457d3bc29 68
bradstew 0:cfe457d3bc29 69 }
bradstew 0:cfe457d3bc29 70 }
bradstew 0:cfe457d3bc29 71