这是从官网上找的I2C例程,结果还是无法完成诊断和调试过程。有没有好心人帮我解答嘛,任务很紧急死人啦
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#define NUM_I2C_DATA 3
#define SLAVE_ADDRESS 0x3C
void
InitConsole(void)
{
//
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
//
// Enable UART0 so that we can configure the clock.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
}
int
main(void)
{
uint32_t pui32DataTx[NUM_I2C_DATA];
uint32_t pui32DataRx[NUM_I2C_DATA];
uint32_t ui32Index;
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01;
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
I2CSlaveEnable(I2C0_BASE);
I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
InitConsole();
UARTprintf("I2C Loopback Example ->");
UARTprintf("\n Module = I2C0");
UARTprintf("\n Mode = Single Send/Receive");
UARTprintf("\n Rate = 100kbps\n\n");
pui32DataTx[0] = 'I';
pui32DataTx[1] = '2';
pui32DataTx[2] = 'C';
for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
{
pui32DataRx[ui32Index] = 0;
}
UARTprintf("Tranferring from: Master -> Slave\n");
for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
{
UARTprintf(" Sending: '%c' . . . ", pui32DataTx[ui32Index]);
I2CMasterDataPut(I2C0_BASE, pui32DataTx[ui32Index]);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
pui32DataRx[ui32Index] = I2CSlaveDataGet(I2C0_BASE);
while(I2CMasterBusy(I2C0_BASE))
{
}
UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]);
}
for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
{
pui32DataRx[ui32Index] = 0;
}
UARTprintf("\n\nTranferring from: Slave -> Master\n");
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ))
{
}
for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
{
UARTprintf(" Sending: '%c' . . . ", pui32DataTx[ui32Index]);
I2CSlaveDataPut(I2C0_BASE, pui32DataTx[ui32Index]);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ))
{
}
pui32DataRx[ui32Index] = I2CMasterDataGet(I2C0_BASE);
UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]);
}
UARTprintf("\nDone.\n\n");
return(0);
}
xyz549040622:
看到发送完第一个字符就没反应了,接收不到字符。回环模式下是不是应该短接I2c的两个引脚呢,你短接看看。
jiahao he:
回复 xyz549040622:
刚刚弄了一下,能收发了。不知道怎么弄好的
Terry Han:
回复 jiahao he:
能分享一下怎么调试好的最好了,如果调试OK了建议将该问题结贴。
xyz549040622:
回复 jiahao he:
太坑了,我就说官方的例程没问题的。我以前试过的,网上的例程有时候就是害人。有次玩STM32的板子,搞了好久都不成功,后来才发现是网上下载的库的原因。
Terry Han:
回复 jiahao he:
非常感谢你的回复及分享!
user5318963:
回复 jiahao he:
您好,
我正在调试tm4c的i2c回环模式,遇到了与题主一样的问题,程序执行到‘等待通讯完成’这一句代码不再往下执行,题主说的解决办法我看的不太明白,请问是否可以详细说一下?383519646@qq.com
祝好!