|
我贴出我的代码,麻烦给分析一下!
在Bootloader2主程序中加如下代码:
void Main(void)
{
Uart_SendString("test iic start ************************\n");
while(1)
{
Test_Iic2(); //查询方式
Delay100us(50000);
}
// Test_Iic(); //中断方式
Uart_SendString("test iic end ************************\n");
}
中断方式在我这里根本没有进入过,我看网上有人提醒,就改为了查询方式
,这里的While(1)不知道什么原因执行几次就停了,帮我查查,
下面的代码就是查询方式读取LM75温度的代码,是从读24C08改的,所以函数名没有换
void Test_Iic2(void)
{
unsigned int i,j,save_E,save_PE;
static U8 data[256]={0};
char tmp[2];
char tmpDisp;
//Uart_Printf("[ IIC Test(Polling) using 24C02 ]\n");
Uart_SendString("IIC Test(Polling) using lm75\n");
save_E = rGPECON;
save_PE = rGPEUP;
rGPEUP = 0xc000;
rGPECON |= 0xa0000000;
// rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);
rIICCON = (1<<7) | (0<<6) | (1<<5) | (0x29);
rIICADD = 0x9e;
rIICSTAT = 0x10;
//Uart_Printf("Write test data into 24C02\n");
Uart_SendString("Write Test(Polling) using lm75\n");
void _Wr24C080(U32 slvAddr,U32 addr,U8 data)
{
_iicMode = WRDATA;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicData[1] = data;
_iicDataCount = 2;
rIICDS = slvAddr;
rIICSTAT = 0xf0;
while(_iicDataCount!=-1)
Run_IicPoll();
_iicMode = POLLACK;
while(1)
{
rIICDS = slvAddr;
_iicStatus = 0x100;
rIICSTAT = 0xf0;
rIICCON = 0xaf;
while(_iicStatus==0x100)
Run_IicPoll();
if(!(_iicStatus & 0x1))
break;
}
rIICSTAT = 0xd0;
rIICCON = 0xaf;
Delay(1);
}
void _Rd24C080(U32 slvAddr,U32 addr,U8 *data)
{
_iicMode = SETRDADDR;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xf0;
while(_iicDataCount!=-1)
Run_IicPoll();
_iicMode = RDDATA;
_iicPt = 0;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xb0;
rIICCON = 0xaf;
while(_iicDataCount!=-1)
Run_IicPoll();
*data = _iicData[1];
}
void Run_IicPoll(void)
{
if(rIICCON & 0x10)
IicPoll();
}
void IicPoll(void)
{
U32 iicSt,i;
iicSt = rIICSTAT;
if(iicSt & 0x8){}
if(iicSt & 0x4){}
if(iicSt & 0x2){}
if(iicSt & 0x1){}
switch(_iicMode)
{
case POLLACK:
_iicStatus = iicSt;
break;
case RDDATA:
if((_iicDataCount--)==0)
{
_iicData[_iicPt++] = rIICDS;
rIICSTAT = 0x90;
rIICCON = 0xaf;
Delay(1);
break;
}
_iicData[_iicPt++] = rIICDS;
if((_iicDataCount)==0)
rIICCON = 0x2f;
else
rIICCON = 0xaf;
break;
case WRDATA:
if((_iicDataCount--)==0)
{
rIICSTAT = 0xd0;
rIICCON = 0xaf;
Delay(1);
break;
}
rIICDS = _iicData[_iicPt++];
for(i=0;i<10;i++);
rIICCON = 0xaf;
break;
case SETRDADDR:
if((_iicDataCount--)==0)
{
break;
}
rIICDS = _iicData[_iicPt++];
for(i=0;i<10;i++);
rIICCON = 0xaf;
break;
default:
break;
}
}
/*
// for(i=0;i<256;i++)
_Wr24C080(0x9e,(U8)i,5);
// for(i=0;i<256;i++)
data = 0;
//Uart_Printf("Read test data from 24C02\n");
Uart_SendString("Read Test(Polling) using lm75\n");
// for(i=0;i<256;i++)
_Rd24C080(0x9e,(U8)i,&(data));
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
{
tmp[0] = data[i*16+j];
sprintf(tmp,"%2x",data[i*16+j]);
Uart_SendString(tmp);
}
Uart_SendString("\n");
}
*/
_Wr24C080(0x9e,TempRegAddr, 5);
_Rd24C080(0x9e,TempRegAddr, data);
tmp[0] = data[0];
tmp[1] = data[1];
sprintf(tmp,"temp reg is %2x %2x\n",data[0],data[1]);
Uart_SendString(tmp);
tmpDisp = (data[0]<<1)|(data[1]>>6);
tmp[0] = tmpDisp;
sprintf(tmp,"curr temperature data is %2d\n",tmpDisp);
Uart_SendString(tmp);
Uart_SendString("Read Test(Polling) end lm75\n");
rGPEUP = save_PE;
rGPECON = save_E;
}
请指教俺,快把俺给急死了,谢谢大家关注! |
|