View previous topic :: View next topic |
Author |
Message |
boily
Joined: 27 Jan 2009 Posts: 19
|
Reading ADXL330 with a 16F877a |
Posted: Wed Jan 28, 2009 8:28 am |
|
|
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](images/smiles/icon_lol.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Jan 28, 2009 6:31 pm |
|
|
thanks, greatly appreciated ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Mon Feb 02, 2009 2:03 pm |
|
|
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
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);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 02, 2009 2:53 pm |
|
|
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 ? |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Mon Feb 02, 2009 3:26 pm |
|
|
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
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](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Feb 02, 2009 5:25 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Mon Feb 02, 2009 9:39 pm |
|
|
so this table should make sense right ?
Since +0.3V = 1 G force and -0.3V = -1 G !
![](http://i43.tinypic.com/2q8ygb8.jpg) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 02, 2009 11:57 pm |
|
|
Are both the PIC and the ADXL330 running at +3.3v ? |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Tue Feb 03, 2009 7:36 am |
|
|
yes |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 03, 2009 2:14 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Tue Feb 03, 2009 2:20 pm |
|
|
thanks alot PCM programmer
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 |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Tue Feb 03, 2009 2:37 pm |
|
|
ok never mind, everything is working fine, thanks a million time for your help ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Wed Feb 04, 2009 7:55 am |
|
|
![](http://i43.tinypic.com/212s2z4.jpg) |
|
![](templates/subSilver/images/spacer.gif) |
boily
Joined: 27 Jan 2009 Posts: 19
|
|
Posted: Tue Mar 03, 2009 3:23 pm |
|
|
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 ? |
|
![](templates/subSilver/images/spacer.gif) |
|