花生漫画

AD5422读写程序

0
阅读(3488)
void WriteToAD5422(unsigned char count,unsigned char *buf)
{

 unsigned char ValueToWrite = 0;
    unsigned char i = 0;
 unsigned char j = 0;
 
  SET_LATCH();
CLR_LATCH();

	delay(5);
 for ( i=count;i>0;i-- )
  {
   ValueToWrite = *(buf+i-1);
  for (j=0; j<8; j++)
  {
   CLR_SCL();
   delay(5);
   if(0x80 == (ValueToWrite & 0x80))
   {
    SET_SDO();		   //Send one to SDIN pin of AD5422
   }
   else
   {
    CLR_SDO();	//Send zero to SDIN pin of AD5422

   }
           
   delay(5);
   SET_SCL();
   ValueToWrite <<= 1; //Rotate data
 
  }

 }
 CLR_SCL();
 delay(5);
 SET_LATCH();
 delay(20);
}

void ReadFromAD5422(unsigned char count,unsigned char *buf)
{
 unsigned char i = 0;
 unsigned char j = 0;
 unsigned int   iTemp = 0;
 unsigned char   RotateData = 0;



 CLR_LATCH();

  delay(5);
 for(j=count; j>0; j--)
 {

  for(i=0; i<8; i++)
  {
      CLR_SCL();
	 
  
   RotateData <<= 1;  //Rotate data
   delay(5);
   CLR_SDO(); 		//Write a nop condition when read the data. 
  delay(5);

   	iTemp = DOUT;	 //Read SDO of AD5422
	 SET_SCL();
   if(iTemp)
   {
    RotateData |= 1; 
   }
   
  }
  *(buf+j-1)= RotateData;
 }
 CLR_SCL();
 delay(1);  
 SET_LATCH();
 delay(20);
} 
void init_serialcomm()
{
	PCON |= 0x00;		//波特率不倍速	
	SCON = 0x50;		//8位数据,可变波特率
	
	TMOD &= 0x0f;		//清除定时器1模式位				 0x20
	TMOD |= 0x20;		//设定定时器1为8位自动重装方式

	TL1 = 0xfd;		//设定定时初值		 0xd0
	TH1 = 0xfd;		//设定定时器重装值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//启动定时器1
}
void Initiate()
{
init_serialcomm();
delay(100);
}
int main (void)
{ 
    delay(100);
    Initiate();

 buf[2] = 0x55;

selecting the current mode
selecting the current mode

buf[1] = 0x10;              //Disable Slew Rate while selecting the current mode
buf[0] = 0x05;
       
WriteToAD5422(3,buf);  //Write 551005 to SHIFT REGISTER  to write 1005 to control register
while(1) 
{ 

 buf[2] = 0x02;
 buf[1] = 0x00;
 buf[0] = 0x02;		   //读控制	 寄存器
 WriteToAD5422(3,buf);
 delay(10000);
ReadFromAD5422(3,buf);  //Read CONTROL REGISTER
 send_char_com(buf[2]);
 send_char_com(buf[1]);
 send_char_com(buf[0]);
 delay(20);

buf[2] = 0x01;
buf[1] = 0x55;
 buf[0] = 0xff;

 WriteToAD5422(3,buf);  //Write 019966H to SHIFT REGISTER  to write 9966H to DATA REGISTER
 delay(10000);

REGISTER
 
 buf[2] = 0x02;
 buf[1] = 0x00;
 buf[0] = 0x01;		//读数据寄存器

 WriteToAD5422(3,buf);
 delay(10000);
 ReadFromAD5422(3,buf);  //Read STATUS REGISTER
 
 send_char_com(buf[2]);
 send_char_com(buf[1]);
 send_char_com(buf[0]);

 buf[2] = 0x02;
 buf[1] = 0x00;
 buf[0] = 0x00;				  //读状态寄存器

 WriteToAD5422(3,buf);
 delay(10000);
 ReadFromAD5422(3,buf);  //Read data REGISTER
 send_char_com(buf[2]);
 send_char_com(buf[1]);
 send_char_com(buf[0]);
}

 }