请完成下面任务:写出基于f28335的DA输出的c语言的代码利用三个引脚分别输出sincos和方波,同时列出使用了那些库函数代码
时间: 2024-01-21 10:17:29 浏览: 195
以下是基于f28335的DA输出的C语言代码,利用三个引脚分别输出sincos和方波,并使用了相应的库函数:
```c
#include "F2837xD_device.h"
#include "F2837xD_Examples.h"
#include "math.h"
#include "DSP2833x_Xintf.h"
// 定义采样频率和波形频率
#define F_SAMPLE 1000000
#define F_SIN 200
#define F_SQUARE 1000
// 定义DAC最大值
#define DAC_MAX 4095
// 定义三角波和方波周期
#define T_TRI (float)1/F_SIN
#define T_SQUARE (float)1/F_SQUARE
// 定义计数器初值
Uint16 count_tri = 0;
Uint16 count_square = 0;
// 定义三角波和方波幅度
float amp_sin = (float)DAC_MAX/2;
float amp_square = (float)DAC_MAX/2;
// 计算每个采样点对应的DAC输出值
Uint16 calcDAC(float value)
{
return (Uint16)(value * DAC_MAX);
}
void main(void)
{
// 初始化系统时钟和GPIO引脚
InitSysCtrl();
InitGpio();
// 初始化SPI
InitSpiGpio();
InitSpi();
// 配置SPI从机选择端口
EALLOW;
GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO31 = 1;
GpioDataRegs.GPASET.bit.GPIO31 = 1;
EDIS;
// 配置DAC输出引脚为EPWM输出口
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;
GpioCtrlRegs.GPADIR.bit.GPIO1 = 1;
GpioCtrlRegs.GPADIR.bit.GPIO2 = 1;
EDIS;
// 设置EPWM时钟分频和计数周期
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;
EPwm1Regs.TBPRD = F_SAMPLE/F_SIN-1;
// 设置EPWM比较值和输出模式
EPwm1Regs.CMPA.bit.CMPA = EPwm1Regs.TBPRD/2;
EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;
EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;
// 启动EPWM
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.TBCTL.bit.PHSEN = TB_ENABLE;
EPwm1Regs.TBCTL.bit.PHSDIR = TB_UP;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;
// EPWM计数器清零
EPwm1Regs.TBCTR = 0;
// 主循环
while(1)
{
// 生成三角波
float tri = 2*amp_sin/T_TRI*fabs((float)count_tri/(float)EPwm1Regs.TBPRD-0.5)-amp_sin;
Uint16 dac_tri = calcDAC(tri);
// 生成正弦波
float sin = amp_sin*sin(2*M_PI*F_SIN*(float)count_tri/(float)F_SAMPLE);
Uint16 dac_sin = calcDAC(sin);
// 生成方波
float square = count_square < F_SAMPLE/F_SQUARE/2 ? amp_square : -amp_square;
Uint16 dac_square = calcDAC(square);
// 输出三角波、正弦波和方波
SpiaRegs.SPITXBUF = (dac_sin << 4) | (dac_tri >> 8);
SpiaRegs.SPITXBUF = dac_tri << 8;
SpiaRegs.SPITXBUF = dac_square << 4;
// 计数器自增
count_tri++;
if(count_tri >= EPwm1Regs.TBPRD)
{
count_tri = 0;
}
count_square++;
if(count_square >= F_SAMPLE/F_SQUARE)
{
count_square = 0;
}
}
}
```
本程序使用了以下库函数:
- "F2837xD_device.h":包含了各种外设寄存器和结构体定义;
- "F2837xD_Examples.h":包含了各种例程常量和结构体定义;
- "math.h":包含了数学函数库,如sin()函数;
- "DSP2833x_Xintf.h":包含了外部存储器接口函数库。
阅读全文