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

Reading ADXL330 with a 16F877a
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

Reading ADXL330 with a 16F877a
PostPosted: Wed Jan 28, 2009 8:28 am     Reply with quote

Hi,

I have a 16f877a and I'm trying to read my ADXL330 and I don't know what I am doing wrong...

can someone help me ?
would be greatly appreciated !

thnks
Code:


#include <16f877A.h>
#use delay (clock=20000000)
#include <gg_lcd.c>
#include <stdlib.h>
#include <stdio.h>

#define   MENUSELECT !input(PIN_D0)     
int8 menu=0;
int16 z;
int32 g;
char *msg   [21];
char *espace [21];

void main (void)

   set_tris_a(0xff);
   set_tris_b(0x00);
   set_tris_c(0x00);         
   set_tris_d(0xFF);           
   set_tris_e(0x00);   

   setup_adc(adc_clock_internal);
   setup_adc_ports(AN0_AN1_AN2_AN3_AN4); 
   set_adc_channel(0);
 
   LCD_init();               
   
 
   while(1)
   {
         if(!MENUSELECT)            
      {
         delay_ms(250);
         menu++;
      }
      if(menu>=5)menu=1;       
      
      switch(menu)                     
         {
      case 0:     
            LCD_inst(0x80);                  //Sélection de la ligne #1
         z = read_adc();
         itoa(z,10, msg);
         strcpy(espace, "                   "); 
            strcat(msg, espace);             //Choix texte à écrire
         ecrire_chaine(msg);

      
Laughing Laughing
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 28, 2009 1:21 pm     Reply with quote

Read my advice here on where to find ADXL330 sample code:
http://www.ccsinfo.com/forum/viewtopic.php?t=32022&start=3
Here's the search page that I refer to (search for ADXL330):
http://forum.sparkfun.com/search.php
Guest








PostPosted: Wed Jan 28, 2009 6:31 pm     Reply with quote

thanks, greatly appreciated Very Happy
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 2:03 pm     Reply with quote

Hi,

I finally got some readings with my adxl330 accelerometer !
The unit is plugged on a 3.3V source...

I am reading about 5600 when the unit is still and it varies to 6300-4500 when I'm tilting the unit !

Now my question is, how do I convert this into Gs ??
I'm a little bit confused..Thanks Smile

what I know:

- 0 to 255 bits
- 0.66V to 2.64V
- 0.3v/g
- 1.65V = 0g

Here is the essential code:

Code:

#include <16f877A.h>
#use delay (clock=20000000)
#include <gg_lcd.c>
#include <stdlib.h>
#include <stdio.h>

#define   MENUSELECT !input(PIN_D0)      //bouton suivant, change de page

int8 menu=0;
int8 x;
int8 y;
int8 z;
//int8 c;
int32 g;
char msg   [21];
char espace [21];

void main (void)

   set_tris_a(0xFF);
   set_tris_b(0x00);
   set_tris_c(0x00);         
   set_tris_d(0xFF);           
   set_tris_e(0x00);   

   setup_adc(adc_clock_internal);
   setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
   set_adc_channel(0);
 
   LCD_init();
 
  while(1)
   {
      LCD_inst(0x80);                  
      x = read_adc();
      g=(225/3.3)*x;
      itoa(x,10, msg);
      strcpy(espace, "                   "); 
      strcat(msg, espace);             
      delay_ms(300);
      ecrire_chaine(msg);
   }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 2:53 pm     Reply with quote

Quote:

I am reading about 5600 when the unit is still and it varies to 6300-4500
when I'm tilting the unit !

Where do you get these large numbers ? Do they come from the A/D, or
from some calculated result ?

Also, why do you have such a complicated display code ? Why not
just use printf and send the result to the LCD putc() routine ?
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 3:26 pm     Reply with quote

I just changed my code and I'm now getting a 510 value (using 10bits, 1024 maximum) when the unit is still !!

So that would be the 0g...
The +1g would be around 682bits Smile
Thats where I'm confused, how would I write the code to show the G values according to the bits I can read ...

Thank you for the reply Smile
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 5:25 pm     Reply with quote

The reading you get right side up is +1G. Turn the accelerometer upside down to read -1G. On edge you should get about 0G, but that won't be as accurate.

From those reading you should be able to calculate a scale and offset to go from counts to Gs.
_________________
The search for better is endless. Instead simply find very good and get the job done.
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 9:39 pm     Reply with quote

so this table should make sense right ?
Since +0.3V = 1 G force and -0.3V = -1 G !

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 11:57 pm     Reply with quote

Are both the PIC and the ADXL330 running at +3.3v ?
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 7:36 am     Reply with quote

yes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 2:14 pm     Reply with quote

Quote:
Since +0.3V = 1 G force

You're using "Typical" values from the table of Specifications on page 3
of the ADXL330 data sheet:
http://www.analog.com/static/imported-files/data_sheets/ADXL330.pdf
These values are for a Vs of 3.0v. But you're using 3.3v, so they don't
apply.

Here are some parameters necessary to calculate the value of g:

Sensitivity:
The specifications on page 3 lists it at 300mv/g for Vs = 3.0 volts.
On page 13, in first paragraph, it says it's 360mv/g for a Vs of 3.6 volts.
A note on the bottom of page 3 says:
Quote:
Sensitivity is essentially ratiometric to VS.

Therefore, with your Vs of 3.3v, the sensitivity will be: 330mv/g
This can also be expressed as 3.03 g/volt

Zero G Bias Level:
The specs on page 3 list it at 1.5v for Vs = 3.0 volts. On page 13 it says:
Quote:

The zero g bias output is also ratiometric, so the zero g output is
nominally equal to VS/2 at all supply voltages.

So for your Vs of 3.3v, the Zero G Bias level will be: 1.65v

A/D range:
According to Ttelmah, the A/D value used in calculations (for the PIC)
should be 1023 (not 1024).

Given these specs, we can now write the g's per volt transfer equation:
Code:
g = (((ADC value * 3.3v) / 1023)  -1.65v)   * 3.03 g/v

In words, this is "Volts read" minus the bias point, times g's per volt.
The volt terms cancel out, giving the result in units of g's.

This equation can be simplified to:
Code:

     ADC value
g = -----------  - 5.0
       102.3

The ADC value is from 0 to 1023.

This equation can be written in C with floating point math.
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 2:20 pm     Reply with quote

thanks alot PCM programmer Smile
Yes I'm using the 330mv/g unit !
Instead of using the 1023bits, I'm reading on 255 bits ...
So yeah, I get the idea, thanks once again !
Now my problem is when I try to print the results on the LCD, I want to put some decimals but can't do it right...

http://www.ccsinfo.com/forum/viewtopic.php?t=37633
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 2:37 pm     Reply with quote

ok never mind, everything is working fine, thanks a million time for your help Very Happy
boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 7:55 am     Reply with quote

Very Happy

boily



Joined: 27 Jan 2009
Posts: 19

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 3:23 pm     Reply with quote

ok another question:

I convert my adc_readings like PCM Programmer told me... (ADC_READ/102.3)-5.00V !

I understand it well but there is a thing I don't get..., when I move my accelerometer really fast, I can see some 5g readings and the device is supposed to top at 3g ...

What could be the problem ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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