Personal G-Force Meter

Questions and Comments on All Projects
lukasoft
Newbie Pyro
Posts: 17
Joined: Sat May 23, 2009 3:48 pm

Postby lukasoft » Sat Mar 12, 2011 5:54 pm

Hello, its been like a year or so, but I've finally gotten the correct hardware to make one of these, and finally have enough time to work on it. I've gotten to the point where I'm ready to compile and write to the device, but I'm getting an error along these lines:
...\g_force_meter.c:66:Error [1105] symbol 'ADC_8ANA_0REF' has not been defined
...\g_force_meter.c:66:Error [1203] too few arguments in function call
with this info:
Language tool versions: MPASMWIN.exe v5.32, mcc18.exe v3.32
Preprocessor symbol `__DEBUG' is defined.
Sat Mar 12 12:45:10 2011
MPLAB IDE v8.36
any info on this would be very helpful, as I am still very new to microcontrollers, and these libraries are very foreign to me. The only thing I changed in your file was #include <p18f4520.h> as I have the newer version of the microcontroller. My guess would be the newer version might not have the same ADC function? But like I said, I'm new to all of this. Thanks.

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Sat Mar 12, 2011 9:26 pm

Hi,

The 18F4520 uses a different architecture than the 18F452, so a few changes need to be made to the ADC functions before they will work properly.

I built this example A/D code a while ago for an 18f4520, maybe it will help you:


Code: Select all

#include <p18f4520.h>
#include <stdlib.h>
#include <adc.h>
//OpenADC()
//ConvertADC()
//BusyADC()
//ReadADC()
//CloseADC() are found in this library
#include <delays.h>
//Delay1KTCYx() is in this library
#include <math.h>
//ceil() is in this library
void main(void)
{
float result=0;
float result2=0;
TRISD = 0x00;

// configure A/D converter
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD, ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0);

Delay10TCYx( 5 ); // Delay for 50TCY

ConvertADC(); // Start conversion

while( BusyADC() ); // Wait for completion

result = ReadADC(); // Read result

//Convert Result To A Value From 1 to 10
result2 = ceil(10*(result/1023));

while(1){

  //Do Servo Signal Stuff
  PORTD = 0xFF;

  Delay1KTCYx((int)result2);

  PORTD = 0x00;

  Delay1KTCYx(200-(int)result2);

  //Find New ADC Value
  ConvertADC(); // Start conversion

  while( BusyADC() ); // Wait for completion

  result = ReadADC(); // Read result

  // Convert Result To A Value From 1 to 10
  result2 = ceil(10*(result/1023));
  }

CloseADC(); // Disable A/D converter
}


I do suggest you look up in the MPLAB C18 Libraries PDF the ADC functions and see which ones can be used with the 18F4520 and how to use them.

lukasoft
Newbie Pyro
Posts: 17
Joined: Sat May 23, 2009 3:48 pm

Postby lukasoft » Tue Mar 15, 2011 10:56 pm

I found the pdf here
http://ww1.microchip.com/downloads/en/d ... 51297f.pdf
and it covers the PIC18F452 (page 14), however I couldn't find anything referring to the 18F4520. It looks to me that the current code:
OpenADC( ADC_FOSC_32
& ADC_RIGHT_JUST
& ADC_8ANA_0REF,
ADC_CH0 & ADC_INT_OFF );
matches what is in the PDF so I am not sure where to go from here. MPLAB doesn't seem to be doing much help as far as telling me what I'm missing in the arguments. I tried reading the adc.h and it doesn't make much sense to me either. There are OpenADC functions with two parameters that seem to be able to be used, but for some reason MPLAB is still complaining.

I would try to just use your code, but if I don't understand the function from the pdf, it becomes difficult to fill in code that needs to be there. Thank you for your help.

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Tue Mar 15, 2011 11:06 pm

Hey Lukasoft,

Yea, I just double checked. They have listed the few unique architectures and their different OpenADC() functions then it says:

OpenADC
All Other Processor


And explains the function. This is where you should look to see how my sample code fits in.

lukasoft
Newbie Pyro
Posts: 17
Joined: Sat May 23, 2009 3:48 pm

Postby lukasoft » Wed Mar 16, 2011 11:36 pm

Thank you for the help. I apologize for the continuous questions, but I've spent a lot of time on google looking to figure this out and I just can't get it. I'm getting an error:

Executing: "C:\MCC18\bin\_mplink.exe" /l"C:\MCC18\lib" /k"C:\MCC18\bin\LKR" "..\..\..\..\MCC18\bin\LKR\18f4520_g.lkr" "g_force_meter.o" /z__MPLAB_BUILD=1 /z__MPLAB_DEBUG=1 /m"18f4520 G force meter.map" /w /o"18f4520 G force meter.cof"
MPLINK 4.32, Linker
Copyright (c) 2009 Microchip Technology Inc.
Error - could not find definition of symbol 'ReadADC' in file './g_force_meter.o'.
Errors : 1

And when I try to add g_force_meter.o to the object files folder in the workspace it says it already exists in the project. I'm not sure what else to do.

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Thu Mar 17, 2011 1:52 am

Hey,

The best thing to do is to clean the project files, Project -> Clean

Other things I've done that fixed that issue was restart the computer and create a new project. Copy and paste the code into a new .c file and add it to that project.

Hopefully one of those helps fix your problem.

lukasoft
Newbie Pyro
Posts: 17
Joined: Sat May 23, 2009 3:48 pm

Postby lukasoft » Thu Apr 14, 2011 7:58 pm

Hello, and thank you for all of the previous help. I've finished everything with the exception of wire-wrapping the LED displays. I haven't done so yet because I found it strange that you didn't use any resistors in between the displays and the micro-controller. Wouldn't wiring them directly have too much voltage going to the displays, which would result in a shortening of life and/or melting them?

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Thu Apr 14, 2011 10:10 pm

Lukasoft,

There is a possibility that the output of the microcontroller could supply too much current to the LED, thus damaging it. The microcontroller limits the output current maximum so the possibility is slim, however it is generally a GOOD idea to add a resistor (~330 ohm) between the microcontroller output and the LED.

I just try to keep to a simplistic design style for your benefit, sometimes this does mean sacrificing reliability.

So, if you plan on using the G-meter a lot, I suggest putting resistors between the output and LEDs. However, it should be fine either way.

lukasoft
Newbie Pyro
Posts: 17
Joined: Sat May 23, 2009 3:48 pm

Postby lukasoft » Sun Apr 17, 2011 9:45 pm

Ok. The datasheet from sparkfun says the max typical voltage input is 2.2 volts. Will it be damaging to connect this to Vdd (+5V), or is there a lower voltage output that can be used?

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Sun Apr 17, 2011 9:50 pm

2.2v or 5v won't make a difference if you're talking about LEDs. At 2.2v the LEDs might not appear so bright, depending upon their output intensity. Use current limiting resistors (~330 ohm) before the LEDs and they will never get damaged.. Do Not connect LEDs directly to VDD, this will surely burn out the LED.

lukasoft
Newbie Pyro
Posts: 17
Joined: Sat May 23, 2009 3:48 pm

Postby lukasoft » Mon Apr 18, 2011 3:26 pm

Hello, I am sorry, as what I meant to say is not what I said. When I said Vdd I meant the 5v voltage regulator output, but I thought of that whole part of the circuit as a power supply in my head, hence the mistake. From what you're saying though, it seems that using the 5v output (from the regulator) is safe.

Tudy
Newbie Pyro
Posts: 4
Joined: Wed Mar 07, 2012 10:13 pm

measuring G's on X and Y axis

Postby Tudy » Wed Mar 07, 2012 10:19 pm

Hi there, does this software which posted nite work properly for measuring G's on dual axis???

Code: Select all

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

temp = current_result - past_result;

SetChanADC(ADC_CH1);
ConvertADC();
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)
   {
      char_disp[i]=g_val_t%10;
      g_val_t = g_val_t/10;
      i--;
   }

}



}

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: Personal G-Force Meter

Postby ThePyroElectro » Thu Mar 08, 2012 4:37 am

Hi Tudy,

This tutorial gives an example of getting data from an analog 3-axis accelerometer:

http://www.pyroelectro.com/tutorials/accel_intro/

It should help get you started.

Tudy
Newbie Pyro
Posts: 4
Joined: Wed Mar 07, 2012 10:13 pm

Re: Personal G-Force Meter

Postby Tudy » Thu Mar 08, 2012 11:29 am

Yes, but this tutorial shows getting data only in one axis - so the software in it is created for only one axis. I'm building Personal G meter for my school project, and I would like to measure G force in two directions (because I bought ADXL320 which doesn't have Z axis, it has only X and Y) but I don't know how to create software for measuring G force in dual axis. I would like to ask you, or someone on this forum to create appropriate software for X and Y axis for me, or confirm that nite's software is working well. Please? :D

And I need to know did you used CA or CC 7-Segment LED Displays?

with regards, Tudy

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: Personal G-Force Meter

Postby ThePyroElectro » Thu Mar 08, 2012 5:20 pm

Hi Tudy,

Sorry about that. I thought that tutorial did a 3-axis example...my memory is fading I guess.

The easiest way to get acceleration data from 2 different outputs is to connect each output to a seperate A/D. So for example if you connect X acceleration out to ADC0/RA0 and Y acceleration out to ADC1/RA1. Then in your firmware/software you will have to switch between channels 0 and 1 to get data from the different ADC input pins on the PIC.

What it ends up looking like, is duplicating the existing connections and firmware for the single axis you already have for this 2nd axis of acceleration that you want to measure.

I'm happy to help you with specific source code/firmware questions, but I can't re-write it for you.

Looking at the schematic and source code, the 7 segments are common-cathode [CC].

Good luck!


Return to “Projects”

Who is online

Users browsing this forum: No registered users and 5 guests