⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 02write1000.c

📁 24c16 收发程序 8051单片机
💻 C
字号:
//AD Convert Program
#include <Reg52.h>

void I2cStart();
void I2cWriteToC16(unsigned char No,unsigned char addr,unsigned char data1);
void WaitForUS(unsigned int val);
void I2cStop();
void SendAck(bit ack);/*finish mark*/
void I2cSendByteToC16(unsigned char data2);
void Write(void);
void start(void);

unsigned char i;
unsigned char j;
unsigned long int clk;
unsigned char z=0x00;
unsigned char page=0xa0;
unsigned char code datatable[] = {
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
    0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
    0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
    0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99};

unsigned char code hex[] = {
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};

sfr s=0x80;
sbit ISCL=P1^0;
sbit start_0809=P1^1;
sbit ale_0809=P1^1;
sbit ISDA=P1^2;
sbit clk_0809=P1^3;
sbit enable_0809=P1^4;
sbit eoc_0809=P1^5;
sbit LED=P3^5;

void main(void)
{        
    j=0x00;
    LED=0;
    for(i=0;i<100;i++)
      {WaitForUS(50000);}
    LED=1;	       //indicator LED;
    for(i=0;i<100;i++)
      {WaitForUS(50000);}
    Write();
    LED=0;
    for(i=0;i<200;i++)
      {WaitForUS(50000);}
    LED=1;
while(1);
}


void Write(void)
{ 
   unsigned char h,m,n; 
 for(n=0;n<10;n++)
  {
   for(m=0;m<100;m++)
   {
     h=datatable[m];    
	   I2cWriteToC16(page,z,h);    //write in the 24c16;
//     I2cWriteToC16(page,0xff,j); 
	   z=z+1;
//	   j++;
	   if (z>=0xff)
	   {page=page+2;
        z=0x00;  
//	      j=0x00;
       } 
//    LED=0;
//    for(i=0;i<15;i++)
//      {WaitForUS(50000);}
//    LED=1;	       //indicator LED;
//    for(i=0;i<15;i++)
//      {WaitForUS(50000);}
   }
  }
}


void I2cStart()
{
     ISDA=1;
     WaitForUS(8);      /* 4us       */
     ISCL=1;            /*initial state*/
     WaitForUS(8);
     ISDA=0;            /*start*/
     WaitForUS(8);
     ISCL=0;            /*ready*/  //??????????????????????????????            
     WaitForUS(10);   
}       

void I2cStop()
{
    ISDA=0;
    WaitForUS(10);
    ISCL=1;
    WaitForUS(10);
    ISDA=1;
    WaitForUS(6000);
    WaitForUS(6000);
}


void SendAck(bit ack)/*finish mark*/
{ 
   ISDA=ack;  //                        ack????????????????????????????????????????
   WaitForUS(2);
   ISCL=1;
   WaitForUS(10);
   ISCL=0;
   WaitForUS(10);
}

void I2cSendByteToC16(unsigned char data2)
{
    unsigned char BitCounter;
    for(BitCounter=0;BitCounter<8;BitCounter++)
       {
		ISDA=(bit)(data2&0x80);
        WaitForUS(2);
         ISCL=1;
        WaitForUS(8);
         ISCL=0;
         WaitForUS(10);
		 data2<<=1;
       }
    ISDA=1;            /* this 4line WaitForUS for ack*/      
    WaitForUS(8);
    ISCL=1;
    WaitForUS(8);          
    ISCL=0;
    WaitForUS(10);
}

void I2cWriteToC16(unsigned char No,unsigned char addr,unsigned char data1)
{
    I2cStart();
    I2cSendByteToC16(No);
    I2cSendByteToC16(addr);
    I2cSendByteToC16(data1);
    I2cStop();
   
}

/********************************************/
/* this routine delay CPU N us using timer0 */
/********************************************/
void WaitForUS(unsigned int val)
{
   unsigned int tmp;

   tmp  = 65536-val;
   TL0  = tmp & 0xff;
   TH0  = (tmp>>8)&0xff;
   TF0  = 0;
   TR0  = 1;       /*start timer0*/
   while(TF0==0);   /*wait for timer0 overflow */
   TR0  = 0;       /*stop timer0*/
   TF0  = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -