lightradar

Dependencies:   TextLCD mbed

Fork of Laster_Radar_test by Christopher H

Committer:
chrish
Date:
Mon Jun 22 08:52:27 2015 +0000
Revision:
0:36109edfc712
Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chrish 0:36109edfc712 1 #include "I2C.h"
chrish 0:36109edfc712 2 //#include "lcd.h"
chrish 0:36109edfc712 3 //#include "stm32f4xx_i2c.h"
chrish 0:36109edfc712 4 //#include "stm32f4xx_gpio.h"
chrish 0:36109edfc712 5 //#include "stm32f4xx_rcc.h"
chrish 0:36109edfc712 6 BMP180_CAL_DATA_TYPEDEF BMP180_CAL_DATA;
chrish 0:36109edfc712 7 PRESSURE_DATA_TYPEDEF PRESSURE_DATA;
chrish 0:36109edfc712 8
chrish 0:36109edfc712 9
chrish 0:36109edfc712 10 void PRESSURE_vInit(void){
chrish 0:36109edfc712 11
chrish 0:36109edfc712 12 // uint32_t temp;
chrish 0:36109edfc712 13 uint32_t temp = 0x00;
chrish 0:36109edfc712 14 uint32_t temp_2 = 0x00;
chrish 0:36109edfc712 15 #if 0
chrish 0:36109edfc712 16 RCC->AHB1ENR |= Bit1 ; // GPIOB ENABLE
chrish 0:36109edfc712 17 RCC->APB1ENR |= Bit21 ; // I2C1 clock enable
chrish 0:36109edfc712 18
chrish 0:36109edfc712 19 GPIOB->MODER |= Bit17 | Bit19 ; // SET PB8 & PB9 As alternating function
chrish 0:36109edfc712 20 GPIOB->MODER &= ~(Bit16 | Bit18);
chrish 0:36109edfc712 21 GPIOB->AFR[1] |= Bit2 | Bit6 ; // PB8 & PB9 AF4
chrish 0:36109edfc712 22 GPIOB->AFR[1] &= ~(Bit0 | Bit1 | Bit3 |
chrish 0:36109edfc712 23 Bit4 | Bit5 | Bit7 );
chrish 0:36109edfc712 24 GPIOB->OTYPER |= Bit8 | Bit9 ; // OPEN TRAIN
chrish 0:36109edfc712 25
chrish 0:36109edfc712 26
chrish 0:36109edfc712 27
chrish 0:36109edfc712 28 I2C1->CR2 = 0x0000; // RESET VALUE
chrish 0:36109edfc712 29 //I2C1->CR2 |= Bit1 ; // PHERIVERIAL CLOCK = 2MHz
chrish 0:36109edfc712 30 I2C1->CR2 |= 0b101010; // SET APB SPEEZ TO 42 MHZ
chrish 0:36109edfc712 31
chrish 0:36109edfc712 32 I2C1->CCR = 0x0000; // RESET VALUE AND SM MODE // SLOT DUTY MODE
chrish 0:36109edfc712 33
chrish 0:36109edfc712 34 #if 1
chrish 0:36109edfc712 35 /**GESCHWINDIGKEIT SLOW MODE 100000 HZ*/
chrish 0:36109edfc712 36 I2C1->CCR |= 420; // GESCHWINDIGKEIT 100 000 Hz (42Mhz / 100000)
chrish 0:36109edfc712 37
chrish 0:36109edfc712 38 I2C1->TRISE = 43; // MAXIMUM RISE TIME AT 42 MHZ
chrish 0:36109edfc712 39 // I2C1->TRISE |= Bit3 | Bit0 ;
chrish 0:36109edfc712 40
chrish 0:36109edfc712 41 #else
chrish 0:36109edfc712 42 /**GESCHWINDIGKEIT 350 kHz FAST MODE MAX EINSTELLBAR IN PLL CONFIGURATION (MAX 400k nach datenblatt)*/
chrish 0:36109edfc712 43 I2C1->CCR |= Bit15 ; // FAST MODE
chrish 0:36109edfc712 44 I2C1->CCR |= 112 ; // FREQUENCY 375 kHz (42Mhz / 350000Hz) = 120
chrish 0:36109edfc712 45 I2C1->TRISE = 13; // MAXIMUM RISE TIME
chrish 0:36109edfc712 46 #endif
chrish 0:36109edfc712 47
chrish 0:36109edfc712 48
chrish 0:36109edfc712 49 I2C1->CR1 = 0x0000;
chrish 0:36109edfc712 50 I2C1->CR1 |= Bit10; // AKNOLAGE ENABLE
chrish 0:36109edfc712 51
chrish 0:36109edfc712 52
chrish 0:36109edfc712 53 I2C1->CR1 |= Bit0 ; // ENABLE
chrish 0:36109edfc712 54 I2C1->OAR1 |= Bit15;
chrish 0:36109edfc712 55
chrish 0:36109edfc712 56 #else
chrish 0:36109edfc712 57
chrish 0:36109edfc712 58
chrish 0:36109edfc712 59
chrish 0:36109edfc712 60 RCC->AHB1ENR |= Bit1 ; // GPIOB ENABLE
chrish 0:36109edfc712 61 RCC->APB1ENR |= Bit21 ; // I2C1 clock enable
chrish 0:36109edfc712 62
chrish 0:36109edfc712 63
chrish 0:36109edfc712 64
chrish 0:36109edfc712 65
chrish 0:36109edfc712 66 GPIOB->MODER |= Bit17 | Bit19 ; // SET PB8 & PB9 As alternating function
chrish 0:36109edfc712 67 GPIOB->MODER &= ~(Bit16 | Bit18);
chrish 0:36109edfc712 68 //temp = 0;
chrish 0:36109edfc712 69 //temp
chrish 0:36109edfc712 70
chrish 0:36109edfc712 71 GPIOB->OTYPER |= Bit8 | Bit9 ; // OPEN TRAIN
chrish 0:36109edfc712 72 GPIOB->OSPEEDR |= Bit17 | Bit19 ;
chrish 0:36109edfc712 73 GPIOB->OSPEEDR &= ~(Bit16 | Bit18 );
chrish 0:36109edfc712 74
chrish 0:36109edfc712 75 GPIOB->PUPDR &= ~( Bit16 | Bit17 | Bit18 | Bit19 );
chrish 0:36109edfc712 76
chrish 0:36109edfc712 77 GPIOB->PUPDR |= Bit16 | Bit18 ;
chrish 0:36109edfc712 78
chrish 0:36109edfc712 79
chrish 0:36109edfc712 80 //GPIOB->AFR[1] |= Bit2 | Bit6 ; // PB8 & PB9 AF4
chrish 0:36109edfc712 81 //GPIOB->AFR[1] &= ~(Bit0 | Bit1 | Bit3 |
chrish 0:36109edfc712 82 // Bit4 | Bit5 | Bit7 );
chrish 0:36109edfc712 83
chrish 0:36109edfc712 84 //PIN8
chrish 0:36109edfc712 85 temp = ((uint32_t)(0x04) << ((uint32_t)((uint32_t)0x08 & (uint32_t)0x07) * 4)) ;
chrish 0:36109edfc712 86 GPIOB->AFR[0x08 >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)0x08 & (uint32_t)0x07) * 4)) ;
chrish 0:36109edfc712 87 temp_2 = GPIOB->AFR[0x08 >> 0x03] | temp;
chrish 0:36109edfc712 88 GPIOB->AFR[0x08 >> 0x03] = temp_2;
chrish 0:36109edfc712 89
chrish 0:36109edfc712 90
chrish 0:36109edfc712 91 //PIN9
chrish 0:36109edfc712 92 temp = ((uint32_t)(0x04) << ((uint32_t)((uint32_t)0x09 & (uint32_t)0x07) * 4)) ;
chrish 0:36109edfc712 93 GPIOB->AFR[0x09 >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)0x09 & (uint32_t)0x07) * 4)) ;
chrish 0:36109edfc712 94 temp_2 = GPIOB->AFR[0x09 >> 0x03] | temp;
chrish 0:36109edfc712 95 GPIOB->AFR[0x09 >> 0x03] = temp_2;
chrish 0:36109edfc712 96
chrish 0:36109edfc712 97
chrish 0:36109edfc712 98
chrish 0:36109edfc712 99
chrish 0:36109edfc712 100
chrish 0:36109edfc712 101
chrish 0:36109edfc712 102
chrish 0:36109edfc712 103 I2C1->CR2 = 0x0000; // RESET VALUE
chrish 0:36109edfc712 104 //I2C1->CR2 |= Bit1 ; // PHERIVERIAL CLOCK = 2MHz
chrish 0:36109edfc712 105 //I2C1->CR2 |= 0b101010; // SET APB SPEEZ TO 42 MHZ
chrish 0:36109edfc712 106 I2C1->CR2 |= 0x2A; // SET APB SPEEZ TO 42 MHZ
chrish 0:36109edfc712 107 I2C1->CCR = 0x0000; // RESET VALUE AND SM MODE // SLOT DUTY MODE
chrish 0:36109edfc712 108
chrish 0:36109edfc712 109 #if 1
chrish 0:36109edfc712 110 /**GESCHWINDIGKEIT SLOW MODE 100000 HZ*/
chrish 0:36109edfc712 111 I2C1->CCR |= 420; // GESCHWINDIGKEIT 100 000 Hz (42Mhz / 100000)
chrish 0:36109edfc712 112
chrish 0:36109edfc712 113 I2C1->TRISE = 43; // MAXIMUM RISE TIME AT 42 MHZ
chrish 0:36109edfc712 114 // I2C1->TRISE |= Bit3 | Bit0 ;
chrish 0:36109edfc712 115
chrish 0:36109edfc712 116 #else
chrish 0:36109edfc712 117 /**GESCHWINDIGKEIT 350 kHz FAST MODE MAX EINSTELLBAR IN PLL CONFIGURATION (MAX 400k nach datenblatt)*/
chrish 0:36109edfc712 118 I2C1->CCR |= Bit15 ; // FAST MODE
chrish 0:36109edfc712 119 I2C1->CCR |= 112 ; // FREQUENCY 375 kHz (42Mhz / 350000Hz) = 120
chrish 0:36109edfc712 120 I2C1->TRISE = 13; // MAXIMUM RISE TIME
chrish 0:36109edfc712 121 #endif
chrish 0:36109edfc712 122
chrish 0:36109edfc712 123
chrish 0:36109edfc712 124 I2C1->CR1 = 0x0000;
chrish 0:36109edfc712 125 I2C1->CR1 |= Bit10; // AKNOLAGE ENABLE
chrish 0:36109edfc712 126
chrish 0:36109edfc712 127
chrish 0:36109edfc712 128 I2C1->CR1 |= Bit0 ; // ENABLE
chrish 0:36109edfc712 129 I2C1->OAR1 |= Bit15;
chrish 0:36109edfc712 130
chrish 0:36109edfc712 131
chrish 0:36109edfc712 132
chrish 0:36109edfc712 133 #endif
chrish 0:36109edfc712 134
chrish 0:36109edfc712 135
chrish 0:36109edfc712 136
chrish 0:36109edfc712 137 //PRESSURE_vget_CALDATA();
chrish 0:36109edfc712 138 }
chrish 0:36109edfc712 139
chrish 0:36109edfc712 140
chrish 0:36109edfc712 141 void PRESSURE_vTASK(unsigned long bID){
chrish 0:36109edfc712 142 switch(PRESSURE_DATA.b_TASK_STATEMASCHINE){
chrish 0:36109edfc712 143 case 0:
chrish 0:36109edfc712 144 /**START TEMPERATUR MEASUREMENT*/
chrish 0:36109edfc712 145 PRESSURE_vwrite_EEPROM_REGISTER(BMP180_register_CONTROLL,0x2E); // TEMPERATUR MEASUREMENT
chrish 0:36109edfc712 146 PRESSURE_DATA.b_TASK_STATEMASCHINE = 1;
chrish 0:36109edfc712 147 break;
chrish 0:36109edfc712 148 case 1:
chrish 0:36109edfc712 149 /** REAT TEMP_DATA*/
chrish 0:36109edfc712 150 PRESSURE_DATA.i_RAW_TEMPERATUR = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_ADC_ERG);
chrish 0:36109edfc712 151 PRESSURE_DATA.b_TASK_STATEMASCHINE = 2;
chrish 0:36109edfc712 152 break;
chrish 0:36109edfc712 153 case 2:
chrish 0:36109edfc712 154 /**START PRESSURE MEASUREMENT*/
chrish 0:36109edfc712 155 PRESSURE_vwrite_EEPROM_REGISTER(BMP180_register_CONTROLL,0xB4); // HIGH RESULUTUIN
chrish 0:36109edfc712 156 PRESSURE_DATA.b_TASK_STATEMASCHINE = 3;
chrish 0:36109edfc712 157 break;
chrish 0:36109edfc712 158 case 3:
chrish 0:36109edfc712 159 /**REAT PRESSURE DATA*/
chrish 0:36109edfc712 160 PRESSURE_DATA.i_RAW_PRESSURE = ( (PRESSURE_vread_EEPROM_REGISTER(BMP180_register_ADC_ERG))<<8 ) >>(8-BMP180_oss_high_resulution );
chrish 0:36109edfc712 161 //PRESSURE_DATA.b_TASK_STATEMASCHINE = 2;
chrish 0:36109edfc712 162
chrish 0:36109edfc712 163 PRESSURE_DATA.b_TASK_STATEMASCHINE = 4;
chrish 0:36109edfc712 164 break;
chrish 0:36109edfc712 165 case 4:
chrish 0:36109edfc712 166 /**KALCULATE TEMPERATUR AND PRESSURE*/
chrish 0:36109edfc712 167 PRESSURE_vCALCULATE_TEMPANDPRESSURE();
chrish 0:36109edfc712 168 PRESSURE_DATA.b_TASK_STATEMASCHINE = 0;
chrish 0:36109edfc712 169 break;
chrish 0:36109edfc712 170 default: PRESSURE_DATA.b_TASK_STATEMASCHINE = 0;
chrish 0:36109edfc712 171 }
chrish 0:36109edfc712 172
chrish 0:36109edfc712 173
chrish 0:36109edfc712 174 }
chrish 0:36109edfc712 175
chrish 0:36109edfc712 176
chrish 0:36109edfc712 177 void PRESSURE_vCALCULATE_TEMPANDPRESSURE(void){
chrish 0:36109edfc712 178 long X1,X2,B5,B6,X3,B3,p ;
chrish 0:36109edfc712 179 unsigned long B4,B7;
chrish 0:36109edfc712 180
chrish 0:36109edfc712 181 X1 = ( PRESSURE_DATA.i_RAW_TEMPERATUR - BMP180_CAL_DATA.AC6 ) * BMP180_CAL_DATA.AC5 / 0x8000 ;
chrish 0:36109edfc712 182 X2 = ( BMP180_CAL_DATA.MC * 0x800 )/(X1 + BMP180_CAL_DATA.MD);
chrish 0:36109edfc712 183 B5 = ( X1 + X2 );
chrish 0:36109edfc712 184
chrish 0:36109edfc712 185 PRESSURE_DATA.i_TEMPERATURE = ( B5 + 8 )/ 0x10;
chrish 0:36109edfc712 186
chrish 0:36109edfc712 187 B6 = B5 - 4000;
chrish 0:36109edfc712 188
chrish 0:36109edfc712 189 X1 = ( BMP180_CAL_DATA.B2 *( B6*B6/ 0x1000 )) /0x800;
chrish 0:36109edfc712 190
chrish 0:36109edfc712 191 X2 = ( BMP180_CAL_DATA.AC2 * B6)/0x800;
chrish 0:36109edfc712 192
chrish 0:36109edfc712 193 X3 = X1 + X2;
chrish 0:36109edfc712 194
chrish 0:36109edfc712 195 B3 = ((( BMP180_CAL_DATA.AC1 * 4 + X3) << BMP180_oss_high_resulution)+2)/4;
chrish 0:36109edfc712 196
chrish 0:36109edfc712 197 X1 = BMP180_CAL_DATA.AC3 * B6 / 0x2000;
chrish 0:36109edfc712 198
chrish 0:36109edfc712 199 X2 = ( BMP180_CAL_DATA.B1 * ( B6*B6 / 0x1000))/ 0x10000;
chrish 0:36109edfc712 200
chrish 0:36109edfc712 201 X3 = ((X1+X2)+2)/ 0x4;
chrish 0:36109edfc712 202
chrish 0:36109edfc712 203 B4 = BMP180_CAL_DATA.AC4 * (unsigned long)(X3 + 32768)/ 0x8000 ;
chrish 0:36109edfc712 204
chrish 0:36109edfc712 205 B7 = ( (unsigned long) PRESSURE_DATA.i_RAW_PRESSURE - B3)*(50000 >> BMP180_oss_high_resulution);
chrish 0:36109edfc712 206
chrish 0:36109edfc712 207 //p = (B7 / B4 )*2;
chrish 0:36109edfc712 208 if( B7 < 0x80000000 ) p=(B7 * 2 )/B4;
chrish 0:36109edfc712 209 else p = (B7 / B4 )*2;
chrish 0:36109edfc712 210
chrish 0:36109edfc712 211 X1 = ( p/0x100)*(p/0x100);
chrish 0:36109edfc712 212
chrish 0:36109edfc712 213 X1 = (X1 * 3038)/ 0x10000;
chrish 0:36109edfc712 214
chrish 0:36109edfc712 215 X2 = (-7357 * p)/ 0x10000;
chrish 0:36109edfc712 216
chrish 0:36109edfc712 217 p = p + (X1 + X2 + 3791) / 0x10 ;
chrish 0:36109edfc712 218
chrish 0:36109edfc712 219 PRESSURE_DATA.i_PRESSURE = p;
chrish 0:36109edfc712 220 }
chrish 0:36109edfc712 221
chrish 0:36109edfc712 222
chrish 0:36109edfc712 223
chrish 0:36109edfc712 224
chrish 0:36109edfc712 225 void PRESSURE_vget_CALDATA(void){
chrish 0:36109edfc712 226 //LCD_vDrawString("PRESSURE-i2C2\0",100,80,1,0x0000);
chrish 0:36109edfc712 227
chrish 0:36109edfc712 228 BMP180_CAL_DATA.AC1 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_AC1);
chrish 0:36109edfc712 229 //LCD_vDrawString("PRESSURE-i2C3\0",100,60,1,0x0000);
chrish 0:36109edfc712 230
chrish 0:36109edfc712 231 BMP180_CAL_DATA.AC2 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_AC2);
chrish 0:36109edfc712 232 BMP180_CAL_DATA.AC3 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_AC3);
chrish 0:36109edfc712 233 BMP180_CAL_DATA.AC4 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_AC4);
chrish 0:36109edfc712 234 BMP180_CAL_DATA.AC5 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_AC5);
chrish 0:36109edfc712 235 BMP180_CAL_DATA.AC6 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_AC6);
chrish 0:36109edfc712 236 BMP180_CAL_DATA.B1 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_B1);
chrish 0:36109edfc712 237 BMP180_CAL_DATA.B2 = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_B2);
chrish 0:36109edfc712 238 BMP180_CAL_DATA.MB = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_MB);
chrish 0:36109edfc712 239 BMP180_CAL_DATA.MC = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_MC);
chrish 0:36109edfc712 240 BMP180_CAL_DATA.MD = PRESSURE_vread_EEPROM_REGISTER(BMP180_register_MD);
chrish 0:36109edfc712 241 }
chrish 0:36109edfc712 242
chrish 0:36109edfc712 243
chrish 0:36109edfc712 244 /***EVT NOCH EIN ABSTURZ KRITERIUM */
chrish 0:36109edfc712 245 long PRESSURE_vread_EEPROM_REGISTER(byte b_register){
chrish 0:36109edfc712 246
chrish 0:36109edfc712 247 /**EINLESEN VON ADC WERTEN ODER EEPORM DATEN */
chrish 0:36109edfc712 248 uint16_t local;
chrish 0:36109edfc712 249 uint16_t MSB,LSB;
chrish 0:36109edfc712 250 I2C1->CR1 |= Bit8; // ERZEUGEN STARTKONDITION
chrish 0:36109edfc712 251 //LCD_vDrawString("START\0",2,40,1,0x0000);
chrish 0:36109edfc712 252
chrish 0:36109edfc712 253 while((I2C1->SR1 & Bit0) == 0 ); // WHAITING START BIT GENERAITET
chrish 0:36109edfc712 254 I2C1->DR = BMP180_adress_write;
chrish 0:36109edfc712 255
chrish 0:36109edfc712 256 //LCD_vDrawString("START-G\0",50,40,1,0x0000);
chrish 0:36109edfc712 257
chrish 0:36109edfc712 258
chrish 0:36109edfc712 259 while((I2C1->SR1 & Bit1) == 0 ); // WHAITING ADDR = 1
chrish 0:36109edfc712 260
chrish 0:36109edfc712 261 local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 262
chrish 0:36109edfc712 263 I2C1->DR = b_register;
chrish 0:36109edfc712 264
chrish 0:36109edfc712 265 while( ((I2C1->SR1 & Bit7) == 0) && ((I2C1->SR1 & Bit2) == 0 )); // WHAITING Data register Empte
chrish 0:36109edfc712 266
chrish 0:36109edfc712 267 I2C1->CR1 |= Bit9;
chrish 0:36109edfc712 268
chrish 0:36109edfc712 269 // GGF WEGMACHEN
chrish 0:36109edfc712 270 for(local = 0; local <1000;local++); // WAIT TIME
chrish 0:36109edfc712 271
chrish 0:36109edfc712 272
chrish 0:36109edfc712 273 I2C1->CR1 |= Bit10; // AKNOLLAGE ENABLE ;
chrish 0:36109edfc712 274
chrish 0:36109edfc712 275 I2C1->CR1 |= Bit8; // ERZEUGEN STARTKONDITION
chrish 0:36109edfc712 276
chrish 0:36109edfc712 277 while((I2C1->SR1 & Bit0) == 0 ); // WHAITING START BIT GENERAITET
chrish 0:36109edfc712 278
chrish 0:36109edfc712 279 I2C1->DR = BMP180_adress_read;
chrish 0:36109edfc712 280
chrish 0:36109edfc712 281 while((I2C1->SR1 & Bit1) == 0 ); // WHAITING ADDR = 1
chrish 0:36109edfc712 282
chrish 0:36109edfc712 283 local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 284
chrish 0:36109edfc712 285 while( (I2C1->SR1 & Bit6 )== 0 ); // WHAIT DATA TRANSMIT
chrish 0:36109edfc712 286
chrish 0:36109edfc712 287 MSB = I2C1->DR ;
chrish 0:36109edfc712 288 I2C1->CR1 &= ~Bit10; // NO ACNOLAGE BECAUSE LAST BYTE
chrish 0:36109edfc712 289 I2C1->CR1 |= Bit9; // STOP BETWEEN THE CURRENT TRANSFER
chrish 0:36109edfc712 290 while( (I2C1->SR1 & Bit6 )== 0 ); // WHAIT DATA TRANSMIT
chrish 0:36109edfc712 291 LSB = I2C1->DR ;
chrish 0:36109edfc712 292
chrish 0:36109edfc712 293 return (MSB<<8)|LSB ;
chrish 0:36109edfc712 294 }
chrish 0:36109edfc712 295
chrish 0:36109edfc712 296
chrish 0:36109edfc712 297
chrish 0:36109edfc712 298
chrish 0:36109edfc712 299 void PRESSURE_vwrite_EEPROM_REGISTER(byte b_register, byte data){
chrish 0:36109edfc712 300 uint16_t local;
chrish 0:36109edfc712 301
chrish 0:36109edfc712 302 I2C1->CR1 |= Bit8; // START KONDITION
chrish 0:36109edfc712 303
chrish 0:36109edfc712 304 while((I2C1->SR1 & Bit0) == 0 ); // WHAITING START BIT GENERAITET
chrish 0:36109edfc712 305 /**SENDEN ADRESSE */
chrish 0:36109edfc712 306 I2C1->DR = BMP180_adress_write;
chrish 0:36109edfc712 307
chrish 0:36109edfc712 308 while((I2C1->SR1 & Bit1) == 0 ); // WHAITING ADDR = 1
chrish 0:36109edfc712 309
chrish 0:36109edfc712 310 local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 311
chrish 0:36109edfc712 312
chrish 0:36109edfc712 313 /**Senden erstes PACKET*/
chrish 0:36109edfc712 314 I2C1->DR = b_register;
chrish 0:36109edfc712 315
chrish 0:36109edfc712 316 while((I2C1->SR1 & Bit7) == 0 ); // WHAITING Data register Empte
chrish 0:36109edfc712 317
chrish 0:36109edfc712 318 //local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 319 /**SENDEN Zweites Packet */
chrish 0:36109edfc712 320 I2C1->DR = data;
chrish 0:36109edfc712 321 while( ((I2C1->SR1 & Bit7) == 0) && ((I2C1->SR1 & Bit2) == 0 )); // WHAITING Data register Empte
chrish 0:36109edfc712 322 /**STOP CONDITION */
chrish 0:36109edfc712 323 I2C1->CR1 |= Bit9;
chrish 0:36109edfc712 324 }
chrish 0:36109edfc712 325
chrish 0:36109edfc712 326
chrish 0:36109edfc712 327
chrish 0:36109edfc712 328
chrish 0:36109edfc712 329
chrish 0:36109edfc712 330 byte I2C_write(byte bSlaveWriteAdress, byte bRegister, byte bValue){
chrish 0:36109edfc712 331 uint16_t local;
chrish 0:36109edfc712 332 I2C1->CR1 |= Bit8; // START KONDITION
chrish 0:36109edfc712 333
chrish 0:36109edfc712 334 while((I2C1->SR1 & Bit0) == 0 ); // WHAITING START BIT GENERAITET
chrish 0:36109edfc712 335 /**SENDEN ADRESSE */
chrish 0:36109edfc712 336 I2C1->DR = bSlaveWriteAdress;
chrish 0:36109edfc712 337
chrish 0:36109edfc712 338 while((I2C1->SR1 & Bit1) == 0 ); // WHAITING ADDR = 1
chrish 0:36109edfc712 339
chrish 0:36109edfc712 340 local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 341
chrish 0:36109edfc712 342
chrish 0:36109edfc712 343 /**Senden erstes PACKET*/
chrish 0:36109edfc712 344 I2C1->DR = bRegister;
chrish 0:36109edfc712 345
chrish 0:36109edfc712 346 while((I2C1->SR1 & Bit7) == 0 ); // WHAITING Data register Empte
chrish 0:36109edfc712 347
chrish 0:36109edfc712 348 //local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 349 /**SENDEN Zweites Packet */
chrish 0:36109edfc712 350 I2C1->DR = bValue;
chrish 0:36109edfc712 351 while( ((I2C1->SR1 & Bit7) == 0) && ((I2C1->SR1 & Bit2) == 0 )); // WHAITING Data register Empte
chrish 0:36109edfc712 352 /**STOP CONDITION */
chrish 0:36109edfc712 353 I2C1->CR1 |= Bit9;
chrish 0:36109edfc712 354
chrish 0:36109edfc712 355
chrish 0:36109edfc712 356 return 1;
chrish 0:36109edfc712 357 }
chrish 0:36109edfc712 358
chrish 0:36109edfc712 359
chrish 0:36109edfc712 360
chrish 0:36109edfc712 361
chrish 0:36109edfc712 362
chrish 0:36109edfc712 363
chrish 0:36109edfc712 364 byte I2C_read(byte bSlaveWriteAdress,byte bSlaveReadAdress, byte bRegister, byte bNumOfBytes, byte *abArray){
chrish 0:36109edfc712 365 /**EINLESEN VON ADC WERTEN ODER EEPORM DATEN */
chrish 0:36109edfc712 366 uint16_t local;
chrish 0:36109edfc712 367 byte i;
chrish 0:36109edfc712 368
chrish 0:36109edfc712 369 /*******************************************************/
chrish 0:36109edfc712 370 // SEKTION 1 Auswahl des Registes durch write befehl
chrish 0:36109edfc712 371 /*********************************************************/
chrish 0:36109edfc712 372 I2C1->CR1 |= Bit8; // ERZEUGEN STARTKONDITION
chrish 0:36109edfc712 373 while((I2C1->SR1 & Bit0) == 0 ); // WHAITING START BIT GENERAITET
chrish 0:36109edfc712 374 I2C1->DR = bSlaveWriteAdress;
chrish 0:36109edfc712 375 while((I2C1->SR1 & Bit1) == 0 ); // WHAITING ADDR = 1
chrish 0:36109edfc712 376 local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 377 I2C1->DR = bRegister;
chrish 0:36109edfc712 378 while( ((I2C1->SR1 & Bit7) == 0) && ((I2C1->SR1 & Bit2) == 0 )); // WHAITING Data register Empte
chrish 0:36109edfc712 379 I2C1->CR1 |= Bit9;
chrish 0:36109edfc712 380 // GGF WEGMACHEN
chrish 0:36109edfc712 381 for(local = 0; local <1000;local++); // WAIT TIME
chrish 0:36109edfc712 382
chrish 0:36109edfc712 383
chrish 0:36109edfc712 384 /*******************************************************/
chrish 0:36109edfc712 385 // SEKTION 2 Eigentliches Auslesen der Werte
chrish 0:36109edfc712 386 /*********************************************************/
chrish 0:36109edfc712 387
chrish 0:36109edfc712 388 I2C1->CR1 |= Bit10; // AKNOLLAGE ENABLE ;
chrish 0:36109edfc712 389 I2C1->CR1 |= Bit8; // ERZEUGEN STARTKONDITION
chrish 0:36109edfc712 390 while((I2C1->SR1 & Bit0) == 0 ); // WHAITING START BIT GENERAITET
chrish 0:36109edfc712 391 I2C1->DR = bSlaveReadAdress;
chrish 0:36109edfc712 392 while((I2C1->SR1 & Bit1) == 0 ); // WHAITING ADDR = 1
chrish 0:36109edfc712 393 local = I2C1->SR2 ; // READ SR2 for Delete ADDR
chrish 0:36109edfc712 394
chrish 0:36109edfc712 395 for(i=0; i< bNumOfBytes-1; i++){
chrish 0:36109edfc712 396 while( (I2C1->SR1 & Bit6 )== 0 ); // WHAIT DATA TRANSMIT
chrish 0:36109edfc712 397 abArray[i] = I2C1->DR ;
chrish 0:36109edfc712 398 }
chrish 0:36109edfc712 399 i= bNumOfBytes-1;
chrish 0:36109edfc712 400 I2C1->CR1 &= ~Bit10; // NO ACNOLAGE BECAUSE LAST BYTE
chrish 0:36109edfc712 401 I2C1->CR1 |= Bit9; // STOP BETWEEN THE CURRENT TRANSFER
chrish 0:36109edfc712 402 while( (I2C1->SR1 & Bit6 )== 0 ); // WHAIT DATA TRANSMIT
chrish 0:36109edfc712 403 abArray[i] = I2C1->DR ;
chrish 0:36109edfc712 404
chrish 0:36109edfc712 405
chrish 0:36109edfc712 406 return 1;
chrish 0:36109edfc712 407 }
chrish 0:36109edfc712 408
chrish 0:36109edfc712 409