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

📄 ad5258.c

📁 通过AD5258和单片机控制的LED驱动程序
💻 C
字号:
/*------------------------------------------------------------------------------------------------------
FileName: Ad5258.c

-------------------------------------------------------------------------------------------------------*/
#include "AT89X51.H"
#include "Ad5258.h"



//I2Caddress IC4, IC5, IC7, IC9	  
code  uchar I2C_addr_wr[ADDR_NUM] = {0x30, 0x98, 0x34, 0x9c};		//AD5258
code  uchar I2C_addr_rd[ADDR_NUM] = {0x31, 0x99, 0x35, 0x9d};		//AD5258
code  uchar BIT_TEST[8]={0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};	//测试位

//I2C 的管脚定义
sbit  I2C_SDA = P0^1;
sbit  I2C_SCL = P0^0;

bit	  I2C_ack;

//I2C 函数
void  I2CStart();				//I2C start
void  I2CStop();				//I2C stop
void  I2CSendByte(uchar str); 	//I2C send a byte to slave	
uchar I2CRcvByte(); 	 		//I2C recieve a byte from slave

/*-----------------------------------------------------------------------------------------------------*/
//I2C bus--------------------------------------------------------
void  ConfigAd5258(uchar uIndex, uchar uVal)
{
	 I2CStart();  								//start
	 I2CSendByte(I2C_addr_wr[uIndex]); 			//address
	 I2CSendByte(0x00); 						//instruction
     I2CSendByte(uVal); 		//data 
	 I2CStop();	  								//stop
}

//Ad5258 active or deactive write protect
void  ActiveAd5258(uchar uIndex, uchar bActive)
{
   if(bActive == 1)
   {
		I2CStart();  							//start
		I2CSendByte(I2C_addr_wr[uIndex]); 		//address
		I2CSendByte(0x40); 						//instruction
		I2CSendByte(0x00); 		//data
		I2CStop();	
   }
   else
   {
   	   	I2CStart();  							//start
		I2CSendByte(I2C_addr_wr[uIndex]); 		//address
		I2CSendByte(0x40); 						//instruction
		I2CSendByte(0x01); 		//data
		I2CStop();
   }
}

//write data from ram to eeprom
void  WrData2EEprom(uchar uIndex)
{
	I2CStart();  							//start
	I2CSendByte(I2C_addr_wr[uIndex]); 		//address
	I2CSendByte(0xC0); 						//instruction
	I2CStop();	
}

//read data from eeprom to ram
void RdDataFromEEprom(uchar uIndex)
{
	I2CStart();  							//start
	I2CSendByte(I2C_addr_wr[uIndex]); 		//address
	I2CSendByte(0xA0); 						//instruction
	I2CStop();
}

//read data from ram
uchar RdDataFromRam(uchar uIndex)
{
	uchar val;
	I2CStart();  							//start
	I2CSendByte(I2C_addr_wr[uIndex]); 		//address
	I2CSendByte(0x00); 						//instruction
	I2CStart();  							//start
	I2CSendByte(I2C_addr_rd[uIndex]); 		//address
	val = I2CRcvByte();
	I2CStop();
	return val;
}

//I2C start
void  I2CStart()
{
	I2C_SDA = 1; 
	NOP;
	I2C_SCL = 1;
	NOP;
	I2C_SDA = 0;   	//high to low
	NOP;			//interval time > 0.6us
	I2C_SCL = 0;   	//make scl low to prepare to transfer data
}

//I2C stop	
void  I2CStop()
{
	I2C_SDA = 0;  	//prepare
	NOP;   													
	I2C_SCL = 1;  	//make scl high
	NOP;			//wait
	I2C_SDA = 1;  	//make SDA low to high
	NOP;			//wait
	I2C_SCL = 0;  	//make scl high
}

//I2C send a byte to slave	
void  I2CSendByte(uchar sch)
{
	uchar BitCnt; 
	for(BitCnt = 0; BitCnt < 8; BitCnt ++)  			//8bit to transfer
    {
		if(sch&BIT_TEST[BitCnt]) 
			I2C_SDA = 1; 	 							//judge
    	else  
			I2C_SDA = 0;   
		NOP;             
     	I2C_SCL = 1;               						//clock
      	NOPN;											//
        I2C_SCL = 0; 
    }
    NOP;
    I2C_SDA = 1;         								//make SDA high
    NOP;   
    I2C_SCL = 1;
    if(I2C_SDA == 1)	
		I2C_ack = 0;     
    else	
		I2C_ack = 1;        							//slave acknowledge
    I2C_SCL = 0;
} 	

//I2C recieve a byte from slave	
uchar I2CRcvByte()
{
	uchar BitCnt, retc = 0;	
	I2C_SDA = 1;             							//
	for(BitCnt = 0; BitCnt < 8; BitCnt ++)
	{
		I2C_SCL = 1;       								//
		NOPN;											//wait
		I2C_SCL = 0;  									//high to low
		NOP;
		if(I2C_SDA == 1)	
			retc = retc + BIT_TEST[BitCnt]; 			//read data
		
	}
	NOP;
	I2C_SDA = 1;  
	NOP;  
	return(retc);
}

⌨️ 快捷键说明

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