/*-----------------------ADI AD5422/AD5412 Driver Reference Design Source Code-----------------------------------
Author: 西安济通电气
Date: 2011-9-1
Rev: 1.0
Description: Realize AD5412/22 Driver,Use stc51 as MCU,Development Tool: KEIL C
---------------------------------------------------------------------------------------------------------*/
#include <reg51.h>
#define SET_CLEAR() P1 = (P1 | 0x40) //P1.6->CLEAR
#define CLR_CLEAR() P1 = (P1 & 0xbf)
#define SET_LATCH() P1 = (P1 | 0x10) //P1.4->LATCH
#define CLR_LATCH() P1 = (P1 & 0xef)
#define SET_SCL() P1 = (P1 | 0x04) //P1.2->SCLK
#define CLR_SCL() P1 = (P1 & 0xfb)
#define SET_SDO() P1 = (P1 | 0x08) //P1.3->SDIN
#define CLR_SDO() P1 = (P1 & 0xf7)
sbit DOUT=P1^5;
unsigned char buf[4] = {0,0,0,0};
void delay (int length)
{
while (length >0)
length--;
}
void send_char_com(unsigned char ch)
{
SBUF=ch;
while(!TI);
TI=0;
}
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; //波特率不倍速 // PCON &= 0x7f; //波特率不倍速
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;
// buf[1] = 0x1f; //Enable Slew Rate and the Slew Rate Time is 20s while selecting the current mode
// buf[0] = 0x15;
// buf[1] = 0x1d; //Enable Slew Rate and the Slew Rate Time is 12s while selecting the current mode
// buf[0] = 0x15;
//buf[1] = 0x1c; //Enable Slew Rate and the Slew Rate Time is 4.8s while selecting the current mode
// buf[0] = 0x35;
buf[1] = 0x10; //Disable Slew Rate while selecting the current mode
buf[0] = 0x06;
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);
// delay(10000000); //Dalay some time before reading the STATUS REGISTER and DATA 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]);
}
}
Author: 西安济通电气
Date: 2011-9-1
Rev: 1.0
Description: Realize AD5412/22 Driver,Use stc51 as MCU,Development Tool: KEIL C
---------------------------------------------------------------------------------------------------------*/
#include <reg51.h>
#define SET_CLEAR() P1 = (P1 | 0x40) //P1.6->CLEAR
#define CLR_CLEAR() P1 = (P1 & 0xbf)
#define SET_LATCH() P1 = (P1 | 0x10) //P1.4->LATCH
#define CLR_LATCH() P1 = (P1 & 0xef)
#define SET_SCL() P1 = (P1 | 0x04) //P1.2->SCLK
#define CLR_SCL() P1 = (P1 & 0xfb)
#define SET_SDO() P1 = (P1 | 0x08) //P1.3->SDIN
#define CLR_SDO() P1 = (P1 & 0xf7)
sbit DOUT=P1^5;
unsigned char buf[4] = {0,0,0,0};
void delay (int length)
{
while (length >0)
length--;
}
void send_char_com(unsigned char ch)
{
SBUF=ch;
while(!TI);
TI=0;
}
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; //波特率不倍速 // PCON &= 0x7f; //波特率不倍速
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;
// buf[1] = 0x1f; //Enable Slew Rate and the Slew Rate Time is 20s while selecting the current mode
// buf[0] = 0x15;
// buf[1] = 0x1d; //Enable Slew Rate and the Slew Rate Time is 12s while selecting the current mode
// buf[0] = 0x15;
//buf[1] = 0x1c; //Enable Slew Rate and the Slew Rate Time is 4.8s while selecting the current mode
// buf[0] = 0x35;
buf[1] = 0x10; //Disable Slew Rate while selecting the current mode
buf[0] = 0x06;
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);
// delay(10000000); //Dalay some time before reading the STATUS REGISTER and DATA 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]);
}
}