码迷,mamicode.com
首页 > 其他好文 > 详细

【原创】TLV5618芯片驱动程序

时间:2015-04-21 22:32:51      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:

/**
  ******************************************************************************
  * @file    T_TLV5618.h
  * @author  LuJ
  * @version V3.5.0
  * @date    2015-03-31
  * @brief   DAC头文件管理
  ******************************************************************************
  * (C) Copyright Vango Technoglogy, Inc. 2015
  ******************************************************************************
  */
#ifndef __T_TLV5618_H
#define __T_TLV5618_H  
 
#include "stm32f10x.h"
 
#define TLV5618_Channal_A              ((uint8_t)0x01)    //通道A
#define TLV5618_Channal_B              ((uint8_t)0x02)    //通道B
#define TLV5618_Channal_AB             ((uint8_t)0x03)    //通道A&B
 
#define TLV5618_Slow_mode              ((uint8_t)0x01)    //慢速模式
#define TLV5618_Fast_mode              ((uint8_t)0x02)    //快速模式
 
 
void T_TLV5618_Init(void);//初始化
void T_TLV5618_Conver(uint16_t data_A,uint16_t data_B,uint8_t Channal_x,uint8_t SpeedMode);
void T_TLV5618_Test(void);
      
#endif
 
 
 
/**
  ******************************************************************************
  * @file    T_TLV5618.c
  * @author  LuJ
  * @version V3.5.0
  * @date    2015-03-31
  * @brief   TLV5618电源DAC 驱动
  ******************************************************************************
  * (C) Copyright Vango Technoglogy, Inc. 2015
  ******************************************************************************
  */
#include "stm32f10x_spi.h"
#include "T_TLV5618.h"
#include "V_MC74HC595.h"
#include "st_lib.h"
 
/**
  * 函数功能:TLV5618可调电源DAC初始化,主要初始化STM32的SPI1
  * 输入参数:无
  * 输出参数:无
* 全局变量:无
* 作者:LuJ
* 修改日期:2015/03/31
* 备注:无
  */
void T_TLV5618_Init(void)
{
SPI_InitTypeDef  SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
        
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
          
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//SCK
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
 
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//MISO
//GPIO_Init(GPIOA, &GPIO_InitStructure);
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//MOSI
GPIO_Init(GPIOA, &GPIO_InitStructure);
 
// Data on the DO and DIO pins are clocked out on the falling edge of CLK.
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
 
/* Enable SPI1  */
SPI_Cmd(SPI1, ENABLE);
}
 
/**
  * 函数功能:TLV5618发送数据,得到DAC转换值
  * 输入参数:data_A:DAC转换A通道值,0x0000-0x0fff
  *           data_B:DAC转换B通道值,0x0000-0x0fff
  *           Channal_x:选择通道,TLV5618_Channal_A/TLV5618_Channal_B/TLV5618_Channal_AB
  *           SpeedMode:速度模式,TLV5618_Slow_mode/TLV5618_Fast_mode
  * 输出参数:无
* 全局变量:修改mc74hc595_val
* 作者:LuJ
* 修改日期:2015/03/31
* 备注:TLV5618数据格式为R1.SPD.PWR.R0.D11-D0
  */
void T_TLV5618_Conver(uint16_t data_A,uint16_t data_B,uint8_t Channal_x,uint8_t SpeedMode)
{
uint16_t temp;
uint16_t i =0;
 
if(SpeedMode == TLV5618_Fast_mode)
{
temp=0x4000;
}
else
{
temp=0x0000;
}
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
 
T_TLV5618_SPI1_CS2(Bit_RESET);//TLV5618片选信号拉低,开始传入16位数据
/* Send byte through the SPI1 peripheral */
switch(Channal_x)
{
case TLV5618_Channal_A:
SPI_I2S_SendData(SPI1, (temp|0x8000|(0x0fff&data_A)));
  break;
case TLV5618_Channal_B:
SPI_I2S_SendData(SPI1, (temp|0x0000|(0x0fff&data_B)));
  break;
case TLV5618_Channal_AB:
SPI_I2S_SendData(SPI1, (temp|0x1000|(0x0fff&data_B)));
  T_TLV5618_SPI1_CS2(Bit_SET);//片选信号处理,先拉高再拉低
  T_TLV5618_SPI1_CS2(Bit_RESET);
SPI_I2S_SendData(SPI1, (temp|0x8000|(0x0fff&data_A)));
  break;
default:
break;
}
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
T_TLV5618_SPI1_CS2(Bit_SET);//片选信号处理,拉高
for(i = 500; i>0; i--);
}
 
/**
  * 函数功能:TLV5618可调电源DAC输出测试
  * 输入参数:无
  * 输出参数:无
* 全局变量:无
* 作者:LuJ
* 修改日期:2015/03/31
* 备注:在主函数中调用,然后使用万用表测试Target的电源电压为3.3V
  */
void T_TLV5618_Test(void)
{
V_74HC595_Init();//初始化移位寄存器,TLV5618的片选信号在此
T_TLV5618_Init();//初始化STM32的SPI1
T_Target_VDCIN(Bit_SET);//VDCIN=1
T_Target_V3V(Bit_RESET);//V3V=0
T_Target_MODEx(Bit_RESET,Bit_SET);//MODE0=0,MODE1=1
T_TLV5618_Conver(0x8bb,0x0000,TLV5618_Channal_A,TLV5618_Fast_mode);//A通道输出,快速模式,电源控制位正常
T_Target_vPower_ctr(Target_SET_vPower_ctr1);//打开电源控制开关
T_Target_RSTn();
while (1)
{
;
}
}
 

技术分享

【原创】TLV5618芯片驱动程序

标签:

原文地址:http://www.cnblogs.com/ljlujie/p/4445524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!