|
-
- #include
- #include
- #define AddWr 0xae
- #define AddRd 0xaf
-
- sbit Sda=P1^2;
- sbit Scl=P1^1;
- sbit WP=P1^0;
- /*------------------------------------------------
- 延时程序
- ------------------------------------------------*/
- void mDelay(unsigned char j)
- {
- unsigned int i;
- for(;j>0;j--)
- {
- for(i=0;i<125;i++)
- {;}
- }
- }
- /*------------------------------------------------
- 启动IIC总线
- ------------------------------------------------*/
- void Start(void)
- {
- Sda=1;
- _nop_();_nop_();
- Scl=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Sda=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Scl=0;
- }
- /*------------------------------------------------
- 停止IIC总线
- ------------------------------------------------*/
- void Stop(void)
- {
- Sda=0;
- _nop_();
- Scl=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Sda=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Scl=0;
- }
- /*------------------------------------------------
- 应答IIC总线
- ------------------------------------------------*/
- void Ack(void)
- {
- Sda=0;
- _nop_();_nop_();_nop_();
- Scl=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Scl=0;
- _nop_();_nop_();
- }
- /*------------------------------------------------
- 非应答IIC总线
- ------------------------------------------------*/
- void NoAck(void)
- {
- Sda=1;
- _nop_();_nop_();_nop_();
- Scl=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Scl=0;
- _nop_();_nop_();
- }
- /*------------------------------------------------
- 发送一个字节
- ------------------------------------------------*/
- void Send(unsigned char Data)
- {
- unsigned char BitCounter=8;
- unsigned char temp;
- do
- {
- temp=Data;
- Scl=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- if((temp&0x80)==0x80)
- Sda=1;
- else
- Sda=0;
- Scl=1;
- temp=Data<<1;
- Data=temp;
- BitCounter--;
- }
- while(BitCounter);
- Scl=0;
- }
- /*------------------------------------------------
- 读入一个字节并返回
- ------------------------------------------------*/
- unsigned char Read(void)
- {
- unsigned char temp=0;
- unsigned char temp1=0;
- unsigned char BitCounter=8;
- Sda=1;
- do
- {
- Scl=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Scl=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- if(Sda)
- temp=temp|0x01;
- else
- temp=temp&0xfe;
- if(BitCounter-1)
- {
- temp1=temp<<1;
- temp=temp1;
- }
- BitCounter--;
- }
- while(BitCounter);
- return(temp);
- }
-
- /*------------------------------------------------
- 写入数据
- ------------------------------------------------*/
- void WrToROM(unsigned char Data[],unsigned char Address,unsigned char Num)
- {
- unsigned char i;
- unsigned char *PData;
- PData=Data;
- for(i=0;i
- {
- Start();
- Send(AddWr);
- Ack();
- Send(Address+i);
- Ack();
- Send(*(PData+i));
- Ack();
- Stop();
- mDelay(20);
- }
- }
- /*------------------------------------------------
- 读出数据
- ------------------------------------------------*/
- void RdFromROM(unsigned char Data[],unsigned char Address,unsigned char Num)
- {
- unsigned char i;
- unsigned char *PData;
- PData=Data;
- for(i=0;i
- {
- Start();
- Send(AddWr);
- Ack();
- Send(Address+i);
- Ack();
- Start();
- Send(AddRd);
- Ack();
- *(PData+i)=Read();
- Scl=0;
- NoAck();
- Stop();
- }
- }
- /*------------------------------------------------
- 主程序
- ------------------------------------------------*/
- void main()
- {
- unsigned char Number[4]={0x06,0x5b,0x4f,0x66};
- unsigned char i;
- WP=0;
- P2=0x00;
-
- WrToROM(Number,4,4);
- mDelay(200);
- Number[0]=0;
- Number[1]=0;
- Number[2]=0;
- Number[3]=0;
- RdFromROM(Number,4,4);
-
- while(1)
- {
-
- P0=Number[i];
- mDelay(200);
- mDelay(200);
- i++;
- if(i==4)
- i=0;
- }
- }
复制代码
这有个24c02的程序 看的我直迷糊 哎。。 真是晕的很 是不是太笨了 |
|