This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
大家好、TI 社区、
到目前为止、我可以成功地读取 MPU9250的 whoami 寄存器。 我下一步是写入可写寄存器(0x6B)、然后再次读取该寄存器以查看写入过程是否成功。
这是我的代码:
#include "driverlib.h" #include "MPU9250_reg.h" /*从地址*/ #define SLAVE_ADDRESS 0x68 volatile uint8_t A = 0; volatile uint8_t byte_counter = 0; volatile uint8_t write_read = 0; uint8_t SingData; uint8_t transmit_t uintbyte;uint2C_my_transmit_t uintbyte;uint_transmituSPI uint8_t txData ); int main (void){ WDT_A_HOLD (WDT_A_base); GPIO_setPeripheralModuleFunctionInputPin ( GPIO_PORT_P1、 GPIO_PIN6 + GPIO_PIN7、 GPIO_secondary 模块功能 ); /* *禁用 GPIO 上电默认高阻抗模式以激活 *先前配置的端口设置 * PMM_unlockLPM5 (); //进入 LPM4、带中断 /* WICHTIG、nicht VERGESSEN */ _bis_SR_register (GIE); //初始化传输数据包 transmitData = 0x75; //初始化主设备 EUSCI_B_I2C_initMasterParam param ={0}; param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK; param.i2cClk = CS_getSMCLK (); param.datarate = EUSCI_B_I2C_SET_DATA_RATE_100KBPS; param.byteCounterThreshold = 1; param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP; EUSCI_B_I2C_initMaster (EUSCI_B0_BASE、param); //指定从器件地址 EUSCI_B_I2C_setSlaveAddress (EUSCI_B0_BASE、 从器件地址 ); //设置为发送模式 EUSCI_B_I2C_setMode (EUSCI_B0_BASE、 EUSCI_B_I2C_Transmit 模式 ); //启用 I2C 模块以启动操作 EUSCI_B_I2C_ENABLE (EUSCI_B0_BASE); //启用 TXI 中断 EUSCI_B_I2C_enableInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit _INTERRUPT0); //发送 /*开始条件*/ //之后应激活中断 byte_counter = 1; WRITE_READ = 1; UCB0CTLW0 |= UCTXSTT; while (!(UCB0CTLW0 & UCBBUSY)){} byte_counter = 1; WRITE_READ = 0; EUSCI_B_I2C_enableInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit _INTERRUPT0); UCB0CTLW0 |= UCTXSTT; while (1) { _nop(); } } void my_eUSCI_B_I2C_masterSendSingleByte (uint16_t baseAddress、 uint8_t txData ) { //存储当前 TXIE 状态 uint16_t txieStatus = HWREG16 (baseAddress + OFS_UCBxIE)& UCTXIE; //禁用发送中断使能 HWREG16 (baseAddress + OFS_UCBxIE)&=~(UCTXIE); //发送启动条件。 HWREG16 (baseAddress + OFS_UCBxCTLW0)|= UCTR + UCTXSTT; //轮询发送中断标志。 while (!(HWREG16 (baseAddress + OFS_UCBxIFG)& UCTXIFG)); //发送单字节数据。 HWREG16 (baseAddress + OFS_UCBxTXBUF)= txData; //轮询发送中断标志。 while (!(HWREG16 (baseAddress + OFS_UCBxIFG)& UCTXIFG)); //发送停止条件。 //HWREG16 (baseAddress + OFS_UCBxCTLW0)|= UCTXSTP; //在再次启用中断之前清除发送中断标志 HWREG16 (baseAddress + OFS_UCBxIFG)&=~(UCTXIFG); //恢复发送中断使能 HWREG16 (baseAddress + OFS_UCBxIE)|= txieStatus; } #if defined (__TI_Compiler_version__)|| defined (__IAR_systems_ICC__) #pragma vector=USCI_B0_vector __interrupt #elif defined (__GICS_) __attribute USCI_ENSIR_(void )#USCI_0_interrupt 0_interrupt (UIB0_void 开关(__evo_in_range (UCB0IV、USCI_I2C_UCBIT9IFG)) { USCI_NONE 案例: //无中断中断中断; 中断; USCI_I2C_UCALIFG 案例: //仲裁丢失 中断; 案例 USCI_I2C_UCNACKIFG://接收到 NAK (仅限主器件) 中断; USCI_I2C_UCSTTIFG 案例: //用自己的地址检测到起始条件(只适用于从机模式) 中断; USCI_I2C_UCSTPIFG 案例: //检测到停止条件(主机和从机模式) 中断; USCI_I2C_UCRXIFG3案例: // RXIFG3 中断; USCI_I2C_UCTXIFG3案例: // TXIFG3 中断; USCI_I2C_UCRXIFG2案例: // RXIFG2 中断; USCI_I2C_UCTXIFG2案例: // TXIFG2 中断; USCI_I2C_UCRXIFG1案例: // RXIFG1 中断; USCI_I2C_UCTXIFG1案例: // TXIFG1 中断; USCI_I2C_UCRXIFG0案例: // RXIFG0^ //读取一个字节 RXData = UCB0RXBUF; 中断; USCI_I2C_UCTXIFG0案例: // TXIFG0 if (byte_counter = 1) { //发送寄存器进行读取 //UCB0TXBUF = 0x75; EUSCI_B_I2C_clearInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit _INTERRUPT0); UCB0TXBUF = 0x6B; byte_counter--; } 否则、如果(byte_counter = 0) { if (write_read) { //继续写入 EUSCI_B_I2C_clearInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit _INTERRUPT0); UCB0TXBUF = 0xFF; //快速添加新计数器 byte_counter = 100; } 其他 { //清除和禁用中断 EUSCI_B_I2C_clearInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit _INTERRUPT0); EUSCI_B_I2C_DisableInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit INTERRUPT0); 接收模式下为//set EUSCI_B_I2C_setMode (EUSCI_B0_BASE、 EUSCI_B_I2C_Receive_mode ); //启用接收中断 EUSCI_B_I2C_enableInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Receive_INTERRUPT0); //起始条件 UCB0CTLW0 |= UCTXSTT; //轮询启动条件传输 while (UCB0CTLW0和 UCTXSTT); //发送停止 UCB0CTLW0 |= UCTXSTP; } } 否则、如果(byte_counter = 100) { //发送停止 UCB0CTLW0 |= UCTXSTP; //清除和禁用中断 EUSCI_B_I2C_clearInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit _INTERRUPT0); EUSCI_B_I2C_DisableInterrupt (EUSCI_B0_BASE、EUSCI_B_I2C_Transmit _INTERRUPT0); } 其他 { //不大声 while (1){} 中断; 案例 USCI_I2C_UCBCNTIFG://达到字节计数限制(UCBxTBCNT) 中断; 案例 USCI_I2C_UCCLTOIFG://时钟低电平超时-时钟保持低电平的时间过长 中断; 案例 USCI_I2C_UCBIT9IFG://在发送的第9位上生成(用于调试) 中断; 默认值: 中断; } }
我首先读取、而不是我写入。 但我认为编写过程不起作用。 我不读取我写入的值、在写入过程中写入数据后 UCSTPIEG 标志会被置位。 根据第829页的熟悉用户指南、这意味着数据未发送。 (
"如果 UCTXSTP 为
在从器件地址的发送过程中或在 eUSCI_B 模块等待数据被写入时置位
进入 UCBxTXBUF 时、即使没有数据被发送到从器件、也会产生一个停止条件。 在这里
如果是、则 UCSTPIFG 被置位。 ")
为了使代码正常工作、我需要进行哪些更改?
提前感谢、致以诚挚的问候
很遗憾、我无法编辑我的帖子。 但是、我想更改一些内容。 first
最后、我想稍微解释一下代码。
i read and than i write. However i think that the writing process doesn
't work. I don'
t read the value i wrote and after writing the data in the writing process the UCSTPIEG flag
gets
set. According to the Familiy users guide on pg. 829
this
means data wasn't sent. (
"If UCTXSTP isset during the transmission of the slave address or while the eUSCI_B module waits for data to be writteninto UCBxTXBUF, a STOP condition is generated, even if no data was transmitted to the slave. In thiscase, the UCSTPIFG is set. "
)What
do
i need to change to make my code work?Thanks in advance and best regards
第二:我想指出、函数 my_EUSCI_B_I2C_masterSendSingleByte 未使用、因此可以删除。
非常感谢您的回答。 我只需要从驱动程序库中获取代码来驱动此代码、我认为我的发送过程与 sendmulitbyte 函数中的相同、除此之外、我使用中断例程。 我希望 有人能给我一个提示,问题可能是什么:)我不希望你为我写代码。
您好!
您是否 在代码上设置了 UCTXSTP? 如 UG 所述、当您置位该位时 UCSTPIFG 被置位。
对于您的问题、您似乎无法写入从器件。 我建议您对信号线进行范围界定、以确认主器件是否未发送数据或数据是否已发送、但从器件未接收到数据。
您还可以查看从器件的指南以了解从 I2C 写入数据的数据格式、并在主器件上遵循该指南。