AnsweredAssumed Answered

Sonar like algorithm

Question asked by Oli45 on Nov 7, 2015
Latest reply on Nov 17, 2015 by Muhuho.Adalberto
Hi guys, 

I am trying to read a sensor that is rotating, then record all the values through the ADC and I want the sensor to stop rotatin when it found the hishest value.
Do you guys have any idea how to do that ?
Below is the code that I have written so far.
The sensor is rotating on a motor and I want it to stop once it read the hisgest value.


#include <stm32f4xx.h>
#include "lcd.h"
#include "pwm.h"
 
unsigned int my_array[1000];
 
//////// LCD DECIMAL VALUE DISPLAY ////////////////////////
 
 
void lcd_num (unsigned long int t)
{
    //  variables
 
 
    float a=0;
    float b=0;
    float c=0;
    float d=0;
  unsigned long int Z=((t*3000)/4095);
     
      // output 1st value
      
       a=Z/1000 ;
      LCD_DATA(l+48,TXT);
  
       
      
     
     LCD_DATA('.',TXT);
     
    //output 2nd value
       
      b=(Z/100)%10;
      LCD_DATA(i+48,TXT);
  
         
     
    // output 3rd value
        c= (Z/10)%10;
      LCD_DATA(48+n,TXT);
     
 
 
// output 4th value
        d= Z%10;
      LCD_DATA(48+d,TXT);
  
    LCD_DATA('V',TXT);
}
     
///////////////////////////////////////////////////////////
 
 
///////////////////////// ADC CONFIGURATION //////////////////////////
void ADC_init (void)
{
RCC->AHB1ENR |=((1u<<3)|(1u<<4)|(1u<<2));                   //clock for ports D+E+C enabled
RCC->APB2ENR |=(1<<8);      //clock for ADC1 enabled
  //GPIO port pin set as analogue input
GPIOC->MODER|=(3<<8);   //PC4 is set as analogue
//Select channel sequence
ADC1->SQR3|=14;     // 1st conversion on channel 14 : operates on PC4)
ADC1->CR2|=0x01;    // Turn on ADC
}
 
 
unsigned short int read_adc (void)
{
ADC1->CR2|=(1<<30);        // start conversion by setting SWSTART bit
    while(!(ADC1->SR&(1<<1)));  // wait for EOC bit while it is not set
    return ADC1->DR;            // return value
}
 
 
///////////////////////////////////////////////////////////////////////////
 
 
 
 
 
 
/////////////////////////// PWM /////////////////////////////////
 
void init_pwm_pb5(void)     //timer3, channel 2
{
    RCC->AHB1ENR|= (1UL << 1);      //GPIOB clock enable
    GPIOB->MODER|= (2UL << (2*5));  //PB5 output
    GPIOB->AFR[0]|= 0x200000;       //AF is timer3
     
    RCC->APB1ENR|=0x02;     //timer3 clock enable
     
    TIM3->CCMR1|=0x6000;        // PWM mode 1:
    TIM3->CCER|=0x010;      // Capture compare 1 output enabled
    TIM3->PSC=1000;                 // prescale
    TIM3->ARR=84;       // auto reload value : PWM PERIOD
    TIM3->CNT=0;                // zero counter
    TIM3->CR1|=0x01;        // Counter enabled
}
 
 
void led_pwm_pb5(int duty)
{
    int period = 84;
    duty = duty*period/100;
    TIM3->CCR2=duty;        // load capture/compare register : HI PULSE WIDTH
}
 
 
//////////////////////////////////////////////////////////////
 
/////////////////////////// MAIN /////////////////////////////////
int main(void)
{
unsigned int b;
    unsigned int i;
    unsigned int n;
        RCC->AHB1ENR|= (1UL << 1);      //GPIOB clock enable
    GPIOB->MODER|= (2UL << (2*13));     //PB5 output
 
 
 
 
     
    ADC_init () ;
  init_pwm_pb5 () ;         // Initialization for PB5 pin
      
 
     
      
    while(1)
    {
     
         for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */
    {
       if(my_array[0]<my_array[i]) /* Change < to > if you want to find smallest element*/
        my_array[0]=my_array[i];
    }
         
         
         
         
        for(b=999; b!=0; b--) my_array[b]=my_array[b-1];            
        my_array[0]=read_adc();
         
        LCD_DATA(my_array[0], TXT);
                     
         
         
  
   GPIOB->ODR |= 0x2000;    //    Output pin for PB13       IN1 Enable
     TIM3->CCR2= 2048;          // PWM PB5
 
 
    }    // braket for while(1)
}      // braket for main

Outcomes