PDA

View Full Version : Record, storage and compare analog signals



Kuba230
- 18th April 2014, 08:39
Hi, all, please I need help
I need record analog signal (10Bit) to PIC with length 60us, storage as reference (red line) and compare with next one incoming (blue line). My question is, how to sampling and storage reference signal and compare those signals? I want to use PIC18F2455.
Look Fig17311
Thanks Kuba

Ioannis
- 20th April 2014, 21:59
Looking at the Data sheet of the chip 18F2455 and especially in the A/D Acquisition Requirements, it seems that you need a 2.45usec for every sample. Of course you need also a little time to transfer the AD value to an array and start AD again. Say for the sake of the discussion the the total time is 3usec.

This leaves you with only 20 samples in the 60usec period.

Is that OK with you?

Ioannis

Kuba230
- 21st April 2014, 18:59
I think, 20 samples is enough.
How can I obtain this 20 samples and storage as numbers for calculation?

Thanks Kuba

Ioannis
- 21st April 2014, 21:42
The assumption of 2.45us was based on the fact that Vdd is 5Volts and the source impedance less than 2.5Kohm.

What will be the clock of the your controller?

Ioannis

Kuba230
- 22nd April 2014, 15:47
I will use osc. 20MHz

Kuba

Ioannis
- 22nd April 2014, 17:54
Lets then suppose you have the analog input at AN0.




ADCON0=%00000001 ' Channel 0, ADC on
ADCON1=%00001110 ' AN0 as analog,rest as digital
ADCON2=%10010101 ' Right justify result and Fosc/16

analog_res_array VAR WORD[40]
counter VAR BYTE
start_array VAR BYTE ' first element of the array to store samples
stop_array VAR BYTE ' last element of the array to store samples

start_array=0:stop_array=19 ' first group of 20 samples
gosub adc_routine
start_array=20:stop_array=39 ' second group of 20 samples
gosub adc_routine

......

adc_routine:
for counter=start_array to stop_array
high adcon0.1
while adcon0.1:wend
analog_res_array[counter]=adresh*256+adresl
next counter
return


Now in the array you have 40 samples (two groups of 20) and you can compare them.

Note that code is untested, but you get the idea.

Ioannis

Kuba230
- 22nd April 2014, 18:43
Thanks for your help.

Kuba

Kuba230
- 9th May 2014, 09:36
Hi,
I have code for 10 Bit AD converter with array storage four samples.
If I send results from array separately, I see all numbers of array results on LCD, (Example 1.)
But If I send all results from array together, LCD show number 0. (Example 2.)
My question is, what is wrong, how correct this code, how can I send all results from array to LCD?

Thanks
Kuba

Example 1.
Lcdout DEC array[0]," ", Dec array[1]," ", Dec array[2]," ", Dec array[3]

Example 2.
Lcdout DEC array[counter]



This is my code:

Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTC
Define LCD_RSBIT 4
Define LCD_EREG PORTC
Define LCD_EBIT 5

' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 3 ' Set sampling time in uS

TRISA = %11111111 ' Set PORTA to all input
TRISB = %00000000 ' Set PORTB to all output
TRISC = %00000000 ' Set PORTC to all output

ADCON1 = %10000010 ' Set PORTA analog and right justify result

Pause 500 ' Wait .5 second

array Var Word [4]
counter VAR Byte
Start_array VAR Byte
Stop_array VAR Byte

Start_array =0 : Stop_array = 3

adc_runtime:
for counter = Start_array to Stop_array
ADCIN 0, array[counter] ' Read channel 0 to array[counter]
next counter
Pause 100 ' Wait .1 second

Lcdout $fe, 1 ' Clear LCD
Lcdout DEC array[counter]


Pause 100 ' Wait .1 second

Goto adc_runtime ' Do it forever
End

EarlyBird2
- 9th May 2014, 09:58
Use example 1 as example 2 will only display array[4] or you could do something like this

adc_runtime:
Lcdout $fe, 1 ' Clear LCD
for counter = Start_array to Stop_array
ADCIN 0, array[counter] ' Read channel 0 to array[counter]
Lcdout DEC array[counter]," "
next counter
Pause 100 ' Wait .1 second

Try it a see what happens

Ioannis
- 9th May 2014, 10:42
You try to display only one array element and especially the array[4] element that does not exist. You have defined from 0 to 3 (4 elements) and the variable counter after the end of the For-Next loop ends up with the value of 4.

So you try to display an non-existence variable, that could have a random value.

Steve has given you an example to display the variable within the loop and should work as you expect.

Ioannis

Kuba230
- 9th May 2014, 10:43
Working, great :)

Thanks EarlyBird2

Kuba

Kuba230
- 9th May 2014, 10:57
Hi loannis,
I tried your metod, it does not work.
Method of EarlyBird2 really works, Im surprise.

Thanks
Kuba

EarlyBird2
- 9th May 2014, 10:58
Kuba,

You are welcome.

Steve

Ioannis
- 9th May 2014, 11:15
What method? I explained what Steve has given you as an example.

Ioannis

Kuba230
- 9th May 2014, 12:31
Hi loannis,
now I understand you explanation.
Exist other method, how to send all array elements to LCD, after the completion loop, except my 1. Example?


Thanks
Kuba

Sorry for my english

Ioannis
- 9th May 2014, 13:21
With arrays the STR modifier is used to send without FOR-NEXT loop.

But in your case you need a second modifier and up to this moment this is not supported.

This does work: STR my_array\16 ' sends the first 16 bytes of the array my_array

This does not work: STR DEC my_array\16

But I wish sometime in the next update/upgrade it will be supported!

Ioannis

Kuba230
- 9th May 2014, 13:47
Thanks loannis for your answer

Kuba