这几天学了ADC和DMA,想结合使用一下。但是今天写代码出现了问题。ADC没法把值通过DMA给一个数组。但是可以直接读取ADC的值,说明ADC程序是可以的。贴出代码吧。
#include "DMA.h"
#include "stm32f10x.h"
u16 size;
void MYDMA_Init1(uint32_t MADDR,uint32_t ADC_ADDR,u16 BSIZE)
{
DMA_InitTypeDef DMA_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
size=BSIZE;
DMA_InitStruct.DMA_BufferSize=BSIZE;
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralSRC; //íaéèμ½Äú′æ
DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;
DMA_InitStruct.DMA_MemoryBaseAddr=MADDR;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_Mode=DMA_Mode_Circular;
DMA_InitStruct.DMA_PeripheralBaseAddr=ADC_ADDR;
DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_Priority=DMA_Priority_High;
DMA_Init(DMA1_Channel1,&DMA_InitStruct);
}
void MYDMA_Init2(uint32_t MADDR,uint32_t USART_ADDR,u16 BSIZE)
{
DMA_InitTypeDef DMA_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
size=BSIZE;
DMA_InitStruct.DMA_BufferSize=BSIZE;
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralDST; //Äú′æμ½íaéè
DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;
DMA_InitStruct.DMA_MemoryBaseAddr=MADDR;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Disable;
DMA_InitStruct.DMA_Mode=DMA_Mode_Normal;
DMA_InitStruct.DMA_PeripheralBaseAddr=USART_ADDR;
DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_Priority=DMA_Priority_Medium;
DMA_Init(DMA1_Channel4,&DMA_InitStruct);
}
void A2M_DMA_Enable()
{
DMA_Cmd(DMA1_Channel1,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel1,size);
DMA_Cmd(DMA1_Channel1,ENABLE);
ADC_DMACmd(ADC1,ENABLE);
/*while(DMA_GetFlagStatus(DMA1_FLAG_TC1)==RESET);
DMA_ClearFlag(DMA1_FLAG_TC1);*/
}
void M2U_DMA_Enable()
{
DMA_Cmd(DMA1_Channel4,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel4,size);
DMA_Cmd(DMA1_Channel4,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
while(DMA_GetFlagStatus(DMA1_FLAG_TC4)==RESET);
DMA_ClearFlag(DMA1_FLAG_TC1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
怎么发文件?
是这样的,ADC的值通过DMA发送到RAM,然后吧RAM的值通过DMA发送到串口
[mw_shl_code=c,true]#include "DMA.h"
#include "stm32f10x.h"
u16 size;
void MYDMA_Init1(uint32_t MADDR,uint32_t ADC_ADDR,u16 BSIZE)
{
DMA_InitTypeDef DMA_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
size=BSIZE;
DMA_InitStruct.DMA_BufferSize=BSIZE;
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralSRC; //íaéèμ½Äú′æ
DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;
DMA_InitStruct.DMA_MemoryBaseAddr=MADDR;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_Mode=DMA_Mode_Circular;
DMA_InitStruct.DMA_PeripheralBaseAddr=ADC_ADDR;
DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_Priority=DMA_Priority_High;
DMA_Init(DMA1_Channel1,&DMA_InitStruct);
}
void MYDMA_Init2(uint32_t MADDR,uint32_t USART_ADDR,u16 BSIZE)
{
DMA_InitTypeDef DMA_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
size=BSIZE;
DMA_InitStruct.DMA_BufferSize=BSIZE;
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralDST; //Äú′æμ½íaéè
DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;
DMA_InitStruct.DMA_MemoryBaseAddr=MADDR;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_Mode=DMA_Mode_Circular;
DMA_InitStruct.DMA_PeripheralBaseAddr=USART_ADDR;
DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_Priority=DMA_Priority_Medium;
DMA_Init(DMA1_Channel4,&DMA_InitStruct);
}
void A2M_DMA_Enable()
{
DMA_Cmd(DMA1_Channel1,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel1,size);
DMA_Cmd(DMA1_Channel1,ENABLE);
while(DMA_GetFlagStatus(DMA1_FLAG_TC1)==RESET);
DMA_ClearFlag(DMA1_FLAG_TC1);
}
void M2U_DMA_Enable()
{
DMA_Cmd(DMA1_Channel4,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel4,size);
DMA_Cmd(DMA1_Channel4,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
while(DMA_GetFlagStatus(DMA1_FLAG_TC4)==RESET);
DMA_ClearFlag(DMA1_FLAG_TC4);
}
#include "ADC.h"
#include "stm32f10x.h"
void MYADC_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
ADC_InitTypeDef ADC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1;
//GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;
ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;
ADC_InitStruct.ADC_NbrOfChannel=1;
ADC_InitStruct.ADC_ScanConvMode=DISABLE;
ADC_Init(ADC1,&ADC_InitStruct);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_71Cycles5);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 MYADC_ENABLE()
{
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_71Cycles5);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
[/mw_shl_code]
一周热门 更多>