CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

strange array behaviour?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

strange array behaviour?
PostPosted: Tue Jul 18, 2006 7:26 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 18, 2006 8:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 18, 2006 8:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 18, 2006 9:09 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 18, 2006 11:03 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 18, 2006 11:12 am     Reply with quote

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??
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group