#include "stm32f10x.h"
#include "i2c.h"
#include "gyro.h"
#define GYRO_ADDR 0x68
typedef struct {
int16_t x;
int16_t y;
int16_t z;
} GyroData;
GyroData gyroData;
void I2C_Init(void)
{
}
void ReadGyroData(GyroData *data)
{
uint8_t buf[6];
I2C_Start();
I2C_SendByte(GYRO_ADDR << 1 | 0);
I2C_WaitAck();
I2C_SendByte(0x67);
I2C_WaitAck();
I2C_Start();
I2C_SendByte(GYRO_ADDR << 1 | 1);
I2C_WaitAck();
I2C_ReadByte(buf, 6);
I2C_Stop();
data->x = (int16_t)((buf[0] << 8) | buf[1]);
data->y = (int16_t)((buf[2] << 8) | buf[3]);
data->z = (int16_t)((buf[4] << 8) | buf[5]);
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SystemInit();
I2C_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while (1) {
ReadGyroData(&gyroData);
if ((gyroData.x < 0) && (gyroData.y < 0)) {
GPIO_SetBits(GPIOB, GPIO_Pin_3);
Delay_ms(250);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
Delay_ms(250);
Delay_ms(5000);
}
else if ((gyroData.x > 0) && (gyroData.y > 0)) {
GPIO_SetBits(GPIOB, GPIO_Pin_4);
Delay_ms(250);
GPIO_ResetBits(GPIOB, GPIO_Pin_4);
Delay_ms(250);
Delay_ms(5000);
}
}
}