利用F28335定时DA输出仿真与设计,设计三个小任务,写出流程完整代码
时间: 2023-12-10 07:05:45 浏览: 142
382、基于单片机protues仿真的AD DA实验设计(仿真图、源代码)
任务一:生成一个正弦波
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define PI 3.14159265358979323846
Uint16 sine_table[256];
void Init_Sine_Table(void)
{
int i;
for(i = 0; i < 256; i++)
{
sine_table[i] = (Uint16)(2047*sin(2*PI*i/256) + 2047);
}
}
void main(void)
{
InitSysCtrl();
InitPieCtrl();
InitPieVectTable();
Init_Sine_Table();
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; //PWM1A
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1; //PWM1B
EDIS;
EPwm1Regs.TBPRD = 7500; //设置PWM周期为10kHz
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET;
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR;
EPwm1Regs.AQCTLB.bit.ZRO = AQ_SET;
EPwm1Regs.AQCTLB.bit.CBU = AQ_CLEAR;
EPwm1Regs.CMPA.bit.CMPA = sine_table[0];
EPwm1Regs.ETSEL.bit.SOCAEN = 1;
EPwm1Regs.ETSEL.bit.SOCASEL = ET_CTR_ZERO;
EPwm1Regs.ETPS.bit.SOCAPRD = ET_1ST;
EPwm1Regs.TBCTL.bit.HSPCLKDIV = 0;
EPwm1Regs.TBCTL.bit.CLKDIV = 0;
EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
EPwm1Regs.CMPA.bit.CMPA = sine_table[0];
EPwm1Regs.CMPB.bit.CMPB = sine_table[0];
EPwm1Regs.ETSEL.bit.SOCAEN = 1;
EPwm1Regs.ETSEL.bit.SOCASEL = ET_CTR_ZERO;
EPwm1Regs.ETPS.bit.SOCAPRD = ET_1ST;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.TBCTL.bit.HSPCLKDIV = 0;
EPwm1Regs.TBCTL.bit.CLKDIV = 0;
EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
EPwm1Regs.CMPA.bit.CMPA = sine_table[0];
EPwm1Regs.CMPB.bit.CMPB = sine_table[0];
EPwm1Regs.ETSEL.bit.SOCAEN = 1;
EPwm1Regs.ETSEL.bit.SOCASEL = ET_CTR_ZERO;
EPwm1Regs.ETPS.bit.SOCAPRD = ET_1ST;
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
while(1)
{
}
}
```
任务二:实现PWM波形调制
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
void main(void)
{
InitSysCtrl();
InitPieCtrl();
InitPieVectTable();
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1;
EDIS;
EPwm1Regs.TBPRD = 4500;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.CMPA.bit.CMPA = 2250; //占空比50%
EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; //高电平
EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR; //低电平
EPwm1Regs.AQCTLB.bit.CBU = AQ_SET;
EPwm1Regs.AQCTLB.bit.CBD = AQ_CLEAR;
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
while(1)
{
}
}
```
任务三:实现ADC采样和输出
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define ADC_SOC_NUMBER 0 //使用的ADC SOC号码
Uint16 AdcData[2]; //存放ADC采样结果的数组
void ADC_Init(void)
{
EALLOW;
AdcRegs.ADCCTL1.bit.ADCREFSEL = 0; //内部参考电压
AdcRegs.ADCCTL1.bit.ADCBGPWD = 1; //开启内部参考电压缓冲区
AdcRegs.ADCCTL1.bit.ADCREFPWD = 1; //开启内部参考电压缓冲区
AdcRegs.ADCCTL1.bit.ADCPWDN = 1; //开启ADC电源
AdcRegs.ADCCTL2.bit.CLKDIV2EN = 0; //不分频
AdcRegs.ADCCTL2.bit.ADCNONOVERLAP = 1; //非重叠采样
AdcRegs.ADCCTL2.bit.CHARGEPUMPEN = 1; //开启充电泵
AdcRegs.ADCCTL2.bit.INTPULSEPOS = 1; //中断脉冲在采样之后
AdcRegs.ADCSOC0CTL.bit.CHSEL = 0; //采样通道
AdcRegs.ADCSOC0CTL.bit.ACQPS = 6; //采样保持时间
AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 5; //触发源EPWM1SOCA
AdcRegs.ADCSOC0CTL.bit.HI_PRIORITY = 1; //高优先级
AdcRegs.INTSEL1N2.bit.INT1SEL = ADC_SOC_NUMBER; //中断1与ADC SOC关联
AdcRegs.INTSEL1N2.bit.INT1E = 1; //开启ADC中断1
AdcRegs.INTSEL1N2.bit.INT1CONT = 1; //循环模式
AdcRegs.INTSEL1N2.bit.INT1SEL2 = 0; //中断1触发源为EOC0
AdcRegs.INTSEL1N2.bit.INT1SEL1 = 0; //中断1触发源为EOC0
PieCtrlRegs.PIEIER1.bit.INTx6 = 1; //开启ADC中断1
IER |= M_INT1;
AdcRegs.ADCSOCFRC1.all = 0x0001; //启动ADC SOC0采样
EDIS;
}
interrupt void Adc_ISR(void)
{
AdcData[0] = AdcResult.ADCRESULT0; //存放采样结果
AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //清除ADC中断1标志位
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //清除PIE中断标志位
}
void main(void)
{
InitSysCtrl();
InitPieCtrl();
InitPieVectTable();
ADC_Init();
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1;
EDIS;
EPwm1Regs.TBPRD = 4500;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.CMPA.bit.CMPA = 2250; //占空比50%
EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; //高电平
EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR; //低电平
EPwm1Regs.AQCTLB.bit.CBU = AQ_SET;
EPwm1Regs.AQCTLB.bit.CBD = AQ_CLEAR;
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
while(1)
{
EPwm1Regs.CMPA.bit.CMPA = AdcData[0]; //将ADC采样结果输出到PWM的占空比上
}
}
```
阅读全文