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

📄 iic.c

📁 实验1 ARM汇编指令实验 实验2 C和汇编语言的混合编程实验 实验3 外部中断实验 实验4 看门狗应用实验
💻 C
📖 第 1 页 / 共 2 页
字号:
//====================================================================
// File Name : 2410IIC.c
// Function  : S3C2410 IIC-bus Master Tx/Rx mode Test Program
//             (Interrupt / Non Interrupt (Polling))
// Program   : Shin, On Pil (SOP)
// Date      : May 21, 2002
// Version   : 0.0
// History
//   0.0 : Programming start (March 11, 2002) -> SOP
//====================================================================

#include "def.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h"
#include "IIC.h"
#include "timer.h"

static U8 _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;

//===================================================================
//       SMDK2410 IIC configuration
//  GPE15=IICSDA, GPE14=IICSCL
//  "Interrupt mode" for IIC block
//=================================================================== 

//******************[ Test_Iic ]**************************************
void Test_Iic(void)
{
    unsigned int i,j,save_E,save_PE;
    static U8 data[256];

    Uart_Printf("IIC Test(Interrupt) using AT24C02\n");
    //保存寄存器rGPECON和rGPEUP
    save_E   = rGPECON;
    save_PE  = rGPEUP;
    //设置GPE15->IICSDA和GPE14->IICSCL
    rGPEUP  |= 0xc000;                  //Pull-up disable
    rGPECON &= ~0xf0000000;
    rGPECON |= 0xa0000000;              //GPE15:IICSDA , GPE14:IICSCL 
    //建立中断函数
    pISR_IIC = (unsigned)IicInt;
    //清除中断屏蔽位
    rINTMSK &= ~(BIT_IIC);
    
    //IIC控制寄存器配置
    //Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, 
    //Transmit clock value Tx clock=IICCLK/(IICCON[3:0]+1)
    // If PCLK 50.7MHz, IICCLK = 3.17MHz, Tx Clock = 0.198MHz
    rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);
    //IIC clock = PCLK/(512*1)
    //rIICCON = (1<<7) | (1<<6) | (1<<5) | (0);	//hzh

    rIICADD  = 0x10;                    //2410 slave address = [7:1]
    rIICSTAT = 0x10;                    //IIC bus data output enable(Rx/Tx)

    Uart_Printf("Write test data into AT24C02\n");
    //写入一个page的数据,page的大小是256kbyte,
    //page 的起始地址是0xa0,写入的数据是:0、1、2、...255。
    for(i=0;i<256;i++)
        Wr24C080(0xa0,(U8)i,i+0x41);//Wr24C080(U32 slvAddr,U32 addr,U8 data)
    //初始化data数组的值为0。       
    for(i=0;i<256;i++)
        data[i] = 0;

    Uart_Printf("Read test data from AT24C02\n");
    //读24C02的0xa0地址中数据到data数组中。
    for(i=0;i<256;i++)
        Rd24C080(0xa0,(U8)i,&(data[i])); 
    //输出data数组接收数据的值
    printf("The follow is the data writed to IIC EEPROM just now:\n");

    for(i=0;i<8;i++)
    {
        for(j=0;j<8;j++)
            Uart_Printf("%2x ",data[i*8+j]);
        Uart_Printf("\n");
    }
    //关中断,并恢复端口配置
    rINTMSK |= BIT_IIC;    
    rGPEUP  = save_PE;
    rGPECON = save_E;
}


//*************************[ Wr24C080 ]****************************
void Wr24C080(U32 slvAddr,U32 addr,U8 data)
{
    _iicMode      = WRDATA;
    _iicPt        = 0;
    _iicData[0]   = (U8)addr;
    _iicData[1]   = data;
    _iicDataCount = 2;
    
    rIICDS   = slvAddr;                 //0xa0
    rIICSTAT = 0xf0;                    //MasTx,Start
    //Clearing the pending bit isn't needed because the pending bit has been cleared.
    while(_iicDataCount!=-1);

    _iicMode = POLLACK;

    while(1)
    {
        rIICDS     = slvAddr;
        _iicStatus = 0x100;
        rIICSTAT   = 0xf0;              //MasTx,Start
        rIICCON    = 0xe0;//0xaf;              //Resumes IIC operation. 	//hzh
           
        while(_iicStatus==0x100);
           
        if(!(_iicStatus&0x1))
            break;                      //When ACK is received
    }
    rIICSTAT = 0xd0;                    //Stop MasTx condition 
    rIICCON  = 0xe0;//0xaf;                    //Resumes IIC operation. 	//hzh
    Delay(1);                           //Wait until stop condtion is in effect.
       //Write is completed.
}
        
//**********************[ Rd24C080 ] ***********************************
void Rd24C080(U32 slvAddr,U32 addr,U8 *data)
{
    
    _iicMode      = SETRDADDR;
    _iicPt        = 0;
    _iicData[0]   = (U8)addr;
    _iicDataCount = 1;

    rIICDS   = slvAddr;
    rIICSTAT = 0xf0;                    //MasTx,Start  
      //Clearing the pending bit isn't needed because the pending bit has been cleared.
    while(_iicDataCount!=-1);

    _iicMode      = RDDATA;
    _iicPt        = 0;
    _iicDataCount = 1;
    
    rIICDS        = slvAddr;
    rIICSTAT      = 0xb0;               //MasRx,Start
    rIICCON       = 0xe0;//0xaf;               //Resumes IIC operation.	//hzh
    while(_iicDataCount!=-1);

    *data = _iicData[1];
}


//-------------------------------------------------------------------------
void __irq IicInt(void)
{
    U32 iicSt,i;
    //清除中断判断位
    rSRCPND = BIT_IIC;          //Clear pending bit
    rINTPND = BIT_IIC;
    iicSt   = rIICSTAT; 
    
    if(iicSt & 0x8){}           //When bus arbitration is failed.
    if(iicSt & 0x4){}           //When a slave address is matched with IICADD
    if(iicSt & 0x2){}           //When a slave address is 0000000b
    if(iicSt & 0x1){}           //When ACK isn't received

    switch(_iicMode)
    {
       case POLLACK:                           //等待ACK模式
           _iicStatus = iicSt;                 //读入IICSTAT,第0位表示是否接收到ACK
           break;

       case RDDATA:                            //读数据模式
           if((_iicDataCount--)==0)            //只要读取1字节数据
           {
               _iicData[_iicPt++] = rIICDS;
            
               rIICSTAT = 0x90;                 //Stop MasRx condition 
               rIICCON  = 0xe0;//0xaf;          //Resumes IIC operation.	//hzh
               Delay(1);                        //Wait until stop condtion is in effect.
                                                //Too long time... 
                                                //The pending bit will not be set after issuing stop condition.
               break;    
           }      
           _iicData[_iicPt++] = rIICDS;         //The last data has to be read with no ack.

           if((_iicDataCount)==0)
               rIICCON = 0x60;//0x2f;                  //Resumes IIC operation with NOACK.	//hzh
           else 
               rIICCON = 0xe0;//0xaf;                  //Resumes IIC operation with ACK	//hzh
               break;

        case WRDATA:                            //写数据模式
            if((_iicDataCount--)==0)            //如果写数据完成
            {
                rIICSTAT = 0xd0;                //Stop MasTx condition 
                rIICCON  = 0xe0;//0xaf;                //Resumes IIC operation.	//hzh
                Delay(1);                       //Wait until stop condtion is in effect.
                       //The pending bit will not be set after issuing stop condition.
                break;    
            }
            rIICDS = _iicData[_iicPt++];        //_iicData[0] has dummy.
            for(i=0;i<10;i++);                  //for setup time until rising edge of IICSCL
              
            rIICCON = 0xe0;//0xaf;                     //resumes IIC operation.	//hzh
            break;

        case SETRDADDR:                          // 设置读地址模式
//            Uart_Printf("__irq IicInt SETDADDR [ S%d ]",_iicDataCount);
            if((_iicDataCount--)==0)
                break;                          //IIC operation is stopped because of IICCON[4]    
            rIICDS = _iicData[_iicPt++];
            for(i=0;i<10;i++);                  //For setup time until rising edge of IICSCL
            rIICCON = 0xe0;//0xaf;                     //Resumes IIC operation.	//hzh

⌨️ 快捷键说明

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