Howdy、
我有一个定制板、该板应通过 I2C 与 TM4C1294NCPDT 微控制器进行通信。 我之前使用过外设驱动程序来成功地使用 UART 和 GPIO 功能。 我一直在通过 I2C 外设库进行读取、 传感器库中的 I2C 主驱动程序和传感器库中的 LSM303DLHC 驱动程序、我已尽力将其设置为仅在循环中读取、但在 SCL 或 SDA 线上没有任何内容(使用逻辑分析仪查看、 均保持高电平)。 我复制了下面的代码并对重要事项进行了注释。 我在项目的其他部分有一些功能、但它们根本没有被使用。 感谢您的任何帮助!!
#include
#include
#include
#include
#include
#include
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "piconfconfig.h"//设置引脚
#include "inc/hw_i2c.h"//在 i2c 中使用
#include "inc/hw_timer.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/fpu.h"
#include "driverlib/uart.h"
#include "driverlib/i2c.h"//在 i2c 中使用
#include "sensorlib/i2cm_drv.c"//在 i2c 中使用
#include "sensorlib/i2cm_drv.h"//在 i2c 中使用
#include "sensorlib/lsm303dlhc_mag.c"//用于 i2c
#include "sensorlib/lsm303dlhc_mag.h"//用于 i2c
#include "sensorlib/hw_lsm303dlhc.h"//用于 i2c
//typedef 结构
//{
//浮动纬度;//忽略此项
//浮点经度;
//}位置;
//原型函数
void pinconfig (void);//调用配置引脚的函数
//void UartInit (void);
//void GpsConfigTX (void);
//Position GpsRXData (空);
//float convert_decime_degrees (float num);
//双 Azi (浮点 lat1、浮点 lon1、浮点 late2、浮点 lon2);//忽略这些
//void TimerSetup (void);
//void delay (uint32_t);
//void speedprofile (float、float、float、double);
tI2CMInstance sI2CInst;
volatile bool g_bLSM303DLHCCMagDone;
void inthandler (void);//创建了此函数,由中断处理程序调用
void LSM303DLHCMagCallback (void * pvCallbackData、uint_fast8_t ui8状态)
{
//查看是否发生错误。
if (ui8Status!= I2CM_STATUS_SUCCESS)
{
//发生了错误,所以如果需要,请在此处处理。 //这来自传感器库
}
//指示 LSM303DLHCMag 事务已完成。
G_bLSM303DLHCMADone = true;
}
int main (空)
{
//将时钟频率设置为120MHz
uint32_t freq = SysCtlClockFreqSet (SYSCTL_OSC_MAIN|SYSCTL_USE_PLL|SYSCTL_XTAL_25MHz | SYSCTL_CFG_VCO_480、120000000);
传感器库中的//变量
fMag[3]浮点型;
tLSM303DLHCMag sLSM303DLHCMag;
针对 SDA+SCL 的//configure 引脚(与引脚配置功能中的所有其他引脚一起完成
pinconfig();
//启用系统外设,并等到其就绪后再继续
SysCtlPeripheralEnable (SYSCTL_Periph_I2C7);
while (!SysCtlPeripheralReady (SYSCTL_Periph_I2C7))
{
}
//将 I2C 外设配置为 true 以实现快速数据传输
I2CMasterInitExpClk (I2C7 _BASE、120000000、TRUE);
//清除主中断
I2CMasterIntClear (I2C7 _BASE);
//声明指向 i2c 主中断处理程序的函数指针
void (*处理程序)(void);
handler =收件人;
//未启用 I2C 中断
//为启用的 I2C 中断注册中断处理程序
I2CIntRegister (I2C3_base、(*处理程序));
//初始化磁力计(param #2可能是 I2C 的基址)
int maginit = LSM303DLHCMagInit (&sLSM303DLHCMag、&sI2CInst、0x3D、LSM303DLHCMagCallback、0);
//连续读取
while (1)
{
G_bLSM303DLHCMADONE = false;
LSM303DLHCCMagDataRead (&sLSM303DLHCMag、LSM303DLHCCMagCallback、0);
while (!g_bLSM303DLHCMagDone)//在此处卡住
{
}
//
//获取新的磁力计读数。
//
LSM303DLHCMagDataMagnetoGetFloat (&sLSM303DLHCMag、&fMag[0]、&fMag[1]、&fMag[2]);
}
}
空收款人(空)
{
I2CMIntHandler (sI2CInst);
}
///------------------------------------------------
void pinconfig (void)
{
//启用外设时钟
MAP_SysCtlPeripheralEnable (SYSCTL_Periph_EPHY0);
MAP_SysCtlPeripheralEnable (SYSCTL_Periph_I2C7);
MAP_SysCtlPeripheralEnable (SYSCTL_Periph_UART4);
MAP_SysCtlPeripheralEnable (SYSCTL_Periph_GPIOA);
MAP_SysCtlPeripheralEnable (SYSCTL_Periph_GPIOD);
MAP_SysCtlPeripheralEnable (SYSCTL_Periph_GPIOF);
MAP_SysCtlPeripheralEnable (SYSCTL_Periph_GPIOK);
//
MAP_GPIOPinTypeGPIOOutput (GPIO_PORTK_base、GPIO_PIN_4);
//
MAP_GPIOPinTypeGPIOOutput (GPIO_PORTK_base、GPIO_PIN_6);
// GPIOOutput 的使能引脚 PA4
MAP_GPIOPinTypeGPIOOutput (GPIO_Porta_base、GPIO_PIN_4);
// GPIOOutput 的使能引脚 PA5
MAP_GPIOPinTypeGPIOOutput (GPIO_Porta_base、GPIO_PIN_5);
// GPIOOutput 的使能引脚 PA0
MAP_GPIOPinTypeGPIOOutput (GPIO_Porta_base、GPIO_PIN_0);
// GPIOOutput 的使能引脚 PA1
MAP_GPIOPinTypeGPIOOutput (GPIO_Porta_base、GPIO_PIN_1);
// GPIOOutput 的使能引脚 PA2
MAP_GPIOPinTypeGPIOOutput (GPIO_Porta_base、GPIO_PIN_2);
// GPIOOutput 的使能引脚 PA3
MAP_GPIOPinTypeGPIOOutput (GPIO_Porta_base、GPIO_PIN_3);
// GPIOOutput 的使能引脚 PF0
MAP_GPIOPinTypeGPIOOutput (GPIO_PORTF_BASE、GPIO_PIN_0);
// I2C7 I2C7SCL 的使能引脚 PD0
MAP_GPIOPinConfigure (GPIO_PD0_I2C7SCL);
MAP_GPIOPinTypeI2CSCL (GPIO_PORTD_BASE、GPIO_PIN_0);
// I2C7 I2C7SDA 的使能引脚 PD1
MAP_GPIOPinConfigure (GPIO_PD1_I2C7SDA);
MAP_GPIOPinTypeI2C (GPIO_PORTD_BASE、GPIO_PIN_1);
// UART4 U4RX 的使能引脚 PK0
MAP_GPIOPinConfigure (GPIO_PK0_U4RX);
MAP_GPIOPinTypeUART (GPIO_PORTK_base、GPIO_PIN_0);
// UART4 U4TX 的使能引脚 PK1
MAP_GPIOPinConfigure (GPIO_PK1_U4TX);
MAP_GPIOPinTypeUART (GPIO_PORTK_base、GPIO_PIN_1);
}