佚名通过本文主要向大家介绍了dspf28335,i2c ad,stm32多路ad采集,多路ad采集,多路ad芯片等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: DSP F28335 中I2C 读取多路AD 怎么配置I2C呢
描述:
我把主程序的代码贴出来,大伙帮忙指导一下吧,感激!
#include "DSP28x_Project.h" // Device Headerfile and Examples Include 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);
interrupt void i2c_int1a_isr(void);
void InitEPwm1Example(void);
#define I2C_SLAVE_ADDR 0x48 //如果要用第二组AD 通道【5、6、7、8】 需要把此处改为0x49
#define I2C_NUMBYTES1 3
#define I2C_NUMBYTES2 1
#define I2C_NUMBYTES3 2
#define I2C_EEPROM_HIGH_ADDR 0x00
#define I2C_EEPROM_LOW_ADDR 0x30
#define I2C_MSGSTAT_WRITE_BUSY1 0x0012
#define I2C_MSGSTAT_INACTIVE1 0x0001
// 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_NUMBYTES1, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x01, // Msg Byte 1
0x40, // Msg Byte 2 [把0x40改成0x50 外部电源接5和6 对应通道2 把0x40改成0x60,就是通道3 把0x40改成0x70,就是通道4]
0xe3, // Msg Byte 3
};
// I2cMsgOut1.NumOfBytes=3;
struct I2CMSG I2cMsgOut2 = { I2C_MSGSTAT_SEND_WITHSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES2, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x00, // Msg Byte 1
};
//I2cMsgOut2.NumOfBytes=1;
struct I2CMSG I2cMsgIn1 = { I2C_MSGSTAT_SEND_NOSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES3, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR };
//------------------------------------------------------------------------------------------------------------------------------
struct I2CMSG I2cMsgAD2 = { I2C_MSGSTAT_SEND_WITHSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES1, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x01, // Msg Byte 1
0x50, // Msg Byte 2 [把0x40改成0x50 外部电源接5和6 对应通道2 把0x40改成0x60,就是通道3 把0x40改成0x70,就是通道4]
0xe3, // Msg Byte 3
};
struct I2CMSG I2cMsgAD2_2 = { I2C_MSGSTAT_SEND_WITHSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES2, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x00, // Msg Byte 1
};
struct I2CMSG I2cMsgIn2 = { I2C_MSGSTAT_SEND_NOSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES3, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR };
//------------------------------------------------------------------------------------------------------------------------------
struct I2CMSG *CurrentMsgPtr; // Used in interrupts
Uint16 Count;
float result;
float result_2;
Uint16 CH;
//Uint16 FailCount;
void main(void) {
Uint16 Error;
Uint16 i;
CurrentMsgPtr = &I2cMsgOut1;
InitSysCtrl();
InitEPwm1Gpio();
// InitEPwm2Gpio();
// InitEPwm3Gpio();
// InitEPwm4Gpio();
InitI2CGpio();
InitEPwm1Example();
DINT;
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
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
I2CA_Init();
Count = 0;
for (i = 0; i < 2; i++) {
I2cMsgIn1.MsgBuffer[i] = 0x0000;
}
for (i = 0; i < 2; i++) {
I2cMsgIn2.MsgBuffer[i] = 0x0000;
}
// Enable interrupts required for this example
// Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
PieCtrlRegs.PIEIER8.bit.INTx1 = 1;
// Enable CPU INT8 which is connected to PIE group 8
IER |= M_INT8;
EINT;
for (;;) {
//-----------------------------------AD1----------------------------------------
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP) {
Error = I2CA_WriteData(&I2cMsgOut1);
if (Error == I2C_SUCCESS) {
CurrentMsgPtr = &I2cMsgOut1;
I2cMsgOut1.MsgStatus = I2C_MSGSTAT_WRITE_BUSY;
}
}
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE) {
if (I2cMsgOut2.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP) {
Error = I2CA_WriteData(&I2cMsgOut2);
if (Error == I2C_SUCCESS) {
CurrentMsgPtr = &I2cMsgOut2;
I2cMsgOut2.MsgStatus = I2C_MSGSTAT_WRITE_BUSY1;
}
}
}
if (I2cMsgOut2.MsgStatus == I2C_MSGSTAT_INACTIVE1) //#define I2C_MSGSTAT_INACTIVE 0x0000
{
// Check incoming message status.
if (I2cMsgIn1.MsgStatus == I2C_MSGSTAT_SEND_NOSTOP) //#define I2C_MSGSTAT_SEND_NOSTOP 0x0020
{
// EEPROM address setup portion
while (I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS) // #define I2C_SUCCESS 0x0000
{
CH = 0x0001;
}
// Update current message point
描述:
I2C ADS1115 ADDSPF28335
如题,我要在开发板中通过I2C总线方式读取 一块ADS1115 芯片上的2路AD 转换结果,现在一路一路的读没有问题,2路或者多路就不行,不知道怎么配置I2C我把主程序的代码贴出来,大伙帮忙指导一下吧,感激!
#include "DSP28x_Project.h" // Device Headerfile and Examples Include 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);
interrupt void i2c_int1a_isr(void);
void InitEPwm1Example(void);
#define I2C_SLAVE_ADDR 0x48 //如果要用第二组AD 通道【5、6、7、8】 需要把此处改为0x49
#define I2C_NUMBYTES1 3
#define I2C_NUMBYTES2 1
#define I2C_NUMBYTES3 2
#define I2C_EEPROM_HIGH_ADDR 0x00
#define I2C_EEPROM_LOW_ADDR 0x30
#define I2C_MSGSTAT_WRITE_BUSY1 0x0012
#define I2C_MSGSTAT_INACTIVE1 0x0001
// 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_NUMBYTES1, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x01, // Msg Byte 1
0x40, // Msg Byte 2 [把0x40改成0x50 外部电源接5和6 对应通道2 把0x40改成0x60,就是通道3 把0x40改成0x70,就是通道4]
0xe3, // Msg Byte 3
};
// I2cMsgOut1.NumOfBytes=3;
struct I2CMSG I2cMsgOut2 = { I2C_MSGSTAT_SEND_WITHSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES2, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x00, // Msg Byte 1
};
//I2cMsgOut2.NumOfBytes=1;
struct I2CMSG I2cMsgIn1 = { I2C_MSGSTAT_SEND_NOSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES3, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR };
//------------------------------------------------------------------------------------------------------------------------------
struct I2CMSG I2cMsgAD2 = { I2C_MSGSTAT_SEND_WITHSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES1, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x01, // Msg Byte 1
0x50, // Msg Byte 2 [把0x40改成0x50 外部电源接5和6 对应通道2 把0x40改成0x60,就是通道3 把0x40改成0x70,就是通道4]
0xe3, // Msg Byte 3
};
struct I2CMSG I2cMsgAD2_2 = { I2C_MSGSTAT_SEND_WITHSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES2, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR, 0x00, // Msg Byte 1
};
struct I2CMSG I2cMsgIn2 = { I2C_MSGSTAT_SEND_NOSTOP, I2C_SLAVE_ADDR,
I2C_NUMBYTES3, I2C_EEPROM_HIGH_ADDR, I2C_EEPROM_LOW_ADDR };
//------------------------------------------------------------------------------------------------------------------------------
struct I2CMSG *CurrentMsgPtr; // Used in interrupts
Uint16 Count;
float result;
float result_2;
Uint16 CH;
//Uint16 FailCount;
void main(void) {
Uint16 Error;
Uint16 i;
CurrentMsgPtr = &I2cMsgOut1;
InitSysCtrl();
InitEPwm1Gpio();
// InitEPwm2Gpio();
// InitEPwm3Gpio();
// InitEPwm4Gpio();
InitI2CGpio();
InitEPwm1Example();
DINT;
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
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
I2CA_Init();
Count = 0;
for (i = 0; i < 2; i++) {
I2cMsgIn1.MsgBuffer[i] = 0x0000;
}
for (i = 0; i < 2; i++) {
I2cMsgIn2.MsgBuffer[i] = 0x0000;
}
// Enable interrupts required for this example
// Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
PieCtrlRegs.PIEIER8.bit.INTx1 = 1;
// Enable CPU INT8 which is connected to PIE group 8
IER |= M_INT8;
EINT;
for (;;) {
//-----------------------------------AD1----------------------------------------
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP) {
Error = I2CA_WriteData(&I2cMsgOut1);
if (Error == I2C_SUCCESS) {
CurrentMsgPtr = &I2cMsgOut1;
I2cMsgOut1.MsgStatus = I2C_MSGSTAT_WRITE_BUSY;
}
}
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE) {
if (I2cMsgOut2.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP) {
Error = I2CA_WriteData(&I2cMsgOut2);
if (Error == I2C_SUCCESS) {
CurrentMsgPtr = &I2cMsgOut2;
I2cMsgOut2.MsgStatus = I2C_MSGSTAT_WRITE_BUSY1;
}
}
}
if (I2cMsgOut2.MsgStatus == I2C_MSGSTAT_INACTIVE1) //#define I2C_MSGSTAT_INACTIVE 0x0000
{
// Check incoming message status.
if (I2cMsgIn1.MsgStatus == I2C_MSGSTAT_SEND_NOSTOP) //#define I2C_MSGSTAT_SEND_NOSTOP 0x0020
{
// EEPROM address setup portion
while (I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS) // #define I2C_SUCCESS 0x0000
{
CH = 0x0001;
}
// Update current message point