Martin Johnson / accelerometer_test

Dependencies:   STM32F3-Discovery-minimal

Committer:
MartinJohnson
Date:
Wed May 02 01:56:42 2018 +0000
Revision:
1:c92d1851a036
Parent:
0:ed38888a34c2
Child:
2:742027e49b6f
Accelerometer test without using the std peripheral library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MartinJohnson 0:ed38888a34c2 1
MartinJohnson 0:ed38888a34c2 2 #include <stm32f3_discovery.h>
MartinJohnson 1:c92d1851a036 3 #include <stm32f3_discovery_lsm303dlhc.h>
MartinJohnson 1:c92d1851a036 4
MartinJohnson 1:c92d1851a036 5 #define I2C_CR2_CLEAR_MASK ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)
MartinJohnson 0:ed38888a34c2 6
MartinJohnson 1:c92d1851a036 7 void i2cWrite(int address, int reg, int val) {
MartinJohnson 1:c92d1851a036 8 while(I2C1->ISR & I2C_ISR_BUSY);
MartinJohnson 1:c92d1851a036 9 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_RELOAD | I2C_CR2_START | (1<<16) | address;
MartinJohnson 1:c92d1851a036 10 while(!(I2C1->ISR & I2C_ISR_TXIS));
MartinJohnson 1:c92d1851a036 11 I2C1->TXDR=reg;
MartinJohnson 1:c92d1851a036 12 while(!(I2C1->ISR & I2C_ISR_TCR));
MartinJohnson 1:c92d1851a036 13 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_AUTOEND | (1<<16) | address;
MartinJohnson 1:c92d1851a036 14 while(!(I2C1->ISR & I2C_ISR_TXIS));
MartinJohnson 1:c92d1851a036 15 I2C1->TXDR=val;
MartinJohnson 1:c92d1851a036 16 while(!(I2C1->ISR & I2C_ISR_STOPF));
MartinJohnson 1:c92d1851a036 17 I2C1->ICR = I2C_ICR_STOPCF;
MartinJohnson 1:c92d1851a036 18 }
MartinJohnson 0:ed38888a34c2 19
MartinJohnson 1:c92d1851a036 20 void i2cRead(int address, int reg, uint8_t *data, int nbytes) {
MartinJohnson 1:c92d1851a036 21 while(I2C1->ISR & I2C_ISR_BUSY);
MartinJohnson 1:c92d1851a036 22 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_START | (1<<16) | address;
MartinJohnson 1:c92d1851a036 23 while(!(I2C1->ISR & I2C_ISR_TXIS));
MartinJohnson 1:c92d1851a036 24 if(nbytes>1) reg |= 0x80;
MartinJohnson 1:c92d1851a036 25 I2C1->TXDR=reg;
MartinJohnson 1:c92d1851a036 26 while(!(I2C1->ISR & I2C_ISR_TC));
MartinJohnson 1:c92d1851a036 27 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_AUTOEND | I2C_CR2_START | I2C_CR2_RD_WRN | (nbytes<<16) | address;
MartinJohnson 1:c92d1851a036 28 while(nbytes--) {
MartinJohnson 1:c92d1851a036 29 while(!(I2C1->ISR & I2C_ISR_RXNE));
MartinJohnson 1:c92d1851a036 30 *data++=I2C1->RXDR;
MartinJohnson 1:c92d1851a036 31 }
MartinJohnson 1:c92d1851a036 32 while(!(I2C1->ISR & I2C_ISR_STOPF));
MartinJohnson 1:c92d1851a036 33 I2C1->ICR = I2C_ICR_STOPCF;
MartinJohnson 0:ed38888a34c2 34 }
MartinJohnson 0:ed38888a34c2 35
MartinJohnson 1:c92d1851a036 36 void i2cInit() {
MartinJohnson 1:c92d1851a036 37 RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
MartinJohnson 1:c92d1851a036 38 // configure GPIOPB6 and 7
MartinJohnson 1:c92d1851a036 39 RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
MartinJohnson 1:c92d1851a036 40 GPIOB->AFR[0] = (GPIOC->AFR[0] & 0x00ffffff) | 0x44000000; // alt funcs for GPIOB6 and 7 are 4
MartinJohnson 1:c92d1851a036 41 GPIOB->OSPEEDR |= 0xf000; // speed high
MartinJohnson 1:c92d1851a036 42 GPIOB->OTYPER &= ~0xc0; // output type pp
MartinJohnson 1:c92d1851a036 43 GPIOB->MODER = (GPIOB->MODER & ~0xf000) | 0xa000; // mode af (10)
MartinJohnson 1:c92d1851a036 44 GPIOB->PUPDR = (GPIOB->PUPDR & ~0xf000) | 0xa000; // pull down (10)
MartinJohnson 1:c92d1851a036 45
MartinJohnson 1:c92d1851a036 46 I2C1->TIMINGR = 0x00902025;
MartinJohnson 1:c92d1851a036 47 I2C1->CR1 |= I2C_CR1_PE;
MartinJohnson 1:c92d1851a036 48
MartinJohnson 1:c92d1851a036 49 }
MartinJohnson 1:c92d1851a036 50
MartinJohnson 1:c92d1851a036 51 void MemsConfig(void)
MartinJohnson 0:ed38888a34c2 52 {
MartinJohnson 1:c92d1851a036 53 i2cInit();
MartinJohnson 1:c92d1851a036 54 i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG1_A,LSM303DLHC_NORMAL_MODE | LSM303DLHC_ODR_50_HZ | LSM303DLHC_AXES_ENABLE);
MartinJohnson 1:c92d1851a036 55 i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A,LSM303DLHC_FULLSCALE_2G | LSM303DLHC_BlockUpdate_Continous | LSM303DLHC_BLE_LSB | LSM303DLHC_HR_ENABLE);
MartinJohnson 1:c92d1851a036 56 i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A,LSM303DLHC_HPM_NORMAL_MODE | LSM303DLHC_HPFCF_16 | LSM303DLHC_HPF_AOI1_DISABLE | LSM303DLHC_HPF_AOI2_DISABLE);
MartinJohnson 1:c92d1851a036 57
MartinJohnson 1:c92d1851a036 58 i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_CRA_REG_M, LSM303DLHC_TEMPSENSOR_ENABLE | LSM303DLHC_ODR_30_HZ);
MartinJohnson 1:c92d1851a036 59 i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_CRB_REG_M, LSM303DLHC_FS_8_1_GA);
MartinJohnson 1:c92d1851a036 60 i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_MR_REG_M,LSM303DLHC_CONTINUOS_CONVERSION);
MartinJohnson 1:c92d1851a036 61 }
MartinJohnson 1:c92d1851a036 62
MartinJohnson 1:c92d1851a036 63 void ReadAccelerometer(int16_t * data) {
MartinJohnson 1:c92d1851a036 64 i2cRead(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_L_A, (uint8_t *)data, 6);
MartinJohnson 1:c92d1851a036 65 }
MartinJohnson 1:c92d1851a036 66
MartinJohnson 1:c92d1851a036 67 void ReadMagnetometer(int16_t * data) {
MartinJohnson 1:c92d1851a036 68 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_X_L_M, (uint8_t *)data, 6);
MartinJohnson 1:c92d1851a036 69 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_X_H_M, (uint8_t *)data+1, 6);
MartinJohnson 1:c92d1851a036 70 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Y_L_M, (uint8_t *)data+2, 6);
MartinJohnson 1:c92d1851a036 71 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Y_H_M, (uint8_t *)data+3, 6);
MartinJohnson 1:c92d1851a036 72 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Z_L_M, (uint8_t *)data+4, 6);
MartinJohnson 1:c92d1851a036 73 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Z_H_M, (uint8_t *)data+5, 6);
MartinJohnson 1:c92d1851a036 74 }
MartinJohnson 1:c92d1851a036 75
MartinJohnson 1:c92d1851a036 76 int ReadTemperature() {
MartinJohnson 1:c92d1851a036 77 int t=0;
MartinJohnson 1:c92d1851a036 78 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_TEMP_OUT_L_M, (uint8_t *)&t,1);
MartinJohnson 1:c92d1851a036 79 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_TEMP_OUT_H_M, (uint8_t *)&t+1,1);
MartinJohnson 1:c92d1851a036 80 return t/64;
MartinJohnson 0:ed38888a34c2 81 }
MartinJohnson 0:ed38888a34c2 82
MartinJohnson 0:ed38888a34c2 83 volatile unsigned sysTiming;
MartinJohnson 0:ed38888a34c2 84 volatile unsigned sysTicks = 0;
MartinJohnson 0:ed38888a34c2 85
MartinJohnson 0:ed38888a34c2 86 void SysTick_Handler(void) {
MartinJohnson 0:ed38888a34c2 87 sysTicks++;
MartinJohnson 0:ed38888a34c2 88 if (sysTiming > 0) --sysTiming;
MartinJohnson 0:ed38888a34c2 89 }
MartinJohnson 0:ed38888a34c2 90
MartinJohnson 0:ed38888a34c2 91 void sysDelayMs(unsigned dly) {
MartinJohnson 0:ed38888a34c2 92 sysTiming = dly;
MartinJohnson 0:ed38888a34c2 93 while (sysTiming > 0) __wfi();
MartinJohnson 0:ed38888a34c2 94
MartinJohnson 0:ed38888a34c2 95 }
MartinJohnson 0:ed38888a34c2 96
MartinJohnson 0:ed38888a34c2 97 int main(void) {
MartinJohnson 0:ed38888a34c2 98
MartinJohnson 0:ed38888a34c2 99 SysTick_Config((SystemCoreClock / 1000));
MartinJohnson 1:c92d1851a036 100 RCC->AHBENR |= RCC_AHBENR_GPIOEEN | RCC_AHBENR_GPIOAEN;
MartinJohnson 0:ed38888a34c2 101 GPIOE->MODER = (GPIOE->MODER&0xffff) | 0x55550000; // output mode for PE8-15
MartinJohnson 0:ed38888a34c2 102 GPIOA->MODER = (GPIOA->MODER&0xfffffffc) ; // input mode for PA0
MartinJohnson 0:ed38888a34c2 103
MartinJohnson 1:c92d1851a036 104 MemsConfig();
MartinJohnson 1:c92d1851a036 105 int16_t acc[3],mag[3],temp;
MartinJohnson 0:ed38888a34c2 106 int b=0;
MartinJohnson 1:c92d1851a036 107 int TH=200*16;
MartinJohnson 0:ed38888a34c2 108 while(1) {
MartinJohnson 0:ed38888a34c2 109
MartinJohnson 0:ed38888a34c2 110 ReadAccelerometer(acc);
MartinJohnson 1:c92d1851a036 111 ReadMagnetometer(mag);
MartinJohnson 1:c92d1851a036 112 temp=ReadTemperature();
MartinJohnson 1:c92d1851a036 113 printf("Acc: %d %d %d\n",acc[0]/16,acc[1]/16,acc[2]/16);
MartinJohnson 1:c92d1851a036 114 printf("Mag: %d %d %d %d\n",mag[0],mag[1],mag[2],temp);
MartinJohnson 0:ed38888a34c2 115 GPIOE->BSRR=b<<(8+16);
MartinJohnson 0:ed38888a34c2 116 b=0;
MartinJohnson 0:ed38888a34c2 117 if(acc[1]<-TH) {
MartinJohnson 0:ed38888a34c2 118 if(acc[0]<-TH) b=1<<4;
MartinJohnson 0:ed38888a34c2 119 else if(acc[0]>TH) b=1<<2;
MartinJohnson 0:ed38888a34c2 120 else b=1<<3;
MartinJohnson 0:ed38888a34c2 121 } else {
MartinJohnson 0:ed38888a34c2 122 if(acc[1]>TH) {
MartinJohnson 0:ed38888a34c2 123 if(acc[0]<-TH) b=1<<6;
MartinJohnson 0:ed38888a34c2 124 else if(acc[0]>TH) b=1<<0;
MartinJohnson 0:ed38888a34c2 125 else b=1<<7;
MartinJohnson 0:ed38888a34c2 126 } else {
MartinJohnson 0:ed38888a34c2 127 if(acc[0]<-TH) b=1<<5;
MartinJohnson 0:ed38888a34c2 128 else if(acc[0]>TH) b=1<<1;
MartinJohnson 0:ed38888a34c2 129 }
MartinJohnson 0:ed38888a34c2 130 }
MartinJohnson 0:ed38888a34c2 131 GPIOE->BSRR=b<<8;
MartinJohnson 0:ed38888a34c2 132 while(GPIOA->IDR&1);
MartinJohnson 0:ed38888a34c2 133 sysDelayMs(100);
MartinJohnson 0:ed38888a34c2 134
MartinJohnson 0:ed38888a34c2 135 }
MartinJohnson 0:ed38888a34c2 136 }
MartinJohnson 0:ed38888a34c2 137