⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsp281x_x1228.c.bak

📁 在TI的TMSF2812平台上
💻 BAK
字号:
/* 
   FILE:    DSP281x_X1228.c
  
   TITLE:   X1228 Program(I2C).
   
   AUTHOR:  Xu Kangping  
  
   DESCRIPTION:
  
            This file define some function about I2C to operate the 
            X1228 device(RTC).
  
            The Epower_28x headerfiles are required to build this file.
         
            Watch Variables:
                   real_time
*/

#include "Epower_28x.h"

/* define the initialized data */
Uint16 control[3]={0x90,0x00,0x00};

/* define the delay_us() function */
extern void DSP28x_usDelay(Uint32 Count);

/* define a array of real time */
extern Uint16 real_time[8];

/* define the pin of SCL */
void ConfigI2C_SCL_Out(void)
{
    EALLOW;
    GpioMuxRegs.GPFMUX.bit.SPISIMOA_GPIOF0 = 0;
    GpioMuxRegs.GPFDIR.bit.GPIOF0 = 1;
    EDIS;
}

/* define the pin of SDA to putout */
void ConfigI2C_SDA_Out(void)
{
    EALLOW;
    GpioMuxRegs.GPFMUX.bit.SPISTEA_GPIOF3 = 0;
    GpioMuxRegs.GPFDIR.bit.GPIOF3 = 1;
    EDIS;
}

/* define the pin of SDA to putin */
void ConfigI2C_SDA_In(void)
{
    EALLOW;
    GpioMuxRegs.GPFMUX.bit.SPISTEA_GPIOF3 = 0;
    GpioMuxRegs.GPFDIR.bit.GPIOF3 = 0;
    EDIS;
}

/* the Start Condition of transfer */
void Start_Condition(void)
{
	SCL_0 ;
    DELAY_US(DELAY_TIME);   
    SDA_1;                           //SDA = 1
    DELAY_US(DELAY_TIME);  
    SCL_1;
    DELAY_US(DELAY_TIME);            //delay 1us
    SDA_0;                           //SDA = 0
    DELAY_US(DELAY_TIME);            //delay 1us
    SCL_0;                           //SCL = 0
    DELAY_US(DELAY_TIME);            //delay 1us
}

/* the Stop Condition of transfer */
void Stop_Condition(void)
{
    SCL_0;              //?             //SCL = 0
	DELAY_US(DELAY_TIME);  
    SDA_0;                           //SDA = 0
    DELAY_US(DELAY_TIME);            //delay 1us
    SCL_1;                           //SCL = 1
    DELAY_US(DELAY_TIME);            //delay 1us
    SDA_1;                           //SDA = 1
    DELAY_US(DELAY_TIME);            //delay 1us
}

/* indicating successful data transfer */
Uint16 Acknowledge(void)
{
    Uint16 answer = 1;
    SDA_1;                           //SDA = 1
    ConfigI2C_SDA_In();
    SCL_0;                           //SCL = 0
    DELAY_US(DELAY_TIME);            //delay 1us
    SCL_1;                           //SCL = 1
    DELAY_US(DELAY_TIME/2);          //delay 1us
    answer = SDA;                    //read the SDA bus
    DELAY_US(DELAY_TIME/2);          //delay 1us
    SCL_0;                           //SCL = 0
    DELAY_US(DELAY_TIME);            //delay 1us
    ConfigI2C_SDA_Out();
    return(answer);
}

/* The master made the ACK */
void Ack(void)
{
    SCL_0;                           //SCL = 0
    SDA_0;                           //SDA = 0
    DELAY_US(DELAY_TIME);            //delay 1us
    SCL_1;                           //SCL = 1
    DELAY_US(DELAY_TIME);            //delay 1us
    SCL_0;                           //SCL = 0
    DELAY_US(DELAY_TIME);            //delay 1us
}

/* The master didn't make the ACK */
void NoAck(void)
{
    SCL_0;                           //SCL = 0
    SDA_1;                           //SDA = 1
    DELAY_US(DELAY_TIME);                    //delay 1us
    SCL_1;                           //SCL = 1
    DELAY_US(DELAY_TIME);                    //delay 1us
    SCL_0;                           //SCL = 0
    DELAY_US(DELAY_TIME);                    //delay 1us
}

/* transfer a byte */
void SendByte(Uint16 byte) 
{
    int flag,sz;
	
	for(flag=0x0080;flag!=0x0000;flag=(flag>>1))       //the first MSB,the last LSB
	{
	    sz = byte & flag;
	    SCL_0;                                         //SCL=0
		if(sz == 0) SDA_0;                             //SDA=0
		else SDA_1;                                    //SDA=1
		DELAY_US(DELAY_TIME);                          //delay 1us
		SCL_1;                                         //SCL=1 
		DELAY_US(DELAY_TIME);                          //delay 1us
	    SCL_0;                                         //SCL=0
	    DELAY_US(DELAY_TIME);                          //delay 1us
    }
}

/* Write a byte of data to the device X1228 */
void Write_Data(Uint16 SlaveAdd,Uint16 WordAdd,Uint16 Data)
/* write CCR control=11011110b, EEPOM control=10101110b */
{
    int answer = 1;
    int address_h,address_l;
    address_l = WordAdd & 0x00ff;
	address_h = WordAdd >> 8;
	Write_Enable();                   //Enable write
    Start_Condition();                //Start transfer
    do
    {
        SendByte(SlaveAdd);           //Slave Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_h);          //High Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_l);          //Low Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(Data);               //Send the Data
        answer = Acknowledge();
    }while(answer == 1);
    Stop_Condition();                 //Stop transfer
    DELAY_US(10000L);                 //delay 10ms
}

/* the access to the CCR and memory array during a write operation */
void Write_Enable(void)          
{
    Uint16 answer;
    /* transfer the data 0x02H to SR */
    Start_Condition();                //Start transfer
    do
    {
        SendByte(CCR_W);              //Slave Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x0000);             //High Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(SR);                 //Low Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x02);               //Send the Data
        answer = Acknowledge();
    }while(answer == 1);
    Stop_Condition();                 //Stop transfer
    
    /* transfer the data 0x06H to SR */
    Start_Condition();                //Start transfer
    do
    {
        SendByte(CCR_W);              //Slave Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x0000);             //High Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(SR);                 //Low Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x06);               //Send the Data
        answer = Acknowledge();
    }while(answer == 1);
    Stop_Condition();                 //Stop transfer
}

/* Write the array of data to the device X1228 */
void Write_DataN(Uint16 SlaveAdd,Uint16 WordAdd,Uint16 Data[],Uint16 Num)
{
    int i;
    Uint16 answer = 1;
    int address_h,address_l;
    address_l = WordAdd & 0x00FF;
	address_h = WordAdd >> 8;
	
    Write_Enable();                       //Enable write
    Start_Condition();                    //Start transfer
    do
    {
        SendByte(SlaveAdd);               //Slave Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_h);              //High Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_l);              //Low Address
        answer = Acknowledge();
    }while(answer == 1);
    for(i=0;i<Num;i++)
    {
        do
        {
            SendByte(Data[i]);            //Send the Data
            answer = Acknowledge();
        }while(answer == 1);
    }
    Stop_Condition();                     //Stop transfer
    DELAY_US(10000L);                     //delay 10ms
}

/* receive a byte */
Uint16 ReceiveByte(void)
{
    int flag;
    Uint16 byte = 0x00;
    ConfigI2C_SDA_In();
    
    for(flag=0x0080;flag!=0x0000;flag=(flag>>1))
	{
	    SCL_0;                                           //SCL=0
		DELAY_US(DELAY_TIME);                            //delay 1us
		SCL_1;                                           //SCL=1
		DELAY_US(DELAY_TIME/2);                          //delay 0.5us
		if(SDA == 1) byte |= flag;                       //read the data
		DELAY_US(DELAY_TIME/2);                          //delay 0.5us
		SCL_0;                                           //SCL=0
		DELAY_US(DELAY_TIME);                            //delay 1us
    }
    
    ConfigI2C_SDA_Out();
    return(byte);
}

/* Read a byte of data from the device X1228 */
Uint16 Read_Data(Uint16 SlaveAdd,Uint16 WordAdd)
/* read CCR control=11011111b, EEPOM control=10101111b */
{
    Uint16 answer = 1;
    Uint16 byte;
    int address_h,address_l;
    address_l = WordAdd & 0x00FF;
	address_h = WordAdd >> 8;
	SlaveAdd &= 0x0FFFE;
	
	//Write_Enable();                   //Enable write
	Start_Condition();                //Start transfer
    do
    {
        SendByte(SlaveAdd);           //Slave Address(Write)
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_h);          //High Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_l);          //Low Address
        answer = Acknowledge();
    }while(answer == 1);
    
    SlaveAdd |= 0x0001;
    
    Start_Condition();                //Start transfer
    do
    {
        SendByte(SlaveAdd);           //Slave Address(Read)
        answer = Acknowledge();
    }while(answer == 1);
    byte = ReceiveByte();             //Receive the Data
    Stop_Condition();
    return(byte);
}

/* Read a array of data from the device X1228 */
void Read_DataN(Uint16 SlaveAdd,Uint16 WordAdd,Uint16 Data[],Uint16 Num)
/* read CCR control=11011111b, EEPOM control=10101111b */
{
    int i;
    Uint16 answer = 1;
    int address_h,address_l;
    address_l = WordAdd & 0x00FF;
	address_h = WordAdd >> 8;
	
	SlaveAdd &= 0x0FFFE;
	//Write_Enable();                   //Enable write
	Start_Condition();                //Start transfer
    do
    {
        SendByte(SlaveAdd);           //Slave Address(Write)
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_h);          //High Address
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(address_l);          //Low Address
        answer = Acknowledge();
    }while(answer == 1);
    
    SlaveAdd |= 0x0001;
    Start_Condition();                //Start transfer
    do
    {
        SendByte(SlaveAdd);           //Slave Address(Read)
        answer = Acknowledge();
    }while(answer == 1);
    for(i=0;i<Num;i++)
    {
        Data[i] = ReceiveByte();
        if(i != (Num - 1)) Ack();
        else Stop_Condition();
    }
}

/* set the time 2005.2.21.Monday.10:50:00 */
void SetTime(void)
{
    Uint16 answer;
    Write_Enable();                       /* Enable write */
    do
    {
        Start_Condition();
        SendByte(CCR_W);                  /* Slave Address */
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x00);                   /* High Address */
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(RTC_SC);                 /* Low Address */
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x23);                   /* SC = 00 */
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x05);                   /* MN = 20 */
        answer = Acknowledge();
    }while(answer == 1);do
    {
        SendByte(0x092);                   /* HR =14,MIL = 1,use a 24-hour format */
        answer = Acknowledge();
    }while(answer == 1);do
    {
        SendByte(0x24);                   /* DT = Four */
        answer = Acknowledge();
    }while(answer == 1);do
    {
        SendByte(0x03);                   /* MO = March */
        answer = Acknowledge();
    }while(answer == 1);do
    {
        SendByte(0x05);                   /* YR = 05 */
        answer = Acknowledge();
    }while(answer == 1);do
    {
        SendByte(0x04);                   /* DW = Saturday */
        answer = Acknowledge();
    }while(answer == 1);do
    {
        SendByte(0x20);                   /* Y2K = 20 */
        answer = Acknowledge();
    }while(answer == 1);
    DELAY_US(10000L);
    Stop_Condition();
}

/* Read the real time */
void ReadTime(void)
{
    int i;
    Uint16 answer;
    //Write_Enable();                       /* Enable write */
    do
    {
        Start_Condition();
        SendByte(CCR_W);                  /* Slave Address(Write) */
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(0x00);                   /* High Address */
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        SendByte(RTC_SC);                 /* Low Address */
        answer = Acknowledge();
    }while(answer == 1);
    do
    {
        Start_Condition();
        SendByte(CCR_R);                  /* Slave Address(Read) */
        answer = Acknowledge();
    }while(answer == 1);
    for(i=0;i<8;i++)
    {
        real_time[i] = ReceiveByte();
        if(i != 7) Ack();
        else Stop_Condition();
    }
}

/* Initialize the device X1228 */
void InitX1228(void)
{
    ConfigI2C_SCL_Out();
    ConfigI2C_SDA_Out();
    DELAY_US(10000L);                /* Delay 10ms */
    Write_DataN(CCR_W,Ctrl_INT,control,3);
    /* DTR2 = 0,DTR1 = 0,DTR0 = 0,Estimated frequency PPM is 0 */
    /* ATR5 = 0,ATR4 = 0,ATR3 = 0,ATR2 = 0,ATR1 = 0,ATR0 = 0,
       CATR= [(ATR value, decimal) x 0.25pF] + 11.0pF ATR(000000) = 11.0pF */
    /* IM = 1,Repetitive / Recurring Time Event Set By Alarm
       FO1 = 1,FO0 = 1,Output Frequency 1Hz */
}

/* Set the watchdog */
void WatchDog_Set(void)
{
	Write_Data(CCR_W,Ctrl_BL,0x00);     /* address BL 0x0010
	                                       set all address_protect BP2=0,BP1,BP0=1
	                                       the longest watchdog time :1.75 seconds
                                       WD1 = 1 ,WD0 = 0 */
}

/* Set watchdog time-out period */
void WatchDog_Restart(void)
{
	Start_Condition();
	Stop_Condition();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -