打印

28335与ADS1115的串口通信,程序跑不出来,求助!

[复制链接]
1459|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
江心秋月白|  楼主 | 2016-12-25 09:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
调试28335I2C和ADS1115的串口通信,程序如下,调试不出结果来,可以麻烦各位帮忙看一下问题在哪儿吗?



#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h"   // DSP2833x 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   0x00  //数据的写入地址低位
// 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
};//发送数据1
//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 *CurrentMsgPtr;        // Used in interrupts
Uint16 Count;
float result_0;
float result_1;
Uint16 CH;
//Uint16 FailCount;

void main(void) {
Uint16 Error;
Uint16 i;

CurrentMsgPtr = &I2cMsgOut1;
InitSysCtrl();
InitEPwm1Gpio();
//  InitEPwm2Gpio();
//  InitEPwm3Gpio();
//  InitEPwm4Gpio();

InitI2CGpio();
InitEPwm1Example();  
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 < I2C_MAX_BUFFER_SIZE; i++) {
I2cMsgIn1.MsgBuffer[i] = 0x0000;
}          //clearing incoming message buffer

// 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 (;;) {
//--------------------------------------------------------------------------
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP)
  {
    Error = I2CA_WriteData(&I2cMsgOut1);
    if (Error == I2C_SUCCESS)
    {
        CurrentMsgPtr = &I2cMsgOut1;
        I2cMsgOut1.MsgStatus = I2C_MSGSTAT_WRITE_BUSY;
    }
  }                      //end of write section
   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_BUSY;
             }
          }
    }

if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE) //#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 pointer and message status
        CurrentMsgPtr = &I2cMsgIn1;
        I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_NOSTOP_BUSY; //#define I2C_MSGSTAT_SEND_NOSTOP_BUSY  0x0021
}
else if (I2cMsgIn1.MsgStatus == I2C_MSGSTAT_RESTART)
{
while (I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{

}
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_READ_BUSY;
}

}


}

}

相关帖子

沙发
江心秋月白|  楼主 | 2016-12-25 09:59 | 只看该作者
void I2CA_Init(void) {
// Initialize I2C
I2caRegs.I2CSAR = 0x0048;        // Slave address - EEPROM control code

#if (CPU_FRQ_150MHZ)             // Default - For 150MHz SYSCLKOUT
I2caRegs.I2CPSC.all = 14; // Prescaler - need 7-12 Mhz on module clk (150/15 = 10MHz)
#endif
#if (CPU_FRQ_100MHZ)             // For 100 MHz SYSCLKOUT
I2caRegs.I2CPSC.all = 9; // Prescaler - need 7-12 Mhz on module clk (100/10 = 10MHz)
#endif

I2caRegs.I2CCLKL = 10;        // NOTE: must be non zero
I2caRegs.I2CCLKH = 5;        // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x28;        // 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 = 0x2260;        // Enable RXFIFO, clear RXFFINT,
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)  //停止条件为1
{
return I2C_STP_NOT_READY_ERROR;
}

// Setup slave address
I2caRegs.I2CSAR = msg->SlaveAddress;

// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)  //Bus busy
{
return I2C_BUS_BUSY_ERROR;
}
//判断总线空闲后进行数据发送
I2caRegs.I2CCNT = msg->NumOfBytes;

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 (I2caRegs.I2CSTR.bit.BB == 1) {
return I2C_BUS_BUSY_ERROR;
}

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;
IntSource = I2caRegs.I2CISRC.all;         // Read interrupt source
// Interrupt source = stop condition detected  停止条件检测
if (IntSource == I2C_SCD_ISRC) //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 (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_WRITE_BUSY)  //加了为了写
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE; //不要发送数据
}                                                       // 加了为了写0x00

}                                                      
else if (I2caRegs.I2CFFTX.bit.TXFFINT == 1)                              
{
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->MsgBuffer[0] = I2caRegs.I2CDRR;
CurrentMsgPtr->MsgBuffer[1] = I2caRegs.I2CDRR;
Count++;
CurrentMsgPtr->MsgBuffer[0] = CurrentMsgPtr->MsgBuffer[1] << 8| CurrentMsgPtr->MsgBuffer[0];
if (CH == 0x0001) {
result_0 = (float) (CurrentMsgPtr->MsgBuffer[0]) * 6.144 / 32768;;
//中的6.144改为你对应的电压范围值,不要带符号。如:你的电压值为±4.096V ,则 改为“ads1115_v=(float)pBuffer*4 .096/32768;”
}
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_SEND_NOSTOP;
I2caRegs.I2CFFTX.bit.TXFFINT == 1;
}
} else //
{
// Generate some error due to invalid interrupt source
asm("   ESTOP0");
}
// Enable future I2C (PIE Group 8) interrupts
PieCtrlRegs.PIEACK.all = PIEACK_GROUP8;
}

//===========================================================================
// No more.
//===========================================================================
void InitEPwm1Example() {
EPwm1Regs.TBPRD = 15000;                        // Set timer period 设置时钟周期
EPwm1Regs.TBPHS.half.TBPHS = 0x0000;             // Phase is 0      相位置0
EPwm1Regs.TBCTR = 0x0000;                      // Clear counter 计数初值为0

// Setup TBCLK
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // Count up
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;        // Disable phase loading
EPwm1Regs.TBCTL.bit.HSPCLKDIV = 5;       // Clock ratio to SYSCLKOUT
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;   // Slow just to observe on the scope
EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;          //0
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;          //1
// Setup shadow register load on ZERO

EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
//  EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
//   EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;

// Setup compare
EPwm1Regs.CMPA.half.CMPA = 12638.58;  //比较初始值

// Set actions
EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;             // Set PWM2A on Zero
EPwm1Regs.AQCTLA.bit.PRD = AQ_CLEAR;

//  14128;12638.58;11149;9659.43;8169.86;6680.29;5190.71;3701.14;2211.57;722
//     10;       9;     8;     7 ;      6;      5;      4;      3;     2;  1;
}

使用特权

评论回复
板凳
xyz549040622| | 2016-12-25 21:10 | 只看该作者
调驱动,抓波形,直到和数据手册上的波形一致。

使用特权

评论回复
地板
zhangmangui| | 2016-12-25 22:44 | 只看该作者
有条件先看看波形   这个比较有帮助

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

8

帖子

0

粉丝