View previous topic :: View next topic |
Author |
Message |
Martin_P
Joined: 23 Jul 2013 Posts: 8
|
Help with ADC |
Posted: Mon Sep 09, 2013 8:36 am |
|
|
Hello
I'm trying to work with the ADC now but I have some problems.
I want that a LED lights if the value is under half. But the LED lights allways. So I hope that someone can help me.
I use the dsPIC33FJ256MC710A, the PCD compiler from CCS and the dsPICDEM MCLV development boar.
Code: |
#include <33FJ256MC710A.h>
#include <stdio.h>
#include <stdlib.h>
#fuses XT,NOWDT
#use delay(clock=8000000)
#define LED PIN_E1
void main()
{
int x;
set_tris_e(0); //LED output
set_tris_b(1); //input
setup_adc_ports(sAN8);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_ms(10);
while(1)
{
x=read_adc();
if(x<511)
{
output_high(LED);
}
else
{
output_low(LED);
}
delay_ms(500);
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Mon Sep 09, 2013 8:52 am |
|
|
two comments
1) normally you don't use adc_clock_internal, check the datashet for when you should...
2) what's the resolution of the ADC ? Normally you have to specify how many bits( 8, 10, 12 depending on the PIC type)
also confirm that your ADC source is 'active' and not 'stuck' either high or low.
hmm..
setup_adc_ports(sAN8);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
you've setup AN8 but select channel 0 .....
again , check the datasheet, but that just looks wrong...
I'd think set_adc_channel(8); might be right...
hth
jay |
|
|
Martin_P
Joined: 23 Jul 2013 Posts: 8
|
|
Posted: Mon Sep 09, 2013 9:00 am |
|
|
thank you
that was the mistake |
|
|
|