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.

TM4C123GH6PM I2C不能读写AT24C01的问题

  SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
                    SYSCTL_OSC_MAIN);
    SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C0 );
    SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
	
	//配置相应控制端口
	GPIOPinConfigure( GPIO_PB3_I2C0SDA );
	GPIOPinConfigure( GPIO_PB2_I2C0SCL );
	GPIOPinTypeI2CSCL( GPIO_PORTB_BASE, GPIO_PIN_2 );
	GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_3 );
	//设置I2C模块传输速率,并使能主机控制功能
    
	I2CMasterInitExpClk( I2C0_BASE, SysCtlClockGet(), false );
	I2CMasterEnable( I2C0_BASE );
    //
    // Enable and configure the GPIO port for the LED operation.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED);
    I2CMasterSlaveAddrSet( I2C0_BASE, 0x50, false);	
    I2CMasterDataPut( I2C0_BASE, 0x00 );												//写入主机数据(寄存器地址)
    while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );
    I2CMasterControl( I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START );					    //主机数据开始传输
    while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );
    I2CMasterDataPut( I2C0_BASE, 0x06  );											    //写入主机数据(寄存器内容)
    while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );
    I2CMasterControl( I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND );					//主机数据+stop传输
    while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );
    
    I2CMasterSlaveAddrSet( I2C0_BASE, 0x50, false);										//????′?ê?·??ò?a?÷?úD′
    I2CMasterDataPut( I2C0_BASE, 0x20 ); 										    //D′è??÷?úêy?Y(êy?Y??′??÷μ??·)
    while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );
    I2CMasterControl( I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND  ); 							
    while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );   
    //
    // Loop Forever
    //
    while(1)
    {
        //
        // Turn on the LED
        //
        GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED);
                 
		I2CMasterSlaveAddrSet( I2C0_BASE, 0x50, true);	
		I2CMasterControl( I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE );              //μ¥′??óê?
		while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );		
		Result_H = I2CMasterDataGet( I2C0_BASE ); 	
        //
        // Delay for a bit
        //
        SysCtlDelay(2000000);

        //
        // Turn on the LED
        //
        GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, BLUE_LED);

        //
        // Delay for a bit
        //
        SysCtlDelay(2000000);
    }

可以读其他的,就是不能读写AT24C01,好奇怪

  • 是不是你的24c01坏了,抓波形看看。24c01的地址好像和别的系列不一样。前提是你的I2C的配置是OK的。最好逻辑分析仪抓波形

  • 给你个24c02的测试代码,还是觉得你器件地址的问题

    This is the code that has worked for me:
    
    MY SLAVE ADDR = 0x50
    
    THE CODE WAS ORIGINALLY GIVEN IN SEPARATE FORUMS HERE... I HAVE JUST MODIFIED A BIT ACCORDING TO MY USE
    
    I2C MODULE USED = I2C1
    
    HAVE SAVED SOME DATA AND RECALLED IT BACK USING REPEATED START TECHNIQUE.
    
    THIS CODE I HAVE TESTED WITH PA6 = SCL & PA7 = SDA AND WORKD FINE WITH 24C02
    
    #include <stdint.h> // ift
    #include <stdbool.h>
    #include "inc/hw_types.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/rom.h"
    #include "driverlib/pin_map.h"
    #include "inc/tm4c123gh6pm.h"
    #include "inc/hw_i2c.h"
    
    int i = 0;
    uint8_t lcd[5]={0x99,0x88,0x77,0x66,0x54};
    
    
    int main(void)
    {
    unsigned long data;
    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal on your board.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);
    
    //
    // The I2C1 peripheral must be enabled before use.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
    
    //
    // For this example I2C0 is used with PortB[3:2]. The actual port and
    // pins used may be different on your part, consult the data sheet for
    // more information. GPIO port B needs to be enabled so these pins can
    // be used.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    
    //
    // Select the I2C function for these pins. This function will also
    // configure the GPIO pins pins for I2C operation, setting them to
    // open-drain operation with weak pull-ups. Consult the data sheet
    // to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using.
    //
    //GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);
    GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); // I2CSCL
    GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);
    
    //
    // Configure the pin muxing for I2C0 functions on port B2 and B3.
    // 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_PA6_I2C1SCL);
    GPIOPinConfigure(GPIO_PA7_I2C1SDA);
    
    //
    // Enable and initialize the I2C0 master module. Use the system clock for
    // the I2C0 module. The last parameter sets the I2C data transfer rate.
    // If false the data rate is set to 100kbps and if true the data rate will
    // be set to 400kbps. For this example we will use a data rate of 100kbps.
    //
    I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), false);
    
    //
    // Tell the master module what address it will place on the bus when
    // communicating with the slave. Set the address to SLAVE_ADDRESS
    // (as set in the slave module). The receive parameter is set to false
    // which indicates the I2C Master is initiating a writes to the slave. If
    // true, that would indicate that the I2C Master is initiating reads from
    // the slave.
    //
    
    while(1)
    {
    I2CMasterSlaveAddrSet(I2C1_BASE, 0x50, false); // The address of the Slave is 0x50
    
    I2CMasterDataPut(I2C1_BASE, 0x10); // Addr 0x10
    
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    
    while(I2CMasterBusy(I2C1_BASE));
    
    for (i=0;i<5;i++)
    {
    I2CMasterDataPut(I2C1_BASE, lcd[i]); // data to be saved at Addr 0x11 under autoincrement mode
    
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
    
    while(I2CMasterBusy(I2C1_BASE));
    
    }
    
    //I2CMasterDataPut(I2C1_BASE, 0xee); // data to be saved at Addr 0x12 under autoincrement mode
    
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    
    while(I2CMasterBusy(I2C1_BASE));
    //////// write complete/////////////////////////////////////////////////////////////////////////////////
    SysCtlDelay(5500000); // writing time
    
    ///////////////////////////////////////////////////////////////
    // Read at address 0x11
    I2CMasterSlaveAddrSet(I2C1_BASE, 0x50, false);
    
    I2CMasterDataPut(I2C1_BASE, 0x10); // Addr 0x11
    
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND);
    
    while(I2CMasterBusy(I2C1_BASE));
    
    
    I2CMasterSlaveAddrSet(I2C1_BASE, 0x50, true);
    
    data = I2CMasterDataGet(I2C1_BASE); //Receive
    
    //I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
    
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
    //
    while(I2CMasterBusy(I2C1_BASE));
    
    data = I2CMasterDataGet(I2C1_BASE); //test
    
    for (i=0;i<4;i++)
    {
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    //
    while(I2CMasterBusy(I2C1_BASE));
    
    data = I2CMasterDataGet(I2C1_BASE); //Receive
    }
    
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    //
    while(I2CMasterBusy(I2C1_BASE));
    
    }
    //return(0);
    }
  • 我试了一下,还是一样的死掉了,如果说是芯片坏了的话,我试了好几个了。真不知道为什么

  • 死掉了是什么意思?这种情况,建议你再次检查硬件的连接。庵后抓波形看看,看看波形出去了没有。