![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
RM3000
Fork of magnetomete_new by
main.cpp@0:ac874e417fac, 2015-02-13 (annotated)
- Committer:
- gkumar
- Date:
- Fri Feb 13 16:11:47 2015 +0000
- Revision:
- 0:ac874e417fac
- Child:
- 1:953885edf7a7
MM_RM3000_new
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gkumar | 0:ac874e417fac | 1 | |
gkumar | 0:ac874e417fac | 2 | //magnetometer V1.3 |
gkumar | 0:ac874e417fac | 3 | |
gkumar | 0:ac874e417fac | 4 | #include "mbed.h" |
gkumar | 0:ac874e417fac | 5 | #include "SPI.h" |
gkumar | 0:ac874e417fac | 6 | #include "math.h" |
gkumar | 0:ac874e417fac | 7 | #include "stdlib.h" |
gkumar | 0:ac874e417fac | 8 | Serial pc(USBTX,USBRX); |
gkumar | 0:ac874e417fac | 9 | SPI spi(PTD6,PTD7,PTD5); //MOSI,MISO,SCLK |
gkumar | 0:ac874e417fac | 10 | DigitalOut SSN_MAG(PTD4); //SLAVE SELECT |
gkumar | 0:ac874e417fac | 11 | InterruptIn DRDY(PTD2); |
gkumar | 0:ac874e417fac | 12 | Timeout tr_mag; |
gkumar | 0:ac874e417fac | 13 | uint8_t trflag_mag; |
gkumar | 0:ac874e417fac | 14 | void trsub_mag(); |
gkumar | 0:ac874e417fac | 15 | int a; |
gkumar | 0:ac874e417fac | 16 | //void end(); |
gkumar | 0:ac874e417fac | 17 | |
gkumar | 0:ac874e417fac | 18 | void trsub_mag() |
gkumar | 0:ac874e417fac | 19 | { |
gkumar | 0:ac874e417fac | 20 | trflag_mag=0; |
gkumar | 0:ac874e417fac | 21 | } |
gkumar | 0:ac874e417fac | 22 | |
gkumar | 0:ac874e417fac | 23 | void func() |
gkumar | 0:ac874e417fac | 24 | { |
gkumar | 0:ac874e417fac | 25 | // a=1; |
gkumar | 0:ac874e417fac | 26 | //wait_ms(100); |
gkumar | 0:ac874e417fac | 27 | SSN_MAG=0; |
gkumar | 0:ac874e417fac | 28 | spi.write(0xc9); //command byte for retrieving data |
gkumar | 0:ac874e417fac | 29 | //printf("\n\rahoy\n\r"); |
gkumar | 0:ac874e417fac | 30 | unsigned char axis; |
gkumar | 0:ac874e417fac | 31 | float Bnewvalue[3]={0.0,0.0,0.0}; |
gkumar | 0:ac874e417fac | 32 | int32_t Bvalue[3]={0,0,0}; |
gkumar | 0:ac874e417fac | 33 | int32_t a= pow(2.0,24.0); |
gkumar | 0:ac874e417fac | 34 | int32_t b= pow(2.0,23.0); |
gkumar | 0:ac874e417fac | 35 | |
gkumar | 0:ac874e417fac | 36 | for(axis=0;axis<3;axis++) |
gkumar | 0:ac874e417fac | 37 | { |
gkumar | 0:ac874e417fac | 38 | Bvalue[axis]=spi.write(0x00)<<16; //MSB 1 is send first |
gkumar | 0:ac874e417fac | 39 | wait_ms(100); |
gkumar | 0:ac874e417fac | 40 | Bvalue[axis]|=spi.write(0x00)<<8; //MSB 2 is send next |
gkumar | 0:ac874e417fac | 41 | wait_ms(100); |
gkumar | 0:ac874e417fac | 42 | Bvalue[axis]|=spi.write(0x00); //LSB is send.....total length is 24 bits(3*8bits)...which are appended to get actual bit configuration |
gkumar | 0:ac874e417fac | 43 | |
gkumar | 0:ac874e417fac | 44 | |
gkumar | 0:ac874e417fac | 45 | if((Bvalue[axis]&b)==b) |
gkumar | 0:ac874e417fac | 46 | { |
gkumar | 0:ac874e417fac | 47 | Bvalue[axis]=Bvalue[axis]-a; //converting 2s complement to signed decimal |
gkumar | 0:ac874e417fac | 48 | |
gkumar | 0:ac874e417fac | 49 | } |
gkumar | 0:ac874e417fac | 50 | Bnewvalue[axis]=(float)Bvalue[axis]*22.0*pow(10.0,-3.0); //1 LSB=(22nT)...final value of field obtained in micro tesla |
gkumar | 0:ac874e417fac | 51 | |
gkumar | 0:ac874e417fac | 52 | wait_ms(100); |
gkumar | 0:ac874e417fac | 53 | pc.printf("\t%f\n",Bnewvalue[axis]); |
gkumar | 0:ac874e417fac | 54 | |
gkumar | 0:ac874e417fac | 55 | } |
gkumar | 0:ac874e417fac | 56 | SSN_MAG=1; |
gkumar | 0:ac874e417fac | 57 | } |
gkumar | 0:ac874e417fac | 58 | |
gkumar | 0:ac874e417fac | 59 | |
gkumar | 0:ac874e417fac | 60 | void FUNC_ACS_MAGNETOMETER_INIT(); |
gkumar | 0:ac874e417fac | 61 | void FUNC_ACS_MAGNETOMETER_EXECUTE(); |
gkumar | 0:ac874e417fac | 62 | |
gkumar | 0:ac874e417fac | 63 | void main() |
gkumar | 0:ac874e417fac | 64 | { |
gkumar | 0:ac874e417fac | 65 | |
gkumar | 0:ac874e417fac | 66 | DRDY.rise(&func); |
gkumar | 0:ac874e417fac | 67 | FUNC_ACS_MAGNETOMETER_INIT(); |
gkumar | 0:ac874e417fac | 68 | while(1) |
gkumar | 0:ac874e417fac | 69 | { |
gkumar | 0:ac874e417fac | 70 | |
gkumar | 0:ac874e417fac | 71 | pc.printf("start\n"); |
gkumar | 0:ac874e417fac | 72 | wait(2); |
gkumar | 0:ac874e417fac | 73 | FUNC_ACS_MAGNETOMETER_EXECUTE(); |
gkumar | 0:ac874e417fac | 74 | |
gkumar | 0:ac874e417fac | 75 | } |
gkumar | 0:ac874e417fac | 76 | } |
gkumar | 0:ac874e417fac | 77 | |
gkumar | 0:ac874e417fac | 78 | void FUNC_ACS_MAGNETOMETER_INIT() |
gkumar | 0:ac874e417fac | 79 | { |
gkumar | 0:ac874e417fac | 80 | |
gkumar | 0:ac874e417fac | 81 | SSN_MAG=1; //pin is disabled |
gkumar | 0:ac874e417fac | 82 | spi.format(8,0); // 8bits,Mode 0 |
gkumar | 0:ac874e417fac | 83 | spi.frequency(100000); //clock frequency |
gkumar | 0:ac874e417fac | 84 | |
gkumar | 0:ac874e417fac | 85 | SSN_MAG=0; // Selecting pin |
gkumar | 0:ac874e417fac | 86 | wait_ms(100); //accounts for delay.can be minimised. |
gkumar | 0:ac874e417fac | 87 | |
gkumar | 0:ac874e417fac | 88 | spi.write(0x83); // |
gkumar | 0:ac874e417fac | 89 | |
gkumar | 0:ac874e417fac | 90 | wait_ms(100); |
gkumar | 0:ac874e417fac | 91 | |
gkumar | 0:ac874e417fac | 92 | unsigned char i; |
gkumar | 0:ac874e417fac | 93 | for(i=0;i<3;i++)//initialising values. |
gkumar | 0:ac874e417fac | 94 | { |
gkumar | 0:ac874e417fac | 95 | spi.write(0x00); //MSB of X,y,Z |
gkumar | 0:ac874e417fac | 96 | spi.write(0xc8); //LSB of X,Y,z;pointer increases automatically. |
gkumar | 0:ac874e417fac | 97 | } |
gkumar | 0:ac874e417fac | 98 | SSN_MAG=1; |
gkumar | 0:ac874e417fac | 99 | |
gkumar | 0:ac874e417fac | 100 | } |
gkumar | 0:ac874e417fac | 101 | |
gkumar | 0:ac874e417fac | 102 | void FUNC_ACS_MAGNETOMETER_EXECUTE() |
gkumar | 0:ac874e417fac | 103 | { |
gkumar | 0:ac874e417fac | 104 | SSN_MAG=0; //enabling slave to measure the values |
gkumar | 0:ac874e417fac | 105 | wait_ms(100); |
gkumar | 0:ac874e417fac | 106 | spi.write(0x82); //initiates measurement |
gkumar | 0:ac874e417fac | 107 | wait_ms(100); |
gkumar | 0:ac874e417fac | 108 | spi.write(0x01); //selecting x,y and z axes, measurement starts now |
gkumar | 0:ac874e417fac | 109 | SSN_MAG=1; |
gkumar | 0:ac874e417fac | 110 | |
gkumar | 0:ac874e417fac | 111 | |
gkumar | 0:ac874e417fac | 112 | trflag_mag=1; |
gkumar | 0:ac874e417fac | 113 | tr_mag.attach(&trsub_mag,1); |
gkumar | 0:ac874e417fac | 114 | //a=0; |
gkumar | 0:ac874e417fac | 115 | // while(trflag_mag) |
gkumar | 0:ac874e417fac | 116 | //{ |
gkumar | 0:ac874e417fac | 117 | |
gkumar | 0:ac874e417fac | 118 | //if(a==1) |
gkumar | 0:ac874e417fac | 119 | |
gkumar | 0:ac874e417fac | 120 | // } |
gkumar | 0:ac874e417fac | 121 | pc.printf("exit"); |
gkumar | 0:ac874e417fac | 122 | |
gkumar | 0:ac874e417fac | 123 | } |