• AVR Freaks

Helpful ReplyHot!How to extract ADC value without UART

Page: 12 > Showing page 1 of 2
Author
parkcoding
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
2019/08/21 23:37:18 (permalink)
0

How to extract ADC value without UART

Hi~
 
I'm trying to extract ADC MAX, min value without UART in PIC16F18324
 
But I don't know what the problem is.
 
I'm trying to find out the maximum and minimum of 100 ADC values using an array,
 
but I don't know how to put ADC values buff in array.
 
 
 

 
#include <xc.h>
#include <stdbool.h>
#include <stdint.h>
 
 
 
#pragma config RSTOSC = HFINT32 
#pragma config CSWEN = ON
#pragma config CP = OFF 
#pragma config CLKOUTEN = OFF
#pragma config WDTE = OFF 
#pragma config PWRTE = OFF 
#pragma config BOREN = 0FF 
#pragma config MCLRE = ON 

#define _XTAL_FREQ 20000000
#define ADC_10BIT //ADC 10bit Indication
#define ADC_VOLT //ADC_VOLT Indication
#define ADC_HEX //ADC_HEX Indication
 
 
 
volatile int flag_TMR2 = 0; 
 
 
 
int num[10][10];
int arr;
int x = 0;
int y = 0;
int z = 0;
 

 
adc_read

 
unsigned int adc_read(unsigned int AN_channel)
{
    unsigned int adc_buf;
    ADCON0bits.CHS = AN_channel;
    ADCON0bits.GO = 1;
    while(ADCON0bits.GO_nDONE);
    adc_buf= (unsigned int)(ADRESH << 8) + ADRESL;

    return adc_buf;
}
 
 
 

 
and main
 

 
 
 
int main(void)
{
 
   initial_OSCC();
   initial_PORT();
    initial_INTERRUPT();
    initial_TIMER();
    initial_PPS();
    initial_PWM();
    initial_ADC();

    PORTCbits.RC2 = 1; //enable port

    z = num[x][y];
 
    for(z = 0 ; z < 100 ; z++)
    {
        for(y = 0 ; y < 10 ; y++)
       {
             for(x = 0 ; x < 9 ; x++)
            {
                 if(num[x][y] > num[x+1][y])
                 {
                      arr = num[x][y];
                      num[x][y] = num[x+1][y];
                      num[x+1][y] = arr;
                  }
             }
             if(y < 9)
             {
                  if(num[9][y] > num[0][y+1])
                  {
                       arr = num[9][y];
                       num[9][y] = num[0][y+1];
                       num[0][y+1] = arr;
                   }
               }
       }
   }
    return z;
}
 
 
 

 
post edited by parkcoding - 2019/08/21 23:57:39
#1
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to extract ADC value without UART 2019/08/21 23:47:43 (permalink)
+2 (2)
What model PIC are you using?
I suspect that ADC read code won't work very well on it.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
parkcoding
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to extract ADC value without UART 2019/08/21 23:58:27 (permalink)
0
Mr. ric.
 
ADC does work....
 
I'm using PIC16F18324
#3
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to extract ADC value without UART 2019/08/22 01:40:10 (permalink)
+3 (3)
Well you are ignoring the datasheet instructions on how to drive the ADC.
Specifically "22.2.6 ADC CONVERSION PROCEDURE" you are skipping step 4

4. Wait the required acquisition time(2).


This line sets your CPU clock to 32MHz
#pragma config RSTOSC = HFINT32
but this line tells the compiler it is running at 20MHz
#define _XTAL_FREQ 20000000


Don't write to PORTx bits, use the equivalent LATx bits.

i.e. change
    PORTCbits.RC2 = 1; //enable port
to
    LATCbits.LATC2 = 1; //enable port

Why are you creating a 10x10 element array? Wouldn't just a one-dimensional 100 element array be easier?
 
Why are you swapping array elements around if you just want to find the max and min values?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
parkcoding
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 01:48:37 (permalink)
0
Mr.ric
 
There are no required acquisition time register..
 
In PIC18F87J60,
 
I can use required acquisition time register,  the ACQT of ADCON2...
 
but in 16F18324,
 
There has no ACQT register...
 
And I adjust you mention about LAT and clock.
#5
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to extract ADC value without UART 2019/08/22 01:56:14 (permalink)
+2 (2)
parkcoding
Mr.ric
 
There are no required acquisition time register..
 
In PIC18F87J60,
 
I can use required acquisition time register,  the ACQT of ADCON2...
 
but in 16F18324,
 
There has no ACQT register...

Exactly. The hardware does not do it for you, so it is YOUR job to insert an appropriate delay.
Why do you think the datasheet specifically mentions it?
Fix the _XTAL_FREQ definition, then you can use the __delay_us() macro to insert a delay in your code.
It has to go AFTER you set the new input channel, and BEFORE you set the GO/DONE bit.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
pcbbc
Super Member
  • Total Posts : 1252
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 03:16:25 (permalink)
+3 (3)
If all you are interested in is minimum and maximum of 100 readings, why bother with the array at all? You can simply compare the values on the fly and store in two non-array variables (for example min_value and max_value).

Why are you returning a value from main?
Where do you expect the PIC to “return” to?
Hint: main can’t return anywhere in an embedded application. It will just crash and reboot the PIC.

If you don’t have a UART, what IO devices DO you have?
If you don’t have any your options are limited. Perhaps...
Stop at a break point in the debugger and inspect the variables.
Store values in EEPROM, read out with the PICKIT (or other programmer).
#7
parkcoding
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 16:09:21 (permalink)
0
Mr. pcbbc.
 
I try it but it doesn't work....
 

 
 
 
 
 
 
 
unsigned int function(unsigned int x, unsigned int y)
{
       z = num[x][y];
      for(z = 0 ; z < 100 ; z++)
      {
          for(y = 0 ; y < 10 ; y++)
         {
             for(x = 0 ; x < 9 ; x++)
            {
                if(num[x][y] > num[x+1][y])
                {
                      arr = num[x][y];
                      num[x][y] = num[x+1][y];
                      num[x+1][y] = arr;
                }
            }
 
 
 
            if(y < 9)
            {
                 if(num[9][y] > num[0][y+1])
                 {
                      arr = num[9][y];
                      num[9][y] = num[0][y+1];
                      num[0][y+1] = arr;
                 }
             }
 
 
 
         }
     }
    return z;
}
 
 
 
 
 
 
 

void main(void)
{
 
 
 
       initial_OSCC();
       initial_PORT();
       initial_INTERRUPT();
       initial_TIMER();
       initial_PPS();
       initial_PWM();
       initial_ADC();

        LATCbits.LATC2 = 1; //enable port



       while(1)
       {

           for(n = 0 ; n < 1 ; n++)
           {
 
               n = adc_read (19); 
               function(adc_read(19), adc_read(19));
            }
             MAX = num[0][0];
             min = num[9][9];
        }
}
 
 
 
 
 
 
 

post edited by parkcoding - 2019/08/22 16:46:42
#8
1and0
Access is Denied
  • Total Posts : 9623
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 16:25:06 (permalink)
+2 (2)
parkcoding
       while(1)
       {
            n = adc_read(19);
           for(n = 0 ; n < 1 ; n++)
           {
               n = adc_read (19); 
               function(adc_read(19), adc_read(19));
            }
             MAX = num[0][0];
             min = num[9][9];
        }
}


I got to ask. What do you expect that to do???
 
#9
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to extract ADC value without UART 2019/08/22 16:40:42 (permalink)
+3 (3)
The OP has ignored my question about why they are using a 2 dimensional array.
I think they have picked the worst possible way to do this. They appear to be trying to sort the array by size, then selecting the first and last elements to get the largest and smallest.
Way too complicated an approach, and poorly implemented.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
1and0
Access is Denied
  • Total Posts : 9623
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 16:43:19 (permalink)
#11
parkcoding
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 17:01:04 (permalink)
0
Mr.ric
 
one dimension like this?
 
 

 
 
 
 
 
 
 
while(1)
{
     for(n = 0 ; n < 10 ; n++)
    {
        num[n] = adc_read(19);
     }
     for(m = 0 ; m < 10 ; m++)
     {
         if(num[m] > num[m+1])
         {
              arr = num[m];
              num[m] = num[m+1];
              num[m+1] = arr;
          }
       }
       MAX = num[0];
        min = num[9];
}
 
 
 

post edited by parkcoding - 2019/08/22 17:24:49
#12
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to extract ADC value without UART 2019/08/22 17:17:51 (permalink)
+1 (1)
Yes, but you need to change where you create the array as well.
 
It looks like you are still trying to do a bubble sort on the data, but plainly you don't understand how they work, and it's a silly way to find the min and max value anyway.
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#13
PStechPaul
Super Member
  • Total Posts : 2375
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 17:19:52 (permalink)
+1 (1)
It never made much sense, and each attempt seems to get weirder. The OP should clearly state exactly what he is trying to do, and perhaps why. Complete code would also help, although at this point it's doubtful that it would do anything useful. And none of this has anything to do with the topic, "Extracting ADC value without UART".

 
#14
parkcoding
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 17:28:24 (permalink)
0
 
but I can't save value in MAX, min ...
 
 

 
 
 
    while(1)
 
    {
 
        for(n = 0 ; n < 10 ; n++)
 
        {
 
            num[n] = adc_read(19);
 
        }   
 
        for(m = 0 ; m < 10 ; m++)
 
        {
 
            if(num[m] > num[m+1])
 
            {
 
                arr = num[m];
 
                num[m] = num[m+1];
 
                num[m+1] = arr;
 
            }
 
        }
 
        MAX = num[0];
 
        min = num[9];
 
    }
 
 
 

 
post edited by parkcoding - 2019/08/22 17:30:29
#15
1and0
Access is Denied
  • Total Posts : 9623
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 17:30:23 (permalink)
+4 (4)
To the OP, try to implement this:
 
1. Set min_value = maximum literal value.
2. Set max_value = 0.

3. Read ADC result.
4. Compare ADC result to min_value and update min_value if needed.
5. Compare ADC result to max_value and update max_value if needed.
6. Go back to Step 3 until 100 readings.
 
7. Now, you got the min and max values.
#16
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to extract ADC value without UART 2019/08/22 17:39:34 (permalink)
+3 (3)
parkcoding
but I can't save value in MAX, min ...

Why are you so fixated on your terrible solution of sorting the data first?
You are not doing it right anyway, so throw away the code you have written, and start again using the approach suggested by 1and0.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#17
NKurzman
A Guy on the Net
  • Total Posts : 17719
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 18:02:50 (permalink)
+2 (2)
unsigned char n;
unsigned int counts;
unsigned int max;
unsigned int min;
 
min = 0;
max = 0xFFFF;
for(n = 0; N < 100; n++)
{
    counts = reada2d(); // YOUR read a2d function
    if(count < min)min = counts;
    if(count < >max)max = counts;
}
 
 
 
#18
1and0
Access is Denied
  • Total Posts : 9623
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to extract ADC value without UART 2019/08/22 18:11:36 (permalink)
+2 (2)
NKurzman
unsigned char n;
unsigned int counts;
unsigned int max;
unsigned int min;
 
min = 0;
max = 0xFFFF;
for(n = 0; N < 100; n++)
{
    counts = reada2d(); // YOUR read a2d function
    if(count < min)min = counts;
    if(count < >max)max = counts;
}

NK's code has 3 5 6 bugs.  See if you can find them. ;)
 
post edited by 1and0 - 2019/08/22 18:14:38
#19
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to extract ADC value without UART 2019/08/22 18:15:30 (permalink)
+2 (2)
I found 4, still looking for #5.
(Unless you're counting the changed name of the reada2d function as a bug...)
 
Edit: ok, I just spotted the swapped values making #5 and #6.
post edited by ric - 2019/08/22 18:18:32

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5