View previous topic :: View next topic |
Author |
Message |
neil
Joined: 08 Sep 2003 Posts: 128
|
strange array behaviour? |
Posted: Tue Jul 18, 2006 7:26 am |
|
|
Hi, Can anyone explain this...
When I try to read the ADC into an array element (int16) the reading has an offset of 59904. This value changes with ADC reading. I changed the code to read the ADC into a temp int16 and then copy it to the array element and it reads correctly. What on earth is happening?!
Code: | adc_buff[buff_ptr++]=read_adc(); | This has an offset of 59904
Code: | temp=read_adc();
adc_buff[buff_ptr++]=temp;
| This works.
WTF? |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jul 18, 2006 8:08 am |
|
|
Please post a complete code snippet demonstrating the problem.
We don't know 1)8bit or 10bit mode 2)speed 3)PIC 4)ADC loading
Last edited by treitmey on Tue Jul 18, 2006 10:44 am; edited 2 times in total |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Jul 18, 2006 8:25 am |
|
|
Are you having the ADC result register right or left justified? By default, it is left justified so you need to set the bit that controls that if you want it right justified. That will throw your values out the window if it's not set correctly.
Ronald |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
|
Posted: Tue Jul 18, 2006 9:09 am |
|
|
It's not justification, as the code snippet stated, it works if you read the adc into a temp variable and then copy it to the array. I'm pretty sure it's not the ADC at fault. Something strange is happening when you try to read the ADC directly into an array element!
The PIC is a 16F877, 10-bit ADC, 8MHz, ADC clock Div by 32 and the ADC is driven by an op-amp stage. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jul 18, 2006 11:03 am |
|
|
Since you WON'T post a test. I will.
Code: | #include <16F877>
#device ADC=10
#device *=16 //not needed for now.
#use delay(clock=10000000)
#fuses hs,nowdt,nolvp,protect,put,nobrownout
#use rs232(baud=19200,xmit=PIN_E0,INVERT,stream=DEBUG) // STDERR(same as DEBUG)
#case
#zero_ram
#define INTS_PER_SECOND_TMR2 77 // (20000000/(4*16*254*16)) //adjusted, trial and error
int8 int_count_tmr2;
int16 adc_value[4];
//======================= Main ==============================
void main(void)
{
int8 x;
set_tris_a(0xFF);set_tris_b(0xFF);set_tris_c(0xFF);set_tris_d(0xFF);set_tris_e(0xFF);
//setup_comparator(NC_NC_NC_NC);
//setup_vref(FALSE);
setup_adc_ports(AN0);
set_adc_channel(0);
setup_adc(ADC_CLOCK_INTERNAL);
fprintf(DEBUG,"Starting\n\r");
delay_ms(100);
adc_value[0]=read_adc();
delay_ms(100);
adc_value[1]=read_adc();
delay_ms(100);
adc_value[2]=read_adc();
delay_ms(100);
adc_value[3]=read_adc();
delay_ms(3000);
for(x=0;x<4;x++){
fprintf(DEBUG,"%u-%lu\n\r",x,adc_value[x]);
}
while(1);
}
|
produces
Starting
0-1023
1-1018
2-807
3-438
with picdem2 plus with 5k pot on RA0. 10 bits, full is 1023 as expected and goes down. Seems ok to me. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jul 18, 2006 11:12 am |
|
|
see if #zero ram helps.
seems that 59904=0xEA00=xxxxxx1000000000
ie array has junk in it and the ADC is copied over it.
just a thought?? |
|
|
|