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

📄 iicled.c

📁 VB环境下的串口通讯设计
💻 C
字号:
/************************************************/
#include <REG52.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define int8 unsigned char
#define int16 unsigned int
#define int32 unsigned long 
sbit I2C_SDA= P1^5;
sbit I2C_SCK= P1^4;
sbit I2C_WP= P1^3;

//============================
sbit LED1=P3^4;
sbit LED2=P1^6;
sbit GRE=P1^7;
sbit s1=P0^3;
sbit s2=P2^3;
sbit SCK=P2^4;
sbit RCK=P2^5;
sbit nG=P2^6;
sbit SER=P2^7;
uchar xdata tab[16][6];
uchar count;
uchar l;
uint m;

void McuInit(void);
void Timer_Int();
//===============================================
/* 列选择*/
void lay(int8 n)
{
	int8 i;
	i=n;
	s1=0;
	s2=0;
	if(n<8)
	{
		//_nop_();
		P0=(P0&0xf8)|(~i);
	}
	else
	{	i=i-7;
		i=i&0x07;
		i=i|0xf0;		  //保护nG位
		P2=(P2&0xf8)|(~i);
	}

}		
/*延时*/
void delay(int8 N)
{
	int8 i,j,k;
	for(i=0;i<100;i++)
		for(j=0;j<10;j++)
			for(k=0;k<N;k++)GRE=~GRE;	

}
/*发送字节,移位发送*/
void WriteByte(int8 val)
{
	int8 i,m;
  	m=~val;
  	for (i = 8; i > 0; i --)
	{
    	SCK = 0;//拉低74HC595时钟
    	SER = m&0x01;//发送74HC595一位串行数据
    	SCK = 1;//拉高74HC595时钟
		m=m>>1;
    	_nop_();//延时
  }
  SER = 1;//释放数据总线
}

/*发送字*/
void display(int8 n )
{
	int8 i;
	for(i=0;i<6;i++)
	WriteByte(tab[n][i]);
    RCK=0;	//保存并行数据
    _nop_();
    RCK=1;
}

void delays(int8 n)
{	n=n;
	m=m+0x1;
	LED1=m&0x800;
	GRE=~LED1;
	LED2=m&0x1000;
}

void XianShiX(void)
{
	/**/display(0);//行0
	   	lay(14);   //列
	   	delays(1); //短延时
	/**/display(1);//行1
	   	lay(13);
	   	delays(1);
	/**/display(2);//行2
	   	lay(12);
	   	delays(1);
	/**/display(3);//行3
	   	lay(11);
	   	delays(1);
	/**/display(4);//行4
		lay(10);
		delays(1);
	/**/display(5);//行5
	   	lay(9);
	   	delays(1);
	/**/display(6);//行6
	   	lay(8);
	   	delays(1);

	///**/Timer_Int();
	/**/display(7);//行7
	   	lay(15);
	   	delays(1);
	/**/display(8);//行8
	   	lay(7);
	   	delays(1);
	/**/display(9);//行9
	   	lay(6);
	   	delays(1);
	/**/display(10);//行10
	   	lay(5);
	   	delays(1);
	/**/display(11);//行11
	   	lay(4);
	   	delays(1);
	/**/display(12); //行12
	   	lay(0);
	   	delays(1);
	/**/display(13); //行13
	   	lay(1);
	   	delays(1);
	/**/display(14); //行14
	   	lay(2);
	   	delays(1);
	/**/display(15);  //行15
	   	lay(3);
	   	delays(1);
}
//=============================
//延时10US
//内部调用函数
void  Delay_10_uS(void)
{
     char i=2;
     while(i--);
}



//开始位
//内部调用函数
bit I2C_Start(void)
{
     Delay_10_uS();
     I2C_SDA =1;
     Delay_10_uS();
     I2C_SCK =1;
     Delay_10_uS();
     if ( I2C_SDA == 0) return 0;
     if ( I2C_SCK == 0) return 0;
     I2C_SDA = 0;
     Delay_10_uS();
     I2C_SCK = 0;
     Delay_10_uS();
     return 1;
}

//停止位
//内部调用函数
void  I2C_Stop(void)
{
     Delay_10_uS();
     I2C_SDA = 0;
     Delay_10_uS();
     I2C_SCK = 1;
     Delay_10_uS();
     I2C_SDA = 1;
     Delay_10_uS();
}


//读数据后拉高应答信号
//内部调用函数
void I2C_Nack(void)
{
     Delay_10_uS();
     I2C_SDA=1;
     Delay_10_uS();
     I2C_SCK=1;
     Delay_10_uS();
     I2C_SCK=0;
     Delay_10_uS();
}

//写单字节数据
//内部调用函数
bit I2C_Send_Byte( uchar d)
{
     uchar i = 8;
     bit bit_ack;
     while( i-- )
     {
          Delay_10_uS();
          if ( d &0x80 )   I2C_SDA =1;
          else             I2C_SDA =0;
          Delay_10_uS();
          I2C_SCK = 1;
          Delay_10_uS();
          I2C_SCK = 0;
          d = d << 1;
     }
     Delay_10_uS();
     I2C_SDA = 1;
     Delay_10_uS();
     I2C_SCK = 1;
     Delay_10_uS();
     bit_ack = I2C_SDA;
     I2C_SCK =0;
     Delay_10_uS();
     return bit_ack;
}

//接收单字节数据
//内部调用函数
uchar I2C_Receive_Byte(void)
{
     uchar i = 8, d;
     Delay_10_uS();
     I2C_SDA = 1;
     while ( i--)
     {
          d = d << 1;
          Delay_10_uS();
          I2C_SCK =1;
          if ( I2C_SDA ) d++;
          Delay_10_uS();
          I2C_SCK =0;
     }
     return d;
}



//读多字节数据
//供外部调用函数
void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)
{
	 s1=0;s2=0;nG=1;
     while(count--)
     {
          I2C_Start();
          /*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 
*2 );*/   /* 24C16 USE */
          I2C_Send_Byte( 0xa0 );//写命令设备地址为10100000最后一位为0"写标志"
          I2C_Send_Byte( AT24C64_address/256 );//写存储器页地址
          I2C_Send_Byte( AT24C64_address % 256 );//写存储器字节地址
          I2C_Start();
          /*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
          I2C_Send_Byte( 0xa1 );//写命令设备地址为10100001最后一位为1"读标志"
          *(uchar*)mcu_address = I2C_Receive_Byte();//读单字节数据到MCU指针指向的地址
          I2C_Nack();//回应答信号
          I2C_Stop();
          ((uchar*)mcu_address)++;
          AT24C64_address++;
     }
	s1=1;s2=1;nG=0;	 
}

//=====================================
void main()
{
	uchar len;
	McuInit();
	GRE=1;
	AT24C64_R(tab,0x00,96);
	TR0=0;
	//TR1=1;
	EA=0;
	while(1) 
	{
	nG=0;
	len=250;
	AT24C64_R(tab,0x00,96);
	while(len>0){XianShiX();len--;}
	len=250;
	AT24C64_R(tab,0x60,96);
	while(len>0){XianShiX();len--;}
	len=250;
	AT24C64_R(tab,0xc0,96);
	while(len>0){XianShiX();len--;}
	
	//AT24C64_R(tab,0x00,96);
	}

}



void McuInit(void)
{
	s1=0;s2=0;nG=1;
	EA=0;
	LED1=0;
	LED2=0;
	GRE=0;
	delay(250);
	LED1=1;
	LED2=0;
	GRE=0;
	delay(100);
	LED1=0;
	LED2=1;
	GRE=0;
	delay(90);
	LED1=0;
	LED2=0;
	GRE=1;
	delay(60);
	LED1=1;
	LED2=0;
	GRE=1;
	delay(40);
	LED1=1;
	LED2=1;
	GRE=0;
	delay(20);
	LED1=0;
	LED2=0;
	GRE=0;
	
    ET0 = 0;        //允许定时器0中断
    TMOD = 0X01;       //定时器工作方式选择
    TL0 = 0x00;     
    TH0 = 0x00;     //定时器赋予初值
    TR0 = 0;        //启动定时器
	TR1=0;
	EX0=0;
	EX1=0;
	EA =0 ;         //开总中断
	nG=0;

}

⌨️ 快捷键说明

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