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 CCS Technical Support

Why is max set to 0 and min to 255 in the code?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kel



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

Why is max set to 0 and min to 255 in the code?
PostPosted: Tue Dec 27, 2005 10:11 pm     Reply with quote

from the following code, can some exolain to me why min is set 255 and max set 0 initially...i.e. min = 255; //and displays the min and max = 0; //values for that 100ms period

Thanks budds..
Happy chrismass
Code:
[/////////////////////////////////////////////////////////////////////////
#include<16f877a.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

void main() {
   int i, value, min, max;
   
   printf("Sampling:");
   
   setup_adc_ports( RA0_ANALOG );
   setup_adc( ADC_CLOCK_INTERNAL );
   set_adc_channel( 0 );
   
   do {                       //Takes 30 samples from pin A0
      min = 255;              //and displays the min and max
      max = 0;                //values for that 100ms period
      for(i = 0; i <= 30; ++i) {
         delay_ms(100);
         value = read_adc();
         if(value < min)
            min = value;
         if(value > max)
            max = value;
      }
      printf("\n\rMin:%x MAX: %x", min, max);
   } while (TRUE);
}
]
[/code]
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Tue Dec 27, 2005 10:42 pm     Reply with quote

Those are "seed" values for the first comparison of the values of interest. If "min" was initially defined as 100 any value between 100 and 255 would not be registered as a new min.

Good luck,

John
kel



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

why not the otherway around?
PostPosted: Tue Dec 27, 2005 11:08 pm     Reply with quote

Why not the min=0; and max=255 instead?
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Tue Dec 27, 2005 11:40 pm     Reply with quote

If you initialized "min" with zero and "max" with 255 everytime through the MAIN DO LOOP you'd get:

Code:
Min:0 MAX:ff
(I think that is the correct formatting for %x)

Look at the comparisons and give yourself a couple of "for instance" ADC readings and see how the code works. Remember you'll never get a value smaller than 0 or greater than 255.... The new min value would never get put into the min variable and the new max value would never get put into the max variable....
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Tue Dec 27, 2005 11:53 pm     Reply with quote

I have to agree with jecottrell here. If you want your code to work as you intended, you need to do an initial read of the A/D, right after you initialize it, and just before your do.....while loop.

i.e.:

Code:
min = read_adc();
max = min;


This sets your max and min to be the same, but more importantly within the range you'll see.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Dec 28, 2005 2:33 am     Reply with quote

newguy wrote:
I have to agree with jecottrell here. If you want your code to work as you intended, you need to do an initial read of the A/D, right after you initialize it, and just before your do.....while loop.

i.e.:

Code:
min = read_adc();
max = min;


This sets your max and min to be the same, but more importantly within the range you'll see.
The code as given is correct. Your suggested change is just another method of getting the same result at the cost of an extra call to read_adc().
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Wed Dec 28, 2005 7:20 am     Reply with quote

Upon re-reading your original post I see I missed a subtle but important point that may be confusing you.

kel wrote:
//and displays the min and max = 0; //values for that 100ms period


It appears your ADC may be reading 0 constantly and you're assuming that the code is behind getting a min and max = 0? (HINT: The code is not the problem.)

Code:
do {                       //Takes 30 samples from pin A0
      min = 255;              //and displays the min and max
      max = 0;                //values for that 100ms period
      for(i = 0; i <= 30; ++i) {
         delay_ms(100);
         value = read_adc();
         if(value < min)
            min = value;
         if(value > max)
            max = value;
      }
      printf("\n\rMin:%x MAX: %x", min, max);
   } while (TRUE);


Also, the comment about taking thirty samples and reporting for a 100ms period is incorrect. The 100mS delay is within the FOR loop. (HINT: 30 x 100mS = ?)

Newguy, my suggestion was not to change the code, it was to sit down with pencil and paper and what if some values through the code to see how it reacted. I'm afraid I'm a victim of my own unclear explanation... I also agree with ckielstra that the initial ADC is not required.

Maybe our biggest problem here is translating between Australian, American, Canadian, and Dutch English?

I will reiterate my original suggestion. Give yourself some "made-up" ADC readings between 0 and 255 and step through the code and see how it reacts. Make a column for "min" and "max" and note their values each time through and I think you'll see what I'm talking about.

Good luck,

John
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Wed Dec 28, 2005 10:47 am     Reply with quote

jecottrell wrote:
Newguy, my suggestion was not to change the code, it was to sit down with pencil and paper and what if some values through the code to see how it reacted. I'm afraid I'm a victim of my own unclear explanation... I also agree with ckielstra that the initial ADC is not required.


Doh!

I hate it when I miss things! I just now realized that he was setting 'min' to 255 and 'max' to 0. When I first read that, my mind switched them around. You're right - setting the variables like this is proper, no extra read is required.

jecottrell wrote:
Maybe our biggest problem here is translating between Australian, American, Canadian, and Dutch English?


Very Happy

You're probably right! I've run into this type of thing before......just so you know, the north american definition of 'fanny' is vastly different from the British definition! Wink
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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