正弦波关键代码:(DAC用的是单通道)
TIM_InitStruct.TIM_Period = 0x19;
TIM_InitStruct.TIM_Prescaler = 0x0;
TIM_InitStruct.TIM_ClockDivision = 0x0;
TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2 , &TIM_InitStruct);
/*TIM_PrescalerConfig(TIM6, 0xF, TIM_PSCReloadMode_Update);
TIM_SetAutoreload(TIM6, 0xFF);
TIM6 TRGO selection */
//TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
TIM_SelectOutputTrigger(TIM2 , TIM_TRGOSource_Update);
/* DAC 构造 */
DAC_InitStruct.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1 , &DAC_InitStruct);
for(Idx=0;Idx<256;Idx++)
{
SigSine12bit[Idx] = (0xffff0000 << 16) + (Sine12bit[Idx]);
}/**/
/* DMA 构造 */
DMA_InitStruct.DMA_PeripheralBaseAddr = 0x40007408;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&SigSine12bit;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStruct.DMA_BufferSize = 256;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel3 , &DMA_InitStruct);//要用DMA2
DMA_Cmd(DMA2_Channel3 , ENABLE);//使能DMA2
DAC_Cmd(DAC_Channel_1 , ENABLE);//使能DAC通道1
DAC_DMACmd(DAC_Channel_1 , ENABLE);
TIM_Cmd(TIM2 , ENABLE);
三角波关键代码:
TIM_InitStruct.TIM_Period = 0x19;
TIM_InitStruct.TIM_Prescaler = 0x0;
TIM_InitStruct.TIM_ClockDivision = 0x0;
TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2 , &TIM_InitStruct);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* Set GPIO_DAC */
GPIOA_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIOA_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIOA_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(DAC_GPIOA , &GPIOA_InitStruct);
/* Set DAC */
//DAC_Cmd(DAC_Channel_1,ENABLE);
DAC_InitStruct.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;//三角波触发方式
DAC_Init(DAC_Channel_1 , &DAC_InitStruct);
DAC_Cmd(DAC_Channel_1, ENABLE);//使能DAC通道1
DAC_SetChannel1Data(DAC_Align_12b_R , 0x000);
TIM_Cmd(TIM2 , ENABLE);
定时器定时和中断先附上代码(没有相机,要找别人借,这得天天麻烦别人囧)
/* 结构体 */
TIM_TimeBaseInitTypeDef TIM_BaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//TIM_OCInitTypeDef TIM_OCInitStruct;
/* 构造基本定时单元TIM2 */
TIM_BaseInitStruct.TIM_Prescaler = 9999;
TIM_BaseInitStruct.TIM_Period = 7199;
TIM_BaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStruct.TIM_ClockDivision = 0x0;
TIM_TimeBaseInit(TIM2 , &TIM_BaseInitStruct);
/*TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStruct.TIM_Pulse = 0x0;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM2 , &TIM_OCInitStruct);*/
/* 构造嵌套向量中断控制 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/* 开启时钟中断 */
TIM_ITConfig(TIM2 , TIM_IT_Update , ENABLE);
/* 使能定时器2 */
TIM_Cmd(TIM2 , ENABLE);
自己手上的固件库使用手册不是最新版的,而固件库是最新版的,不搭!结果悲剧,NVIC让我搞半天,NVIC头文件是misc.h旧版的却是stm32f10x_nvic.h!
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图四被打赏50分 | |
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 |