代码如下:
DMA部分:
#include "dma.h"
#include "delay.h"
const u16 TEST[20]={1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,1900,1800,1700,1600,1500,1400,1300,1200,1100};
void MYDMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1ê±Öóê1Äü
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_7; //í¨μàÑ¡Ôñ
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&DAC->DHR12R1;//DMAíaéèμØÖ·
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)TEST;//DMA ′æ′¢Æ÷0μØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//′æ′¢Æ÷μ½íaéèÄ£ê½
DMA_InitStructure.DMA_BufferSize = 20;//êy¾Y′«êäá¿
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//íaéè·ÇÔöá¿Ä£ê½
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//′æ′¢Æ÷Ôöá¿Ä£ê½
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//íaéèêy¾Y3¤¶è:8λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//′æ′¢Æ÷êy¾Y3¤¶è:8λ
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// ê1óÃÆÕí¨Ä£ê½
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//ÖDμèóÅÏ輶
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//′æ′¢Æ÷í»·¢μ¥′Î′«êä
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//íaéèí»·¢μ¥′Î′«êä
DMA_Init(DMA1_Stream5, &DMA_InitStructure);//3õê¼»ˉDMA Stream
DMA_Cmd(DMA1_Stream5, ENABLE);
DAC_DMACmd(DAC_Channel_1,ENABLE);
}
DAC部分:
#include "dac.h"
void Dac1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitType;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//ê1ÄüDACê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//Ä£Äaêäèë
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//ÏÂà-
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉ
DAC_InitType.DAC_Trigger=DAC_Trigger_T2_TRGO; //ê1óÃ′¥·¢1|Äü
DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//2»ê1óÃ2¨Dη¢éú
DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//Æá±Î¡¢·ùÖμéèÖÃ
DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; //DAC1êä3ö»o′æ1Ø±Õ BOFF1=1
DAC_Init(DAC_Channel_1,&DAC_InitType); //3õê¼»ˉDACí¨μà1
DAC_Cmd(DAC_Channel_1, ENABLE); //ê1ÄüDACí¨μà1
DAC_SetChannel1Data(DAC_Align_12b_R, 0); //12λóò¶ÔÆëêy¾Y¸ñê½éèÖÃDACÖ
}
定时器部分:
#include "timer.h"
#include "stm32f4xx.h"
void TIM2_CH1_Init(u32 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeDAC;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_TimeBaseInitTypeDAC.TIM_Period=arr;
TIM_TimeBaseInitTypeDAC.TIM_Prescaler=psc;
TIM_TimeBaseInitTypeDAC.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitTypeDAC.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitTypeDAC);
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);
TIM_Cmd(TIM2,ENABLE);
}
主函数部分:
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "lcd.h"
#include "timer.h"
#include "dac.h"
#include "dma.h"
int main(void)
{
u16 adcx=0;
float temp=0;
delay_init(168);
uart_init(115200);
LED_Init();
LCD_Init();
Dac1_Init();
TIM2_CH1_Init(5399,83);
MYDMA_Config();
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"DMA DAC TEST");
LCD_ShowString(30,150,200,16,16,"DAC VAL:");
LCD_ShowString(30,170,200,16,16,"DAC VOL:0.000V");
while(1)
{
adcx=DAC_GetDataOutputValue(DAC_Channel_1);//¶áè¡Ç°ÃæéèÖÃDACμÄÖμ
LCD_ShowxNum(94,150,adcx,4,16,0); //ÏÔê¾DAC¼Ä′æÆ÷Öμ
temp=(float)adcx*(3.3/4096); //μÃμ½DACμçÑ1Öμ
adcx=temp;
LCD_ShowxNum(94,170,temp,1,16,0); //ÏÔê¾μçÑ1ÖμÕûêy2¿·Ö
temp-=adcx;
temp*=1000;
LCD_ShowxNum(110,170,temp,3,16,0X80); //ÏÔê¾μçÑ1ÖμμÄD¡êy2¿·Ö
LED0=!LED0;
delay_ms(300);
}
}
这个 实验也不知道哪里出了问题,麻烦大家指导一下!!!!!
找到没办法了才来论坛求救的,定时器触发dac没问题,就是dma传输不了数据,也不知道少了哪一步,而且,你们给的例程并没有这个实例
一周热门 更多>