C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Tiva上的I2C接口大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在Tiva(德州仪器Cortex M4F ARM)TM4C129XNCZAD上,我遇到I2C接口问题.我已经在I2C模块4到端口K上启用了主设备,在I2C模块6到端口B上启用了从设备.我已经连接了两个I2C模块.使用德州仪器驱动程序库,我尝试使用I2C_MASTER_CMD_SINGLE_SEND命令发送1个字节.我花了很多时间让它工作,但SCK线保持低逻辑水平.我完全遵循TivaWare™外设驱动程序库用户指南,但通信不起作用.有人有一些经验吗?

有我的代码

#include <stdint.h>
#include <stdbool.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 "inc/tm4c129xnczad.h"

#define SLAVE_ADDRESS 0x3C

void  delay  (void)
{
    volatile uint32_t ui32Loop; 
    for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
}

volatile  uint32_t  result;

int  main  (void)
{
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHz); 
    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    SYSCTL_RCGCGPIO_R = SYSCTL_RCGCGPIO_R3 | SYSCTL_RCGCGPIO_R9 | SYSCTL_RCGCGPIO_R1;
    //
    // Do a dummy read to insert a few cycles after enabling the peripheral.
    //
    result = SYSCTL_RCGCGPIO_R;
    //
    // Enable the GPIO pin for the LED (PD3).  Set the direction as output,and
    // enable the GPIO pin for digital function.
    //
    GPIO_PORTD_AHB_DIR_R = 0x8;
    GPIO_PORTD_AHB_DEN_R = 0x8;
    GPIO_PORTK_DEN_R = 0xC0;        // Enable Port K for I2C module 4

    GPIO_PORTB_AHB_DEN_R = 0xC0;    // Enable Port B for I2C module 6

    SYSCTL_RCGCI2C_R = (1 << 4) | (1 << 6);  // Mode Clock GaTing Control for I2C modules 4 and 6
    GPIO_PORTK_AFSEL_R = 0xC0;      // Alternate Function SELEct PK6,PK7
    GPIO_PORTB_AHB_AFSEL_R = 0xC0;  // Alternate Function SELEct PB6,PB7
    GPIOPinConfigure(GPIO_PK6_I2C4SCL);
    GPIOPinConfigure(GPIO_PK7_I2C4SDA);
    GPIOPinConfigure(GPIO_PB6_I2C6SCL);
    GPIOPinConfigure(GPIO_PB7_I2C6SDA);

    GPIOPinTypeI2C(GPIO_PORTK_BASE,7);       // Configurtes SDA
    GPIOPinTypeI2CSCL(GPIO_PORTK_BASE,6);    // Configurtes SCL
    GPIOPinTypeI2C(GPIO_PORTB_BASE,7);       // Configurtes SDA
    GPIOPinTypeI2CSCL(GPIO_PORTB_BASE,6);    // Configurtes SCL

    I2CMasterInitExpClk(I2C4_BASE,SysCtlClockGet(),falsE);

    I2CSlaveEnable(I2C6_BASE);
    I2CSlaveInit(I2C6_BASE,SLAVE_ADDRESS);
    I2CMasterSlaveAddrSet(I2C4_BASE,SLAVE_ADDRESS,falsE);
    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Turn on the LED.
        //
        GPIO_PORTD_AHB_DATA_R |= 0x8;

        I2CMasterDataPut(I2C4_BASE,0x33);
        I2CMasterControl(I2C4_BASE,I2C_MASTER_CMD_SINGLE_SEND);
        //
        // Wait until the slave has received and ackNowledged the data.
        //
        while(!(I2CSlaveStatus(I2C6_BASE) & I2C_SLAVE_ACT_RREQ));
        //
        // Read the data from the slave.
        //
        result = I2CSlaveDataGet(I2C6_BASE);
        //
        // Wait until master module is done transferring.
        //
        while(I2CMasterBusy(I2C4_BASE));
        //
        // Delay for a bit.
        //
        delay ();
        //
        // Turn off the LED.
        //
        GPIO_PORTD_AHB_DATA_R &= ~(0x8);
        //
        // Delay for a bit.
        //
        delay ();
    }
}

解决方法

这个问题已经解决.有问题:

>填充外部上拉是至关重要的.
>使用GPIOPinTypeI2C()第二个参数作为位字段而不是位数.
> SysCtlClockSet()程序专门用于TM4C123设备.而是使用g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_XTAL_25MHZ | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_320),40000000);
>对于主时钟设置,请勿使用SysCtlClockGet()过程.这也专用于TM4C123器件.而是使用I2CMasterInitExpClk(I2C4_BASE,g_ui32SysClock,falsE);

这是更新的代码,

#include <stdint.h>
#include <stdbool.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 "inc/tm4c129xnczad.h"

#define SLAVE_ADDRESS 0x3C

void    delay   (void)
{
  volatile uint32_t ui32Loop;   
    for(ui32Loop = 0; ui32Loop < 200; ui32Loop++);
}


volatile uint32_t  result;
    uint32_t    g_ui32SysClock;

int main(void)
{
    g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_XTAL_25MHZ | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_320),40000000); 
    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    SYSCTL_RCGCGPIO_R = SYSCTL_RCGCGPIO_R3 | SYSCTL_RCGCGPIO_R9 | SYSCTL_RCGCGPIO_R1;
    //
    // Do a dummy read to insert a few cycles after enabling the peripheral.
    //
    result = SYSCTL_RCGCGPIO_R;
    //
    // Enable the GPIO pin for the LED (PD3).  Set the direction as output,and
    // enable the GPIO pin for digital function.
    //
    GPIO_PORTD_AHB_DIR_R = 0x8;
    GPIO_PORTD_AHB_DEN_R = 0x8;

    SYSCTL_RCGCI2C_R = (1 << 4) | (1 << 6); // Mode Clock GaTing Control for I2C modules 4 and 6

    GPIOPinConfigure(GPIO_PK6_I2C4SCL);
    GPIOPinConfigure(GPIO_PK7_I2C4SDA);
    GPIOPinConfigure(GPIO_PB6_I2C6SCL);
    GPIOPinConfigure(GPIO_PB7_I2C6SDA);

    GPIOPinTypeI2C(GPIO_PORTK_BASE,(1 << 7));       // Configures SDA
    GPIOPinTypeI2CSCL(GPIO_PORTK_BASE,(1 << 6));    // Configures SCL
    GPIOPinTypeI2C(GPIO_PORTB_BASE,(1 << 7));       // Configures SDA
    GPIOPinTypeI2CSCL(GPIO_PORTB_BASE,(1 << 6));    // Configures SCL

    I2CMasterInitExpClk(I2C4_BASE,I2C_MASTER_CMD_SINGLE_SEND);
        //
        // Wait until the slave has received and ackNowledged the data.
        //
        while(!(I2CSlaveStatus(I2C6_BASE) & I2C_SLAVE_ACT_RREQ));           
        //
        // Read the data from the slave.
        //
        result = I2CSlaveDataGet(I2C6_BASE);
        //
        // Wait until master module is done transferring.
        //
        while(I2CMasterBusy(I2C4_BASE));
        //
        // Delay for a bit.
        //
        delay   ();
        //
        // Turn off the LED.
        //
        GPIO_PORTD_AHB_DATA_R &= ~(0x8);
        //
        // Delay for a bit.
        //
        delay   ();
    }
}

大佬总结

以上是大佬教程为你收集整理的Tiva上的I2C接口全部内容,希望文章能够帮你解决Tiva上的I2C接口所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:ci2c接口