/****************************************Copyright (c)****************************************************
**
**
**
**--------------File Info---------------------------------------------------------------------------------
** File name: ad5410_driver.c
** Last modified Date: 2015-08-15
** Last Version: V1.0
** Descriptions:
**
**--------------------------------------------------------------------------------------------------------
** Created by: czm
** Created date: 2017-04-05
** Version: V1.00
** Descriptions:
**
**--------------------------------------------------------------------------------------------------------
***************************** ****************************************************************************/
#include <string.h>
#include "stdio.h"
#include "LPC17xx.h" /* LPC17xx外设寄存器 */
#include "lpc17xx_gpio.h"
#include "ad5410_driver.h"
#include "debug.h"
/*********************************************************************************************************
** Function name: WriteToAD5410
** Descriptions:
** input parameters: 无
** output parameters: 无
**********************************************************************************************************/
/*************************************************************************************
AD5410写数据
*************************************************************************************/
void WriteToAD5410(uint8_t *ADdata)
{
uint8_t j;
for(j = 0; j < 48; j++)//一路AD5410为24位,两路即为48个clk
{
GPIO_ClearValue(AD5410_SCLK_PORT, AD5410_SCLK_BIT);
if(ADdata[j >> 3] & (0x80 >> (j & 0x07)))
GPIO_SetValue(AD5410_MOSI_PORT, AD5410_MOSI_BIT);
else
GPIO_ClearValue(AD5410_MOSI_PORT, AD5410_MOSI_BIT);
delay_us(20);
GPIO_SetValue(AD5410_SCLK_PORT, AD5410_SCLK_BIT);;
delay_us(20);
}
GPIO_ClearValue(AD5410_SCLK_PORT, AD5410_SCLK_BIT);
delay_us(10);
GPIO_SetValue(AD5410_LATCH_PORT, AD5410_LATCH_BIT);
delay_us(10);
GPIO_ClearValue(AD5410_LATCH_PORT, AD5410_LATCH_BIT);
delay_us(20);
}
/*************************************************************************************
AD5410数据寄存器
输入:两路400~2000
**************************************************************************************/
void ADOut(uint32_t Iout1, uint32_t Iout2)
{
uint8_t AD5410data[6];
uint32_t AD5410_databuf[2] = {0};
AD5410_databuf[0] = Iout2;
AD5410_databuf[1] = Iout1;
if(AD5410_databuf[0] <= 400)
AD5410_databuf[0] = 400;
if(AD5410_databuf[0] >= 1998)
AD5410_databuf[0] = 1998;
AD5410_databuf[0] = AD5410_databuf[0] * 41-16384;
if(AD5410_databuf[1] <= 400)
AD5410_databuf[1] = 400;
if(AD5410_databuf[1] >= 1998)
AD5410_databuf[1] = 1998;
AD5410_databuf[1] = AD5410_databuf[1] * 41-16384;
AD5410data[0] = AD5410_DATAREG;
AD5410data[1] = (AD5410_databuf[0] >> 8 ) & 0xff;
AD5410data[2] = AD5410_databuf[0] & 0xff;
AD5410data[3] = AD5410_DATAREG;
AD5410data[4] = (AD5410_databuf[1] >> 8 ) & 0xff;
AD5410data[5] = AD5410_databuf[1] & 0xff;
WriteToAD5410(AD5410data);
}
/*************************************************************************************
AD5410初始化
*************************************************************************************/
void Init_AD5410(void)
{
uint8_t AD5410data[6];
uint8_t i;
GPIO_SetDir(AD5410_LATCH_PORT, AD5410_LATCH_BIT, 1);
GPIO_SetDir(AD5410_SCLK_PORT, AD5410_SCLK_BIT, 1);
GPIO_SetDir(AD5410_MOSI_PORT, AD5410_MOSI_BIT, 1);
GPIO_SetDir(AD5410_ERR_PORT, AD5410_ERR_BIT, 0);
GPIO_ClearValue(AD5410_LATCH_PORT, AD5410_LATCH_BIT);
GPIO_ClearValue(AD5410_SCLK_PORT, AD5410_SCLK_BIT);
/*******首先访问AD5410复位寄存器,使5片AD5410复位***********/
/* AD5410data[0] = AD5410_RSTREG;
AD5410data[1] = 0x00;
AD5410data[2] = 0x01;
AD5410data[3] = AD5410_RSTREG;
AD5410data[4] = 0x00;
AD5410data[5] = 0x01;
WriteToAD5410(AD5410data);
delay_us(200); */
for(i =0; i < 3; i++)
{
/********访问控制寄存器**************/
AD5410data[0] = AD5410_CTRREG;
AD5410data[1] = 0x30; //0b 0011_0000
AD5410data[2] = 0x0D; //0b 0000_1101
AD5410data[3] = AD5410_CTRREG;
AD5410data[4] = 0x30; //0b 0011_0000
AD5410data[5] = 0x0D; //0b 0000_0101
WriteToAD5410(AD5410data);
/********访问控制寄存器**************/
AD5410data[0] = NOP;
AD5410data[1] = 0; //0b 0011_0000
AD5410data[2] = 0; //0b 0000_1101
AD5410data[3] = NOP;
AD5410data[4] = 0; //0b 0011_0000
AD5410data[5] = 0; //0b 0000_0101
WriteToAD5410(AD5410data);
}
// printf("AD5410 initilise success\r\n");
}