|
我用V850E/IF3 uPD70F3452的IIC串口写microchip的241c16bEEPROM的程序,3452芯片为主设备,241c16b为从设备,程序编译通过,运行后,数据就是写不进EEPROM,不知道哪里出错误了,请大家帮我看看。我用另外的程序把写入EEPROM的数据读出来并显示。
我的程序如下:
//设置端口
void PORT_Init(void)
{
PM0 = 0xff;
PMC0 = 0x00;
PM1 = 0x7f;
PMC1 = 0x00;
PM2 = 0xff;
PMC2 = 0x00;
PM3 |= 0xfd; //设置IIC端口及模式
PFC3 |= 0x03;
PFCE3 &= 0xfd;
PF3 |= 0x03;
PMC3 |= 0x03;
PM4 = 0xff;
PMC4 = 0x00;
P7 = 0x00;
PMC7 = 0x00;
PMDLH = 0xff;
PMDLL = 0xff;
PMCDLH = 0x00;
PMCDLL = 0x00;
}
void eeprom_input(uchar eeprom_in)
{
uchar i;
for (i=7;i>=0;i--)
{
SDA = (eeprom_in>> i) & 0x01;
}
}
//字节写
void byte_write(uchar control_byte,uchar write_add,uchar data)
{
eeprom_input(control_byte);
eeprom_input(write_add);
eeprom_input(data);
}
//IIC初始化
void IIC_Init(void)
{
/* transfer clock setting */
IICX0 = 0x00; /* Communicate at transfer rate set by the IICCL0.CL00 bit.*/
IICOCKS = 0x12; /* IIC division clock selection: fxx/8 */
IICCL0 = 0x01; /* operation mode:low-speed mode,digital filter operation:digital filter off */
/* start condition setting */
IICF0 |= 0x02; /* IICRSV0=1:enable communication reservation,STCEN0=1 */
IICC0 |= 0x3c; /* ACKE0=WTIM0=SPIE0=1 */
IICC0 |= 0x80; /* IICE0=1,enable IIC */
}
// IIC发送数据
void IIC_Send(uchar out_data1,uchar out_data2,uchar out_data3)
{
uchar i;
uchar data[3]= {out_data1,out_data2,out_data3};
IIC_Init();
while (!STCEN0)
{
SPT0 = 1; /* stop-condition */
while (!IICIF) /* waiting for stop-condition detection */
{
;
}
__EI();
break;
}
STT0 = 1; /* start-condition */
for (i=0; i<3; i++)
{
IIC0 = data;
while (!IICIF) /* waiting for data transmission */
{
;
}
__EI();
if (!ACKD0)
{
break;
}
}
SPT0 = 1;
IICE0 = 0;
}
//主程序
main()
{
/* port and module initialization */
PORT_Init();
CLOCK_Init();
IIC_Init();
ei(); /* system interrupt enable */
eeprom_add = 0xa0;
data = 0;
for (m=8; m>0; m--) //把EEPROM的8个block(256bytes/block)都写入0~9
{
for (j_sea=0;j_sea<=255;j_sea++)
{
IIC_Send(eeprom_add,j_sea,data++);
byte_write(eeprom_add,j_sea,data++);
if (data> 9)
{
data = 0;
}
}
eeprom_add |= 0x02;
}
P1 &= 0x00;//蜂鸣器响,作为结束标志
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|