TLV5618 双路12位DAC 模拟SPI驱动

  • Post author:
  • Post category:其他



一、TLV65618芯片

TLV5618A 带掉电功能 2.7V-5.5V,低功耗双路 12 位数模转换器

特点 ●双路

12 位电压输出 DAC

●可编程调节转换时间 – 快速模式 3μs – 低速模式 10μs ●兼容 TMS320 和 SPI 串行接口 16位串行接口包含4位控制和12位数据。


二、驱动代码

采用模拟SPI方式实现对TLV5618的控制。

头文件:

/*
 * FILE                : tlv5618.h
 * DESCRIPTION         : This file is for tlv5618.c
 * Author              : JackWang
 * --------------------
 */
#ifndef __tlv5618_h__
#define __tlv5618_h__

#define Channal_A  1
#define Channal_B  2
#define Channal_AB 3

//-------------------------- Typedef----------------------------//
typedef struct {
	int (* initialize)(void);
	int (* Write_A_B)(unsigned short,unsigned short,unsigned char);
}TLV5618_T;

//--------------------------- Extern ---------------------------//
extern TLV5618_T tlv5618;

#endif //__tlv5618_h__

实现文件:

/*
 * FILE                : tlv5618.c
 * DESCRIPTION         : This file is tlv5618 driver.
 * Author              : JackWang
 */
//--------------------------- Include ---------------------------//
#include "tlv5618.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"

//--------------------------- Define ---------------------------//
#define TLV5618_CS_H     GPIO_SetBits(GPIOH,GPIO_Pin_10)
#define TLV5618_CS_L     GPIO_ResetBits(GPIOH,GPIO_Pin_10)

#define TLV5618_DIN_H	 GPIO_SetBits(GPIOH,GPIO_Pin_8)
#define TLV5618_DIN_L    GPIO_ResetBits(GPIOH,GPIO_Pin_8)

#define TLV5618_CLK_H	 GPIO_SetBits(GPIOH,GPIO_Pin_11)
#define TLV5618_CLK_L    GPIO_ResetBits(GPIOH,GPIO_Pin_11)


//--------------------- Function Prototype ----------------------//
static int initialize(void);
static int Write_A_B(unsigned short,unsigned short,unsigned char);
static int EX_DAC_Conver(unsigned short);

//--------------------------- Variable --------------------------//
TLV5618_T tlv5618 = {
	.initialize = initialize,
	.Write_A_B=Write_A_B
};

/*!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!*/
static int 
initialize(void)
{
	GPIO_InitTypeDef   GPIO_uInitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH,ENABLE);
	GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_10 | GPIO_Pin_11;    
	GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_OUT;                        
	GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;                    
	GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;                         
	GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_50MHz;                  

	GPIO_Init(GPIOH,&GPIO_uInitStructure);
    
	TLV5618_CS_H ;
	TLV5618_CLK_L ;
	TLV5618_DIN_L ;
	
	return 0;
}

/*!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!*/
static int 
EX_DAC_Conver(unsigned short Dignum)
{
	unsigned short Dig = 0;

	TLV5618_CS_L;	
	TLV5618_CLK_H;
	
	for (int idx = 0; idx < 16; ++idx) {
		
        Dig=Dignum&0x8000;

		if (Dig) { TLV5618_DIN_H; }
		else     { TLV5618_DIN_L; }

		for (int delayTime = 0; delayTime < 100; ++delayTime );//for delay only

		TLV5618_CLK_L;

		Dignum<<=1;

		for (int delayTime = 0; delayTime < 100; ++delayTime );//for delay only

		TLV5618_CLK_H;
	}

	TLV5618_CLK_L;
	TLV5618_CS_H;	

	for (int delayTime = 0; delayTime < 1000; ++delayTime );//for delay only

	return 0;
}
/*!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!*/
static int 
Write_A_B(unsigned short Data_A, unsigned short Data_B, unsigned char Channal)
{
	unsigned short Temp=0x4000;
	switch(Channal)
	{
		case Channal_A:		EX_DAC_Conver(Temp|0x8000|(0x0fff&Data_A));break;
		case Channal_B:		EX_DAC_Conver(Temp|0x0000|(0x0fff&Data_B));break;
		case Channal_AB:	EX_DAC_Conver(Temp|0x1000|(0x0fff&Data_B));EX_DAC_Conver(Temp|0x8000|(0x0fff&Data_A));break;
		default:break;
	}
	
	return 0;
}



版权声明:本文为u010058695原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。