这下面是我的程序 求帮我看看啊
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);}}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
- /**
- ******************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] MyI2C.h
- * [url=home.php?mod=space&uid=187600]@author[/url] KEN
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.0
- * [url=home.php?mod=space&uid=212281]@date[/url] 2015年6月21日
- * [url=home.php?mod=space&uid=247401]@brief[/url] My Inter-Integrated Circuit, IIC相关函数的接口
- ******************************************************************************
- * @attention
- *
- * 本固件仅供参考,为客户提供产品的编码信息,以便节省客户时间。由该固件的使用而
- * 造成产品上的损失的,Ks'P概不承担直接或间接的责任。
- *
- * <h2><center>© COPYRIGHT 2015 Ks'P</center></h2>
- ******************************************************************************
- */
- #ifndef __MYI2C_H
- #define __MYI2C_H
- /********I2C Pin***********
- * PA6 -> I2C1_SCL
- * PA7 -> I2C1_SDA
- *************************
- */
- #define EEPROM_Addr 0xA0
- void MyI2C_WaitDeviceForStandBy(uint8_t DeviceAddr);
- void MyI2C_WriteReg(uint8_t DeviceAddr, uint32_t RegAddr, uint8_t *wbuff, uint32_t LenOfReg, uint32_t LenOfBuff);
- void MyI2C_ReadReg(uint8_t DeviceAddr, uint32_t RegAddr, uint8_t *rbuff, uint32_t LenOfReg, uint32_t LenOfBuff);
- #endif
复制代码一周热门 更多>