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

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



Joined: 23 Feb 2014
Posts: 35

View user's profile Send private message

accelerometer adc
PostPosted: Sun Feb 23, 2014 9:43 am     Reply with quote

hello, i am new to pic programming and im trying to interface an analog ADXL335 to my pic16f877A.
searching for hours i found myself lost with all the different code examples and commands, so i am asking for your help.

my code so far is below and i'd like to know what needed to be add, removed, what is missing and why.
Code:
#include <16f877a.h>
#fuses NOWDT, NOPROTECT, HS
#device ADC=10
#use DELAY (crystal=20MHz)

void main(void)
{
   
   int res;
   SET_TRIS_B(0xFF);
   setup_ADC_ports(0);
   set_adc_channel(0);
   setup_adc(ADC_CLOCK_DIV_32);
   while(TRUE)
   {     
   res=read_adc();
   delay_ms(100);
}

i'd like to connect an LED to the output to have it blink in different freq according to the tilt of the breadboard. i saw the following code in a guide and i'd like to know how to convert it correctly to my use
Code:
while(1)
{
   //Flash At The Determined Rate
PORTB = 0x00;
Delay1KTCYx(flash);
PORTB = 0x01;
Delay1KTCYx(flash);
   //Get A/D Converted Value
Delay10TCYx(5);
ConvertADC();
while( BusyADC() );
   result = ReadADC();
      //If Within Normal Range Blink Slowly
if(result > 506 && result < 518)
   flash = 200;
      //If Slightly Tilted Blink Faster
if(result > 518 && result < 550)
   flash = 100;
if(result > 545 && result < 506)
   lash = 100;
      //If Really Tilted Blink FAST!
if(result >= 550)
   flash = 25;
if(result <= 450)
   flash = 25;
}


sorry if it's too much but im really lost ><
temtronic



Joined: 01 Jul 2010
Posts: 9177
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 23, 2014 11:00 am     Reply with quote

first problem is that the PIC is a 5 volt device , and the sensor is a 3 volt device.
Did you power the sensor with 5 volts?
if so, you've probably killed it....
sigh.. you MUST read and re-read the data sheets. These days most 'sensors' are 3 volt devices and most PIC type come in 3 volt styles or some will run on 3 volt( at a lower speed). the details are in the spec sheets.

you can test the PIC though with one of the ADC examples that CCS supplies in the examples folder. I strongly suggest you get that working BEFORE proceeding any further.Maybe do that while you'rew waiting for a new sensor to arrive.

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19381

View user's profile Send private message

PostPosted: Sun Feb 23, 2014 11:22 am     Reply with quote

and the old comment. Will a 10bit value fit into an int?...
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