什么情况,楼上晓得其中猫腻?
贴一段代码过来。void Send_Data(uchar ch)
{
IICS_IICIF |= 1;
IICD = ch;
while(!IICS_IICIF);
}
void Wait_Ack(void)
{
while(IICS_RXAK);
}
void main(void) {
byte second = 0;
byte i, temp;
Init_MCG();
IICF_MULT = 0b00;
// 00 mul=1
IICF_ICR = 0x1D;
// 011101 ICR = 160
// IIC baud rate is: 8M/(1*160)=50kb
IICC1_IICEN |= 0b1;
while(IICS_BUSY);
// start IIC in master transmit mode
IICC1 |= 0x30;
while(!IICS_BUSY);
Send_Data(0xa2);
Wait_Ack();
Send_Data(0x0);
IICS_IICIF |= 1;
Wait_Ack();
IICS_IICIF |= 1;
Send_Data(0x0);
IICC1_RSTA |= 1;
Send_Data(0xa3);
Wait_Ack();
IICC1_TX &= 0;
IICD;
for(i = 0; i < 16; ++i) {
IICS_IICIF |= 1;
second = IICD;
}
IICC1_MST &= 0;
EnableInterrupts; /* enable interrupts */
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
/*
** MCG in PEE mode
** using external oscillator
** oscillator clock 16MHz
** bus system clock 8MHz
*/
void Init_MCG(void)
{
/* set system clock */
MCGC2 = 0x36;
// 00 BDIV divide-by-1
// 1 RANGE high frequency range
// 1 HGO external oscillator for high gain operation
// 0
// 1 EREFS a crystal is being used
// 1 ERCLKEN ensure the external reference clock is active
// 0
while(!MCGSC_OSCINIT);
MCGC1 = 0xB8;
// 10 CLKS select external reference clock as system clock source
// 111 RDIV divide-by-128
// 0 IREFS select the external reference clock
// 00
while(MCGSC_IREFST);
while(MCGSC_CLKST != 0b10);
MCGC1 = 0xA0;
MCGC3 = 0x44;
// 0
// 1 PLLS select the PLL
// 0
// 0
// 0100 VDIV multiply-by-16 1MHz reference * 16 = 16MHz
while(!MCGSC_PLLST);
while(!MCGSC_LOCK);
MCGC1 = 0x20;
while(MCGSC_CLKST != 0b11);
}
16M晶振, 8M总线频率。
用示波器查看SCL, SDA时序,与寄存器反应的情况相符合:发A2H, slave device有应答,然后是字地址00H无应答。SDA第9位高电平。
求教是哪个地方原因。程序中有错误也恳请指出。 |