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.

[参考译文] TMS320F280025C:DS3231N

Guru**** 657930 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/1306409/tms320f280025c-ds3231n

器件型号:TMS320F280025C

//#############################################################################
//
// FILE:   i2c_ex4_RTC_polling.c
//
// TITLE:  I2C RTC Write / Read using polling
//
//! \addtogroup driver_example_list
//! <h1>I2C RTC</h1>
//!
//! This program will shows how to perform different RTC write and read
//! commands using I2C polling method
//! RTC used for this example is AT24C256
//!
//! \b External \b Connections \n
//!  - Connect external I2C RTC at address 0x50
//!  --------------------------------
//!    Signal   |  I2CA   |  RTC
//!  --------------------------------
//!     SCL     | DEVICE_GPIO_PIN_SCLA   |  SCL
//!     SDA     | DEVICE_GPIO_PIN_SDAA   |  SDA
//!     Make sure to connect GND pins if RTC and C2000 device are in different board.
//!  --------------------------------

//! //Example 1: RTC Byte Write
//! //Example 2: RTC Byte Read
//! //Example 3: RTC word (16-bit) write
//! //Example 4: RTC word (16-bit) read
//! //Example 5: RTC Page write
//! //Example 6: RTC word Paged read
//!
//! \b Watch \b Variables \n
//!  - \b TX_MsgBuffer - Message buffer which stores the data to be transmitted
//!  - \b RX_MsgBuffer - Message buffer which stores the data to be received
//!
//!
//#############################################################################
//
//
// $Copyright:
// Copyright (C) 2023 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
//   Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// 
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the   
//   distribution.
// 
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################

//
// Included Files
//
#include "driverlib.h"
#include "device.h"

#include "i2cLib_FIFO_polling.h"


//
// Globals
//
struct I2CHandle RTC;

struct I2CHandle *currentMsgPtr;                   // Used in interrupt

//!  --------------------------------
//!    Signal   |  I2CA   |  RTC
//!  --------------------------------
//!     SCL     | DEVICE_GPIO_PIN_SCLA   |  SCL
//!     SDA     | DEVICE_GPIO_PIN_SDAA   |  SDA
//!  --------------------------------


uint16_t passCount = 0;
uint16_t failCount = 0;

uint16_t AvailableI2C_targets[20];
uint16_t TX_MsgBuffer[MAX_BUFFER_SIZE];
uint16_t RX_MsgBuffer[MAX_BUFFER_SIZE];
uint32_t ControlAddr;
uint16_t status;
//uint8_t set_time[3];
typedef struct{
   unsigned int seconds;
      unsigned int minutes ;
      unsigned int hour ;
      unsigned int dayofweek;
      unsigned int dayofmonth;
      unsigned int month ;
      unsigned int year ;
}TIME;

TIME time;


void fail(void);
void pass(void);

void I2C_GPIO_init(void);
void I2Cinit(void);
void verifyRTCRead(void);
void Get_Time (void);
unsigned int bcdToDec(unsigned int );
#define ds_3231N 0x68;
//
// Main
//
void main(void)
{
    //
    // Initialize device clock and peripherals
    //
    Device_init();

    //
    // Disable pin locks and enable internal pullups.
    //
    Device_initGPIO();

    //
    // Initialize I2C pins
    //
    I2C_GPIO_init();

    //
    // Initialize PIE and clear PIE registers. Disable CPU interrupts.
    //
    Interrupt_initModule();

    //
    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    //
    Interrupt_initVectorTable();

    I2Cinit();

    //I2Cs connected to I2CA will be found in AvailableI2C_targets buffer
    //after you run I2CBusScan function.
    //uint16_t *pAvailableI2C_targets = AvailableI2C_targets;
  //  status = I2CBusScan(I2CA_BASE, pAvailableI2C_targets);

    uint16_t i;

    for(i=0;i<5;i++)
    {
        TX_MsgBuffer[i] = 0;
        RX_MsgBuffer[i] = 0;
    }

    RTC.TargetAddr      = ds_3231N;
    RTC.base           = I2CA_BASE;
    RTC.pControlAddr   = &ControlAddr;
    RTC.NumOfAddrBytes = 2;
    RTC.pTX_MsgBuffer  = TX_MsgBuffer;
    RTC.pRX_MsgBuffer  = RX_MsgBuffer;
    RTC.NumOfAttempts  = 5;
    RTC.Delay_us       = 10;
    RTC.WriteCycleTime_in_us = 1200;    //10ms for RTC this code was tested


  while(1)
  {

    ControlAddr = 0X00;
    RTC.pControlAddr   = &ControlAddr;
    RTC.NumOfDataBytes = 3;
    status = I2C_ControllerReceiver(&RTC);

    while(I2C_getStatus(RTC.base) & I2C_STS_BUS_BUSY);
    Get_Time ();
    //verifyRTCRead();
  }


    if(status)
    {
        fail();
    }
    else
    {
        pass();
    }



    if(status)
    {
        fail();
    }
    else
    {
        pass();
    }

}

//
// pass - Function to be called if data written matches data read
//
void
pass(void)
{
    asm("   ESTOP0");
    for(;;);
}

//
// fail - Function to be called if data written does NOT match data read
//
void fail(void)
{
    asm("   ESTOP0");
    for(;;);
}

void verifyRTCRead(void)
{
    uint16_t i;
    while(I2C_getStatus(RTC.base) & I2C_STS_BUS_BUSY);

    for(i=0;i<RTC.NumOfDataBytes;i++)
    {
        if(RX_MsgBuffer[i] != TX_MsgBuffer[i])
        {
            //Transmitted data doesn't match received data
            //Fail condition. PC shouldn't reach here
            ESTOP0;
            fail();
        }
    }
}


void I2C_GPIO_init(void)
{
    // I2CA pins (SDAA / SCLA)
    GPIO_setDirectionMode(DEVICE_GPIO_PIN_SDAA, GPIO_DIR_MODE_IN);
    GPIO_setPadConfig(DEVICE_GPIO_PIN_SDAA, GPIO_PIN_TYPE_PULLUP);
    GPIO_setQualificationMode(DEVICE_GPIO_PIN_SDAA, GPIO_QUAL_ASYNC);

    GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCLA, GPIO_DIR_MODE_IN);
    GPIO_setPadConfig(DEVICE_GPIO_PIN_SCLA, GPIO_PIN_TYPE_PULLUP);
    GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCLA, GPIO_QUAL_ASYNC);

    GPIO_setPinConfig(DEVICE_GPIO_CFG_SDAA);
    GPIO_setPinConfig(DEVICE_GPIO_CFG_SCLA);
}

void I2Cinit(void)
{
    //myI2CA initialization
    I2C_disableModule(I2CA_BASE);
    I2C_initController(I2CA_BASE, DEVICE_SYSCLK_FREQ, 400000, I2C_DUTYCYCLE_50);
    I2C_setConfig(I2CA_BASE, I2C_CONTROLLER_SEND_MODE);
    I2C_setTargetAddress(I2CA_BASE, 0x50);
    I2C_setOwnAddress(I2CA_BASE, 96); //I2CA address
    I2C_disableLoopback(I2CA_BASE);
    I2C_setBitCount(I2CA_BASE, I2C_BITCOUNT_8);
    I2C_setDataCount(I2CA_BASE, 2);
    I2C_setAddressMode(I2CA_BASE, I2C_ADDR_MODE_7BITS);
    I2C_enableFIFO(I2CA_BASE);
    I2C_clearInterruptStatus(I2CA_BASE, I2C_INT_ARB_LOST | I2C_INT_NO_ACK);
    I2C_setFIFOInterruptLevel(I2CA_BASE, I2C_FIFO_TXEMPTY, I2C_FIFO_RX2);
    I2C_enableInterrupt(I2CA_BASE, I2C_INT_ADDR_TARGET | I2C_INT_ARB_LOST | I2C_INT_NO_ACK | I2C_INT_STOP_CONDITION);
    I2C_setEmulationMode(I2CA_BASE, I2C_EMULATION_FREE_RUN);
    I2C_enableModule(I2CA_BASE);
}


void Get_Time (void)
{

    time.seconds = bcdToDec(RX_MsgBuffer[0]);
    time.minutes = bcdToDec(RX_MsgBuffer[1]);
    time.hour = bcdToDec(RX_MsgBuffer[2]);

}


unsigned int decToBcd( unsigned int val)
{
  return (uint8_t)( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
unsigned int bcdToDec(unsigned int val)
{
  return (int)( (val/16*10) + (val%16) );
}
//
// End of File
//

此代码用于 DS3231N RTC 与 TMSF280025c board...in 接口此代码旨在读取 RTC 中的...但我无法读取数据...

我知道当我调试时、我吸入了这个函数

 状态= I2C_ControllerReceiver (&RTC);在该函数中,发生了此函数.........  while (I2C_getStopConditionStatus (base)&& quertCount <= 3U);

请告诉我解决这个问题的方法。。。 如何知道在哪里我遇到问题...和多少时钟,我们必须给时钟 th i2c

和总线扫描功能也不工作....没有总线功能代码会工作吗?