使用mc9s12dg128的iic接口调试at24c01a,一个iic接口的eeprom
老是调不通,程序老死机。大家给看看是什么问题。。
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
void IIC_int()
{
IBFD=0xfc;
IBAD=0xee;
IBCR|=0x80;
}
void IIC_Start()
{
IBCR|=0x30;
}
void IIC_Stop()
{
IBCR&=0xdf; //stop
}
void uart_init(void)
{
SCI1CR2=0x08;
SCI1BDH=0x00;
SCI1BDL=0x9C;
}
void uart_putchar(unsigned char ch)
{
while(!(SCI1SR1&0x40))
{}
SCI1DRL=ch;
}
uchar send_data(uchar ch)
{
IBSR_IBIF=1; //clear flag
IBDR=ch;
//while(!IBSR_IBB); //if the bus is busy
while(!IBSR_IBIF);//wait for acknowledge
}
void wait_ack()
{
while(IBSR_RXAK); //
}
void main(void) {
/* put your own code here */
EnableInterrupts;
uart_init();
IIC_int();
while(IBSR_IBB);
IIC_Start();
//write
send_data(0xa0);
wait_ack();
send_data(0x02);
wait_ack();
send_data(0x78);
wait_ack();
IIC_Stop(); //stop
//read
//IIC_int();
IIC_Start();
send_data(0xa0);
wait_ack();
send_data(0x02);
wait_ack();
IIC_Start();
send_data(0xa1);
wait_ack();
//while(!IBSR_RXAK); //
IBCR&=0xef; //receive
IBSR_IBIF=1; //clear flag
//uart_putchar(IBSR);
while(!IBSR_IBIF);
//uart_putchar(0xf0);
//IBDR=0xff;
uart_putchar(IBDR);
IIC_Stop(); //stop
IBSR_IBIF=1; //clear flag
for(;;) {} /* wait forever */
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |