Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Find the highest and lowest number in array with PIC16F877A

Status
Not open for further replies.

jean12

Advanced Member level 2
Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
www.ntigltd.com
Activity points
5,497
Hello there ,I am design a pressure sensor alarm which uses MPX2100GP interfaced to PIC16F877A;the MPX2100GP is interfaced to an amplifier with TL084 which amplifiers the 1mv generated at 1KPa for getting a good analyzable value;because that sensor MPX2100GP its output is a voltage of some 2.815V and when the pressure is applied on it it changes a little bit, with ccs c compiler I am reading 20 successive numbers in 1minutes and then find the largest and lowest and the difference of the two numbers gives the pressure so that pressure measured can be used to activate the buzzer for example for some limit.

Can you please help me to find the largest and lowest numbers in those 20 digits?

Thanks.
 

You can re-arrange the data in your program(by ascending/descending). then you can find the difference from first and last value.
 

Do this in your program

Code C - [expand]
1
2
3
4
5
6
7
8
9
big_value    = 0;
small_value = 0xFFFF;
for ( i = 0; i < 20; i++)
{
if ( array[i] < small_value )
small_value = array[i];
if ( array[i] > big_value )
big_value = array[i];
}



after executing this lines you will have the small and big values....
 

Hello,the array contains the values from adc module,I am reading the value on AN0 and then they make in array in which I find the highet and lowest ;see here below the codes I was trying to use:
PHP:
#include"16f877a.h"
#device adc=10
#fuses HS,NOWDT,NOLVP,NODEBUG,NOCPD,NOBROWNOUT
#use delay(clock=4M)
#include"lcd.c"


int value,i,k,valuet;
unsigned long value1,valuenew;
//unsigned int value2,value3;
float value3,result;
int array[10];

void main()

{
	set_tris_d(0x00);
	set_tris_a(0xff);
	set_tris_c(0x00);
	set_tris_b(0x00);

	setup_comparator(NC_NC_NC_NC);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(AN0);
   set_adc_channel(0);
   setup_vref(FALSE);
	lcd_init();
	lcd_gotoxy(1,1);
	while(true)
	{
	delay_us(100);
  
for(i=0;i<10;i++)
{
delay_ms(50);
read_adc(adc_start_only);
delay_us(100);
value=read_adc(adc_read_only);
delay_us(100);
value1=value;
value3=(value1*50)/1023;
delay_us(100);
array[i]=value3;

}
printf(lcd_putc,"\f%d",array[i]);
delay_ms(500);


}
}



for(i=1;i<10;i++)
{
if(array[0]<array[i])
{
//printf(lcd_putc,"\f%f",array[0]);
//delay_ms(500);
printf(lcd_putc,"\fThe result000 is");
delay_ms(1500);
printf(lcd_putc,"\f%f",array[0]);
delay_ms(1500);
 

value3 is float and array[] is int. change array[] to float type.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
float a[10] = {10.2, 55.4, 9.3, 4.6, 234.8, 20.1, 30.5, 40.2, 22.6, 34.1};
 
  float max = a[0];
  float min = a[0];
  usigned int i;
 
 
  for (i = 0; i < 10; i++)
 
    {
 
      if (a[i] > max)
 
        {
 
          max = a[i];
 
        }
 
      else if (a[i] < min)
 
        {
 
          min = a[i];
 
        }
 
    }

 

No the array is not fixed it has to store the values read by the PIC,so after the PIC reads some 20 values,those 20 will be in one array and then the program has to find the lowest and the highest values among them.

Thanks for the previous response.
 

Use a counter to count no of array elements then in the below code


Code C - [expand]
1
for (i = 0; i < 10; i++)



change


Code C - [expand]
1
i < 10;



to


Code C - [expand]
1
i < counter;

 

Its only possible if your compiler allows to create dynamic array or create an array of 100 elements and then store the adc values to the elements and increment a counter (int variable). After array is loaded with the max adc readings then use the code I posted to get the biggest and smallest values of the array.
 

seriously ?

all you have to do is step thru the array and compare 'current <highest/ lowest>' with the next number in the array. D-uh
 

Array length is fixed while program starts executing.

as said by jayanth.devarayanadurga.

you can find the count and start comparing with other values.

you can find the program in any c programming book.
 

Hello,here are my codes
PHP:
#include"16f877a.h"
#device adc=10
#fuses HS,NOWDT,NOLVP,NODEBUG,NOCPD,NOBROWNOUT
#use delay(clock=4M)
#include"lcd.c"


int value,i,k,valuet;
unsigned long value1,valuenew;
//unsigned int value2,value3;
float value3,result;
int array[10];

void main()

{
    set_tris_d(0x00);
    set_tris_a(0xff);
    set_tris_c(0x00);
    set_tris_b(0x00);

    setup_comparator(NC_NC_NC_NC);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(AN0);
   set_adc_channel(0);
   setup_vref(FALSE);
    lcd_init();
    lcd_gotoxy(1,1);
    while(true)
    {
    delay_us(100);
  
for(i=0;i<10;i++)
{
delay_ms(50);
read_adc(adc_start_only);
delay_us(100);
value=read_adc(adc_read_only);
delay_us(100);
value1=value;
value3=(value1*50)/1023;
delay_us(100);
array[i]=value3;

}
printf(lcd_putc,"\f%d",array[i]);
delay_ms(500);


}
}



for(i=1;i<10;i++)
{
if(array[0]<array[i])
{
//printf(lcd_putc,"\f%f",array[0]);
//delay_ms(500);
printf(lcd_putc,"\fThe result000 is");
delay_ms(1500);
printf(lcd_putc,"\f%f",array[0]);
delay_ms(1500);
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include"16f877a.h"
#device adc=10
#fuses HS,NOWDT,NOLVP,NODEBUG,NOCPD,NOBROWNOUT
#use delay(clock=4M)
#include"lcd.c"
 
 
int value,i,k,valuet;
unsigned long value1,valuenew;
float value3,result;
int array[25], arraySize = 0;
float min, max;
 
void main()
 
{
        set_tris_d(0x00);
        set_tris_a(0xff);
        set_tris_c(0x00);
        set_tris_b(0x00);
 
        setup_comparator(NC_NC_NC_NC);
    setup_adc(ADC_CLOCK_INTERNAL);
    setup_adc_ports(AN0);
    set_adc_channel(0);
    setup_vref(FALSE);
 
        lcd_init();
        lcd_gotoxy(1,1);
 
        while(true)
        {
            delay_us(100);
  
        for(i = 0; i < 10; i++)
        {
            delay_ms(50);
            read_adc(adc_start_only);
            delay_us(100);
            value=read_adc(adc_read_only);
            delay_us(100);
            value1=value;
            value3=(value1*50)/1023;
            delay_us(100);
            array[i]=value3;
 
            printf(lcd_putc,"\f%d", array[i]);
 
            arraySize++;
 
        }
 
        min  array[0];
        max = array[0];
        
        for (i = 0; i = arraySize; i++) 
            { 
                if (array[i] > max) 
                { 
                    max = array[i];
 
                } 
                else if (array[i] < min) 
                { 
                    min = array[i];
 
                }
 
            }
 
        printf(lcd_putc,"\fThe min is %d", min );
        printf(lcd_putc,"\fThe max is %d", max );
        
        delay_ms(500);
 
 
    }
}

 

Code:
#include"16f877a.h"
#device adc=10
#fuses HS,NOWDT,NOLVP,NODEBUG,NOCPD,NOBROWNOUT
#use delay(clock=4M)
#include"lcd.c"
 
 
int value,i,k,valuet;
unsigned long value1,valuenew;
float value3,result;
int array[25], arraySize = 0;
float min, max;
 
void main()
 
{
        set_tris_d(0x00);
        set_tris_a(0xff);
        set_tris_c(0x00);
        set_tris_b(0x00);
 
        setup_comparator(NC_NC_NC_NC);
    setup_adc(ADC_CLOCK_INTERNAL);
    setup_adc_ports(AN0);
    set_adc_channel(0);
    setup_vref(FALSE);
 
        lcd_init();
        lcd_gotoxy(1,1);
 
        while(true)
        {
            delay_us(100);
        min =0;
        max = 0;

  
        for(i = 0; i < 10; i++)
        {
            delay_ms(50);
            read_adc(adc_start_only);
            delay_us(100);
            value=read_adc(adc_read_only);
            delay_us(100);
            value1=value;
            value3=(value1*50)/1023;
            delay_us(100);
            array[i]=value3;
 
            printf(lcd_putc,"\f%d", array[i]);
 

		if (array[i] > max) 
                { 
                    max = array[i];
 
                } 
                else if (array[i] < min) 
                { 
                    min = array[i];
 
                }
 
        }
 

        printf(lcd_putc,"\fThe min is %d", min );
        printf(lcd_putc,"\fThe max is %d", max );
        
        delay_ms(500);
 
 
    }
}
 

Here the problem is that,the system is always printing 0 as the minimum number.

Please help
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top