CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Auto calibration of line sensor
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
srikrishna



Joined: 06 Sep 2017
Posts: 50

View user's profile Send private message

PostPosted: Fri Nov 03, 2017 4:44 pm     Reply with quote

Yes
Ttelmah



Joined: 11 Mar 2010
Posts: 12502

View user's profile Send private message

PostPosted: Sat Nov 04, 2017 12:30 am     Reply with quote

So, change the thread title to include [solved]....
srikrishna



Joined: 06 Sep 2017
Posts: 50

View user's profile Send private message

PostPosted: Wed Nov 08, 2017 1:01 am     Reply with quote

But it is for one sensor. I am not very good in programming but tried to convert it for 5 sensor using array. If anything is wrong in this program please help me.


Code:
#include <18f2550.h>
#device adc=10
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#FUSES HS,NOWDT
#USE delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,PARITY=N,BITS=8)
#USE TIMER(TIMER=1,TICK=1s,BITS=16,NOISR)

unsigned int16 tick_difference(unsigned int16 current,unsigned int16 previous)
{
return (current - previous);
}
 
double map(double value, float min, float max, float y_min, float y_max)//function definition
{                   
    return (y_min + (((y_max - y_min)/(max - min)) * (value - min)));
   

typedef struct
{
   unsigned int16 min[5];
   unsigned int16 max[5];
} calibration_t; //calibration_t is the new name of our structure definition

void calibrate_adc(calibration_t  *calibration_data)//calibration_t is structure variable
{
   unsigned int16 current_tick;
   unsigned int16 starting_tick;
   unsigned int16 sensor_value[]={0,0,0,0,0};

   unsigned int16 max[] = {0,0,0,0,0};
   unsigned int16 min[] = {1023,1023,1023,1023,1023};
   unsigned int8 i;
   
   delay_us(15);

   starting_tick = current_tick  = get_ticks();
    current_tick = get_ticks();

   //get as many readings as you can.  the more the
   //merrier
   while(tick_difference(current_tick,starting_tick) < 5)
   { 
     for (i=0;i<5;i++)
     {
     set_adc_channel(i);
     delay_us(15);
      sensor_value[i] = read_adc();
      delay_us(15);
      if(sensor_value[i] > max[i])
      {
         max[i] = sensor_value[i];
      }
      if(sensor_value[i] < min[i])
      {
         min[i] = sensor_value[i];
      }
      output_toggle(PIN_B0);
      current_tick = get_ticks();
   }
   
   calibration_data->min[i] = min[i]; //a->b is just short for (*a).b in every way (same for functions: a->b() is short for (*a).b()).
   calibration_data->max[i] = max[i]; //(*calibration_data).max,
   //(*calibration_data).min = min[i];//a->b is equivalent to (*a).b, i.e. it gets the member called b from the struct that a points to.
   //(*calibration_data).max = max[i];
}
}
void main(void)
{
   calibration_t cal;// structure varible 
   unsigned int16 sensor_value[]={0,0,0,0,0};
   double map_result[]={0,0,0,0,0};
   unsigned int8 i;
   SETUP_ADC_PORTS( AN0_TO_AN4_ANALOG);
   setup_adc(ADC_CLOCK_DIV_32);

   delay_ms(1000);  //some delay to stabilize sensor

   calibrate_adc(&cal);  //takes about 5 secs

   //next two lines not technically needed
   //since you only have one ADC and it
   //was set in calibrate_adc(), but here
   //for later expansion
   
   delay_us(15);

   
   while(TRUE)
   {
      for (i=0;i<5;i++)
      {
      set_adc_channel(i);
      sensor_value[i] = read_adc();
      delay_us(15);
      map_result[i] = map(sensor_value[i],cal.min[i],cal.max[i],0,1023);
      printf("%f \r\n",map_result[i]);
      delay_ms(1000);
     
   }
   
}


Last edited by srikrishna on Wed Nov 08, 2017 6:41 am; edited 2 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 12502

View user's profile Send private message

PostPosted: Wed Nov 08, 2017 1:50 am     Reply with quote

One thing doesn't matter but worth changing. Use 'float' not 'double'. Your PIC does not have a 'double' type. The compiler will automatically be changing all references to double into standard floats, so you might as well do this yourself...

However the area that will cause problems is this:
Code:

      set_adc_channel(i);
      sensor_value[i] = read_adc();
      delay_us(15);
      map_result = map(sensor_value[i],cal.min,cal.max,0,1023);
      printf("%f \r\n",map_result);
      delay_ms(1000)

Change to:
Code:

      set_adc_channel(i);
      delay_us(15);
      sensor_value[i] = read_adc();
      map_result = map(sensor_value[i],cal.min[i],cal.max[i],0,1023);
      printf("%f \r\n",map_result);
      delay_ms(1000)


There has to be a delay (depending on the chip), of between 2uSec and 10uSec (or slightly more), between selecting an ADC channel, and reading it.
Your calibration code has 15uSec here, but the main code does not. It instead has a pointless 15uSec delay between the reading and doing the maths....

The other problem is you need to be reading the cal for the channel in the map function. You are using cal.min, not cal.min[i] (and the same for max).....
srikrishna



Joined: 06 Sep 2017
Posts: 50

View user's profile Send private message

PostPosted: Wed Nov 08, 2017 3:06 am     Reply with quote

ok I have corrected it . it is my request please check the code carefully.I am not a very good programmer and i have not worked previously with structure and pointer in embedded environment .


and is this part is ok ??




I have done it by guess.I am not sure
Ttelmah



Joined: 11 Mar 2010
Posts: 12502

View user's profile Send private message

PostPosted: Wed Nov 08, 2017 3:21 am     Reply with quote

Not quite....

What is 'i' at this point?. It is not 0 to 5...

The structure reference is OK, but you need to copy all five min's and max's to the external array.
srikrishna



Joined: 06 Sep 2017
Posts: 50

View user's profile Send private message

PostPosted: Wed Nov 08, 2017 5:26 am     Reply with quote

Ttelmah wrote:
Not quite....

What is 'i' at this point?. It is not 0 to 5...

The structure reference is OK, but you need to copy all five min's and max's to the external array.


I think now it is ok Confused
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group