It is currently 26 May 2018 03:27

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: problem with read time
PostPosted: 16 May 2018 12:37 
Offline

Joined: 25 Apr 2018 10:56
Posts: 4
I have a problem when I read the time difference. The value is correct in the first ms, it gives me 8 for each us with 8 Mhz clock frequency. But to more time the the value loses the relation 1-> 8.

I'm use a STM32F303VCT6, programming with mikroC pro for ARM.

The LCD is FDCC1602B.

This is my code:

 
  sbit LCD_RS at GPIOD_ODR.B7;
  sbit LCD_EN at GPIOD_ODR.B4;
  sbit LCD_D4 at GPIOD_ODR.B0;
  sbit LCD_D5 at GPIOD_ODR.B1;
  sbit LCD_D6 at GPIOD_ODR.B2;
  sbit LCD_D7 at GPIOD_ODR.B3;
  char number_print [12];
  unsigned long t_1;
  unsigned long t_2;
  unsigned long diff;
 
void main() {

      RCC_APB2ENR.TIM1EN=1;
      TIM1_CR1.CEN=1;

       GPIO_Digital_Output(&GPIOE_BASE, _GPIO_PINMASK_8);
       GPIOE_ODR.B8=0;
       GPIO_Digital_Input(&GPIOE_BASE, _GPIO_PINMASK_8);
       GPIO_Digital_Output(&GPIOE_BASE, _GPIO_PINMASK_9);
     
       t_1=TIM1_CNT;
       delay_us(1000);
       t_2=TIM1_CNT;
       diff= (t_2-t_1-9);
       
           while(1){

           GPIOE_ODR.B9=1;
           LongToStr(diff,number_print);
           Lcd_Init();
           Lcd_Out(1, 3, number_print);
//        }
     }
}


This is set of project:

Image

Image

I would appreciate if someone could tell me what is wrong.


Top
 Profile  
 
PostPosted: 22 May 2018 08:56 
Offline

Joined: 25 Apr 2018 10:56
Posts: 4
I resolved the problem, the number are mor bigger that long int. But I have a new problem now. When I start the ADC for read the external signal the clock close. I have a pin to ADC which no work whit the same clock that timer.

This is the program:

  float speed, temperature, salinity;
  const float basic_speed = 1520.8;
  const float basic_temperature  = 19.0; min_temperature;
  const float basic_salinity = 37.5, min_salinity = 5.0;
 
  float load_temperature = 37.5;
  float load_salinity = 19.0;
 
  unsigned ratio;
 
  int signal;
  unsigned int flag=0;
 
  unsigned long t_1, t_2, diff;
  float s_time, distance;
 
  sbit LED  at GPIOE_ODR.B9;
  sbit LCD_RS at GPIOD_ODR.B7;
  sbit LCD_EN at GPIOD_ODR.B4;
  sbit LCD_D4 at GPIOD_ODR.B0;
  sbit LCD_D5 at GPIOD_ODR.B1;
  sbit LCD_D6 at GPIOD_ODR.B2;
  sbit LCD_D7 at GPIOD_ODR.B3;

  char number_print [12];

void variables(){
      if (load_salinity<min_salinity){
          load_salinity=5.0;
          //
      }
   salinity = basic_salinity - load_salinity;
   temperature = basic_temperature - load_temperature;
   speed = basic_speed -1.4*salinity -2.5*temperature;
//
}

void active(){

   GPIO_Config(&GPIOA_BASE, _GPIO_PINMASK_1, _GPIO_CFG_MODE_ANALOG );
   GPIO_Analog_Input( &GPIOA_IDR, _GPIO_PINMASK_1);
   GPIO_Digital_Output(&GPIOE_BASE, _GPIO_PINMASK_13);

   ADC1_Set_Input_Channel(_ADC1_CHANNEL_2);
   
   RCC_APB2ENR.TIM1EN=1;
   TIM1_CR1.CEN = 0;                      // Disable timer
   TIM1_PSC = 2;           //Prescala entre 2 pero hay que tener a 4 el APB2 clock
   TIM1_CR1.CEN=1;

//
}

void pulse() {

   ratio = PWM_TIM1_Init(4100000);
   PWM_TIM1_Set_Duty(ratio,_PWM_INVERTED,_PWM_CHANNEL3);
   PWM_TIM1_Start(_PWM_CHANNEL3, &_GPIO_MODULE_TIM1_CH3_PE13);
   Delay_us(5);
   PWM_TIM1_Stop(_PWM_CHANNEL3);
   t_1=TIM1_CNT;                          //Here READ TIME
 //
}

void read_signal(){

       ADC1_Init();

      while(flag<1){                         // in while NO READ the TIME signal

       signal= ADC1_Get_Sample(2);    //Pin A1 Use: USART2_RTS_DE, TIM2_CH2, TSC_G1_IO2, TIM15_CH1N,                                               
                                                  // RTC_REFIN, EVENTOUT, ADC1_IN2, COMP1_INP, OPAMP1_VINP, OPAMP3_VINP

         if (signal>30 ){
         t_2=TIM1_CNT;
         diff= (t_2-t_1);
         s_time = diff/2000000.f;
         distance = s_time*speed;
         flag=1;
         LED =1;
         //
         }
      //
      }

//
}

void screen (){
     LongToStr(t_2, number_print);
     Lcd_Init();
     Lcd_Out(1, 3, number_print);
//
}

void main() {
   GPIO_Digital_Output(&GPIOE_BASE, _GPIO_PINMASK_9);
   signal=0;
   ratio=0;
   flag=0;
   LED=0;
   t_1=0;
   t_2=0;

   variables();
   active();
   pulse();
   read_signal();
   screen ();
 //
}


Does anyone know what is happening? I have completely lost.

Thanks for your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: