ADC1的扫描模式,DMA传输问题,求大神指点
2019-07-14 18:43发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
ADC采集的八路数据并没有被DMA传到储存器,导致输出的八路数据都是0;其他定时器、LED,按键模块都没问题 请看代码:
#include "dma.h"
#include "STM32f10x.h"
void dma_init(u32 peradd,u32 memadd,u16 num){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
DMA_InitTypeDef dmainit;
DMA_DeInit(DMA1_Channel1);
dmainit.DMA_PeripheralBaseAddr = peradd; //外设地址
dmainit.DMA_MemoryBaseAddr = memadd; //储存器地址
dmainit.DMA_DIR = DMA_DIR_PeripheralSRC; //外设做数据来源
dmainit.DMA_BufferSize = num; //DMA通道缓存大小
dmainit.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址不递增
dmainit.DMA_MemoryInc = DMA_PeripheralInc_Enable;//储存器地址递增
dmainit.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设数据宽度16位
dmainit.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//储存器数据宽度16位
dmainit.DMA_Mode = DMA_Mode_Normal;//普通模式
dmainit.DMA_Priority = DMA_Priority_VeryHigh;//优先级最高
dmainit.DMA_M2M = DMA_M2M_Disable;//不使能内存到内存传输
DMA_Init(DMA1_Channel1,&dmainit);
}
void dma_run(u16 num){
DMA_Cmd(DMA1_Channel1,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel1,num);
DMA_Cmd(DMA1_Channel1,ENABLE);
}
#include "stm32f10x.h"
#include "adc.h"
void adc_init(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOA
|RCC_APB2Periph_ADC1,ENABLE);
GPIO_InitTypeDef gpio_init;
ADC_InitTypeDef adc_init;
gpio_init.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|
GPIO_Pin_6|GPIO_Pin_7;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA,&gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_0;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB,&gpio_init);
adc_init.ADC_Mode = ADC_Mode_Independent;//独立模式
adc_init.ADC_ScanConvMode = ENABLE;//多通道
adc_init.ADC_ContinuousConvMode = DISABLE;//单词转换
adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//软件触发
adc_init.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
adc_init.ADC_NbrOfChannel = 8;//8个通道
ADC_Init(ADC1,&adc_init);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,2,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3,3,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_4,4,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_5,5,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_6,6,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_7,7,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,8,ADC_SampleTime_239Cycles5);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
ADC_Cmd(ADC1,ENABLE);
ADC_DMACmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
#include "stm32f10x.h"
#include "delay.h"
#include "stdio.h"
#include "my_usart.h"
#include "led.h"
#include "key.h"
#include "timer.h"
#include "adc.h"
#include "dma.h"
u16 key_flag = 0;
uint8_t buf[4];
u16 data_buf[9];
void se_send(){
unsigned char i;
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
delay_ms(30);
for(i=0;i<4;i++){
USART_SendData(USART1,buf);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
void val_real(){
float temp;
unsigned char i;
unsigned int temp_int;
uint8_t ge,shi,bai;
for(i=0;i<8;i++){
temp = (float)(data_buf*3.3/4096);
temp_int = temp*100;
ge = temp_int%10;
shi = temp_int/10%10;
bai = temp_int/100;
buf[0] = bai+0x30;
buf[1] = '.';
buf[2] = shi+0x30;
buf[3] = ge+0x30;
se_send(); //串口发送
}
}
int main(){
my_usart_init(115200);
led_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();
init_key();
timer_init(4999,7199);//
adc_init();
dma_init((u32)&ADC1->DR,(u32)&data_buf[0],8);
while(1){
if(keyscan(0)==1)
key_flag = ~key_flag;
else
;
}
}
void TIM3_IRQHandler(void){
if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET&&key_flag){
dma_run(8);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(1){
if(DMA_GetFlagStatus(DMA1_FLAG_TC1)!=RESET){
DMA_ClearFlag(DMA1_FLAG_TC1);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
break;
}
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
}
val_real();//数据处理
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
一周热门 更多>