This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
1、无法收到总线的应答信号
��i nclude <reg52.h> ��i nclude <intrins.h> #define uchar unsigned char sbit SDA=P3^4; sbit SCL=P3^5; sbit P10=P1^0; uchar g8563_Store[4]; /*ʱ�佻����,ȫ�ֱ�������*/ uchar code c8563_Store[4]={0x00,0x59,0x07,0x01}; /*д��ʱ���ֵ������һ 07:59:00*/ /******************************************** �ڲ���������ʱ1 ********************************************/ void Delay() { // {P10=1;_nop_();P10=0;} _nop_(); _nop_(); /*���ݾ���Ƶ���ƶ���ʱʱ��*/ } /******************************************** �ڲ�������I2C��ʼ ********************************************/ void Start() { EA=0; SDA=1; SCL=1; Delay(); SDA=0; Delay(); SCL=0; } /******************************************** �ڲ�������I2C���� ********************************************/ void Stop() { SDA=0; SCL=0; Delay(); SCL=1; Delay(); SDA=1; Delay(); EA=1; } /******************************************** �ڲ����������ACK ,ÿ���ֽڴ�����ɣ����ack=0,��������ݣ�ack=1; ********************************************/ void WriteACK(uchar ack) { SDA=ack; Delay(); SCL=1; Delay(); SCL=0; } /******************************************** �ڲ��������ȴ�ACK ********************************************/ void WaitACK() { uchar errtime=20; SDA=1; Delay(); /*��ACK*/ SCL=1; Delay(); while(SDA) { errtime--; if(!errtime) Stop(); } SCL=0; Delay(); } /******************************************** �ڲ�����.��������ֽ� ���:B=���� ********************************************/ void writebyte(uchar wdata) { uchar i; for(i=0;i<8;i++) { if(wdata&0x80) SDA=1; else SDA=0; wdata<<=1; SCL=1; Delay(); SCL=0; } WaitACK(); //I2C������ͨѶ����������˳�I2CͨѶ } /******************************************** �ڲ�����.�������� ����:B ********************************************/ uchar Readbyte() { uchar i,bytedata; SDA=1; for(i=0;i<8;i++) { SCL=1; bytedata<<=1; bytedata|=SDA; SCL=0; Delay(); } return(bytedata); } /******************************************** �������->pcf8563 ********************************************/ void writeData(uchar address,uchar mdata) { Start(); writebyte(0xa2); /*д����*/ writebyte(address); /*д��ַ*/ writebyte(mdata); /*д����*/ Stop(); } /******************************************** ��������<-pcf8563 ********************************************/ uchar ReadData(uchar address) /*���ֽ�*/ { uchar rdata; Start(); writebyte(0xa2); /*д����*/ writebyte(address); /*д��ַ*/ Start(); writebyte(0xa3); /*������*/ rdata=Readbyte(); WriteACK(1); Stop(); return(rdata); } void ReadData1(uchar address,uchar count,uchar * buff) /*���ֽ�*/ { uchar i; Start(); writebyte(0xa2); /*д����*/ writebyte(address); /*д��ַ*/ Start(); writebyte(0xa3); /*������*/ for(i=0;i<count;i++) { buff[i]=Readbyte(); if(i<count-1) WriteACK(0); } WriteACK(1); Stop(); } /******************************************** �ڲ�����,����ʱ�䵽�ڲ������� ********************************************/ void P8563_Read() { uchar time[7]; ReadData1(0x02,0x07,time); g8563_Store[0]=time[0]&0x7f; /*��*/ g8563_Store[1]=time[1]&0x7f; /*��*/ g8563_Store[2]=time[2]&0x3f; /*Сʱ*/ g8563_Store[3]=time[4]&0x07; /*����*/ } /******************************************** ����ʱ�䵽�ڲ�������----�ⲿ���� ********************************************/ void P8563_gettime() { P8563_Read(); if(g8563_Store[0]==0) P8563_Read(); /*���Ϊ��=0��Ϊ��ֹʱ��仯���ٶ�һ��*/ } /******************************************** дʱ����ֵ ********************************************/ void P8563_settime() { uchar i; for(i=2;i<=4;i++) { writeData(i,g8563_Store[i-2]); } writeData(6,g8563_Store[3]); } /******************************************** P8563�ij�ʼ��-----�ⲿ���� ********************************************/ void P8563_init() { uchar i; if((ReadData(0xa)&0x3f)!=0x8) /*����Ƿ��һ������������ʼ��ʱ��*/ { for(i=0;i<=3;i++) g8563_Store[i]=c8563_Store[i]; /*��ʼ��ʱ��*/ P8563_settime(); writeData(0x0,0x00); writeData(0xa,0x8); /*8:00����*/ writeData(0x1,0x12); /*������Ч*/ writeData(0xd,0xf0); } }
你好。简单浏览了一下代码,发现你使用的是GPIO模拟I2C。建议使用G2553片上自带的USCI模块。在www.ti.com/msp430中有关于G2xxx系列的I2C例程,包括Master和Slave。可以作为参考。