这下面是我的程序 求帮我看看啊
void i2cinit()
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_2,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_3,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_OD);
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
I2CMasterEnable(I2C0_BASE);
}
void write(uint8_t DevAddr,uint8_t RegAddr,uint8_t writedata)
{
I2CMasterSlaveAddrSet(I2C0_BASE, DevAddr, false);
I2CMasterDataPut(I2C0_BASE,RegAddr);
while(I2CMasterBusy(I2C0_BASE)){}
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C0_BASE)){}
I2CMasterDataPut(I2C0_BASE, writedata);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C0_BASE)){}
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
i2cinit();
}
uint8_t read(uint8_t DevAddr,uint8_t RegAddr)
{
int data;
I2CMasterSlaveAddrSet(I2C0_BASE, DevAddr, false);
I2CMasterDataPut(I2C0_BASE,RegAddr);
while(I2CMasterBusy(I2C0_BASE)){}
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C0_BASE)){}
I2CMasterSlaveAddrSet(I2C0_BASE, DevAddr, true);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
while(I2CMasterBusy(I2C0_BASE)){}
data= I2CMasterDataGet(I2C0_BASE);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
i2cinit();
return data;
}
void mpuinit()
{
SysCtlDelay(100* (SysCtlClockGet() / 3000));
write(0x50,PWR_MGMT_1, 0x80);
SysCtlDelay(100* (SysCtlClockGet() / 3000));
write(0x50,PWR_MGMT_1, 0x01);
write(0x50,SMPLRT_DIV, 0x07);
write(0x50,CONFIG, 0x06);
write(0x50,GYRO_CONFIG, 0x18);
write(0x50,ACCEL_CONFIG, 0x00);
}
int
main(void)
{
init();
i2cinit();
InitConsole();
mpuinit();
while(1){
UARTprintf("%o
",read(0x50,0x68));
delay(500);}}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
- #define MPU6050_ADDR 0xD0
- #define MPU6050_O_PWR_MGMT_1 0x6B // Power management 1 register
- #define MPU6050_PWR_MGMT_1_DEVICE_RESET 0x80 // Device reset
- #define MPU6050_O_ACCEL_XOUT_H 0x3B // X-axis acceleration data MSB register
- uint8_t reg_temp;
- uint8_t raw_data[14];
- reg_temp = MPU6050_PWR_MGMT_1_DEVICE_RESET;
- MyI2C_WriteReg(MPU6050_ADDR, MPU6050_O_PWR_MGMT_1, ®_temp, 1, 1); //设备复位
- MyI2C_ReadReg(MPU6050_ADDR, MPU6050_O_ACCEL_XOUT_H, raw_data, 1, 14); //读原始数据
复制代码随手写了几行,再不会用我就实在没办法了
一周热门 更多>