stm32f407多波形发生器默认输出正弦?
stm32新人,在做按键控制简单多波形切换,然后发现无论怎样初始一直默认正弦,如果切换三角波需按两下。
程序代码如下
#include "dac.h"
//#include "key.h"
//#include "SysTick.h"
//#define DAC_DHR12R1_ADDRESS 0x40007408
#define DAC_DHR12RD_Address 0x40007420 //0x40007420
uint32_t DualSine12bit[32];
// 波形数据 -正弦波-------//
const uint16_t Sine12bit[32] =
{
2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056,
4095, 4056, 3939, 3750, 3495, 3185, 2831, 2447,
2047, 1647, 1263, 909, 599, 344, 155, 38,
0, 38, 155, 344, 599, 909, 1263, 1647
};
// 波形数据 -三角波-------//
const uint16_t Triangle12bit[32] =
{
0,250,500,750,1000,1250,1500,1750,
2000,2250,2500,2750,3000,3250,3500,3750,
4000,3750,3500,3250,3000,2750,2500,2250,
2000,1750,1500,1250,1000,750,500,250
};
// 波形数据 -锯齿波-------//
const uint16_t Sawtooth12bit[32] =
{
0,100,200,300,400,500,600,700,
800,900,1000,1100,1200,1300,1400,1500,
1600, 1700,1800,1900,2000,2100,2200,
2300,2400,2500,2600,2700,2800,2900,3000,3100
};
// 波形数据 -PWM波-------//
const uint16_t Pwm12bit[32] =
{
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
4095, 4095,4095,4095,4095,4095,4095,4095,
4095, 4095,4095,4095,4095,4095,4095,4095
};
// 波形数据 -梯形波-------//
const uint16_t Trapezoidal12bit[32] =
{
0,0,0,0,0,0,300,600,
900,1200,1500,1800,2100,2400,2700,3000,
3000,3000,3000,3000,3000,3000,3000,2700,
2400,2100,1800,1500,1200,900,600,300,
};
/*******************************************************************************
* 函 数 名 : DAC1_Init
* 函数功能 : DAC1初始化函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void TIM6_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
TIM_PrescalerConfig(TIM6, 83, TIM_PSCReloadMode_Update);
TIM_SetAutoreload(TIM6, 4);
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
TIM_Cmd(TIM6, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void DAC_DMA_Configuration(void)
{
DAC_InitTypeDef DAC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);//使能DMA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//使能DAC时钟
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;//选择TIM6触发
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;//是否使用DAC波形发生
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;//不使用输出缓冲
DAC_Init(DAC_Channel_1, &DAC_InitStructure);//初始化DAC
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_7; //DMA通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_Address;//外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DualSine12bit;//存储器0地址
DMA_InitStructure.DMA_BufferSize = 64;//数据数目
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设数据宽度
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;//存储器数据宽度
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//传输方向
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器递增
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//模式选择
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //FIFO模式
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;//FIFO阈值
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//储存器突发传输
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发传输
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
DMA_Cmd(DMA1_Stream5, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
void DAC_Mode_Init(void)
{
uint32_t idc = 0;
for (idc = 0; idc < 32; idc++)
{
DualSine12bit[idc] = (Sine12bit[idc] << 16) + (Sine12bit[idc]);//初始化数据
}
}
void Chg_Out_Sine(uint32_t Mod)
{
uint32_t Idx = 0;
switch(Mod)
{
case (1):
{
for (Idx = 0; Idx < 32; Idx++)
{
DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);//正弦波输出
}
};break;
case (2):
{
for (Idx = 0; Idx < 32; Idx++)
{
DualSine12bit[Idx] = (Triangle12bit[Idx] << 16) + (Triangle12bit[Idx]);//三角波输出
}
};break;
case (3):
{
for (Idx = 0; Idx < 32; Idx++)
{
DualSine12bit[Idx] = (Sawtooth12bit[Idx] << 16) + (Sawtooth12bit[Idx]);//锯齿波输出
}
};break;
case (4):
{
for (Idx = 0; Idx < 32; Idx++)
{
DualSine12bit[Idx] = (Pwm12bit[Idx] << 16) + (Pwm12bit[Idx]);//pwm波输出
}
};break;
case (5):
{
for (Idx = 0; Idx < 32; Idx++)
{
DualSine12bit[Idx] = (Trapezoidal12bit[Idx] << 16) + (Trapezoidal12bit[Idx]);//梯形波输出
}
};break;
default:break;
}
}
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "key.h"
#include "dac.h"
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
int main(void)
{ u8 i=0;
u8 j=0;
u8 key=0;
DAC_Mode_Init();
SysTick_Init(168);
GPIO_Configuration();
TIM6_Configuration();
DAC_DMA_Configuration();
KEY_Init();
LED_Init();
while(1)
{
key=KEY_Scan(0);
if(key==KEY_UP)
{
switch(j)
{
case 1: Chg_Out_Sine(1);break;
case 2: Chg_Out_Sine(2);break;
case 3: Chg_Out_Sine(3);break;
case 4: Chg_Out_Sine(4);break;
case 5: Chg_Out_Sine(5);break;
//defau6lt:break;
}
j++;
}
i++;
if(i%20==0)
{
led1=!led1;
}
delay_ms(10);
}
}