TI中文支持网
TI专业的中文技术问题搜集分享网站

基于launchpad28027的I2C问题

这是controlsuit里面的I2C例程,我稍微修改了下,但是收不了数据,请解答一下,我用的slave是AT24C02

//V200
//#############################################################################
//
// File: f2802x_examples_ccsv4/i2c_eeprom/Example_F2802xI2c_eeprom.c
//
// Title: F2802x I2C EEPROM Example
//
// Group: C2000
// Target Device: TMS320F2802x
//
//! \addtogroup example_list
//! <h1>I2C EEPROM</h1>
//!
//! This program will write 1-14 words to EEPROM and read them back.
//! The data written and the EEPROM address written to are contained
//! in the message structure, I2cMsgOut1. The data read back will be
//! contained in the message structure I2cMsgIn1.
//!
//! This program will work with the on-board I2C EEPROM supplied on
//! the F2802x eZdsp or another EEPROM connected to the devices I2C bus
//! with a slave address of 0x50
//
// (C) Copyright 2012, Texas Instruments, Inc.
//#############################################################################
// $TI Release: f2802x Support Library v200 $
// $Release Date: Tue Jul 24 10:01:39 CDT 2012 $
//#############################################################################

#include "DSP28x_Project.h" // Device Headerfile and Examples Include File

// Note: I2C Macros used in this example can be found in the
// DSP2802x_I2C_defines.h file
// Prototype statements for functions found within this file.

void I2CA_Init(void);
Uint16 I2CA_WriteData(struct I2CMSG *msg);
Uint16 I2CA_ReadData(struct I2CMSG *msg);
unsigned char c;
interrupt void i2c_int1a_isr(void);
void pass(void);
void fail(void);

#define I2C_SLAVE_ADDR 0xa0//写操作
#define I2C_NUMBYTES 1
#define I2C_EEPROM_HIGH_ADDR 0x02
#define I2C_EEPROM_LOW_ADDR 0x30

// Global variables
// Two bytes will be used for the outgoing address,
// thus only setup 14 bytes maximum
struct I2CMSG I2cMsgOut1={I2C_MSGSTAT_SEND_WITHSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR,
0x36,
0x12};
struct I2CMSG I2cMsgIn1={ I2C_MSGSTAT_SEND_NOSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR
};

struct I2CMSG *CurrentMsgPtr; // Used in interrupts当前信息指针
Uint16 PassCount;
Uint16 FailCount,a=0,b=0,i_delay,j_delay;
void delay_ms (Uint16 ms)
{
for(i_delay=0;i_delay<ms;i_delay++) //外层循环若干毫秒
{
for(j_delay=0;j_delay<1041;j_delay++); //内层循环1毫秒定时
}
}
void main(void)
{
Uint16 Error;
Uint16 i;

CurrentMsgPtr = &I2cMsgOut1;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();
// Setup only the GP I/O only for I2C functionality
InitI2CGpio();

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
InitPieVectTable();

// Interrupts that are used in this example are re-mapped重新映射 to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.I2CINT1A = &i2c_int1a_isr;
EDIS; // This is needed to disable write to EALLOW protected registers

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
I2CA_Init();

// Step 5. User specific code

// Clear Counters
PassCount = 0;
FailCount = 0;

// Clear incoming message buffer
for (i = 0; i < (I2C_MAX_BUFFER_SIZE – 2); i++)
{
I2cMsgIn1.MsgBuffer[i] = 0x0000;
}
// I2caRegs.I2CMDR.bit.XA;
//清除输入信息缓存, but why?
// Enable interrupts required for this example

// Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
PieCtrlRegs.PIEIER8.bit.INTx1 = 1;//使能I2C中断

// Enable CPU INT8 which is connected to PIE group 8
IER |= M_INT8;//IER=IER|M_INT8 |为按位或,IER为中断使能寄存器
EINT;

// Application loop
for(;;)
{
//////////////////////////////////
// Write data to EEPROM section //
//////////////////////////////////

// Check the outgoing message to see if it should be sent.
// In this example it is initialized to send with a stop bit.初始化已经发送停止位

if(I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP)//I2cMsgOut1.MsgStatus初始值为I2C_MSGSTAT_SEND_WITHSTOP
{
Error = I2CA_WriteData(&I2cMsgOut1);
// If communication is correctly initiated, set msg status to busy
// and update CurrentMsgPtr for the interrupt service routine.
// Otherwise, do nothing and try again next loop. Once message is
// initiated, the I2C interrupts will handle the rest. Search for
// ICINTR1A_ISR in the i2c_eeprom_isr.c file.
if (Error == I2C_SUCCESS)
{ a++;
CurrentMsgPtr = &I2cMsgOut1;
I2cMsgOut1.MsgStatus = I2C_MSGSTAT_WRITE_BUSY;
}
// I2cMsgOut1.MsgStatus =I2C_MSGSTAT_SEND_WITHSTOP;
} // end of write section

///////////////////////////////////
// Read data from EEPROM section //
///////////////////////////////////

// Check outgoing message status. Bypass read section if status is
// not inactive.
// I2cMsgOut1.MsgStatus = I2C_MSGSTAT_INACTIVE;
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE)
{
// Check incoming message status.
if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
// EEPROM address setup portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
// Maybe setup an attempt counter to break an infinite while
// loop无限while循环. The EEPROM will send back a NACK否定回答 while it is performing执行
// a write operation. Even though the write communique is
// complete at this point, the EEPROM could still be busy
// programming the data. Therefore, multiple多个的 attempts are
// necessary.
}

// c=I2CA_ReadData(&I2cMsgIn1);
// Update current message pointer指针 and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_NOSTOP_BUSY;
}

// Once message has progressed past setting up the internal address内部地址
// of the EEPROM, send a restart to read the data bytes from the
// EEPROM. Complete the communique with a stop bit. MsgStatus is
// updated in the interrupt service routine. MSSG状态在中断服务函数里面更新
else if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_RESTART)
{
// Read data portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
// Maybe setup an attempt counter to break an infinite while
// loop.
}
// Update current message pointer and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_READ_BUSY;
delay_ms(100);

}
//I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_WITHSTOP;
} // end of read section
} // end of for(;;)
} // end of main

void I2CA_Init(void)
{
// Initialize I2C
I2caRegs.I2CSAR = 0xa0; // Slave address – EEPROM control code。I2C模块从地址寄存器

// I2CCLK = SYSCLK/(I2CPSC+1)描述:I2C时钟=系统时钟/(分频器+1)
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
I2caRegs.I2CPSC.all = 4; // Prescaler – need 7-12 Mhz on module clk
#endif

#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler – need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero必须非0
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero.
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts.

I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended

I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
I2caRegs.I2CMDR.all = 0x0020; //退出复位模式.
// I2caRegs.I2CMDR.bit.STP == 1;//dbfaklbjgapeg;
return;
}

Uint16 I2CA_WriteData(struct I2CMSG *msg)//写数据
{
Uint16 i;

// Wait until the STP bit is cleared from any previous master communication.

// Clearing of this bit by the module模块 is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}//等待直到停止位被清除

// Setup slave address
I2caRegs.I2CSAR = msg->SlaveAddress;//配置从机地址

// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}

// Setup number of bytes to send
// MsgBuffer + Address// 需要发送的数据个数:=需要发送的数据个数+1个数据长度的地址
I2caRegs.I2CCNT = msg->NumOfBytes+1;//?????????????????

// Setup data to send
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
// for (i=0; i<msg->NumOfBytes-2; i++)
for (i=0; i<msg->NumOfBytes; i++)
{
I2caRegs.I2CDXR = *(msg->MsgBuffer+i);
}//发送数据

// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20;//主机发送模式

return I2C_SUCCESS;//发送成功标志
}

Uint16 I2CA_ReadData(struct I2CMSG *msg)//读数据
{
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}

I2caRegs.I2CSAR = msg->SlaveAddress;//把信息中的从地址赋给从地址寄存器

if(msg->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
I2caRegs.I2CCNT = 2;
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
I2caRegs.I2CMDR.all = 0x2620; // Send data to setup EEPROM address
}
else if(msg->MsgStatus == I2C_MSGSTAT_RESTART)
{
I2caRegs.I2CCNT = msg->NumOfBytes; // Setup how many bytes to expect
I2caRegs.I2CMDR.all = 0x2C20; // Send restart as master receiver
}

return I2C_SUCCESS;
}

interrupt void i2c_int1a_isr(void) // I2C-A
{
Uint16 IntSource, i;
b++;
// Read interrupt source
IntSource = I2caRegs.I2CISRC.all;

// Interrupt source = stop condition detected
if(IntSource == I2C_SCD_ISRC)
{
// If completed message was writing data, reset msg to inactive state
if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_WRITE_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
}
else
{
// If a message receives a NACK during the address setup portion of the
// EEPROM read, the code further below included in the register access ready
// interrupt source code will generate a stop condition. After the stop
// condition is received (here), set the message status to try again.
// User may want to limit the number of retries before generating an error.
if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_SEND_NOSTOP;
}
// If completed message was reading EEPROM data, reset msg to inactive state
// and read data from FIFO.
else if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_READ_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
for(i=0; i < I2C_NUMBYTES; i++)
{
CurrentMsgPtr->MsgBuffer[i] = I2caRegs.I2CDRR;
}
{
// Check recieved data
for(i=0; i < I2C_NUMBYTES; i++)
{
if(I2cMsgIn1.MsgBuffer[i] == I2cMsgOut1.MsgBuffer[i])
{
PassCount++;
}
else
{
FailCount++;
}
}
if(PassCount == I2C_NUMBYTES)
{
pass();
}
else
{
fail();
}

}

}
}
} // end of stop condition detected

// Interrupt source = Register Access Ready
// This interrupt is used to determine when the EEPROM address setup portion of the
// read data communication is complete. Since no stop bit is commanded, this flag
// tells us when the message has been sent instead of the SCD flag. If a NACK is
// received, clear the NACK bit and command a stop. Otherwise, move on to the read
// data portion of the communication.
else if(IntSource == I2C_ARDY_ISRC)
{
if(I2caRegs.I2CSTR.bit.NACK == 1)
{
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT;
}
else if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_RESTART;
}
} // end of register access ready

else
{
// Generate some error due to invalid interrupt source
asm(" ESTOP0");
}

// Enable future I2C (PIE Group 8) interrupts
PieCtrlRegs.PIEACK.all = PIEACK_GROUP8;
}

void pass()
{
asm(" ESTOP0");
for(;;);
}

void fail()
{
asm(" ESTOP0");
for(;;);
}

//===========================================================================
// No more.
//===========================================================================

kqian0327:

你好,

我正在读你的代码。请问你IIC自己有外挂上拉电阻吗?

另外请附上你示波器得到的波形图,谢谢。

这是controlsuit里面的I2C例程,我稍微修改了下,但是收不了数据,请解答一下,我用的slave是AT24C02

//V200
//#############################################################################
//
// File: f2802x_examples_ccsv4/i2c_eeprom/Example_F2802xI2c_eeprom.c
//
// Title: F2802x I2C EEPROM Example
//
// Group: C2000
// Target Device: TMS320F2802x
//
//! \addtogroup example_list
//! <h1>I2C EEPROM</h1>
//!
//! This program will write 1-14 words to EEPROM and read them back.
//! The data written and the EEPROM address written to are contained
//! in the message structure, I2cMsgOut1. The data read back will be
//! contained in the message structure I2cMsgIn1.
//!
//! This program will work with the on-board I2C EEPROM supplied on
//! the F2802x eZdsp or another EEPROM connected to the devices I2C bus
//! with a slave address of 0x50
//
// (C) Copyright 2012, Texas Instruments, Inc.
//#############################################################################
// $TI Release: f2802x Support Library v200 $
// $Release Date: Tue Jul 24 10:01:39 CDT 2012 $
//#############################################################################

#include "DSP28x_Project.h" // Device Headerfile and Examples Include File

// Note: I2C Macros used in this example can be found in the
// DSP2802x_I2C_defines.h file
// Prototype statements for functions found within this file.

void I2CA_Init(void);
Uint16 I2CA_WriteData(struct I2CMSG *msg);
Uint16 I2CA_ReadData(struct I2CMSG *msg);
unsigned char c;
interrupt void i2c_int1a_isr(void);
void pass(void);
void fail(void);

#define I2C_SLAVE_ADDR 0xa0//写操作
#define I2C_NUMBYTES 1
#define I2C_EEPROM_HIGH_ADDR 0x02
#define I2C_EEPROM_LOW_ADDR 0x30

// Global variables
// Two bytes will be used for the outgoing address,
// thus only setup 14 bytes maximum
struct I2CMSG I2cMsgOut1={I2C_MSGSTAT_SEND_WITHSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR,
0x36,
0x12};
struct I2CMSG I2cMsgIn1={ I2C_MSGSTAT_SEND_NOSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR
};

struct I2CMSG *CurrentMsgPtr; // Used in interrupts当前信息指针
Uint16 PassCount;
Uint16 FailCount,a=0,b=0,i_delay,j_delay;
void delay_ms (Uint16 ms)
{
for(i_delay=0;i_delay<ms;i_delay++) //外层循环若干毫秒
{
for(j_delay=0;j_delay<1041;j_delay++); //内层循环1毫秒定时
}
}
void main(void)
{
Uint16 Error;
Uint16 i;

CurrentMsgPtr = &I2cMsgOut1;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();
// Setup only the GP I/O only for I2C functionality
InitI2CGpio();

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
InitPieVectTable();

// Interrupts that are used in this example are re-mapped重新映射 to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.I2CINT1A = &i2c_int1a_isr;
EDIS; // This is needed to disable write to EALLOW protected registers

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
I2CA_Init();

// Step 5. User specific code

// Clear Counters
PassCount = 0;
FailCount = 0;

// Clear incoming message buffer
for (i = 0; i < (I2C_MAX_BUFFER_SIZE – 2); i++)
{
I2cMsgIn1.MsgBuffer[i] = 0x0000;
}
// I2caRegs.I2CMDR.bit.XA;
//清除输入信息缓存, but why?
// Enable interrupts required for this example

// Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
PieCtrlRegs.PIEIER8.bit.INTx1 = 1;//使能I2C中断

// Enable CPU INT8 which is connected to PIE group 8
IER |= M_INT8;//IER=IER|M_INT8 |为按位或,IER为中断使能寄存器
EINT;

// Application loop
for(;;)
{
//////////////////////////////////
// Write data to EEPROM section //
//////////////////////////////////

// Check the outgoing message to see if it should be sent.
// In this example it is initialized to send with a stop bit.初始化已经发送停止位

if(I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP)//I2cMsgOut1.MsgStatus初始值为I2C_MSGSTAT_SEND_WITHSTOP
{
Error = I2CA_WriteData(&I2cMsgOut1);
// If communication is correctly initiated, set msg status to busy
// and update CurrentMsgPtr for the interrupt service routine.
// Otherwise, do nothing and try again next loop. Once message is
// initiated, the I2C interrupts will handle the rest. Search for
// ICINTR1A_ISR in the i2c_eeprom_isr.c file.
if (Error == I2C_SUCCESS)
{ a++;
CurrentMsgPtr = &I2cMsgOut1;
I2cMsgOut1.MsgStatus = I2C_MSGSTAT_WRITE_BUSY;
}
// I2cMsgOut1.MsgStatus =I2C_MSGSTAT_SEND_WITHSTOP;
} // end of write section

///////////////////////////////////
// Read data from EEPROM section //
///////////////////////////////////

// Check outgoing message status. Bypass read section if status is
// not inactive.
// I2cMsgOut1.MsgStatus = I2C_MSGSTAT_INACTIVE;
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE)
{
// Check incoming message status.
if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
// EEPROM address setup portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
// Maybe setup an attempt counter to break an infinite while
// loop无限while循环. The EEPROM will send back a NACK否定回答 while it is performing执行
// a write operation. Even though the write communique is
// complete at this point, the EEPROM could still be busy
// programming the data. Therefore, multiple多个的 attempts are
// necessary.
}

// c=I2CA_ReadData(&I2cMsgIn1);
// Update current message pointer指针 and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_NOSTOP_BUSY;
}

// Once message has progressed past setting up the internal address内部地址
// of the EEPROM, send a restart to read the data bytes from the
// EEPROM. Complete the communique with a stop bit. MsgStatus is
// updated in the interrupt service routine. MSSG状态在中断服务函数里面更新
else if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_RESTART)
{
// Read data portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
// Maybe setup an attempt counter to break an infinite while
// loop.
}
// Update current message pointer and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_READ_BUSY;
delay_ms(100);

}
//I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_WITHSTOP;
} // end of read section
} // end of for(;;)
} // end of main

void I2CA_Init(void)
{
// Initialize I2C
I2caRegs.I2CSAR = 0xa0; // Slave address – EEPROM control code。I2C模块从地址寄存器

// I2CCLK = SYSCLK/(I2CPSC+1)描述:I2C时钟=系统时钟/(分频器+1)
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
I2caRegs.I2CPSC.all = 4; // Prescaler – need 7-12 Mhz on module clk
#endif

#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler – need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero必须非0
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero.
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts.

I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended

I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
I2caRegs.I2CMDR.all = 0x0020; //退出复位模式.
// I2caRegs.I2CMDR.bit.STP == 1;//dbfaklbjgapeg;
return;
}

Uint16 I2CA_WriteData(struct I2CMSG *msg)//写数据
{
Uint16 i;

// Wait until the STP bit is cleared from any previous master communication.

// Clearing of this bit by the module模块 is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}//等待直到停止位被清除

// Setup slave address
I2caRegs.I2CSAR = msg->SlaveAddress;//配置从机地址

// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}

// Setup number of bytes to send
// MsgBuffer + Address// 需要发送的数据个数:=需要发送的数据个数+1个数据长度的地址
I2caRegs.I2CCNT = msg->NumOfBytes+1;//?????????????????

// Setup data to send
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
// for (i=0; i<msg->NumOfBytes-2; i++)
for (i=0; i<msg->NumOfBytes; i++)
{
I2caRegs.I2CDXR = *(msg->MsgBuffer+i);
}//发送数据

// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20;//主机发送模式

return I2C_SUCCESS;//发送成功标志
}

Uint16 I2CA_ReadData(struct I2CMSG *msg)//读数据
{
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}

I2caRegs.I2CSAR = msg->SlaveAddress;//把信息中的从地址赋给从地址寄存器

if(msg->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
I2caRegs.I2CCNT = 2;
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
I2caRegs.I2CMDR.all = 0x2620; // Send data to setup EEPROM address
}
else if(msg->MsgStatus == I2C_MSGSTAT_RESTART)
{
I2caRegs.I2CCNT = msg->NumOfBytes; // Setup how many bytes to expect
I2caRegs.I2CMDR.all = 0x2C20; // Send restart as master receiver
}

return I2C_SUCCESS;
}

interrupt void i2c_int1a_isr(void) // I2C-A
{
Uint16 IntSource, i;
b++;
// Read interrupt source
IntSource = I2caRegs.I2CISRC.all;

// Interrupt source = stop condition detected
if(IntSource == I2C_SCD_ISRC)
{
// If completed message was writing data, reset msg to inactive state
if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_WRITE_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
}
else
{
// If a message receives a NACK during the address setup portion of the
// EEPROM read, the code further below included in the register access ready
// interrupt source code will generate a stop condition. After the stop
// condition is received (here), set the message status to try again.
// User may want to limit the number of retries before generating an error.
if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_SEND_NOSTOP;
}
// If completed message was reading EEPROM data, reset msg to inactive state
// and read data from FIFO.
else if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_READ_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
for(i=0; i < I2C_NUMBYTES; i++)
{
CurrentMsgPtr->MsgBuffer[i] = I2caRegs.I2CDRR;
}
{
// Check recieved data
for(i=0; i < I2C_NUMBYTES; i++)
{
if(I2cMsgIn1.MsgBuffer[i] == I2cMsgOut1.MsgBuffer[i])
{
PassCount++;
}
else
{
FailCount++;
}
}
if(PassCount == I2C_NUMBYTES)
{
pass();
}
else
{
fail();
}

}

}
}
} // end of stop condition detected

// Interrupt source = Register Access Ready
// This interrupt is used to determine when the EEPROM address setup portion of the
// read data communication is complete. Since no stop bit is commanded, this flag
// tells us when the message has been sent instead of the SCD flag. If a NACK is
// received, clear the NACK bit and command a stop. Otherwise, move on to the read
// data portion of the communication.
else if(IntSource == I2C_ARDY_ISRC)
{
if(I2caRegs.I2CSTR.bit.NACK == 1)
{
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT;
}
else if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_RESTART;
}
} // end of register access ready

else
{
// Generate some error due to invalid interrupt source
asm(" ESTOP0");
}

// Enable future I2C (PIE Group 8) interrupts
PieCtrlRegs.PIEACK.all = PIEACK_GROUP8;
}

void pass()
{
asm(" ESTOP0");
for(;;);
}

void fail()
{
asm(" ESTOP0");
for(;;);
}

//===========================================================================
// No more.
//===========================================================================

ruoyu gu:

回复 kqian0327:

程序初始化的时候已经将内部上拉电阻使能,我外部没有加上拉电阻。明天将波形发到这儿。谢谢你阅读代码!

这是controlsuit里面的I2C例程,我稍微修改了下,但是收不了数据,请解答一下,我用的slave是AT24C02

//V200
//#############################################################################
//
// File: f2802x_examples_ccsv4/i2c_eeprom/Example_F2802xI2c_eeprom.c
//
// Title: F2802x I2C EEPROM Example
//
// Group: C2000
// Target Device: TMS320F2802x
//
//! \addtogroup example_list
//! <h1>I2C EEPROM</h1>
//!
//! This program will write 1-14 words to EEPROM and read them back.
//! The data written and the EEPROM address written to are contained
//! in the message structure, I2cMsgOut1. The data read back will be
//! contained in the message structure I2cMsgIn1.
//!
//! This program will work with the on-board I2C EEPROM supplied on
//! the F2802x eZdsp or another EEPROM connected to the devices I2C bus
//! with a slave address of 0x50
//
// (C) Copyright 2012, Texas Instruments, Inc.
//#############################################################################
// $TI Release: f2802x Support Library v200 $
// $Release Date: Tue Jul 24 10:01:39 CDT 2012 $
//#############################################################################

#include "DSP28x_Project.h" // Device Headerfile and Examples Include File

// Note: I2C Macros used in this example can be found in the
// DSP2802x_I2C_defines.h file
// Prototype statements for functions found within this file.

void I2CA_Init(void);
Uint16 I2CA_WriteData(struct I2CMSG *msg);
Uint16 I2CA_ReadData(struct I2CMSG *msg);
unsigned char c;
interrupt void i2c_int1a_isr(void);
void pass(void);
void fail(void);

#define I2C_SLAVE_ADDR 0xa0//写操作
#define I2C_NUMBYTES 1
#define I2C_EEPROM_HIGH_ADDR 0x02
#define I2C_EEPROM_LOW_ADDR 0x30

// Global variables
// Two bytes will be used for the outgoing address,
// thus only setup 14 bytes maximum
struct I2CMSG I2cMsgOut1={I2C_MSGSTAT_SEND_WITHSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR,
0x36,
0x12};
struct I2CMSG I2cMsgIn1={ I2C_MSGSTAT_SEND_NOSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR
};

struct I2CMSG *CurrentMsgPtr; // Used in interrupts当前信息指针
Uint16 PassCount;
Uint16 FailCount,a=0,b=0,i_delay,j_delay;
void delay_ms (Uint16 ms)
{
for(i_delay=0;i_delay<ms;i_delay++) //外层循环若干毫秒
{
for(j_delay=0;j_delay<1041;j_delay++); //内层循环1毫秒定时
}
}
void main(void)
{
Uint16 Error;
Uint16 i;

CurrentMsgPtr = &I2cMsgOut1;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();
// Setup only the GP I/O only for I2C functionality
InitI2CGpio();

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
InitPieVectTable();

// Interrupts that are used in this example are re-mapped重新映射 to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.I2CINT1A = &i2c_int1a_isr;
EDIS; // This is needed to disable write to EALLOW protected registers

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
I2CA_Init();

// Step 5. User specific code

// Clear Counters
PassCount = 0;
FailCount = 0;

// Clear incoming message buffer
for (i = 0; i < (I2C_MAX_BUFFER_SIZE – 2); i++)
{
I2cMsgIn1.MsgBuffer[i] = 0x0000;
}
// I2caRegs.I2CMDR.bit.XA;
//清除输入信息缓存, but why?
// Enable interrupts required for this example

// Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
PieCtrlRegs.PIEIER8.bit.INTx1 = 1;//使能I2C中断

// Enable CPU INT8 which is connected to PIE group 8
IER |= M_INT8;//IER=IER|M_INT8 |为按位或,IER为中断使能寄存器
EINT;

// Application loop
for(;;)
{
//////////////////////////////////
// Write data to EEPROM section //
//////////////////////////////////

// Check the outgoing message to see if it should be sent.
// In this example it is initialized to send with a stop bit.初始化已经发送停止位

if(I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP)//I2cMsgOut1.MsgStatus初始值为I2C_MSGSTAT_SEND_WITHSTOP
{
Error = I2CA_WriteData(&I2cMsgOut1);
// If communication is correctly initiated, set msg status to busy
// and update CurrentMsgPtr for the interrupt service routine.
// Otherwise, do nothing and try again next loop. Once message is
// initiated, the I2C interrupts will handle the rest. Search for
// ICINTR1A_ISR in the i2c_eeprom_isr.c file.
if (Error == I2C_SUCCESS)
{ a++;
CurrentMsgPtr = &I2cMsgOut1;
I2cMsgOut1.MsgStatus = I2C_MSGSTAT_WRITE_BUSY;
}
// I2cMsgOut1.MsgStatus =I2C_MSGSTAT_SEND_WITHSTOP;
} // end of write section

///////////////////////////////////
// Read data from EEPROM section //
///////////////////////////////////

// Check outgoing message status. Bypass read section if status is
// not inactive.
// I2cMsgOut1.MsgStatus = I2C_MSGSTAT_INACTIVE;
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE)
{
// Check incoming message status.
if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
// EEPROM address setup portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
// Maybe setup an attempt counter to break an infinite while
// loop无限while循环. The EEPROM will send back a NACK否定回答 while it is performing执行
// a write operation. Even though the write communique is
// complete at this point, the EEPROM could still be busy
// programming the data. Therefore, multiple多个的 attempts are
// necessary.
}

// c=I2CA_ReadData(&I2cMsgIn1);
// Update current message pointer指针 and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_NOSTOP_BUSY;
}

// Once message has progressed past setting up the internal address内部地址
// of the EEPROM, send a restart to read the data bytes from the
// EEPROM. Complete the communique with a stop bit. MsgStatus is
// updated in the interrupt service routine. MSSG状态在中断服务函数里面更新
else if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_RESTART)
{
// Read data portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
// Maybe setup an attempt counter to break an infinite while
// loop.
}
// Update current message pointer and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_READ_BUSY;
delay_ms(100);

}
//I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_WITHSTOP;
} // end of read section
} // end of for(;;)
} // end of main

void I2CA_Init(void)
{
// Initialize I2C
I2caRegs.I2CSAR = 0xa0; // Slave address – EEPROM control code。I2C模块从地址寄存器

// I2CCLK = SYSCLK/(I2CPSC+1)描述:I2C时钟=系统时钟/(分频器+1)
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
I2caRegs.I2CPSC.all = 4; // Prescaler – need 7-12 Mhz on module clk
#endif

#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler – need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero必须非0
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero.
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts.

I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended

I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
I2caRegs.I2CMDR.all = 0x0020; //退出复位模式.
// I2caRegs.I2CMDR.bit.STP == 1;//dbfaklbjgapeg;
return;
}

Uint16 I2CA_WriteData(struct I2CMSG *msg)//写数据
{
Uint16 i;

// Wait until the STP bit is cleared from any previous master communication.

// Clearing of this bit by the module模块 is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}//等待直到停止位被清除

// Setup slave address
I2caRegs.I2CSAR = msg->SlaveAddress;//配置从机地址

// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}

// Setup number of bytes to send
// MsgBuffer + Address// 需要发送的数据个数:=需要发送的数据个数+1个数据长度的地址
I2caRegs.I2CCNT = msg->NumOfBytes+1;//?????????????????

// Setup data to send
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
// for (i=0; i<msg->NumOfBytes-2; i++)
for (i=0; i<msg->NumOfBytes; i++)
{
I2caRegs.I2CDXR = *(msg->MsgBuffer+i);
}//发送数据

// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20;//主机发送模式

return I2C_SUCCESS;//发送成功标志
}

Uint16 I2CA_ReadData(struct I2CMSG *msg)//读数据
{
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}

I2caRegs.I2CSAR = msg->SlaveAddress;//把信息中的从地址赋给从地址寄存器

if(msg->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
I2caRegs.I2CCNT = 2;
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
I2caRegs.I2CMDR.all = 0x2620; // Send data to setup EEPROM address
}
else if(msg->MsgStatus == I2C_MSGSTAT_RESTART)
{
I2caRegs.I2CCNT = msg->NumOfBytes; // Setup how many bytes to expect
I2caRegs.I2CMDR.all = 0x2C20; // Send restart as master receiver
}

return I2C_SUCCESS;
}

interrupt void i2c_int1a_isr(void) // I2C-A
{
Uint16 IntSource, i;
b++;
// Read interrupt source
IntSource = I2caRegs.I2CISRC.all;

// Interrupt source = stop condition detected
if(IntSource == I2C_SCD_ISRC)
{
// If completed message was writing data, reset msg to inactive state
if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_WRITE_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
}
else
{
// If a message receives a NACK during the address setup portion of the
// EEPROM read, the code further below included in the register access ready
// interrupt source code will generate a stop condition. After the stop
// condition is received (here), set the message status to try again.
// User may want to limit the number of retries before generating an error.
if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_SEND_NOSTOP;
}
// If completed message was reading EEPROM data, reset msg to inactive state
// and read data from FIFO.
else if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_READ_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
for(i=0; i < I2C_NUMBYTES; i++)
{
CurrentMsgPtr->MsgBuffer[i] = I2caRegs.I2CDRR;
}
{
// Check recieved data
for(i=0; i < I2C_NUMBYTES; i++)
{
if(I2cMsgIn1.MsgBuffer[i] == I2cMsgOut1.MsgBuffer[i])
{
PassCount++;
}
else
{
FailCount++;
}
}
if(PassCount == I2C_NUMBYTES)
{
pass();
}
else
{
fail();
}

}

}
}
} // end of stop condition detected

// Interrupt source = Register Access Ready
// This interrupt is used to determine when the EEPROM address setup portion of the
// read data communication is complete. Since no stop bit is commanded, this flag
// tells us when the message has been sent instead of the SCD flag. If a NACK is
// received, clear the NACK bit and command a stop. Otherwise, move on to the read
// data portion of the communication.
else if(IntSource == I2C_ARDY_ISRC)
{
if(I2caRegs.I2CSTR.bit.NACK == 1)
{
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT;
}
else if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_RESTART;
}
} // end of register access ready

else
{
// Generate some error due to invalid interrupt source
asm(" ESTOP0");
}

// Enable future I2C (PIE Group 8) interrupts
PieCtrlRegs.PIEACK.all = PIEACK_GROUP8;
}

void pass()
{
asm(" ESTOP0");
for(;;);
}

void fail()
{
asm(" ESTOP0");
for(;;);
}

//===========================================================================
// No more.
//===========================================================================

ruoyu gu:

回复 kqian0327:

这个是我观察的发送和接收FIFO

赞(0)
未经允许不得转载:TI中文支持网 » 基于launchpad28027的I2C问题
分享到: 更多 (0)