AT91SAM7S256芯片与温度传感器的I2C通信,我向传感器的寄存器写值,并把它读出来,但不知道为什么,我用的是KEIL 4,单步调试有问题,程序执行调用函数时,调用完子函数后,它返回不了原来的地方,而是继续往子函数下执行,是我代码有问题吗?请各位帮忙分析一下,我新手刚学,快焦头烂额了,谢谢各位好心人啊。。。。init_twi(void)
{
// Configure PIO controllers to periph mode, in open drain
AT91F_TWI_CfgPIO();
AT91F_PIO_CfgOpendrain(AT91C_BASE_PIOA,AT91C_PA3_TWD | AT91C_PA4_TWCK);
// AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA ) ;
AT91F_TWI_CfgPMC();
AT91F_TWI_DisableIt(AT91C_BASE_TWI,0xFFFFFFFF); // Disable TWI IT
AT91F_TWI_Configure(AT91C_BASE_TWI); // Configure TWI in master mode
}
uint32_t MLX620_I2C_Driver_WriteRead(AT91PS_TWI pTWI,uint8_t *pWriteData,uint8_t *pReadData)
{
int i;
AT91C_BASE_TWI->TWI_CWGR = 0x40f0f;
pTWI->TWI_CR = AT91C_TWI_MSEN;
pTWI->TWI_MMR = (AT91C_TWI_IADRSZ_NO | (!AT91C_TWI_MREAD) | AT91C_TWI_DADR); //AT91C_TWI_DADR已经为从器件的地址了
pTWI->TWI_IADR = 0x0; //INTERNAL ADDRESS
pTWI->TWI_THR =*pWriteData++;
pTWI->TWI_CR = AT91C_TWI_START; //start
while(pTWI->TWI_SR&0x4==0 ){};
for(i=0;i<3;i++){
pTWI->TWI_THR=*pWriteData++;
while(pTWI->TWI_SR&0x4==0 ){};
}
// must have a short delay when corresponding to the at24c02
time_dly(100);
//pTWI->TWI_CR = AT91C_TWI_STOP ; //stop
// while(pTWI->TWI_SR&0x1==0){};
AT91C_BASE_TWI->TWI_CWGR = 0x40f0f;
AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;
pTWI->TWI_MMR = (AT91C_TWI_IADRSZ_NO | AT91C_TWI_MREAD | AT91C_TWI_DADR);
pTWI->TWI_IADR = 0x0; //INTERNAL ADDRESS
pTWI->TWI_CR |= AT91C_TWI_START; //start
while((pTWI->TWI_SR & 0x2) == 0 ){};
*pReadData++ = pTWI->TWI_RHR;
// must have a short delay when corresponding to the at24c02
time_dly(100);
pTWI->TWI_CR |= AT91C_TWI_STOP; //stop
while((pTWI->TWI_SR & 0x1) == 0){};
}
uint32_t MLX620_I2C_Driver_Write(AT91PS_TWI pTWI, uint8_t *pData)
{
int i;
AT91C_BASE_TWI->TWI_CWGR = 0x40f0f;
pTWI->TWI_CR = AT91C_TWI_MSEN;
pTWI->TWI_MMR = (AT91C_TWI_IADRSZ_NO | (!AT91C_TWI_MREAD) | AT91C_TWI_DADR); //AT91C_TWI_DADR已经为从器件的地址了
pTWI->TWI_IADR = 0x0; //INTERNAL ADDRESS
pTWI->TWI_THR =*pData++;
pTWI->TWI_CR = AT91C_TWI_START;
while(pTWI->TWI_SR&0x4==0 )
{
};
for(i=0;i<4;i++){ //start
pTWI->TWI_THR =*pData++;
while(pTWI->TWI_SR&0x4==0 )
{
};
}
// must have a short delay when corresponding to the at24c02
time_dly(100);
pTWI->TWI_CR = AT91C_TWI_STOP ; //stop
while(pTWI->TWI_SR&0x1==0){};
}
uint8_t MLX620_ReadRAM(uint8_t startAddr,
uint8_t addrStep,
uint8_t nWords,
uint8_t *pData)
{
uint8_t writeBuffer[] = {MLX620_CMD_READ,0,0,0};
writeBuffer[1]=startAddr;
writeBuffer[2]=addrStep;
writeBuffer[3]=nWords;
return MLX620_I2C_Driver_WriteRead(AT91C_BASE_TWI,writeBuffer, pData);
}
uint8_t MLX620_WriteTrim(uint16_t trimReg)
{
uint8_t pOut[] = {MLX620_CMD_WRITE_TRIM,0,0,0,0};
pOut[1]=(uint8_t)((trimReg & 0xFF) - 0xAA);
pOut[2]=(uint8_t)(trimReg & 0xFF);
pOut[3]=(uint8_t)((trimReg >> 8) - 0xAA);
pOut[4]=(uint8_t)(trimReg >> 8);
MLX620_I2C_Driver_Write(AT91C_BASE_TWI,pOut);
}
uint8_t MLX620_ReadTrim (uint16_t *pTrimReg)
{
return (MLX620_ReadRAM(MLX620_RAM_TRIM,
0,
sizeof(uint16_t),
(uint8_t*)pTrimReg));
}
int main()
{
uint16_t *trim;
init_twi();
MLX620_WriteTrim(0x740E);
MLX620_ReadTrim(trim);
do
{
}while(1);
}
|