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 support@ccsinfo.com

accelerometer adc code

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



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

accelerometer adc code
PostPosted: Wed Aug 15, 2007 12:46 am     Reply with quote

Am I setting this code up correctly? I'm using a PIC18F4680 chip, interfaced with a 3-axis accelerometer. I have the x-axis on pin AN1 and the y-axis on pin AN2. Currently I am not using the z-axis for anything.

Code:
// Accelerometer Test

#include <18F4680>
#fuses NOPROTECT,NOBROWNOUT,NOWDT,NOLVP,PUT
#use delay(clock=4000000)

#include "lcd.c"

void main()
{
   int count = 1;
   int x_axis, y_axis;

   lcd_init();

   printf(lcd_putc, "\fAccelerometer:");
   delay_ms(1000);
   setup_adc_ports( ALL_ANALOG );
   setup_adc( ADC_CLOCK_INTERNAL );

while(1)
   {
      set_adc_channel(1);  // X-AXIS reading
      x_axis = read_adc();
      printf(lcd_putc, "\f%d : %d", count, x_axis);

      set_adc_channel(2);  // Y-AXIS reading
      y_axis = read_adc();
      printf(lcd_putc, "\n%d : %d", count, y_axis);

      delay_ms(500);

      j++;
   }
}
jfk1965



Joined: 21 Oct 2003
Posts: 58

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 1:19 am     Reply with quote

You need to put a delay in between selecting the adc channel and reading that channel. Time doesn't seem to be an issue s I wouls suggest a 100us delay should be ok.

JFK
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 1:26 am     Reply with quote

I added the delay and am still having problems with it. To me, the rest of the code looks fine, but I may be wrong. I'm no expert in PICs. I'm wondering if my interface is wrong.
jfk1965



Joined: 21 Oct 2003
Posts: 58

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 1:44 am     Reply with quote

What problems are you having?
what version is your compiler?

JFK
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 8:13 am     Reply with quote

With the delay, the main code looks like it should work. There are a lot of other things that should be done carefully, though. Make sure you have a
Code:
#device ADC=10

line so it will use all 10 bits, the include files don't usually have this. Also, if the voltage/G of the accelerometer is not enough, you won't have very good resolution. That can be fixed by setting up the ADC Vref high and low on AN2 and AN3, but read the datasheet there is a minimum range. Also if you have time you can oversample to decrease random noise, if any.

Mark S.
Ttelmah
Guest







PostPosted: Wed Aug 15, 2007 8:53 am     Reply with quote

And (of course), change to using int16's for the values to hold the adc output, together with using 'LD' for the prints.
You don't say what the accelerometer 'is', but with 5v reference, and 8bit operation, from most units, the output change will probably be pretty undetectable.

Best Wishes
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 11:48 am     Reply with quote

I am using the LIS3L02AS4 accelerometer. The code compiles perfectly and is programmed into the PIC, but I'm not seeing any readings from the accelerometer on my LCD screen. It just reads "0" for both the x and y axis, no matter how much I jerk the accelerometer chip. I placed a voltmeter on the outputs and made sure that it was outputting something and it was.

I'll try adding the "#device ADC=10" in my code. Also, I'm using 3.3V for the chip.
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Aug 15, 2007 12:27 pm     Reply with quote

You should change x_axis and y_axis to int16, and change your code as below (do for y axis too)

Code:
x_axis = read_adc();
printf(lcd_putc, "\f%u : %lu", count, x_axis);


The accelerometer should not output zero, the outputs should be half of supply voltage (1.6v) plus or minus an amount depending on how much you tilt the chip.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 12:37 pm     Reply with quote

The code shown in the first post is not the real code.
I can tell this because it's missing the oscillator fuse (XT for 4 MHz),
and it's missing the declaration of variable 'j'.
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 12:47 pm     Reply with quote

oops, yeah I forgot to edit it a little to make it readable, sorry. Here's the code I was originally using:

Code:
// Accelerometer Test

#include <18F4680.H>
#fuses NOPROTECT,NOBROWNOUT,NOWDT,NOLVP,PUT
#use delay(clock=4000000)

#include "lcd.c"

void main()
{
   int j = 1;
   int x_axis, y_axis;

   lcd_init();

   printf(lcd_putc, "\fAccelerometer:");
   delay_ms(1000);
   setup_adc_ports( ALL_ANALOG );
   setup_adc( ADC_CLOCK_INTERNAL );

while(1)
   {
      set_adc_channel(1);  // X-AXIS reading
      x_axis = read_adc();
      printf(lcd_putc, "\f%d : %d", j, x_axis);

      set_adc_channel(2);  // Y-AXIS reading
      y_axis = read_adc();
      printf(lcd_putc, "\n%d : %d", j, y_axis);

      delay_ms(500);

      j++;
   }
}


I've never used the XT fuse in my other codes and they seemed to have worked fine. Should I start using this? It probably wouldn't hurt to try
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 2:22 pm     Reply with quote

If I compile the program without an oscillator fuse setting, with PCH
vs. 4.049, I get this for Word 1 of the Config Bits:
Code:
Word  1: 0F00   NOIESO NOFCMEN RESERVED

This line is near the bottom of the .LST file for the project.

When I compile it with XT in the #fuses statement, I get this:
Code:
Word  1: 0100   XT NOIESO NOFCMEN RESERVED


Based on the table below, which comes from the 18F4680 data sheet,
if the msb byte of config word #1 is 0x0F, then it sets up the PIC to
use an external RC oscillator.

Maybe you've got an external resistor and capacitor on the oscillator pin.
I doubt it. I think there's no way your program could be working,
unless, when you program the code into the PIC, you're setting the
oscillator type at that time (manually, by selecting a menu option in
the programmer application).

I just had one more idea. Is this all being done in Proteus ?
Quote:

bit 3-0 FOSC3:FOSC0: Oscillator Selection bits
11xx = External RC oscillator, CLKO function on RA6
101x = External RC oscillator, CLKO function on RA6
1001 = Internal oscillator block, CLKO fn. on RA6, port fn. on RA7
1000 = Internal oscillator block, port function on RA6 and RA7
0111 = External RC oscillator, port function on RA6
0110 = HS oscillator, PLL enabled (Clock Frequency = 4 x FOSC1)
0101 = EC oscillator, port function on RA6
0100 = EC oscillator, CLKO function on RA6
0011 = External RC oscillator, CLKO function on RA6
0010 = HS oscillator
0001 = XT oscillator
0000 = LP oscillator
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 2:58 pm     Reply with quote

Okay, that makes sense now. I'm not sure what Proteus is. For the most part, I was testing my chipping using the microchip PICDEM-2 PLUS development board. The development board does have an external RC oscillator onboard, so that's probably where the PIC is getting it's clock speed from.

I'll throw XT in #FUSE when I get home tonight. That may actually solve a few of my problems (hopefully Rolling Eyes ). I don't really know why I stopped using that. I checked some of my old test codes (for various other projects) and I used to include that.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 3:07 pm     Reply with quote

To use the RC oscillator, jumper J7 has to be installed on the PicDem2-
Plus board. But the PicDem2-Plus comes with a 4 MHz oscillator.
It's the rectangular "can" above the PIC sockets. I recommend using
that oscillator. Remove jumper J7. Then you can use either the EC or
EC_IO fuse setting. I think XT will also work. But remember to remove
jumper J7.
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