View previous topic :: View next topic |
Author |
Message |
moryoav
Joined: 23 Feb 2014 Posts: 35
|
accelerometer adc |
Posted: Sun Feb 23, 2014 9:43 am |
|
|
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 >< |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9177 Location: Greensville,Ontario
|
|
Posted: Sun Feb 23, 2014 11:00 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19381
|
|
Posted: Sun Feb 23, 2014 11:22 am |
|
|
and the old comment. Will a 10bit value fit into an int?... |
|
![](templates/subSilver/images/spacer.gif) |
|