save adc readings in an array(100 samples)

Status
Not open for further replies.

judfid

Member level 3
Joined
Feb 19, 2009
Messages
57
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,288
Visit site
Activity points
1,597
i have trying to read adc and save to array(100 samples).i have tried different codes in mikro c.pls help with simple code on hw this is done.
Thanks.
 

I am using pic18f2550. but i will divide the 10 bit adc readings by 2 before saving in an array so that it can be accomondated in an 8bit ram(ie it will be save in 8 bit arrays)
thanks
 

A 10-bit ADC reading divided by 2 still needs 9 bits !

I don't use MikroC but the basic principle is:
Code:
unsigned char ADCSamples[100];   << make this unsigned int if you use two bytes to store the result
unsigned char SampleCount;

for(SampleCount = 0; SampleCount < 100; SampleCount++)
{
ADCSamples[SampleCount] = ADC_Read(channel);
}

Obviously configure the ADC and set the channel first.

Brian.
 
l don't understand

- - - Updated - - -

how do u declear they array,how do u write to all the elements of the array.
 

Code:
unsigned char ADCSamples[100];   << make this unsigned int if you use two bytes to store the result
declares an array called 'ADCSamples' with 100 spaces to store data of type unsigned char. An unsigned char in PIC languages means 8 data bits. If you want to store 16-bits of data so the whiole ADC output can be saved, use 'unsigned int' instead, that will make space for 100 16-bit values.

Code:
ADCSamples[SampleCount] = ADC_Read(channel);
means take the value returned from the ADC_Read function and store it in the array at index 'SampleCount'. In the loop the value of SampleCount is incremented from zero to 99 (100 in total) so each time it repeats it stores the next ADC result in the next position in the array.

When it has finished, ADCSamples[0] holds the first ADC value and ADCSamples[99] holds the last one.

Brian.
 


Code C - [expand]
1
2
3
4
5
6
7
unsigned char ADCSamples[100];   << make this unsigned int if you use two bytes to store the result
unsigned char SampleCount;
 
for(SampleCount = 0; SampleCount < 100; SampleCount++)
{
ADCSamples[SampleCount] = ADC_Read(channel);
}



1st line declares ADCSamples as an array of unsigned char type with 100 elements [0...99] each 1 byte wide.

SampleCount is a counter used for for loop and it is also 1 byte wide and its max value can be 255.

In for loop counter starts from 0 and loops until it is 99 (loops 100 times)

ADC is read 100 times and each time the ADC value is assigned to one element of the array whose index is decided by the for loop counter SampleCount.

As mentioned declare ADCSamples[100] as unsigned int as your ADC value will be 1023 / 2 = 511 which is > 255.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…