本帖最后由 56007904 于 2020-5-28 16:20 编辑
大家好,最近在研究赛元的8617 TWI功能读写外部EEPROM 24C02的问题,没有调通,无法正常读写!恳请搞过赛元的大神指教!代码如下:void main(void)
{
unsigned char rd_eeprom;
Uart0Init();
USCI0_TWI_Init();
Uart2_Uart_Init();
Timer0Init();
enableInterrupts();
TWIBUS_ByteWrite(EEPROM_ADDR,0x01, 0x55);
while(1)
{
rd_eeprom = TWIBUS_ByteRead(EEPROM_ADDR,0x01);
if(rd_eeprom == 0x55)
{
UART0_Send_String(send_cmd,8);
}
}
}
其中USCI0_TWI_Init()如下:
void USCI0_TWI_Init()
{
USCI0_TWI_MasterCommunicationRate(USCI0_TWI_256); //125K 设置IIC时钟频率
USCI0_TWI_AcknowledgeConfig(ENABLE); //使能应答
USCI0_TWI_Cmd(ENABLE);//TWI功能开关函数 开启TWI功能
USCI0_ITConfig(ENABLE, LOW);//开启USCI0功能
}
关于24C02的写时序如下:
//===============向TWI总线上写入一个数据==============//
void TWIBUS_ByteWrite(unsigned char SLAVE_ADDR,unsigned char addr, unsigned char dat)
{
USCI0_TWI_Start(); //产生起始信号
while(!TWI0Flag);
TWI0Flag = 0;
USCI0_TWI_SendData(SLAVE_ADDR); //发送从机地址 A0
while(!TWI0Flag);
TWI0Flag = 0;
USCI0_TWI_SendData(addr); //发送地址
while(!TWI0Flag);
TWI0Flag = 0;
USCI0_TWI_SendData(dat); //发送数据
while(!TWI0Flag);
TWI0Flag = 0;
Delay(100); // 系统延时
USCI0_TWI_MasterModeStop(); //停止位
Delay(100); // 系统延时
}
关于24C02的读时序如下:
//==================================从TWI总线上读入一个数据=================================//
unsigned char TWIBUS_ByteRead(unsigned char SLAVE_ADDR,unsigned char addr)
{
uint8_t TWI_Read_dat;
USCI0_TWI_Start(); //产生起始信号
while(!TWI0Flag);
TWI0Flag = 0;
USCI0_TWI_SendData(SLAVE_ADDR); //发送从机地址 A0
while(!TWI0Flag);
TWI0Flag = 0;
USCI0_TWI_SendData(addr); //发送地址
while(!TWI0Flag);
TWI0Flag = 0;
USCI0_TWI_Start(); //产生起始信号
while(!TWI0Flag);
TWI0Flag = 0;
USCI0_TWI_SendData(0xa1); //发送地址
while(!TWI0Flag);
TWI0Flag = 0;
TWI_Read_dat = USCI0_TWI_ReceiveData( );
Delay(100); // 系统延时
USCI0_TWI_MasterModeStop(); //停止位
Delay(100); // 系统延时
return TWI_Read_dat ;
}
串口TWI中断配置如下:
void USCI0Interrupt() interrupt 7
{
if(US0CON0&0x40)
{
US0CON0 &= 0xbf; //中断清零
TWI0Flag = 1;
}
}
|