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