本帖最后由 dql2015 于 2023-3-24 16:30 编辑
avr64dd32支持DAC功能,分辨率达10bit,内置基准电压,在要求不高的场合生成模拟信号足够。
官方提供了测试例程,打开MPLAB X IDE v6.05编译烧录到开发板上,PD6管脚就能输出正弦波了。
6#include "mcc_generated_files/system/system.h"
#include <math.h>
#include <util/delay.h>
/* Number of samples for a sine wave period */
#define SINE_PERIOD_STEPS (100)
/* Sine wave amplitude */
#define SINE_AMPLITUDE (511)
/* Sine wave DC offset */
#define SINE_DC_OFFSET (512)
/* Frequency of the sine wave */
#define SINE_FREQ (50)
/* Step delay for the loop */
#define STEP_DELAY_TIME ((1000000 / SINE_FREQ) / SINE_PERIOD_STEPS)
/* Buffer to store the sine wave samples */
uint16_t sineWave[SINE_PERIOD_STEPS];
static void sineWaveInit(void)
{
uint8_t i;
for(i = 0; i < SINE_PERIOD_STEPS; i++)
{
sineWave[i] = SINE_DC_OFFSET + SINE_AMPLITUDE * sin(2 * M_PI * i / SINE_PERIOD_STEPS);
}
}
/*
Main application
*/
int main(void)
{
uint16_t sineIndex = 0;
/* Initializes MCU, drivers and middle-ware */
SYSTEM_Initialize();
sineWaveInit();
while(1)
{
/* Creating the sine wave signal by setting each sample */
DAC0_SetOutput(sineWave[sineIndex++]);
sineIndex = sineIndex % SINE_PERIOD_STEPS;
_delay_us(STEP_DELAY_TIME);
}
}
测试例程频率设置是50Hz,实测45Hz,Delay延时不太准,如果改用定时器触发输出,效果应该好很多。
|