STM32F4-Discovery ADC-DMA Problemi

Başlatan Bertan, 04 Aralık 2015, 13:38:28

Bertan

Merhabalar

    ARM ve Keil konularında yeni sayılırım.DMA kullanmayı öğrenmek adına basit bir kod yazmaya çalışıyorum.ADC den okuduğum verileri "hedef" değişkenine atayıp , bu değişkenide USART ile bilgisayarıma göndermek istiyorum.Ama nerede hata yapıyorsam programı bir türlü düzgün çalıştıramadım.

Kodlar bunlar :

int main()
{	
	GPIO_InitTypeDef GPIO_InitStructure ;
	USART_InitTypeDef USART_InitStructure ;
	ADC_InitTypeDef ADC_InitStructure ;
	ADC_CommonInitTypeDef ADC_CommonInitStructure ;
	DMA_InitTypeDef DMA_InitStructure ;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE) ;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE) ;

//GPIO//
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ; 
	GPIO_Init(GPIOA, &GPIO_InitStructure) ;
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
//ADC//	
	
        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
	ADC_CommonInit(&ADC_CommonInitStructure) ;
	
	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
        ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_NbrOfConversion = 1;
        ADC_InitStructure.ADC_ExternalTrigConv = 0x00;
	ADC_Init(ADC1, &ADC_InitStructure) ;

//USART//

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2) ;
	
	USART_InitStructure.USART_BaudRate = 115200 ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
	USART_InitStructure.USART_Mode = USART_Mode_Tx ;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_StopBits = USART_StopBits_1 ;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b ;
	USART_Init(USART2, &USART_InitStructure) ;
	USART_Cmd(USART2, ENABLE) ;

//DMA//

	DMA_InitStructure.DMA_Channel = DMA_Channel_0 ;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord ;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord ;
	DMA_InitStructure.DMA_MemoryInc= DMA_MemoryInc_Disable ;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable ;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = 1 ;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x4001204C;
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)hedef ;
	DMA_Init(DMA2_Stream0, &DMA_InitStructure) ;

//PROGRAM//

	ADC_Cmd(ADC1, ENABLE) ;
	ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_56Cycles) ;
	ADC_SoftwareStartConv(ADC1) ;
	ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
	ADC_DMACmd(ADC1, ENABLE);	
	DMA_Cmd(DMA2_Stream0, ENABLE);
	
	while(1)
	{
		sprintf(str, "Hedef:%d  , ADC: %d\n",hedef,ADC1->DR) ;
		USART_put(USART2, str) ;
		Delay(10000000) ;
	}

}


Usart dan gelen verilerin bir kısmıda bunlar :

Hedef:0  , ADC: 3865
Hedef:0  , ADC: 3862
Hedef:0  , ADC: 3864
Hedef:0  , ADC: 3865
Hedef:0  , ADC: 3861
Hedef:0  , ADC: 3866
Hedef:0  , ADC: 3864
Hedef:0  , ADC: 3863
Hedef:0  , ADC: 3866
Hedef:0  , ADC: 3863
Hedef:0  , ADC: 3863
Hedef:0  , ADC: 3865
Hedef:0  , ADC: 3864
Hedef:0  , ADC: 3862
Hedef:0  , ADC: 3864
Hedef:0  , ADC: 3863
Hedef:0  , ADC: 3866
Hedef:0  , ADC: 3864
Hedef:0  , ADC: 3862
Hedef:0  , ADC: 3865


Yardımcı olursanız sevinirim.Teşekkürler.

acesim

#1
   ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
   ADC_InitStructure.ADC_NbrOfConversion = 1;

numberof conversion 1 yap . bir de while döngüsü içine ADC_SoftwareStartConv(ADC1) ; sürekli çağır peşine delay koy .

dma kesmesinde de USARTA gönder.
sprintf(str, "Hedef:%d  , ADC: %d\n",hedef,ADC1->DR) ;
USART_put(USART2, str) ;

aşağıda tek çevirim adcInit(); adc'den çevirim almak için sürekli start etmen gerek , her start sonunda dma datayı taşıyınca kesme üretsin , dma handler içinde isee usarta datayı gönder.

ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = 0;
ADC_InitStructure.ADC_ExternalTrigConv = 0;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);


ADC_Cmd(ADC1, ENABLE);

while(1)
{
ADC_SoftwareStartConv(ADC1) ;
delay();
}

Bertan

İlginiz için teşekkürler.

Dediklerinizi yaptım ancak bir sonuca ulaşamadım.Ben de kendi kendime uğraşarak kodu çalıştırmayı başardım :)

Çalışan kodlar :

#include "stm32f4xx.h"                  // Device header
#include <stdio.h>
#include <stdlib.h>

char str[50] ;
uint16_t hedef2[1] ;
uint16_t i ;


void USART_put(USART_TypeDef* USARTx, volatile char *s)
{
	while(*s)
	{
		while(!(USARTx ->SR & 0x00000040) ) ;
		USART_SendData(USARTx, *s) ;
		*s++ ;
	}
}

void Delay(uint32_t zaman)
{
	while(zaman)
	zaman--;
}


int main()
{	
	GPIO_InitTypeDef GPIO_InitStructure ;
	USART_InitTypeDef USART_InitStructure ;
	ADC_InitTypeDef ADC_InitStructure ;
	ADC_CommonInitTypeDef ADC_CommonInitStructure ;
	DMA_InitTypeDef DMA_InitStructure ;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE) ;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE) ;

//GPIO//
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ; 
	GPIO_Init(GPIOA, &GPIO_InitStructure) ;
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
//ADC//	
	
        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
	ADC_CommonInit(&ADC_CommonInitStructure) ;
	
	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
        ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_NbrOfConversion = 1;
        ADC_InitStructure.ADC_ExternalTrigConv = 0x00;
	ADC_Init(ADC1, &ADC_InitStructure) ;
	
	

//USART//

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2) ;
	
	USART_InitStructure.USART_BaudRate = 115200 ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
	USART_InitStructure.USART_Mode = USART_Mode_Tx ;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_StopBits = USART_StopBits_1 ;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b ;
	USART_Init(USART2, &USART_InitStructure) ;
	USART_Cmd(USART2, ENABLE) ;

//DMA//

	DMA_InitStructure.DMA_Channel = DMA_Channel_0 ;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x4001204C;
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) hedef2 ;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord ;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord ;
	DMA_InitStructure.DMA_MemoryInc= DMA_MemoryInc_Disable ;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable ;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;
	DMA_InitStructure.DMA_BufferSize = 1 ;
	DMA_Init(DMA2_Stream0, &DMA_InitStructure) ;
	
	//DMA_ITConfig(DMA2_Stream0, DMA_IT_TCIF0, ENABLE) ;
	//DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE) ;
	
	

//PROGRAM//

  
	ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_56Cycles) ;
	
	ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);	
	DMA_Cmd(DMA2_Stream0, ENABLE);
	ADC_DMACmd(ADC1, ENABLE);
	ADC_Cmd(ADC1, ENABLE) ;
        ADC_SoftwareStartConv(ADC1) ;	
	
	
	
	while(1)
	{
		sprintf(str, "Hedef:%d  , ADC: %d\n",hedef2[0],ADC1->DR) ;
		USART_put(USART2, str) ;	
		Delay(10000000) ;
	}

}



Usartdan gelen veriler :

Hedef:2  , ADC: 2
Hedef:518  , ADC: 518
Hedef:2138  , ADC: 2138
Hedef:172  , ADC: 172
Hedef:1291  , ADC: 1291
Hedef:2198  , ADC: 2198
Hedef:663  , ADC: 663
Hedef:784  , ADC: 784
Hedef:1927  , ADC: 1927
Hedef:1928  , ADC: 1928
Hedef:1833  , ADC: 1833
Hedef:1909  , ADC: 1909
Hedef:221  , ADC: 221
Hedef:1924  , ADC: 1924
Hedef:1919  , ADC: 1919
Hedef:1883  , ADC: 1883