msp430单片机可用的DAC7311库函数源码
[复制链接]
16位DAC向下兼容,7311/8411/7811
3个IO口即可驱动
原来用在msp430g2553,宏定义快速移植
单片机源程序如下:
/*
* DAC7311.c
*
* Created on: 2018年
* Author: Thinkpad
*/
/**********DAC8411.c******************/
#include "global.h"
#include "f2802x_common/include/gpio.h"
#include "f2802x_common/include/pie.h"
#define SAMPLENUM 400 //采样点数,删除link cmd前只能200
unsigned int data[12]={32768,49151,61145,65535,61145,49151,32768,16384,4390,1,4390,16384};
const unsigned int Data[SAMPLENUM]={32768,33282,33797,34311,34825,35338,35851,36363,36874,37384,37893,38401,38907,39412,39915,40417,
40916,41414,41909,42402,42893,43381,43867,44350,44830,45307,45781,46252,46719,47183,47643,48100,
48553,49002,49447,49888,50325,50757,51185,51609,52027,52441,52851,53255,53654,54048,54437,54820,
55198,55570,55937,56298,56654,57003,57346,57684,58015,58340,58659,58971,59277,59576,59868,60154,
60434,60706,60971,61230,61481,61726,61963,62193,62416,62631,62840,63040,63233,63419,63597,63768,
63931,64086,64233,64373,64505,64629,64745,64854,64954,65047,65131,65208,65276,65337,65389,65433,
65470,65498,65518,65530,65535,65530,65518,65498,65470,65433,65389,65337,65276,65208,65131,65047,
64954,64854,64745,64629,64505,64373,64233,64086,63931,63768,63597,63419,63233,63040,62840,62631,
62416,62193,61963,61726,61481,61230,60971,60706,60434,60154,59868,59576,59277,58971,58659,58340,
58015,57684,57346,57003,56654,56298,55937,55570,55198,54820,54437,54048,53654,53255,52851,52441,
52027,51609,51185,50757,50325,49888,49447,49002,48553,48100,47643,47183,46719,46252,45781,45307,
44830,44350,43867,43381,42893,42402,41909,41414,40916,40417,39915,39412,38907,38401,37893,37384,
36874,36363,35851,35338,34825,34311,33797,33282,32768,32253,31738,31224,30710,30197,29684,29172,
28661,28151,27642,27134,26628,26123,25620,25118,24619,24121,23626,23133,22642,22154,21668,21185,
20705,20228,19754,19283,18816,18352,17892,17435,16982,16533,16088,15647,15210,14778,14350,13926,
13508,13094,12684,12280,11881,11487,11098,10715,10337,9965,9598,9237,8881,8532,8189,7851,
7520,7195,6876,6564,6258,5959,5667,5381,5101,4829,4564,4305,4054,3809,3572,3342,
3119,2904,2695,2495,2302,2116,1938,1767,1604,1449,1302,1162,1030,906,790,681,
581,488,404,327,259,198,146,102,65,37,17,5,1,5,17,37,
65,102,146,198,259,327,404,488,581,681,790,906,1030,1162,1302,1449,
1604,1767,1938,2116,2302,2495,2695,2904,3119,3342,3572,3809,4054,4305,4564,4829,
5101,5381,5667,5959,6258,6564,6876,7195,7520,7851,8189,8532,8881,9237,9598,9965,
10337,10715,11098,11487,11881,12280,12684,13094,13508,13926,14350,14778,15210,15647,16088,16533,
16982,17435,17892,18352,18816,19283,19754,20228,20705,21185,21668,22154,22642,23133,23626,24121,
24619,25118,25620,26123,26628,27134,27642,28151,28661,29172,29684,30197,30710,31224,31738,32253};
/************************************************************
* STANDARD BITS
************************************************************/
#define BITA (0x0400)
#define BITB (0x0800)
#define BITC (0x1000)
#define BITD (0x2000)
#define BITE (0x4000)
#define BITF (0x8000)
//--------IO宏定义----------
#define SYNC_HIGH GPIO_setHigh(myGpio, GPIO_Number_0)//P1OUT |=BIT0
#define SYNC_LOW GPIO_setLow(myGpio, GPIO_Number_0)//P1OUT &=~BIT0
#define SCLK_HIGH GPIO_setHigh(myGpio, GPIO_Number_1)//P2OUT |=BIT2
#define SCLK_LOW GPIO_setLow(myGpio, GPIO_Number_1)//P2OUT &=~BIT2
#define DIN_HIGH GPIO_setHigh(myGpio, GPIO_Number_2)//P1OUT |=BIT3
#define DIN_LOW GPIO_setLow(myGpio, GPIO_Number_2)//P1OUT &=~BIT3
int send_1=0;
int dacisr_in=0;
/******************************************************************************************************
* 名 称:DAC8411_Init()
* 功 能:初始化DAC8411
* 入口参数:无
* 出口参数:无
* 说 明:就是初始化相关IO的状态
* 范 例:无
******************************************************************************************************/
void DAC_Init()
{
//-----设置IO为输出-----
//P1DIR |=BIT0+BIT3;
//P2DIR |=BIT2;
GPIO_setDirection(myGpio, GPIO_Number_0, GPIO_Direction_Output);
GPIO_setDirection(myGpio, GPIO_Number_1, GPIO_Direction_Output);
GPIO_setDirection(myGpio, GPIO_Number_2, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_GeneralPurpose);
GPIO_setMode(myGpio, GPIO_Number_1, GPIO_0_Mode_GeneralPurpose);
GPIO_setMode(myGpio, GPIO_Number_2, GPIO_0_Mode_GeneralPurpose);
//-----设置IO初始状态为高-----
SCLK_HIGH;
SYNC_HIGH;
//先不打开DAC,防止影响1602的GPIO0-2
PIE_disableInt(myPie,PIE_GroupNumber_1,PIE_InterruptSource_TIMER_0);
}
/******************************************************************************************************
* 名 称:write2DAC8411(unsigned int Data)
* 功 能:对DAC8411写16位数据
* 入口参数:无
* 出口参数:无
* 说 明:共需发18位,头两位0,后16位是DAC量化数据
* 范 例:无
******************************************************************************************************/
void write2DAC(unsigned int Data)
{
unsigned int Temp;
unsigned char i;
Temp=Data;
SYNC_LOW; //拉低同步线,传输开始
//-----发送00,代表是非节能模式(节能就停止工作了)。-----
SCLK_HIGH;
DIN_LOW; //数据0
SCLK_LOW;
SCLK_HIGH;
DIN_LOW; //数据0
SCLK_LOW;
//-----依次发送14位----- 最后两位无效,数据位只截取前12位 相当于/8 位数越大循环次数多 越慢
for(i=0;i<14;i++)
{
SCLK_HIGH;
//-----通过位与,判断最高位是1还是0,已决定发什么数据-----
if(Temp & BITF)
{
DIN_HIGH;
send_1++;
}
else DIN_LOW;
SCLK_LOW;
Temp=Temp<<1; //左移一位,永远发最高位
}
SYNC_HIGH; //有效的数据序列是第16个拉高数据线,停止转换
}
/******************************************************************************************************
* 名 称: TA0_OnTime()
* 功 能:TA0的定时事件处理函数
* 入口参数:无
* 出口参数:无
* 说 明:无
* 范 例:无
******************************************************************************************************/
void DAC_Output()
{
static unsigned int i=0;
static unsigned int MUL=0;
write2DAC(Data[i]); //写入正弦表第i位数据
dacisr_in++;
// if(freq>600)
// i=i+freq/100; //每隔 freq/10 取一个点,相当于倍频
// else
i+=add;//需要耗时较多
if(i>SAMPLENUM-1)
i=0; //超出
}
|