stm32f103c8t6螺旋仪GY-521代码
时间: 2023-07-02 15:06:47 浏览: 103
以下是基于STM32F103C8T6芯片和GY-521螺旋仪的示例代码:
```c
#include "stm32f10x.h"
#include "MPU6050.h"
/* 定义IIC引脚宏 */
#define SDA_H GPIOB->BSRR = GPIO_Pin_9
#define SDA_L GPIOB->BRR = GPIO_Pin_9
#define SCL_H GPIOB->BSRR = GPIO_Pin_8
#define SCL_L GPIOB->BRR = GPIO_Pin_8
/* 延时函数 */
void Delay_us(int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<10;j++);
}
}
/* 初始化IIC */
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/* IIC发送一个字节 */
void IIC_SendByte(uint8_t byte)
{
uint8_t i;
for(i=0; i<8; i++)
{
if(byte & 0x80)
SDA_H;
else
SDA_L;
SCL_L;
Delay_us(5);
SCL_H;
Delay_us(5);
byte <<= 1;
}
SCL_L;
Delay_us(5);
SDA_H;
Delay_us(5);
}
/* IIC读取一个字节 */
uint8_t IIC_ReadByte(void)
{
uint8_t i,byte=0;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
for(i=0; i<8; i++)
{
byte <<= 1;
SCL_L;
Delay_us(5);
SCL_H;
Delay_us(5);
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9))
byte |= 0x01;
}
SCL_L;
Delay_us(5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
return byte;
}
/* 启动IIC通信 */
void IIC_Start(void)
{
SDA_H;
SCL_H;
Delay_us(5);
SDA_L;
Delay_us(5);
SCL_L;
Delay_us(5);
}
/* 停止IIC通信 */
void IIC_Stop(void)
{
SDA_L;
SCL_H;
Delay_us(5);
SDA_H;
Delay_us(5);
}
/* 写入一个字节数据到MPU6050 */
void MPU6050_WriteByte(uint8_t addr, uint8_t data)
{
IIC_Start();
IIC_SendByte(0xD0);
IIC_SendByte(addr);
IIC_SendByte(data);
IIC_Stop();
}
/* 读取一个字节数据从MPU6050 */
uint8_t MPU6050_ReadByte(uint8_t addr)
{
uint8_t data;
IIC_Start();
IIC_SendByte(0xD0);
IIC_SendByte(addr);
IIC_Start();
IIC_SendByte(0xD1);
data = IIC_ReadByte();
IIC_Stop();
return data;
}
/* 初始化MPU6050 */
void MPU6050_Init(void)
{
MPU6050_WriteByte(MPU_PWR_MGMT_1, 0x00);
MPU6050_WriteByte(MPU_SMPLRT_DIV, 0x07);
MPU6050_WriteByte(MPU_CONFIG, 0x06);
MPU6050_WriteByte(MPU_GYRO_CONFIG, 0x18);
MPU6050_WriteByte(MPU_ACCEL_CONFIG, 0x01);
}
/* 读取MPU6050加速度传感器数据 */
void MPU6050_ReadAcc(int16_t *acc)
{
uint8_t buf[6];
uint8_t i;
IIC_Start();
IIC_SendByte(0xD0);
IIC_SendByte(MPU_ACCEL_XOUT_H);
IIC_Start();
IIC_SendByte(0xD1);
for(i=0; i<6; i++)
{
buf[i] = IIC_ReadByte();
if(i == 5)
IIC_Stop();
else
IIC_SendByte(1);
}
acc[0] = ((int16_t)buf[0] << 8) | buf[1];
acc[1] = ((int16_t)buf[2] << 8) | buf[3];
acc[2] = ((int16_t)buf[4] << 8) | buf[5];
}
int main(void)
{
int16_t acc[3];
IIC_Init();
MPU6050_Init();
while(1)
{
MPU6050_ReadAcc(acc);
printf("AccX:%d, AccY:%d, AccZ:%d\n", acc[0], acc[1], acc[2]);
Delay_us(1000);
}
}
```
上述代码中的`MPU6050.h`文件如下:
```c
#ifndef __MPU6050_H
#define __MPU6050_H
#include "stm32f10x.h"
#define MPU_ADDR 0x68
#define MPU_PWR_MGMT_1 0x6B
#define MPU_SMPLRT_DIV 0x19
#define MPU_CONFIG 0x1A
#define MPU_GYRO_CONFIG 0x1B
#define MPU_ACCEL_CONFIG 0x1C
#define MPU_ACCEL_XOUT_H 0x3B
void MPU6050_WriteByte(uint8_t addr, uint8_t data);
uint8_t MPU6050_ReadByte(uint8_t addr);
void MPU6050_Init(void);
void MPU6050_ReadAcc(int16_t *acc);
#endif
```
需要注意的是,上述代码只是一个简单的示例,如果需要更精确的数据,还需要进行校准和滤波等操作。