Page 2 of 5


Posted: Tue Apr 29, 2008 8:14 pm
by eh2k8
Yeah thats exactly what I suspected but since I don't know how to program I though maybe... just maybe... the programming might have worked that out... but obviously not! So anyway is there any chance when you get a free moment or two could you just insert the new code and post it as I cant stress enough how little programming knowledge I have and it's almost guranteed to get mucked up if I try to do it, it'd be much appreciated!


Posted: Tue Apr 29, 2008 8:19 pm
by ThePyroElectro
I can put something up later on today, but it will be untested code which is good for guiding you but might not work the way you expect. :shock:

Posted: Tue Apr 29, 2008 8:22 pm
by eh2k8
ah that'd be excellent, seeing that I'll be heading to bed soon seeing its nearly nite over here, and I don't think I'll have the whole thing running until a week or so as the AD chip will take a bit to get my hands on, other then that chip I'll get the rest done soonish!!

Thanks again!

Posted: Wed May 21, 2008 7:10 pm
by nite
hi. i'm getting this strange error when compiling the source code:

Code: Select all

MPLAB C18 v3.20 (demo)Copyright 1999-2005 Microchip Technology Inc.Days remaining until demo becomes feature limited:  58
C:\PIC\Projects\TEST1\g_force_meter.c:66:Error [1105] symbol 'ADC_8ANA_0REF' has not been defined
C:\PIC\Projects\TEST1\g_force_meter.c:66:Error [1203] too few arguments in function callHalting build on first failure as requested.

it's defined in adc.h which is in the included in the main program. i had to change p18f452.h to p18f4520.h since i use this uC. maybe that's the problem?

Posted: Wed May 21, 2008 8:09 pm
by ThePyroElectro
The different microcontroller should make no difference when compiling the code.

I've come across this error before as there are multiple ways to use the OpenADC(...) function.

Check out the C18 Library Documentation file. The OpenADC function is documented very well. One of the example forms for using it should fit for the device/compiler.

This is one error I haven't found a good answer for other than using another form of OpenADC as seen in the documentation for the C18 functions.

Good luck. :D

Posted: Thu May 22, 2008 7:53 am
by nite
thx, the build succeeds. now i'm just waiting for my pic burner :)

Posted: Fri Jun 06, 2008 9:38 pm
by nite
hmm, it's displaying ~2.15 when lying flat
it's displaying total crap on the first digit when tilting it 90° (second and third digit are displaying around 12) - tilting it 90° the other side results in 2.89
i assume i have to do some calibrations?

i got to say my sensors looks different, i had to solder it myself ...

i would post a picture if the antispam protection would allow me :)

edit Image

Posted: Sat Jun 07, 2008 4:23 pm
by ThePyroElectro
Hmm that sounds strange.

If you're using the same sensor I used then it should be 2.5v when laying flat. If it's a different one, then the output might be different.

So maybe you have calibrations to do? I'd have to see the datasheet for your device. What's the part number?

Posted: Sat Jun 07, 2008 6:12 pm
by nite
it's an adxl320
i got about 2,45 volts when lying flat
and i'm using a rc-oscillator instead of a 20mhz crytal ... 20&x=0&y=0
there's a data sheet on that page

edit: seems to be the output ...
if i use
2.00 is displayed ...

Code: Select all

#include <p18f452.h>
#include <delays.h>
void main(void){
TRISB = 0;
PORTB = 0;

        PORTB = 0;
        PORTB = 0xFF;

everything is blinking except rb5
the cabling is ok, happens on 2 pics!

edit: ok found the problem with rb5. LVP needs to be disabled to use RB5 as an output :evil:

edit: i think it's working now
0.01-0.04 flat
0.97-1.04 on right side
1.05-1.10 on left side

Posted: Thu Jun 12, 2008 8:30 am
by nite
here's the modified code for using both axis on the first two ADC channels

Code: Select all

#include <p18f452.h>
#include <delays.h>
#include <adc.h>
#include <math.h>

#define gravity_ss 509
#define gravity_ss_y 505

#define display_divider 20

#define one    0b10100000
#define two    0b11010110
#define three    0b11110100
#define four    0b11100001
#define five    0b01110101
#define six    0b01110111
#define seven    0b10100100
#define eight    0b11110111
#define nine    0b11110101
#define zero    0b10110111
#define blank   0b00000000

void update_char_display(int *char_disp)
   int i=0;
   int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};

      case 0 :   PORTB = all_digits[char_disp[i]]; + 0b1000;
      case 1 :   PORTD = all_digits[char_disp[i]];
      case 2 :   PORTC = all_digits[char_disp[i]];
      default :


void main(void)

int char_disp[3];

int current_result, past_result = 0, current_result_y, past_result_y = 0;
int g_val, temp = 0, g_val_y, temp_y = 0, g_val_t, temp2;

int i=0;

TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
            //Turn All Displays Off Initially
PORTB = blank;      //PORTB -> Display 1
PORTC = blank;      //PORTC -> Display 2
PORTD = blank;      //PORTD -> Display 3

               //Initialize A/D Converter
      & ADC_8ANA_0REF,
      ADC_CH0 & ADC_INT_OFF );



while( BusyADC() );
   current_result = ReadADC();

temp = current_result - past_result;

while( BusyADC() );
   current_result_y = ReadADC();

temp_y = current_result_y - past_result_y;

   if( temp > 2 || temp < -2 )
   past_result = current_result;
   g_val = current_result - gravity_ss;
   g_val = g_val << 5;
   g_val = g_val / display_divider;
   if(g_val < 0)
      g_val = g_val * -1;

   i = 2;

   if( temp_y > 2 || temp_y < -2 )
   past_result_y = current_result_y;
   g_val_y = current_result_y - gravity_ss_y;
   g_val_y = g_val_y << 5;
   g_val_y = g_val_y / display_divider;
   if(g_val_y < 0)
      g_val_y = g_val_y * -1;

   i = 2;

   temp2 = g_val * g_val + g_val_y * g_val_y;
   g_val_t = sqrt(temp2);

   while(i!= 255)
      g_val_t = g_val_t/10;



Posted: Mon Jun 16, 2008 3:05 pm
by ThePyroElectro
Sorry I haven't been getting back to you, I don't have internet at my new place.

How well did this code work?

Posted: Mon Jun 16, 2008 10:03 pm
by nite
very good. if i rotate the gmeter the whole 360° it keeps displaying around 1G
the only problem i get is that one side of one axis is a bit off
displays 1,2G when the gmeter is standing on the right side.
1.0 on the left side and 0.0x when lying flat :(

Posted: Tue Jun 17, 2008 3:07 pm
by ThePyroElectro
Hmm, strange. I can't think of a reason for why it might do that. :?

You could fix it up in software but the sensor should be working appropriately. Other than that, pretty cool you got it working :D.

Posted: Tue Jul 08, 2008 9:38 pm
by cyber

this my be a stupid/inappropriate question but something isnt realy clear to me,
the ADXL320 Accelerometer does it only measure the acceleration or realy measure G

for example:
i place this Accelerometer in a centrifugal machine (where astronauts are trained in) and i let it spin at a constant velocity, does the ADXL320 measure something ?

Posted: Wed Jul 09, 2008 12:14 am
by ThePyroElectro
Hey Cyber,

The ADXL320 accelerometer measures acceleration which is the same as measuring 'G'. It can naturally then measure earth's gravity see the video here: ... heory.html

If the sensor were in a centrifugal machine it would detect the acceleration applied. If it is at constant velocity, no acceleration should be detected.