注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
Linchpin的个人空间 https://home.eeworld.com.cn/space-uid-525682.html [收藏] [复制] [分享] [RSS]
日志

【TIVA C Launchpad 学习笔记四】外接DAC(TLV5618)驱动程序,硬件SPI和IO模拟两种方式

热度 1已有 5825 次阅读2014-8-17 10:16 |个人分类:TIVA C Launchpad 学习笔记一

方式一:GPIO模拟SPI通信
/*******************************************
开发坏境:CCSv5.4
开发板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:DAC_TLV5618 输出正弦波
程序说明:1.DIN——PC6 2.SCLK——PC5 3.CS——PC4 4.OUTA 5.AGND 6.REF 7.OUTB 8.VDD
编程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/fpu.h"
#include "driverlib/sysctl.h"
#include "grlib/grlib.h"
#include "driverlib/gpio.h"
#include "inc/hw_gpio.h"


#define uchar unsigned char
#define uint unsigned int
//void CS(uchar i);
//void SCLK(uchar i);
//void DIN(uchar i);


void init_DAC(void);
void DA_conver(uint temp);
void Write_A(float out_volt);
int VoltToData(float out_volt) ;
void delay_Nms(uint n);


float  sinx_128[]={0.299734,0.313719,0.327637,0.341454,0.355137,0.368653,0.381968,0.395052,0.407873,0.420399,0.432601,0.444449,0.455914,0.466969,0.477588,0.487745,0.497414,0.506574,0.515202,0.523277,0.530779,0.537692,0.543997,0.549680,0.554727,0.559126,0.562866,0.565939,0.568336,0.570053,0.571084,0.571429,0.571084,0.570053,0.568336,0.565939,0.562866,0.559126,0.554727,0.549680,0.543997,0.537692,0.530780,0.523277,0.515202,0.506575,0.497415,0.487745,0.477589,0.466970,0.455915,0.444449,0.432601,0.420400,0.407873,0.395053,0.381969,0.368653,0.355138,0.341455,0.327638,0.313720,0.299734,0.285715,0.271696,0.257710,0.243792,0.229975,0.216292,0.202777,0.189461,0.176377,0.163556,0.151030,0.138829,0.126981,0.115515,0.104460,0.093841,0.083684,0.074015,0.064855,0.056227,0.048152,0.040649,0.033737,0.027432,0.021749,0.016702,0.012303,0.008563,0.005490,0.003093,0.001376,0.000344,0.000005,0.000344,0.001376,0.003092,0.005490,0.008562,0.012302,0.016701,0.021748,0.027431,0.033736,0.040648,0.048151,0.056226,0.064853,0.074013,0.083683,0.093839,0.104458,0.115513,0.126979,0.138827,0.151028,0.163554,0.176375,0.189459,0.202774,0.216290,0.229973,0.243790,0.257708,0.271693,0.285713,};
uint num;


void main(void)
{


        //使能FPU
        FPUEnable();
        FPULazyStackingEnable();
        SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_10MHZ|SYSCTL_OSC_MAIN);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);//使能GPIO外设
        SysCtlGPIOAHBEnable(GPIO_PORTC_BASE);//挂在AHB高性能总线上
        GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
        init_DAC();
        while(1)
    {
                Write_A(sinx_128[num++]);
                if(num>=128) num=0;
    }
}
//片选信号端,低电平有效,PC4
/*void CS(uchar i)
{
         if(i) GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,GPIO_PIN_4);
         else  GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,~GPIO_PIN_4);
}
//同步串行时钟输入,PC5
void SCLK(uchar i)
{
          if(i)  GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5 );
          else   GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,~GPIO_PIN_5);
}
//串行数据输入端,串行时钟上升沿输入,PC6
void DIN(uchar i)
{
        if(i)  GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,GPIO_PIN_6 );
    else   GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,~GPIO_PIN_6);
}*/


/******初始化DAC***************/
void init_DAC(void)
{
        GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,GPIO_PIN_4);//CS(1);
        GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,~GPIO_PIN_5);//SCLK(0);
        GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,~GPIO_PIN_6);//DIN(0);
}


void DA_conver(uint temp)
{
        uchar i;
        GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,~GPIO_PIN_4);//CS(0);
        GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5 );//SCLK(1);
        for(i=0;i<16;i++)
    {


                if( (temp & 0x8000) ==0) GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,~GPIO_PIN_6);
                else GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,GPIO_PIN_6);
                GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,~GPIO_PIN_5 );
         //;
                temp<<=1;
                GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5 );


    }


        GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,GPIO_PIN_4);
}


void Write_A(float out_volt)
{
        //int Data_A;
        //Data_A=VoltToData(out_volt);
   //A通道
   DA_conver(0xc000|(0x0fff&VoltToData(out_volt)));
}
int VoltToData(float out_volt) //说明ref为参考源?out_volt为输出模拟电压值
{
   int temp2;
   temp2=(int)((out_volt*4096)/(2*3.3));
   return(temp2&0xfff);
}//将电压值转换为对应的12位数字量函数




方式二:硬件SPI通信(速度比GPIO模拟快很多)
/*******************************************
开发坏境:CCSv5.4
开发板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:TLV5618 SPI通信(TI模式)
程序说明:1.DIN——PF1 2.SCLK——PF2 3.CS——PF3
       DAC采样速率:95KHZ
编程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ssi.h"
#include "grlib/grlib.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/ssi.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h"


#define uchar unsigned char
#define uint unsigned int


void DAC_Write_A(float out_volt);


void main(void)
{


        SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |SYSCTL_XTAL_10MHZ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
        //SysCtlGPIOAHBEnable(GPIO_PORTF_BASE);//挂在AHB高性能总线上
    GPIOPinConfigure(GPIO_PF2_SSI1CLK);
        GPIOPinConfigure(GPIO_PF3_SSI1FSS);
        GPIOPinConfigure(GPIO_PF1_SSI1TX);
        GPIOPinTypeSSI(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_2);
        //SSI1_BASE/时钟频率/时钟协议,moto代表SPI协议/工作模式:主机模式/位速率4M/数据帧位数16
        //SSIConfigSetExpClk(SSI1_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0 , SSI_MODE_MASTER, 25000000, 16);
        SSIConfigSetExpClk(SSI1_BASE, SysCtlClockGet(), SSI_FRF_TI , SSI_MODE_MASTER, 25000000, 16);
        SSIEnable(SSI1_BASE);


    while(1)
        {
            DAC_Write_A(1);
            //SysCtlDelay(SysCtlClockGet()/3000);
            DAC_Write_A(2);
            //SysCtlDelay(SysCtlClockGet()/3000);
    }
}


void DAC_Write_A(float out_volt)
{
    //(uint)(out_volt*4095)/(2*3.281) 先将电压值转化为0~4095的整数
        //0x0fff&  转化成16位二进制数,后12位为数据位
        //0xc000|  前面加上4位为1100(命令位):快速模式,正常功耗,写数据到A通道
        SSIDataPut(SSI1_BASE, 0xc000|(0x0fff&((uint)((out_volt*4095)/(2*3.281)))));
    while(SSIBusy(SSI1_BASE)) ;//等待发送完毕
}




本文来自论坛,点击查看完整帖子内容。

发表评论 评论 (1 个评论)
回复 ChangC 2014-11-3 00:27
苦于对TivaC的硬件SPI配置琢磨不透,很好的学习资料,感谢分享。

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章