我们从2011年坚守至今,只想做存粹的技术论坛。  由于网站在外面,点击附件后要很长世间才弹出下载,请耐心等待,勿重复点击不要用Edge和IE浏览器下载,否则提示不安全下载不了

 找回密码
 立即注册
搜索
查看: 651|回复: 1

I2c测试

[复制链接]

该用户从未签到

4

主题

8

回帖

2

积分

一级逆天

天行健,君子以自强不

积分
2

社区居民

QQ
发表于 2015-8-26 08:37:16 | 显示全部楼层 |阅读模式
主函数
#include <string.h>
#include <stdio.h>
#include "Target\44blib.h"
#include "Target\Def.h"
#include "Target\44b.h"
#include "iictest\iic.h"

/********************************************/
//   ARMSYS实验十一:IIC接口应用测试(AT24C04)
//   描述:读写IIC接口EEPROM
/********************************************/
void * function[][2]=
{
    (void *)Test_Iic,            "IIC(AT24LCxx)   ",
    0,0
};


void Isr_Init(void)
{
    rINTCON=0x5;          //Non-vectored,IRQ enable,FIQ disable //
    rINTMOD=0x0;          //All=IRQ mode//
    rINTMSK=~(BIT_EINT0 | BIT_GLOBAL);   
}

void Main(void)
{
    char aa;
    int i;
   
    Port_Init();
    Uart_Init(0,115200);
    Isr_Init();//it's needed.
    Led_Display(0xf);
    Delay(0);
    Beep(0x1);
   
    Uart_Select(0); //Select UART0//
    Uart_Printf("\n\n*************************************************************************");
    Beep(0x0);
    Uart_Printf("\n*                             立泰电子                                   *");
    Uart_Printf("\n*                     -S3C44B0X功能部件:IIC测试-                         *");
    Uart_Printf("\n*                          Version 1.11                                 *");   
    Uart_Printf("\n*                     Email:rao_dali@263.net                            *");
    Uart_Printf("\n*             UART Config--COM:115.2kbps,8Bit,NP,UART0                  *");
    Uart_Printf("\n*------------------Begin to Start IIC test,OK? (Y/N)--------------------*");
   
    Led_Display(0x0);
    aa= Uart_Getch();
    if((aa=='Y')||(aa=='y'))
    while(1)
    {
            i=0;
            Uart_Printf("\n");
            while(1)
            {   //display menu
                Uart_Printf("%2d:%s",i+1,function[1]);
                i++;
                if((int)(function[0])==0)
                {
                    Uart_Printf("\n");
                    break;
                }
                if((i%4)==0)
                    Uart_Printf("\n");
            }
            Uart_Printf("\nSelect the function to test?");
            i=Uart_GetIntNum();
            i--;
            Uart_Printf("\n");
            if(i>=0 && (i<(sizeof(function)/8)) )
                ( (void (*)(void)) (function[0]) )();
    }
}



#include <string.h>
#include "..\Target\44b.h"
#include "..\Target\44blib.h"
#include "..\Target\def.h"
#include "iic.h"

#define WRDATA        (1)
#define POLLACK     (2)
#define RDDATA        (3)
#define SETRDADDR   (4)

#define IICBUFSIZE 0x20

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

void __irq IicInt(void);

void Test_Iic(void)
{
    unsigned int i,j,save_F,save_PF;
    static U8 data[256];

    Uart_Printf("[IIC Test using AT24LCxx]\n");

    save_F=rPCONF;
    save_PF=rPUPF;
    rPCONF |=0xa;    //PF0:IICSCL, PF1:IICSDA
    rPUPF |=0x3;    //pull-up disable

    pISR_IIC=(unsigned)IicInt;
    rINTMSK=~(BIT_GLOBAL|BIT_IIC);

    rIICCON=(1<<7)|(0<<6)|(1<<5)|(0xf);
    //Enable interrupt, IICCLK=MCLK/16, Enable ACK
    //40Mhz/16/(15+1) = 257Khz   
    rIICADD=0x10;   // S3C44B0X slave address

    rIICSTAT=0x10;

    Uart_Printf("Write test data into AT24LCxx\n");

    for(i=0;i<256;i++)
    Wr24LCxx(0xa0,(U8)i,i);
    for(i=0;i<256;i++)
    data=0;

    Uart_Printf("Read test data from AT24LCxx\n");
    for(i=0;i<256;i++)
    Rd24LCxx(0xa0,(U8)i,&(data));

    for(i=0;i<16;i++)
    {
    for(j=0;j<16;j++)
        Uart_Printf("%2x ",data[i*16+j]);
    Uart_Printf("\n");
    }
   
    rPCONF=save_F;
    rPUPF=save_PF;
}


void Wr24LCxx(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=0xaf;  //resumes IIC operation.
    while(_iicStatus==0x100);
    if(!(_iicStatus&0x1))
        break; // when ACK is received
    }
    rIICSTAT=0xd0;  //stop MasTx condition
    rIICCON=0xaf;   //resumes IIC operation.
    Delay(1);        //wait until stop condtion is in effect.

    //write is completed.
}
   

void Rd24LCxx(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=0xaf;  //resumes IIC operation.   
    while(_iicDataCount!=-1);

    *data=_iicData[1];
}



void __irq IicInt(void)
{
    U32 iicSt,i;
    rI_ISPC=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:
        _iicStatus=iicSt;
        break;

    case RDDATA:
        if((_iicDataCount--)==0)
        {
        _iicData[_iicPt++]=rIICDS;
        
        rIICSTAT=0x90;  //stop MasRx condition
        rIICCON=0xaf;   //resumes IIC operation.
        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=0x2f;    //resumes IIC operation with NOACK.  
        else
        rIICCON=0xaf;    //resumes IIC operation with ACK
        break;

    case WRDATA:
        if((_iicDataCount--)==0)
        {
        rIICSTAT=0xd0;    //stop MasTx condition
        rIICCON=0xaf;    //resumes IIC operation.
        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=0xaf;        //resumes IIC operation.
        break;

    case SETRDADDR:
        //Uart_Printf("[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=0xaf;        //resumes IIC operation.
        break;

    default:
        break;      
    }
}
刚把得
回复

使用道具 举报

该用户从未签到

459

主题

1539

回帖

4574

积分

二级逆天

PCB画板兼职QQ469573335

积分
4574

社区居民忠实会员社区劳模最爱沙发社区明星原创达人终身成就奖优秀斑竹奖宣传大使奖

QQ
发表于 2015-8-26 09:00:09 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

论坛开启做任务可以
额外奖励金币快速赚
积分升级了


Copyright ©2011-2024 NTpcb.com All Right Reserved.  Powered by Discuz! (NTpcb)

本站信息均由会员发表,不代表NTpcb立场,如侵犯了您的权利请发帖投诉

平平安安
TOP
快速回复 返回顶部 返回列表