Zhhu218 发表于 2011-11-5 20:13:26

51单片机仿真IIC总线,用proteus调试成功

以下是源程序:
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
#include<stdlib.h>
#include<string.h>

#define uchar unsigned char
#define uint unsigned int
#define WSLA 0xa0
#define RSLA 0xa1
                                                  
bit NACK;
sbit SDA=P1^6;
sbitSCL =P1^7        ;
sbit LED1=P3^0;
sbit LED2=P3^1;
sbit LED3=P3^2;
sbit LED4=P3^3;
bitflag=0,flag1,flag2=0;
uchar n=8;
uchar idata BufWrite;
uchar idata BufRead;
uchar addr=0x00;
uchar word={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07};
void display(uchar ch);

void delay()
{
uchar k;
for(k=0;k<5;k++)
      _nop_();
}

void start()
{
   SDA=1;
   _nop_();
   SCL=1;
   delay();
   SDA=0;
   delay();
   SCL=0;
   _nop_();
   
}       

void stop()
{

SDA=0;
_nop_();
_nop_();
SCL=1;
delay();
SDA=1;
delay();
SCL=0;
SDA=0;

}
void CACK()
{
SDA=1;
SCL=1;
flag=0;
flag1=SDA;
if( flag1==1)
      flag=1;
          
SCL=0;
   _nop_();       

}
void N_ACK()
{
    SDA=1;
        _nop_();
        _nop_();
        _nop_();
        SCL=1;
        delay();
        SCL=0;
        _nop_();
        _nop_();
        SDA=0;
       
}
voidACK()
{
          SDA=0;
          _nop_();
          _nop_();
          _nop_();
          SCL=1;
          delay();
          SCL=0;
          _nop_();
          _nop_();
          _nop_();
          SDA=1;
          _nop_();
          
}




void WriteOneByte(uchar ch)
{
uchar i;
uchar Write;
Write= ch;
   
for( i=0;i<8;i++)
   {
      
   if((Write&0x80)==0x80)
           {
               SDA=1;
                   }
          else
           {
               SDA=0;
                  }
          _nop_();
          SCL=1;
          delay();
          SCL=0;
          Write= Write<<1;

    }
}
uchar ReadOneByte()
{
uchar i;
uchar Read=0x00;
uchar m=8;
SDA=1;
for( i=0;i<8;i++)
    {
           SDA=1;
           _nop_();
           _nop_();
           SCL=1;
           delay();

           Read=Read<<1;
           if(SDA==1)
                 Read=Read|0x01;
           else
                     Read=Read &0xfe;
          
                SCL=0;
       
           /*
           flag2=SDA;
           Read=(_crol_(flag2,m--)|| Read);
           SCL=0; */
       }
       
        return Read;
}

void WriteByte(uchar addr)
{
uchar i;
start();
WriteOneByte(WSLA);
CACK();
       
WriteOneByte(addr);
CACK();
for(i=0;i<n;i++)
{
       
        WriteOneByte(BufWrite);
    CACK();
       
               }

    stop();
       
}

voidReadByte(uchar addr)
{
uchar i;
start();
WriteOneByte(WSLA);
CACK();
WriteOneByte(addr);
CACK();
start();
WriteOneByte(RSLA);
CACK();
for(i=0;i<n;i++)
{
    BufRead=ReadOneByte();
       if(i==7)
          N_ACK();
       else
          ACK();
        display(BufRead);
        }
stop();
          
          
}


void display(uchar ch)
{
       uchar j;
          uint k;
      P0= ch;
          LED1=0;
          for(k=0;k<10000;k++)
          {
          for(j=0;j<20;j++);
                }
   
}
/*   
void display()
{
uchar i,j;
uint k;
for(i=0;i<n;i++)
   {
      P0= BufRead;
          LED1=0;
          for(k=0;k<10000;k++)
          {
          for(j=0;j<10;j++);
                }
   }
}
*/
void main()
{
uchar i;
uint k;
for(i=0;i<n;i++)
   {
      BufWrite= word;
          
           }
while(1)
{

   WriteByte(0x00);
   for(k=0;k<10000;k++);
   ReadByte(0x00);
   for(k=0;k<10000;k++);
}

}
加入proteus的调试ourdev_692249ESQROJ.rar(文件大小:53K) (原文件名:IIC.rar)

littlepig 发表于 2011-11-5 20:41:19

good

楼主的程序是要实现什么功能,说明一下吧

Zhhu218 发表于 2011-11-5 21:25:59

发送数据到IIC的存储器中,再读取出来。很简单的

millwood0 发表于 2011-11-5 21:29:56

think about how you could package the code to make it more generic for other i2c applications.

craigtao 发表于 2014-7-31 16:52:01

重新拾回单片机的乐趣,丢弃arm,嘿嘿,
页: [1]
查看完整版本: 51单片机仿真IIC总线,用proteus调试成功