小学生
最后登录1970-1-1
在线时间 小时
注册时间2020-8-24
|
5火花
我用STM32的DAC输出一个如下图所示的正弦波,为何在最高点时会突然跌落,代码如下,请求大佬指导,谢谢啦
正弦输出函数
#include "bsp_dac.h"
#define POINT_NUM 256
const uint16_t Sine12bit[POINT_NUM] = {
2148,2198,2248,2298,2347,2395,2443,2490,2536,2580,2624,2667,2708,2747,2785,2822,2856,2880,2920,2949,2976,3001,3023,3044,3062,
3078,3091,3102,3111,3111,3111,3111,3111,3111,3111,3102,3091,3078,3062,3044,3023,3001,2976,2949,2920,2889,2856,2822,2785,2747,
2708,2667,2624,2580,2536,2490,2443,2395,2347,2298,2248,2198,2148,2098,2048,1997,1947,1898,1849,1800,1753,1706,1660,1615,1571,
1529,1488,1448,1410,1374,1339,1306,1275,1246,1219,1195,1172,1152,1134,1118,1104,1093,1085,1079,1075,1074,1075,1079,1085,1093,
1104,1118,1134,1152,1172,1195,1219,1246,1275,1306,1339,1374,1410,1448,1488,1529,1571,1615,1660,1706,1753,1800,1849,1898,1947,
1997,2048,2098,
2148,2249,2349,2448,2546,2643,2738,2832,2924,3013,3101,3186,3268,3347,3423,3496,3565,3631,3693,3751,3805,3854,3899,3940,3976,
4008,4035,4057,4074,4074,4074,4074,4074,4074,4074,4057,4035,4008,3976,3940,3899,3854,3805,3751,3693,3631,3565,3496,3423,3347,
3268,3186,3101,3013,2924,2832,2738,2643,2546,2448,2349,2249,2148,2048,1948,1847,1747,1648,1550,1453,1358,1264,1172,1083,995,
910,828,749,673,600,531,465,403,345,291,242,197,156,120,88,61,39,22,10,2,0,2,10,22,39,61,88,120,156,197,242,291,345,403,465,
531,600,673,749,828,910,995,1083,1172,1264,1358,1453,1550,1648,1747,1847,1948,2048
};
uint32_t DualSine12bit[POINT_NUM];
/**
* @brief ʹÄÜDACµÄʱÖÓ£¬³õʼ»¯GPIO
* @param ÎÞ
* @retval ÎÞ
*/
static void DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
/* ʹÄÜGPIOAʱÖÓ */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* ʹÄÜDACʱÖÓ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* DACµÄGPIOÅäÖã¬Ä£ÄâÊäÈë */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ÅäÖÃDAC ͨµÀ1 */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; //ʹÓÃTIM2×÷Ϊ´¥·¢Ô´
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //²»Ê¹Óò¨Ðη¢ÉúÆ÷
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //²»Ê¹ÓÃDACÊä³ö»º³å
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* ÅäÖÃDAC ͨµÀ2 */
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* ʹÄÜͨµÀ1 ÓÉPA4Êä³ö */
DAC_Cmd(DAC_Channel_1, ENABLE);
/* ʹÄÜͨµÀ2 ÓÉPA5Êä³ö */
DAC_Cmd(DAC_Channel_2, ENABLE);
/* ʹÄÜDACµÄDMAÇëÇó */
DAC_DMACmd(DAC_Channel_2, ENABLE);
}
/**
* @brief ÅäÖÃTIM
* @param ÎÞ
* @retval ÎÞ
*/
static void DAC_TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* ʹÄÜTIM2ʱÖÓ£¬TIM2CLK Ϊ72M */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* TIM2»ù±¾¶¨Ê±Æ÷ÅäÖà */
// TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = (280-1); //¶¨Ê±ÖÜÆÚ 20
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; //Ô¤·ÖƵ£¬²»·ÖƵ 72M / (0+1) = 72M
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //ʱÖÓ·ÖƵϵÊý
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* ÅäÖÃTIM2´¥·¢Ô´ */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* ʹÄÜTIM2 */
TIM_Cmd(TIM2, ENABLE);
}
/**
* @brief ÅäÖÃDMA
* @param ÎÞ
* @retval ÎÞ
*/
static void DAC_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* ʹÄÜDMA2ʱÖÓ */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
/* ÅäÖÃDMA2 */
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_ADDRESS; //ÍâÉèÊý¾ÝµØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DualSine12bit ; //ÄÚ´æÊý¾ÝµØÖ· DualSine12bit
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //Êý¾Ý´«Êä·½ÏòÄÚ´æÖÁÍâÉè
DMA_InitStructure.DMA_BufferSize = POINT_NUM; //»º´æ´óСΪPOINT_NUM×Ö½Ú
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //ÍâÉèÊý¾ÝµØÖ·¹Ì¶¨
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //ÄÚ´æÊý¾ÝµØÖ·×ÔÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //ÍâÉèÊý¾ÝÒÔ×ÖΪµ¥Î»
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //ÄÚ´æÊý¾ÝÒÔ×ÖΪµ¥Î»
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //Ñ»·Ä£Ê½
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //¸ßDMAͨµÀÓÅÏȼ¶
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //·ÇÄÚ´æÖÁÄÚ´æģʽ
DMA_Init(DMA2_Channel4, &DMA_InitStructure);
/* ʹÄÜDMA2-14ͨµÀ */
DMA_Cmd(DMA2_Channel4, ENABLE);
}
/**
* @brief DAC³õʼ»¯º¯Êý
* @param ÎÞ
* @retval ÎÞ
*/
void DAC_Mode_Init(void)
{
uint32_t Idx = 0;
DAC_Config();
DAC_TIM_Config();
/* Ìî³äÕýÏÒ²¨ÐÎÊý¾Ý£¬Ë«Í¨µÀÓÒ¶ÔÆë*/
for (Idx = 0; Idx < POINT_NUM; Idx++)
{
DualSine12bit[Idx] = (Sine12bit[Idx]<<16) +Sine12bit[Idx];
}
DAC_DMA_Config();
}
DAC输出波形如图所示
|
-
-
|