在线时间3 小时
UID1745397
注册时间2017-9-27
NXP金币0
该用户从未签到
注册会员

- 积分
- 59
- 最后登录
- 2020-5-11
|
本帖最后由 coolhe88 于 2020-5-11 21:35 编辑
您好!关于LPC824M201的IIC,有个问题请教一下,谢谢!
硬件:I2C主机:LPC824M201,内部晶体;IIC从机:APDS-9960;IIC两线上拉10K电阻
工具:J-Link V9
问题:在线仿真,主机可以读到从机,功能正常;下载后重新上电,主机识别不了从机。
IIC初始化代码:
//************************************************************//
void I2C_Init(void)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (I2C0 | SWM); // Enable clocks to I2C0, SWM (see lpc8xx_syscon.h)
LPC_SWM->PINENABLE0 &= ~(I2C0_SCL|I2C0_SDA); // Use for LPC824(Enable I2C on P0_10 and P0_11)
// Give I2C0 a reset (see lpc8xx_syscon.h)
LPC_SYSCON->PRESETCTRL &= (I2C0_RST_N); //Reset IIC.
LPC_SYSCON->PRESETCTRL |= ~(I2C0_RST_N); //Clear IIC reset.
// Configure the I2C0 clock divider
// Desired bit rate = Fscl = 100,000 Hz (1/Fscl = 10 us, 5 us low and 5 us high)
// Use default clock high and clock low times (= 2 clocks each)
// So 4 I2C_PCLKs = 100,000/second, or 1 I2C_PCLK = 400,000/second
// I2C_PCLK = SystemClock = 30,000,000/second, so we divide by 30/.4 = 75
// Remember, value written to DIV divides by value+1
LPC_I2C0->DIV = (75 - 1);
LPC_I2C0->CFG = CFG_MSTENA; //Enable LPC824 as IIC master.
}
//************************************************************//
从机检测代码:
//************************************************************//
unsigned char Slave_Check(void)
{
unsigned char uTemp=0;
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait the master state to be idle
LPC_I2C0->MSTDAT = (Slave_address<<1) | 0; // Address with 0 for RWn bit (WRITE)
LPC_I2C0->MSTCTL = CTL_MSTSTART|CTL_MSTCONTINUE; // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_MTR); // Wait for the address to be ACK'd
LPC_I2C0->MSTDAT = APDS9960_ID; // Send the data to the slave
LPC_I2C0->MSTCTL = CTL_MSTCONTINUE; // Continue the transaction
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_MTR); // Wait for the data to be ACK'd
LPC_I2C0->MSTDAT = (Slave_address<<1) | 1; // Address with 1 for RWn bit (Read)
LPC_I2C0->MSTCTL = CTL_MSTSTART; // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_MRR); // Wait for the address to be ACK'd
uTemp=LPC_I2C0->MSTDAT;
LPC_I2C0->MSTCTL = CTL_MSTSTOP; // Send a stop to end the transaction
if(uTemp==0xAB)
{
return True;
}
else
{
return False;
}
}
//************************************************************//
|
|