2440 IIC 总是不发生中断

草根聪 2012-06-11 05:17:05
平台是2440,编译器是MDK,现在在做裸机的IIC读写AT24C02,但是总是不发生中断
代码如下

#include <string.h>
#include "2440addr.h"
#include "2440lib.h"
#include "def.h"
#include "IIC.h"

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

//===================================================================
// SMDK2440 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("\nIIC Test(Interrupt) using AT24C02\n");

save_E = rGPECON;
save_PE = rGPEUP;
rGPEUP |= 0xc000; //Pull-up disable
rGPECON |= 0xa00000; //GPE15:IICSDA , GPE14:IICSCL

pISR_IIC = (unsigned)IicInt;
rINTMSK &= ~(BIT_IIC);

//Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
// If PCLK 50.7MHz, IICCLK = 3.17MHz, Tx Clock = 0.198MHz
rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);

rIICADD = 0x10; //2440 slave address = [7:1] 地址不是 000 吗?为什么这里配置为0001000呢 只要后三位是0应该就行了。
rIICSTAT = 0x10; //IIC bus data output enable(Rx/Tx) (模式没有进行配置)
rIICLC = (1<<2)|(1); // Filter enable, 15 clocks SDA output delay added by junon (应该是5clocks)

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

for(i=0;i<256;i++) //这里进行了256次写操作,也就是函数Wr24C080(0xa0,(U8)i,i);每次只进行一个数据的写操作。
Wr24C080(0xa0,(U8)i,i); //slvaddr, addr, data
//而且从i从0递增到255也能够看出,写数据的地址是从EEPROM的最低地址0进行写直到,256

for(i=0;i<256;i++)
data[i] = 0;

Uart_Printf("Read test data from AT24C02\n");

for(i=0;i<256;i++)
Rd24C080(0xa1,(U8)i,&(data[i]));

//Line changed 0 ~ f
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
Uart_Printf("%2x ",data[i*16+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 = 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.
}

//**********************[ 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 = 0xaf; //Resumes IIC operation.
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:
_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;
}
}
这时事例程序的代码,我自己写的也是无法进入到中断中去,,我已经把我自己的源程序时钟频率改的和事例的时钟频率一致了,但是仍然不中断。
程序一直停在写函数里面的
while(_iicDataCount!=-1);死循环,并且显示_iicDataCount=2;根本就一直没有变,这可能是什么原因呀?或者请大家推荐一下我遇到这个问题,怎么去调试呀?
...全文
373 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
txler 2012-12-03
  • 打赏
  • 举报
回复
问题解决了吗?
clz168 2012-06-28
  • 打赏
  • 举报
回复
我也是个小菜鸟,懂的不多,对于那个延时,我的理解是只是为了确保写操作的完全结束,防止在结束前有别的操作介入,所以对于延时不用太精确,差不多就行,第二个问题我也没搞明白,希望明白的大侠们给个解释[Quote=引用 6 楼 的回复:]
嗯,的确,当时自己太粗心了,没有看清楚,改了后,这个程序就可以中断,并且正常工作了。
还想请教一下,
问题1、对于 Delay(1); //Wait until stop condtion is in effect.
这个延时,需要精确时延吗?
问题2、这个代码其实是MINI2440的实例代码(原始配置就是rGPECON=0xa00000,却能够正确的执行),我当时因为这样测试过,才觉得这……
[/Quote]
草根聪 2012-06-14
  • 打赏
  • 举报
回复
嗯,的确,当时自己太粗心了,没有看清楚,改了后,这个程序就可以中断,并且正常工作了。
还想请教一下,
问题1、对于 Delay(1); //Wait until stop condtion is in effect.
这个延时,需要精确时延吗?
问题2、这个代码其实是MINI2440的实例代码(原始配置就是rGPECON=0xa00000,却能够正确的执行),我当时因为这样测试过,才觉得这个代码肯定正确,没有过多的检查,这是为什么呀?好奇怪
[Quote=引用 5 楼 的回复:]
我的也不能进入中断,但是你那个上面最明显的错误就是rGPECON=0xa00000;因为用到的是GPE15和GPE14,按道理应该是rGPECON=0xa0000000;
[/Quote]
Wind_Cloud2012 2012-06-13
  • 打赏
  • 举报
回复
再加上 ClearPending(BIT_IIC),这个是要手处理的
clz168 2012-06-13
  • 打赏
  • 举报
回复
我的也不能进入中断,但是你那个上面最明显的错误就是rGPECON=0xa00000;因为用到的是GPE15和GPE14,按道理应该是rGPECON=0xa0000000;
Wind_Cloud2012 2012-06-12
  • 打赏
  • 举报
回复
你的中断都没放开啊
草根聪 2012-06-12
  • 打赏
  • 举报
回复
而且我在线看了,如果是没有开中断,那SRCPND这个寄存器仍然会有一位为1,但是我检测这个寄存器时,他的值为1,说明中断根本没有产生,而不是并屏蔽的原因
[Quote=引用 1 楼 的回复:]
你的中断都没放开啊
[/Quote]
草根聪 2012-06-12
  • 打赏
  • 举报
回复
在Test_Iic()函数中
save_E = rGPECON;
save_PE = rGPEUP;
rGPEUP |= 0xc000; //Pull-up disable
rGPECON |= 0xa00000; //GPE15:IICSDA , GPE14:IICSCL

pISR_IIC = (unsigned)IicInt;
rINTMSK &= ~(BIT_IIC);

[Quote=引用 1 楼 的回复:]
你的中断都没放开啊
[/Quote]

19,519

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧