Driving Gyro MLX90609 using the SPI of mbed

Dependencies:   mbed

Committer:
hack08
Date:
Sat May 12 14:27:40 2012 +0000
Revision:
0:d847fd15f903

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hack08 0:d847fd15f903 1 /*
hack08 0:d847fd15f903 2 Gyroscope Interfacing
hack08 0:d847fd15f903 3 After recieving 16bit from Gyro if 15th bit is zero, the instruction is accepted
hack08 0:d847fd15f903 4 */
hack08 0:d847fd15f903 5
hack08 0:d847fd15f903 6 #include "mbed.h"
hack08 0:d847fd15f903 7 SPI spi(p5, p6, p7); // mosi, miso, sclk
hack08 0:d847fd15f903 8 DigitalOut csGyro(p8);
hack08 0:d847fd15f903 9 Serial pc(USBTX, USBRX); // tx, rx
hack08 0:d847fd15f903 10
hack08 0:d847fd15f903 11 void activateADC()
hack08 0:d847fd15f903 12 {
hack08 0:d847fd15f903 13 unsigned int data1, data2;
hack08 0:d847fd15f903 14 csGyro=0;
hack08 0:d847fd15f903 15 spi.write(0x93); // 0b10010100
hack08 0:d847fd15f903 16 /*Check if instruction recieved i.e */
hack08 0:d847fd15f903 17 //Send 2 dymmy byte of any combination
hack08 0:d847fd15f903 18 data1 = spi.write(0x00);
hack08 0:d847fd15f903 19 data2 = spi.write(0x00);
hack08 0:d847fd15f903 20 wait_us(200);
hack08 0:d847fd15f903 21 csGyro=1;
hack08 0:d847fd15f903 22
hack08 0:d847fd15f903 23 if(data1 & 0x80) /*0b10000000*/
hack08 0:d847fd15f903 24 {
hack08 0:d847fd15f903 25 pc.printf("\nInstruction not recieved %d %d", &data1, &data2);
hack08 0:d847fd15f903 26 }
hack08 0:d847fd15f903 27 else
hack08 0:d847fd15f903 28 pc.printf("\n ADC activated ");
hack08 0:d847fd15f903 29 }
hack08 0:d847fd15f903 30
hack08 0:d847fd15f903 31 void sleepADC()
hack08 0:d847fd15f903 32 {
hack08 0:d847fd15f903 33 unsigned int data1, data2;
hack08 0:d847fd15f903 34 csGyro=0;
hack08 0:d847fd15f903 35 spi.write(0x90); // 0b10010000
hack08 0:d847fd15f903 36 /*Check if instruction recieved i.e */
hack08 0:d847fd15f903 37 //Send 2 dymmy byte of any combination
hack08 0:d847fd15f903 38 data1 = spi.write(0x00);
hack08 0:d847fd15f903 39 data2 = spi.write(0x00);
hack08 0:d847fd15f903 40 wait_us(200);
hack08 0:d847fd15f903 41 csGyro=1;
hack08 0:d847fd15f903 42
hack08 0:d847fd15f903 43 if(data1 & 0x80) /*0b10000000*/
hack08 0:d847fd15f903 44 {
hack08 0:d847fd15f903 45 pc.printf("\nInstruction not recieved %d %d", &data1, &data2);
hack08 0:d847fd15f903 46 }
hack08 0:d847fd15f903 47 else
hack08 0:d847fd15f903 48 pc.printf("\n ADC in sleep ");
hack08 0:d847fd15f903 49 }
hack08 0:d847fd15f903 50
hack08 0:d847fd15f903 51 unsigned int getAngularRate()
hack08 0:d847fd15f903 52 {
hack08 0:d847fd15f903 53 unsigned int result=0, data1, data2;
hack08 0:d847fd15f903 54 csGyro=0;
hack08 0:d847fd15f903 55 spi.write(0x94); /* 0b10010100 Send SPI ADCC for Angular Rate (CHAN bit = 0)*/
hack08 0:d847fd15f903 56 /*Check if instruction recieved*/
hack08 0:d847fd15f903 57 //Send 2 dymmy byte of any combination
hack08 0:d847fd15f903 58 data1 = spi.write(0x00);
hack08 0:d847fd15f903 59 data2 = spi.write(0x00);
hack08 0:d847fd15f903 60 wait_us(200);
hack08 0:d847fd15f903 61 csGyro=1;
hack08 0:d847fd15f903 62
hack08 0:d847fd15f903 63 if(data1 & 0x80 ) /*0b10000000*/
hack08 0:d847fd15f903 64 {
hack08 0:d847fd15f903 65 pc.printf("\nInstruction not recieved");
hack08 0:d847fd15f903 66 }
hack08 0:d847fd15f903 67
hack08 0:d847fd15f903 68 csGyro=0;
hack08 0:d847fd15f903 69 spi.write(0x80); /* 0b10000000 Send SPI ADCR Instruction*/
hack08 0:d847fd15f903 70 /*Send 2 dymmy byte*/
hack08 0:d847fd15f903 71 data1 = spi.write(0x00);
hack08 0:d847fd15f903 72 data2 = spi.write(0x00);
hack08 0:d847fd15f903 73 wait_us(200);
hack08 0:d847fd15f903 74 csGyro=1;
hack08 0:d847fd15f903 75
hack08 0:d847fd15f903 76 /*Check if instruction recieved*/
hack08 0:d847fd15f903 77 if(!(data1 & 0x80))
hack08 0:d847fd15f903 78 {
hack08 0:d847fd15f903 79 while(!(data1 & 0x20)); // Wait while EOC is 0
hack08 0:d847fd15f903 80 result = ((data1 & 0x0F) << 7) + (data2>>1);
hack08 0:d847fd15f903 81 }
hack08 0:d847fd15f903 82 return result;
hack08 0:d847fd15f903 83
hack08 0:d847fd15f903 84 }
hack08 0:d847fd15f903 85
hack08 0:d847fd15f903 86 unsigned int getTemperature()
hack08 0:d847fd15f903 87 {
hack08 0:d847fd15f903 88 unsigned int result=0, data1, data2;
hack08 0:d847fd15f903 89 csGyro=0;
hack08 0:d847fd15f903 90 spi.write(0x9C); /* 0b10011100 Send SPI ADCC for Temperature (CHAN bit = 1)*/
hack08 0:d847fd15f903 91 /*Check if instruction recieved*/
hack08 0:d847fd15f903 92 //Send 2 dymmy byte of any combination
hack08 0:d847fd15f903 93 data1 = spi.write(0x00);
hack08 0:d847fd15f903 94 data2 = spi.write(0x00);
hack08 0:d847fd15f903 95 wait_us(200);
hack08 0:d847fd15f903 96 csGyro=1;
hack08 0:d847fd15f903 97
hack08 0:d847fd15f903 98 if(data1 & 0x80 ) /*0b10000000*/
hack08 0:d847fd15f903 99 {
hack08 0:d847fd15f903 100 pc.printf("\nInstruction not recieved");
hack08 0:d847fd15f903 101 }
hack08 0:d847fd15f903 102
hack08 0:d847fd15f903 103 csGyro=0;
hack08 0:d847fd15f903 104 spi.write(0x80); /* 0b10000000 Send SPI ADCR Instruction*/
hack08 0:d847fd15f903 105 /*Send 2 dymmy byte*/
hack08 0:d847fd15f903 106 data1 = spi.write(0x00);
hack08 0:d847fd15f903 107 data2 = spi.write(0x00);
hack08 0:d847fd15f903 108 wait_us(200);
hack08 0:d847fd15f903 109 csGyro=1;
hack08 0:d847fd15f903 110
hack08 0:d847fd15f903 111 /*Check if instruction accepted*/
hack08 0:d847fd15f903 112 if(!(data1 & 0x80))
hack08 0:d847fd15f903 113 {
hack08 0:d847fd15f903 114 while(!(data1 & 0x20)); // Wait while EOC is 0
hack08 0:d847fd15f903 115 result = ((data1 & 0x0F) << 7) + (data2>>1);
hack08 0:d847fd15f903 116 }
hack08 0:d847fd15f903 117 return result;
hack08 0:d847fd15f903 118 }
hack08 0:d847fd15f903 119
hack08 0:d847fd15f903 120 int adcToAngularRate(unsigned int result)
hack08 0:d847fd15f903 121 {
hack08 0:d847fd15f903 122 int conversion = (result * 25/12)+400;
hack08 0:d847fd15f903 123 signed int offset = 2500;
hack08 0:d847fd15f903 124 return (conversion - offset)/6.67; /*from the data sheet, R2 version is 6.67 E2 is 13.33 and N2 is 26.67 mV/deg change accordingly.*/
hack08 0:d847fd15f903 125 }
hack08 0:d847fd15f903 126
hack08 0:d847fd15f903 127 int adcToTemperature(unsigned int result)
hack08 0:d847fd15f903 128 {
hack08 0:d847fd15f903 129
hack08 0:d847fd15f903 130 int conversion = (result * 25/16)+300;
hack08 0:d847fd15f903 131 signed int offset = 2500;
hack08 0:d847fd15f903 132 return 25 + ((conversion - offset)/10); /* from the data sheet factor is 10mV/K */
hack08 0:d847fd15f903 133 }
hack08 0:d847fd15f903 134
hack08 0:d847fd15f903 135 int main()
hack08 0:d847fd15f903 136 {
hack08 0:d847fd15f903 137 unsigned int angle, temper;
hack08 0:d847fd15f903 138 spi.format(8,3);
hack08 0:d847fd15f903 139 spi.frequency(1000000);
hack08 0:d847fd15f903 140 while(1){
hack08 0:d847fd15f903 141 activateADC();
hack08 0:d847fd15f903 142
hack08 0:d847fd15f903 143 angle=getAngularRate();
hack08 0:d847fd15f903 144 temper=getTemperature();
hack08 0:d847fd15f903 145 pc.printf("\nAngular Rate %d",adcToAngularRate(angle));
hack08 0:d847fd15f903 146 pc.printf("\nTemperature %d",adcToTemperature(temper));
hack08 0:d847fd15f903 147 sleepADC();
hack08 0:d847fd15f903 148 wait(1);
hack08 0:d847fd15f903 149 }
hack08 0:d847fd15f903 150 }