![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
cdms i2c working - but not working after hk data is sent
Fork of pcb_test_vr1_1_2 by
mnm.cpp@0:e91ee0e99213, 2015-04-07 (annotated)
- Committer:
- sakthipriya
- Date:
- Tue Apr 07 16:11:54 2015 +0000
- Revision:
- 0:e91ee0e99213
acs integration working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:e91ee0e99213 | 1 | #include<mbed.h> |
sakthipriya | 0:e91ee0e99213 | 2 | #include "mnm.h" |
sakthipriya | 0:e91ee0e99213 | 3 | #include "pin_config.h" |
sakthipriya | 0:e91ee0e99213 | 4 | #include "pni.h" //pni header file |
sakthipriya | 0:e91ee0e99213 | 5 | Serial mnm(USBTX,USBRX); //for usb communication |
sakthipriya | 0:e91ee0e99213 | 6 | I2C i2c (PIN85,PIN84); //PTC9-sda,PTC8-scl |
sakthipriya | 0:e91ee0e99213 | 7 | /*void INIT_PNI(void); //initialization of registers happens |
sakthipriya | 0:e91ee0e99213 | 8 | float *EXECUTE_PNI(); //data is obtained |
sakthipriya | 0:e91ee0e99213 | 9 | void T_OUT(); //timeout function to stop infinite loop*/ |
sakthipriya | 0:e91ee0e99213 | 10 | Timeout to; //Timeout variable to |
sakthipriya | 0:e91ee0e99213 | 11 | int toFlag; |
sakthipriya | 0:e91ee0e99213 | 12 | void T_OUT() |
sakthipriya | 0:e91ee0e99213 | 13 | { |
sakthipriya | 0:e91ee0e99213 | 14 | toFlag=0; //as T_OUT function gets called the while loop gets terminated |
sakthipriya | 0:e91ee0e99213 | 15 | } |
sakthipriya | 0:e91ee0e99213 | 16 | |
sakthipriya | 0:e91ee0e99213 | 17 | //DEFINING VARIABLES |
sakthipriya | 0:e91ee0e99213 | 18 | char cmd[2]; |
sakthipriya | 0:e91ee0e99213 | 19 | char raw_gyro[6]; |
sakthipriya | 0:e91ee0e99213 | 20 | char raw_mag[6]; |
sakthipriya | 0:e91ee0e99213 | 21 | char store,status; |
sakthipriya | 0:e91ee0e99213 | 22 | int16_t bit_data; |
sakthipriya | 0:e91ee0e99213 | 23 | float gyro_data[3], mag_data[3],combined_values[6],*data; |
sakthipriya | 0:e91ee0e99213 | 24 | float senstivity_gyro =6.5536; //senstivity is obtained from 2^15/5000dps |
sakthipriya | 0:e91ee0e99213 | 25 | float senstivity_mag =32.768; //senstivity is obtained from 2^15/1000microtesla |
sakthipriya | 0:e91ee0e99213 | 26 | float gyro_error[3]= {0,0,0}, mag_error[3]= {0,0,0}; |
sakthipriya | 0:e91ee0e99213 | 27 | |
sakthipriya | 0:e91ee0e99213 | 28 | Timer r0; |
sakthipriya | 0:e91ee0e99213 | 29 | Timer r1; |
sakthipriya | 0:e91ee0e99213 | 30 | Timer r2; |
sakthipriya | 0:e91ee0e99213 | 31 | Timer r3; |
sakthipriya | 0:e91ee0e99213 | 32 | Timer r4; |
sakthipriya | 0:e91ee0e99213 | 33 | |
sakthipriya | 0:e91ee0e99213 | 34 | |
sakthipriya | 0:e91ee0e99213 | 35 | /* |
sakthipriya | 0:e91ee0e99213 | 36 | int main(void) |
sakthipriya | 0:e91ee0e99213 | 37 | { |
sakthipriya | 0:e91ee0e99213 | 38 | |
sakthipriya | 0:e91ee0e99213 | 39 | INIT_PNI(); |
sakthipriya | 0:e91ee0e99213 | 40 | data=EXECUTE_PNI(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3 |
sakthipriya | 0:e91ee0e99213 | 41 | mnm.printf("gyro values\n"); //printing the angular velocity and magnetic field values |
sakthipriya | 0:e91ee0e99213 | 42 | for(int i=0; i<3; i++) { |
sakthipriya | 0:e91ee0e99213 | 43 | mnm.printf("%f\t",data[i]); |
sakthipriya | 0:e91ee0e99213 | 44 | } |
sakthipriya | 0:e91ee0e99213 | 45 | mnm.printf("mag values\n"); |
sakthipriya | 0:e91ee0e99213 | 46 | for(int i=3; i<6; i++) { |
sakthipriya | 0:e91ee0e99213 | 47 | mnm.printf("%f\t",data[i]); |
sakthipriya | 0:e91ee0e99213 | 48 | } |
sakthipriya | 0:e91ee0e99213 | 49 | }*/ |
sakthipriya | 0:e91ee0e99213 | 50 | |
sakthipriya | 0:e91ee0e99213 | 51 | void INIT_PNI() |
sakthipriya | 0:e91ee0e99213 | 52 | { r0.start(); |
sakthipriya | 0:e91ee0e99213 | 53 | cmd[0]=RESETREQ; |
sakthipriya | 0:e91ee0e99213 | 54 | cmd[1]=BIT_RESREQ; |
sakthipriya | 0:e91ee0e99213 | 55 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 0:e91ee0e99213 | 56 | r0.stop(); //When 0x01 is written in reset request register Emulates a hard power down/power up |
sakthipriya | 0:e91ee0e99213 | 57 | wait_ms(2000); //waiting for loading configuration file stored in EEPROM |
sakthipriya | 0:e91ee0e99213 | 58 | cmd[0]=SENTRALSTATUS; |
sakthipriya | 0:e91ee0e99213 | 59 | r1.start(); |
sakthipriya | 0:e91ee0e99213 | 60 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 0:e91ee0e99213 | 61 | i2c.read(SLAVE_ADDR_READ,&store,1); |
sakthipriya | 0:e91ee0e99213 | 62 | r1.stop(); |
sakthipriya | 0:e91ee0e99213 | 63 | wait_ms(100); |
sakthipriya | 0:e91ee0e99213 | 64 | //to check whether EEPROM is uploaded |
sakthipriya | 0:e91ee0e99213 | 65 | |
sakthipriya | 0:e91ee0e99213 | 66 | /* switch((int)store) { |
sakthipriya | 0:e91ee0e99213 | 67 | case(3): { |
sakthipriya | 0:e91ee0e99213 | 68 | printf("\nstore :%d\n",store); |
sakthipriya | 0:e91ee0e99213 | 69 | break; |
sakthipriya | 0:e91ee0e99213 | 70 | } |
sakthipriya | 0:e91ee0e99213 | 71 | case(11): { |
sakthipriya | 0:e91ee0e99213 | 72 | printf("\nstore11 :%d\n",store); |
sakthipriya | 0:e91ee0e99213 | 73 | break; |
sakthipriya | 0:e91ee0e99213 | 74 | } |
sakthipriya | 0:e91ee0e99213 | 75 | default: { |
sakthipriya | 0:e91ee0e99213 | 76 | cmd[0]=RESETREQ; |
sakthipriya | 0:e91ee0e99213 | 77 | cmd[1]=BIT_RESREQ; |
sakthipriya | 0:e91ee0e99213 | 78 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 0:e91ee0e99213 | 79 | wait_ms(2000); |
sakthipriya | 0:e91ee0e99213 | 80 | } |
sakthipriya | 0:e91ee0e99213 | 81 | }*/ |
sakthipriya | 0:e91ee0e99213 | 82 | //mnm.printf("Sentral Status is %x\n",(int)store); |
sakthipriya | 0:e91ee0e99213 | 83 | r2.start(); |
sakthipriya | 0:e91ee0e99213 | 84 | cmd[0]=HOST_CTRL; //0x01 is written in HOST CONTROL register to enable the sensors |
sakthipriya | 0:e91ee0e99213 | 85 | cmd[1]=BIT_RUN_ENB; |
sakthipriya | 0:e91ee0e99213 | 86 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 0:e91ee0e99213 | 87 | r2.stop(); |
sakthipriya | 0:e91ee0e99213 | 88 | wait_ms(100); |
sakthipriya | 0:e91ee0e99213 | 89 | r3.start(); |
sakthipriya | 0:e91ee0e99213 | 90 | cmd[0]=MAGRATE; //Output data rate of 100Hz is used for magnetometer |
sakthipriya | 0:e91ee0e99213 | 91 | cmd[1]=BIT_MAGODR; |
sakthipriya | 0:e91ee0e99213 | 92 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 0:e91ee0e99213 | 93 | r3.stop(); |
sakthipriya | 0:e91ee0e99213 | 94 | wait_ms(100); |
sakthipriya | 0:e91ee0e99213 | 95 | r4.start(); |
sakthipriya | 0:e91ee0e99213 | 96 | cmd[0]=GYRORATE; //Output data rate of 150Hz is used for gyroscope |
sakthipriya | 0:e91ee0e99213 | 97 | cmd[1]=BIT_GYROODR; |
sakthipriya | 0:e91ee0e99213 | 98 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 0:e91ee0e99213 | 99 | r4.stop(); |
sakthipriya | 0:e91ee0e99213 | 100 | wait_ms(100); |
sakthipriya | 0:e91ee0e99213 | 101 | cmd[0]=ALGO_CTRL; //When 0x00 is written to ALGO CONTROL register we get scaled sensor values |
sakthipriya | 0:e91ee0e99213 | 102 | cmd[1]=0x00; |
sakthipriya | 0:e91ee0e99213 | 103 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 0:e91ee0e99213 | 104 | wait_ms(100); |
sakthipriya | 0:e91ee0e99213 | 105 | cmd[0]=ENB_EVT; //enabling the error,gyro values and magnetometer values |
sakthipriya | 0:e91ee0e99213 | 106 | cmd[1]=BIT_EVT_ENB; |
sakthipriya | 0:e91ee0e99213 | 107 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 0:e91ee0e99213 | 108 | wait_ms(100); |
sakthipriya | 0:e91ee0e99213 | 109 | printf("\n \r %d %d %d %d %d",r0.read_us(),r1.read_us(),r2.read_us(),r3.read_us(),r4.read_us()); |
sakthipriya | 0:e91ee0e99213 | 110 | } |
sakthipriya | 0:e91ee0e99213 | 111 | |
sakthipriya | 0:e91ee0e99213 | 112 | float *EXECUTE_PNI() |
sakthipriya | 0:e91ee0e99213 | 113 | { |
sakthipriya | 0:e91ee0e99213 | 114 | //printf("\n\r mnm func \n"); |
sakthipriya | 0:e91ee0e99213 | 115 | toFlag=1; //toFlag is set to 1 so that it enters while loop |
sakthipriya | 0:e91ee0e99213 | 116 | to.attach(&T_OUT,2); //after 2 seconds the while loop gets terminated |
sakthipriya | 0:e91ee0e99213 | 117 | while(toFlag) { |
sakthipriya | 0:e91ee0e99213 | 118 | cmd[0]=EVT_STATUS; |
sakthipriya | 0:e91ee0e99213 | 119 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 0:e91ee0e99213 | 120 | i2c.read(SLAVE_ADDR_READ,&status,1); |
sakthipriya | 0:e91ee0e99213 | 121 | wait_ms(100); |
sakthipriya | 0:e91ee0e99213 | 122 | //mnm.printf("\nEvent Status is %x\n",(int)status); |
sakthipriya | 0:e91ee0e99213 | 123 | //if the 6th and 4th bit are 1 then it implies that gyro and magnetometer values are ready to take |
sakthipriya | 0:e91ee0e99213 | 124 | if(((int)status&40)==40) { |
sakthipriya | 0:e91ee0e99213 | 125 | printf("\nin if of mnm\n"); |
sakthipriya | 0:e91ee0e99213 | 126 | cmd[0]=GYRO_XOUT_H; //0x22 gyro LSB of x |
sakthipriya | 0:e91ee0e99213 | 127 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 0:e91ee0e99213 | 128 | i2c.read(SLAVE_ADDR_READ,raw_gyro,6); |
sakthipriya | 0:e91ee0e99213 | 129 | cmd[0]=MAG_XOUT_H; //LSB of x |
sakthipriya | 0:e91ee0e99213 | 130 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 0:e91ee0e99213 | 131 | i2c.read(SLAVE_ADDR_READ,raw_mag,6); |
sakthipriya | 0:e91ee0e99213 | 132 | //mnm.printf("\nGyro Values:\n"); |
sakthipriya | 0:e91ee0e99213 | 133 | for(int i=0; i<3; i++) { |
sakthipriya | 0:e91ee0e99213 | 134 | //concatenating gyro LSB and MSB to get 16 bit signed data values |
sakthipriya | 0:e91ee0e99213 | 135 | bit_data= ((int16_t)raw_gyro[2*i+1]<<8)|(int16_t)raw_gyro[2*i]; |
sakthipriya | 0:e91ee0e99213 | 136 | gyro_data[i]=(float)bit_data; |
sakthipriya | 0:e91ee0e99213 | 137 | gyro_data[i]=gyro_data[i]/senstivity_gyro; |
sakthipriya | 0:e91ee0e99213 | 138 | gyro_data[i]+=gyro_error[i]; |
sakthipriya | 0:e91ee0e99213 | 139 | //mnm.printf("%f\t",gyro_data[i]); |
sakthipriya | 0:e91ee0e99213 | 140 | } |
sakthipriya | 0:e91ee0e99213 | 141 | //mnm.printf("\nMag Values:\n"); |
sakthipriya | 0:e91ee0e99213 | 142 | for(int i=0; i<3; i++) { |
sakthipriya | 0:e91ee0e99213 | 143 | //concatenating mag LSB and MSB to get 16 bit signed data values |
sakthipriya | 0:e91ee0e99213 | 144 | bit_data= ((int16_t)raw_mag[2*i+1]<<8)|(int16_t)raw_mag[2*i]; |
sakthipriya | 0:e91ee0e99213 | 145 | mag_data[i]=(float)bit_data; |
sakthipriya | 0:e91ee0e99213 | 146 | mag_data[i]=mag_data[i]/senstivity_mag; |
sakthipriya | 0:e91ee0e99213 | 147 | mag_data[i]+=mag_error[i]; |
sakthipriya | 0:e91ee0e99213 | 148 | //mnm.printf("%f\t",mag_data[i]); |
sakthipriya | 0:e91ee0e99213 | 149 | } |
sakthipriya | 0:e91ee0e99213 | 150 | for(int i=0; i<3; i++) { |
sakthipriya | 0:e91ee0e99213 | 151 | combined_values[i]=gyro_data[i]; |
sakthipriya | 0:e91ee0e99213 | 152 | combined_values[i+3]=mag_data[i]; |
sakthipriya | 0:e91ee0e99213 | 153 | } |
sakthipriya | 0:e91ee0e99213 | 154 | return(combined_values); //returning poiter combined values |
sakthipriya | 0:e91ee0e99213 | 155 | } |
sakthipriya | 0:e91ee0e99213 | 156 | //checking for the error |
sakthipriya | 0:e91ee0e99213 | 157 | |
sakthipriya | 0:e91ee0e99213 | 158 | else if (((int)status&2)==2) { |
sakthipriya | 0:e91ee0e99213 | 159 | INIT_PNI(); //when there is any error then Again inilization is done to remove error |
sakthipriya | 0:e91ee0e99213 | 160 | } |
sakthipriya | 0:e91ee0e99213 | 161 | |
sakthipriya | 0:e91ee0e99213 | 162 | |
sakthipriya | 0:e91ee0e99213 | 163 | } |
sakthipriya | 0:e91ee0e99213 | 164 | } |