这下面是我的程序 求帮我看看啊
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.c
- * [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日
- * @brief My Inter-Integrated Circuit, IIC相关函数的实现
- ******************************************************************************
- * @attention
- *
- * 本固件仅供参考,为客户提供产品的编码信息,以便节省客户时间。由该固件的使用而
- * 造成产品上的损失的,Ks'P概不承担直接或间接的责任。
- *
- * <h2><center>© COPYRIGHT 2015 Ks'P</center></h2>
- ******************************************************************************
- */
- #include "stdint.h"
- #include "stdbool.h"
- #include "stdio.h"
- #include "MyI2C.h"
- #include "sysctl.h"
- #include "gpio.h"
- #include "i2c.h"
- #include "pin_map.h"
- #include "bit_band.h"
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] 等待目标从机准备完毕(wait target device stand by until ACK)
- * @param DeviceAddr:从机地址
- * @retval 无
- */
- void MyI2C_WaitDeviceForStandBy(uint8_t DeviceAddr)
- {
- I2CMasterSlaveAddrSet(I2C1_BASE, DeviceAddr >> 1, false);
-
- do
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START | (~BIT0)); //no data
-
- while(I2CMasterBusy(I2C1_BASE))
- ;
- }
- while(I2CMasterErr(I2C1_BASE) & BIT2);
- }
- /**
- * @brief 向目标从机内部寄存器写入数据
- * @param DeviceAddr:从机地址
- RegAddr:寄存器地址
- wbuff:要写入的数据
- LenOfReg:寄存器大小(按字节)(count by Byte)(必须小于等于4)
- LenOfBuff:写入数据的长度(按字节)(count by Byte)
- * @retval 无
- */
- void MyI2C_WriteReg(uint8_t DeviceAddr, uint32_t RegAddr, uint8_t *wbuff, uint32_t LenOfReg, uint32_t LenOfBuff)
- {
- uint32_t i = 0;
-
- I2CMasterSlaveAddrSet(I2C1_BASE, DeviceAddr >> 1, false);
-
- while(LenOfReg--)
- {
- I2CMasterDataPut(I2C1_BASE, *((uint8_t *)(&RegAddr) + LenOfReg));
-
- if(i ==0 )
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START);
- }
- else
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
- }
-
- while(I2CMasterBusy(I2C1_BASE))
- ;
-
- i++;
- }
-
- i = 0;
-
- while(LenOfBuff--)
- {
- I2CMasterDataPut(I2C1_BASE, *(wbuff + i));
-
- if(LenOfBuff != 0)
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
- }
- else
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
- }
-
- while(I2CMasterBusy(I2C1_BASE))
- ;
-
- i++;
- }
- }
- /**
- * @brief 从目标从机内部寄存器读出数据
- * @param DeviceAddr:从机地址
- RegAddr:寄存器地址
- wbuff:要读出的数据缓冲区
- LenOfReg:寄存器大小(按字节)(count by Byte)(必须小于等于4)
- LenOfBuff:读出数据的缓冲区长度(按字节)(count by Byte)
- * @retval 无
- */
- void MyI2C_ReadReg(uint8_t DeviceAddr, uint32_t RegAddr, uint8_t *rbuff, uint32_t LenOfReg, uint32_t LenOfBuff)
- {
- uint32_t i = 0;
-
- I2CMasterSlaveAddrSet(I2C1_BASE, DeviceAddr >> 1, false);
-
- while(LenOfReg--)
- {
- I2CMasterDataPut(I2C1_BASE, *((uint8_t *)(&RegAddr) + LenOfReg));
-
- if(i == 0)
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START);
- }
- else
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
- }
-
- while(I2CMasterBusy(I2C1_BASE))
- ;
- i++;
- }
-
- I2CMasterSlaveAddrSet(I2C1_BASE, DeviceAddr >> 1, true);
-
- i = 0;
-
- while(LenOfBuff--)
- {
- if((LenOfBuff == 0)&&(i==0))
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); //NACK
- }
- else
- {
- if(i == 0)
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); //ACK
- }
- else
- {
- if(LenOfBuff != 0)
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); //ACK
- }
- else
- {
- I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); //NACK
- }
- }
- }
-
- while(I2CMasterBusy(I2C1_BASE))
- ;
- *(rbuff + i) = (uint8_t)I2CMasterDataGet(I2C1_BASE);
-
- i++;
- }
- }
复制代码楼主可以参考我调通过的I2C的代码(TM4C123GH6PM)
一周热门 更多>