使用这个程序发送时每次发送紧跟器件地址(0xae)后面的从地址发送总是(0x3f)应为IIC_Read()语句第二个数据值
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_i2c.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "driverlib/pwm.h"
#include "driverlib/rom.h"
#include "driverlib/fpu.h"
#include "driverlib/timer.h"
void IIC_Init(void);
void IIC_Send(uint8_t adr,uint8_t sla,uint8_t dat);
void MPU6050_Init(void);
uint8_t IIC_Read(uint8_t adr,uint8_t sla);
int main(void)
{
uint16_t dat=0;
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
IIC_Init();
MPU6050_Init();
while(1)
{
dat=IIC_Read(0xae,0x0f);
SysCtlDelay(2000);
}
}
void IIC_Init(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// 配置PA6、PA7为上拉端口
GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
// PA6配置为I2C协议时钟信号、 PA7配置为I2C协议数据信号
GPIOPinConfigure(GPIO_PA6_I2C1SCL);
GPIOPinConfigure(GPIO_PA7_I2C1SDA);
GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);
GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
// 初始化I2C主机模块。设置总线速度和使能主机模块
I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), false);
// 使能I2C主机模块
I2CMasterEnable(I2C1_BASE);
}
void IIC_Send(uint8_t adr,uint8_t sla,uint8_t dat)
{
adr=adr/2;
I2CMasterSlaveAddrSet(I2C1_BASE, adr, false);
I2CMasterDataPut(I2C1_BASE,sla);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusBusy(I2C1_BASE));
SysCtlDelay(2000);
I2CMasterDataPut(I2C1_BASE,dat);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusBusy(I2C1_BASE));
SysCtlDelay(2000);
}
uint8_t IIC_Read(uint8_t adr,uint8_t sla)
{
uint8_t dat=0;
adr=adr/2;
I2CMasterSlaveAddrSet(I2C1_BASE, adr, false);
SysCtlDelay(2000);
I2CMasterDataPut(I2C1_BASE,sla);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusBusy(I2C1_BASE));
SysCtlDelay(2000);
I2CMasterSlaveAddrSet(I2C1_BASE, adr, true);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C1_BASE));
dat = I2CMasterDataGet(I2C1_BASE);
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
SysCtlDelay(2000);
return dat;
}
下面附上逻辑分析仪采样(CH1接PA6,CH2接PA7)
Pengfei Zheng:
关于代码我去掉了多余的内容,所以中间的一些在上面没有定义的语句请忽略