cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I have a question, should I move adc information to pwm outputs with dma or do I have other methods?I would really appreciate if you can help.

bnymntrz
Associate III

5 REPLIES 5

Why do you want to do that? What do you want to accomplish?

JW

potentiometer to get instant adc information and send it to the pegs (pwm) outputs, in the program is a random value in the DR register of adc2 unit and it throws it I read the potentiometer continuously I want to send the value to pwm I would like to help you ..

bnymntrz
Associate III

@ [Clive Two.Zero] (Topluluk Üyesi) /Clive Turvey

sir, I have corrected the sections you say now in the adc conversion to the DR register is assigned to the value of the pwm slerell is not reading the potentiometer is very good if you are interested?

?? can you perhaps just paste your replies into the response window, and not use the code formatting block [</>] icon

You don't mention a part. Assuming you have the ADC in 12-bit mode you'd need the TIM to have a Period of 4096-1 and you could copy the ADC conversion (right aligned) into the TIM->CCRx register for the channel you are using. The TIM Update could trigger the ADC conversion. For DMA the ADC and TIM would likely need to be on the same APB, with the DMA triggered by the ADC EOC signal, and doing a copy from the DR to CCRx. On the F2/F4 parts likely needing the APB2 based devices to support peripheral-to-peripheral transfer. The interrupt/IRQ perhaps simpler to start with.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
#include "stm32f4xx.h"                  // Device header
#include "stm32f4xx_gpio.h"             // Keil::Device:StdPeriph Drivers:GPIO
#include "stm32f4xx_rcc.h"              // Keil::Device:StdPeriph Drivers:RCC                 
#include "stm32f4xx_tim.h"              // Keil::Device:StdPeriph Drivers:TIM
#include "RTE_Device.h"                 // Keil::Device:Startup
#include "RTE_Components.h"             // Component selection
#include "misc.h "
#include "stm32f4xx_adc.h"              // Keil::Device:StdPeriph Drivers:ADC
 
     
       
 
 
uint16_t adc_okuma(void){
 
ADC_RegularChannelConfig(ADC2,ADC_Channel_9,1,ADC_SampleTime_56Cycles);
ADC_SoftwareStartConv(ADC2);
	
	while(ADC_GetFlagStatus(ADC2,ADC_FLAG_EOC)== RESET);
	
	return ADC_GetConversionValue(ADC2);
}
 
void delay(uint32_t time){
	
 	while(time)
	time--;
}
 
	int main(){
		
		
	GPIO_InitTypeDef APORT;
  GPIO_InitTypeDef CPORT;
	GPIO_InitTypeDef  ADCIO ;
  ADC_InitTypeDef ADC_CONFIG ;
	ADC_CommonInitTypeDef ADCORTAK;
		  TIM_OCInitTypeDef PWM ={0};
	TIM_TimeBaseInitTypeDef TIMER ={0};
	
		
		
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
	
		
	CPORT.GPIO_Mode = GPIO_Mode_AF;
	CPORT.GPIO_OType = GPIO_OType_PP;
	CPORT.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
	CPORT.GPIO_PuPd = GPIO_PuPd_NOPULL;
	CPORT.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOC,&CPORT);
		
	APORT.GPIO_Mode = GPIO_Mode_AF;
	APORT.GPIO_OType = GPIO_OType_PP;
	APORT.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9;
	APORT.GPIO_PuPd = GPIO_PuPd_NOPULL;
	APORT.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOA,&APORT);
	
	
		GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
		GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);
		
		GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8);
		GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM8);
 
 
  
 
 
  ADCIO.GPIO_Mode = GPIO_Mode_AN;
	ADCIO.GPIO_OType = GPIO_OType_PP;
	ADCIO.GPIO_Pin = GPIO_Pin_1;
	ADCIO.GPIO_PuPd = GPIO_PuPd_NOPULL;
	ADCIO.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOB,&ADCIO);
 
	
  ADCORTAK.ADC_Mode = ADC_Mode_Independent;
	ADCORTAK.ADC_Prescaler = ADC_Prescaler_Div4;
  ADC_CommonInit(&ADCORTAK);
 
  ADC_CONFIG.ADC_Resolution = ADC_Resolution_8b;
	ADC_Init(ADC2,  &ADC_CONFIG);
	ADC_Cmd(ADC2,ENABLE);
 
 
	
 
 
 
	
 
	
	
 
  
	uint8_t OKUNAN_DEGER;
	
	
   OKUNAN_DEGER = adc_okuma();
	
	        
 
	TIMER.TIM_Prescaler = 4199;
	TIMER.TIM_Period = 1023;
	TIMER.TIM_CounterMode = TIM_CounterMode_Up;
	TIMER.TIM_ClockDivision = 0;
	TIM_TimeBaseInit(TIM1,&TIMER);
  TIM_Cmd(TIM1,ENABLE);
	
	TIMER.TIM_Prescaler = 4199;
	TIMER.TIM_Period = 1023;
	TIMER.TIM_CounterMode = TIM_CounterMode_Up;
	TIMER.TIM_ClockDivision = 0;
	TIM_TimeBaseInit(TIM8,&TIMER);
  TIM_Cmd(TIM8,ENABLE);
	
	
PWM.TIM_OCMode = TIM_OCMode_PWM1;
PWM.TIM_OCPolarity = TIM_OCPolarity_High;
PWM.TIM_OutputState = TIM_OutputState_Enable;
PWM.TIM_Pulse = 0;                 
TIM_OC1Init(TIM1, &PWM);    
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
 
 
PWM.TIM_OCMode = TIM_OCMode_PWM1;
PWM.TIM_OCPolarity = TIM_OCPolarity_High;
PWM.TIM_OutputState = TIM_OutputState_Enable;
PWM.TIM_Pulse = 0;                 
TIM_OC2Init(TIM1, &PWM);    
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);
 
 
 
	
	PWM.TIM_OCMode = TIM_OCMode_PWM1;
PWM.TIM_OCMode = TIM_OCMode_PWM1;
PWM.TIM_OCPolarity = TIM_OCPolarity_High;
PWM.TIM_OutputState = TIM_OutputState_Enable;
PWM.TIM_Pulse = 0;  
TIM_OC1Init(TIM8, &PWM);    
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
 
	PWM.TIM_OCMode = TIM_OCMode_PWM1;
PWM.TIM_OCPolarity = TIM_OCPolarity_High;
PWM.TIM_OutputState = TIM_OutputState_Enable;
PWM.TIM_Pulse = 0;                 
TIM_OC2Init(TIM8, &PWM);    
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
 
		
PWM.TIM_OCMode = TIM_OCMode_PWM1;
PWM.TIM_OCPolarity = TIM_OCPolarity_High;
PWM.TIM_OutputState = TIM_OutputState_Enable;
PWM.TIM_Pulse = 0;                 
TIM_OC3Init(TIM8, &PWM);    
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
	
	PWM.TIM_OCMode = TIM_OCMode_PWM1;
PWM.TIM_OCPolarity = TIM_OCPolarity_High;
PWM.TIM_OutputState = TIM_OutputState_Enable;
PWM.TIM_Pulse = 0;                 
TIM_OC4Init(TIM8, &PWM);    
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8,ENABLE);
 
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM8,ENABLE);
 
 
 
 
 
		
		
	
	
while(1){
	
	
 
	delay(4000);
		
		TIM1->CCR1 = OKUNAN_DEGER;
		TIM1->CCR2 = 0;
		TIM8->CCR1 = 0;
	  TIM8->CCR2 = OKUNAN_DEGER;
		TIM8->CCR3 = 0;
		TIM8->CCR4 = 0;
		delay(4000);
		
		TIM1->CCR1 = 0;
		TIM1->CCR2 = 0;
		TIM8->CCR1 = 0;
	  TIM8->CCR2 = OKUNAN_DEGER;
		TIM8->CCR3 = OKUNAN_DEGER;
		TIM8->CCR4 = 0;
		delay(4000);
		
		TIM1->CCR1 = 0;
		TIM1->CCR2 = OKUNAN_DEGER;
		TIM8->CCR1 = 0;
	  TIM8->CCR2 = 0;
		TIM8->CCR3 = OKUNAN_DEGER;
		TIM8->CCR4 = 0;
    
		 delay(4000);
 		
		TIM1->CCR1 = 0;
		TIM1->CCR2 = OKUNAN_DEGER;
		TIM8->CCR1 = OKUNAN_DEGER;
	  TIM8->CCR2 = 0;
		TIM8->CCR3 = 0;
		TIM8->CCR4 = 0;
		 
		  delay(4000);
 
    TIM1->CCR1 = 0;
		TIM1->CCR2 = 0;
		TIM8->CCR1 = OKUNAN_DEGER;
	  TIM8->CCR2 = 0;
		TIM8->CCR3 = 0;
		TIM8->CCR4 = OKUNAN_DEGER;
     delay(4000);
 
 
    TIM1->CCR1 = OKUNAN_DEGER;
		TIM1->CCR2 = 0;
		TIM8->CCR1 = 0;
	  TIM8->CCR2 = 0;
		TIM8->CCR3 = 0;
		TIM8->CCR4 = OKUNAN_DEGER;
      delay(4000);
 
	}
	
 
	
	
	
 
 
}
 
 
 

Should I use ınterrupt, sir.