Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: STM32F3-Discovery-minimal
Fork of accelerometer_test by
Revision 4:e38891a2d67c, committed 2019-04-03
- Comitter:
- MartinJohnson
- Date:
- Wed Apr 03 22:00:45 2019 +0000
- Parent:
- 3:eacf05659590
- Commit message:
- Tidy up formatting
Changed in this revision
| STM32F3-Discovery-minimal.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.c | Show annotated file Show diff for this revision Revisions of this file |
--- a/STM32F3-Discovery-minimal.lib Wed May 02 04:11:59 2018 +0000 +++ b/STM32F3-Discovery-minimal.lib Wed Apr 03 22:00:45 2019 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/MartinJohnson/code/STM32F3-Discovery-minimal/#c65854d98061 +https://os.mbed.com/users/MartinJohnson/code/STM32F3-Discovery-minimal/#ff94a7f6ecff
--- a/main.c Wed May 02 04:11:59 2018 +0000
+++ b/main.c Wed Apr 03 22:00:45 2019 +0000
@@ -10,11 +10,11 @@
while(I2C1->ISR & I2C_ISR_BUSY);
I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_RELOAD | I2C_CR2_START | (1<<16) | address;
while(!(I2C1->ISR & I2C_ISR_TXIS));
- I2C1->TXDR=reg;
+ I2C1->TXDR = reg;
while(!(I2C1->ISR & I2C_ISR_TCR));
I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_AUTOEND | (1<<16) | address;
while(!(I2C1->ISR & I2C_ISR_TXIS));
- I2C1->TXDR=val;
+ I2C1->TXDR = val;
while(!(I2C1->ISR & I2C_ISR_STOPF));
I2C1->ICR = I2C_ICR_STOPCF;
}
@@ -24,12 +24,12 @@
I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_START | (1<<16) | address;
while(!(I2C1->ISR & I2C_ISR_TXIS));
if(nbytes>1) reg |= 0x80;
- I2C1->TXDR=reg;
+ I2C1->TXDR= reg;
while(!(I2C1->ISR & I2C_ISR_TC));
I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_AUTOEND | I2C_CR2_START | I2C_CR2_RD_WRN | (nbytes<<16) | address;
while(nbytes--) {
while(!(I2C1->ISR & I2C_ISR_RXNE));
- *data++=I2C1->RXDR;
+ *data++ = I2C1->RXDR;
}
while(!(I2C1->ISR & I2C_ISR_STOPF));
I2C1->ICR = I2C_ICR_STOPCF;
@@ -53,13 +53,13 @@
void MemsConfig(void)
{
i2cInit();
- i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG1_A,LSM303DLHC_NORMAL_MODE | LSM303DLHC_ODR_50_HZ | LSM303DLHC_AXES_ENABLE);
- i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A,LSM303DLHC_FULLSCALE_2G | LSM303DLHC_BlockUpdate_Continous | LSM303DLHC_BLE_LSB | LSM303DLHC_HR_ENABLE);
- i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A,LSM303DLHC_HPM_NORMAL_MODE | LSM303DLHC_HPFCF_16 | LSM303DLHC_HPF_AOI1_DISABLE | LSM303DLHC_HPF_AOI2_DISABLE);
+ i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG1_A, LSM303DLHC_NORMAL_MODE | LSM303DLHC_ODR_50_HZ | LSM303DLHC_AXES_ENABLE);
+ i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A, LSM303DLHC_FULLSCALE_2G | LSM303DLHC_BlockUpdate_Continous | LSM303DLHC_BLE_LSB | LSM303DLHC_HR_ENABLE);
+ i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, LSM303DLHC_HPM_NORMAL_MODE | LSM303DLHC_HPFCF_16 | LSM303DLHC_HPF_AOI1_DISABLE | LSM303DLHC_HPF_AOI2_DISABLE);
i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_CRA_REG_M, LSM303DLHC_TEMPSENSOR_ENABLE | LSM303DLHC_ODR_30_HZ);
i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_CRB_REG_M, LSM303DLHC_FS_8_1_GA);
- i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_MR_REG_M,LSM303DLHC_CONTINUOS_CONVERSION);
+ i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_MR_REG_M, LSM303DLHC_CONTINUOS_CONVERSION);
}
void ReadAccelerometer(int16_t * data) {
@@ -77,15 +77,15 @@
int ReadTemperature() {
int t=0;
- i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_TEMP_OUT_L_M, (uint8_t *)&t,1);
- i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_TEMP_OUT_H_M, (uint8_t *)&t+1,1);
+ i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_TEMP_OUT_L_M, (uint8_t *)&t, 1);
+ i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_TEMP_OUT_H_M, (uint8_t *)&t+1, 1);
return t/64;
}
volatile unsigned sysTiming;
volatile unsigned sysTicks = 0;
-void SysTick_Handler(void) {
+__attribute__ ((section ("ccmram"))) void SysTick_Handler(void) {
sysTicks++;
if (sysTiming > 0) --sysTiming;
}
@@ -97,7 +97,6 @@
}
int main(void) {
-
SysTick_Config((SystemCoreClock / 1000));
RCC->AHBENR |= RCC_AHBENR_GPIOEEN | RCC_AHBENR_GPIOAEN;
GPIOE->MODER = (GPIOE->MODER&0xffff) | 0x55550000; // output mode for PE8-15
@@ -105,38 +104,36 @@
MemsConfig();
int16_t acc[3],mag[3],temp;
- int b=0;
- int TH=200*16;
+ int b = 0;
+ int TH = 200*16;
while(1) {
-
ReadAccelerometer(acc);
ReadMagnetometer(mag);
float heading = atan2(mag[1], mag[0]);
- heading=heading * 180 / 3.14159265359f;
- if (heading < 0) heading +=360;
+ heading = heading * 180 / 3.14159265359f;
+ if (heading < 0) heading += 360;
temp=ReadTemperature();
- printf("Acc: %d %d %d\n",acc[0]/16,acc[1]/16,acc[2]/16);
- printf("Mag: %d %d %d %d %f\n",mag[0],mag[1],mag[2],temp, heading);
- GPIOE->BSRR=b<<(8+16);
- b=0;
- if(acc[1]<-TH) {
- if(acc[0]<-TH) b=1<<4;
- else if(acc[0]>TH) b=1<<2;
- else b=1<<3;
+ printf("Acc: %d %d %d\n", acc[0]/16, acc[1]/16, acc[2]/16);
+ printf("Mag: %d %d %d %d %f\n", mag[0], mag[1], mag[2], temp, heading);
+ GPIOE->BSRR = b<<(8+16);
+ b = 0;
+ if(acc[1] < -TH) {
+ if(acc[0] < -TH) b = 1<<4;
+ else if(acc[0] > TH) b = 1<<2;
+ else b = 1<<3;
} else {
- if(acc[1]>TH) {
- if(acc[0]<-TH) b=1<<6;
- else if(acc[0]>TH) b=1<<0;
- else b=1<<7;
+ if(acc[1] > TH) {
+ if(acc[0] < -TH) b = 1<<6;
+ else if(acc[0] > TH) b = 1<<0;
+ else b = 1<<7;
} else {
- if(acc[0]<-TH) b=1<<5;
- else if(acc[0]>TH) b=1<<1;
+ if(acc[0] < -TH) b = 1<<5;
+ else if(acc[0] > TH) b = 1<<1;
}
}
- GPIOE->BSRR=b<<8;
+ GPIOE->BSRR = b<<8;
while(GPIOA->IDR&1);
sysDelayMs(100);
-
}
}
