View previous topic :: View next topic |
Author |
Message |
digitalfreak
Joined: 16 Feb 2005 Posts: 25
|
ADC question |
Posted: Tue Jul 18, 2006 11:30 am |
|
|
Hi,
I am trying to read an analog signal from a pressure sensor which goes from 1.2V to 2.8V.
Based on which Iam trying turn off and on a stepper motor with a PWM signal, just to test the RA0 and my program.
I couldnot make the code work right. The motor is always ON, no matter what the voltage is at the RA0.
When I checked with multimeter at the RA0 the volatge actually goes from 1.2V to 2.8V. The signal is pretty much filtered using simple RC filter.
This was just a test program for RA0, The hardware and the program was all running good for few of my previous codes which doesnot have any Analog Ins... My feeling is I am messing up with my ADC calculations?
If am successfull with this I have to use few more similar analog sensors, any suggestions regarding coding will be greatly appreciated
Thanks,
Code: |
#include <18F452>
#device adc=10
#use delay(clock=20000000, restart_wdt)
#fuses HS, PUT, BROWNOUT, NOWDT, NOLVP, NOCPD, NOPROTECT, NOWRT
#include <math>
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
void main(void) {
int16 k=0;
float Value=0.0, ValueAvg=0.0, adcValue=0.0;
set_tris_a(0b11111111);
set_tris_b(0b11111111);
set_tris_c(0b11000001);
set_tris_d(0b11110000);
set_tris_e(0b00000000);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 99, 1);
set_pwm1_duty(0);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(RA0_ANALOG);
output_high(PIN_D0);
set_adc_channel(0);
delay_ms(10);
while(TRUE){
for (k=0;k<11>= 2.0){
delay_ms(100);
set_pwm1_duty(0);
delay_ms(10);
}
else{
set_pwm1_duty(50);
}
}
}
|
Last edited by digitalfreak on Tue Jul 18, 2006 1:40 pm; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
digitalfreak
Joined: 16 Feb 2005 Posts: 25
|
|
Posted: Tue Jul 18, 2006 11:36 am |
|
|
sorry, for some ridiculous reason, while loop in above post got messed up and i couldnot edit it!.
here it is...
Code: |
while(TRUE){
for (k=0;k<11;k++) {
adcValue = (5.0/1024.0)*(float)read_adc();
Value = Value + adcValue;
delay_ms(5);
}
ValueAvg = Value/(float)k;
if (ValueAvg >= 2.0){
delay_ms(100);
set_pwm1_duty(0);
delay_ms(10);
}
else{
set_pwm1_duty(50);
}
}
} |
Last edited by digitalfreak on Tue Jul 18, 2006 11:43 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 18, 2006 11:41 am |
|
|
The board was re-configured in early July to enable HTML in all posts.
This is what causes anything inside angle brackets to be mangled.
You have to manually disable HTML. Below the posting window there
is a checkbox to do this. It looks like this:
Code: | X Disable HTML in this post |
|
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Jul 18, 2006 12:47 pm |
|
|
Can you put the A/D reading out a serial port, or somehow tell if the counts are reasonable? Maybe pulse a pin high for a time set by the A/D count. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
digitalfreak
Joined: 16 Feb 2005 Posts: 25
|
|
Posted: Tue Jul 18, 2006 3:58 pm |
|
|
Thanks, PCM & Sherpa.
The variables are over running, Zeroing did the trick!.
Code: |
while(TRUE){
adcValue = 0.0;
Value = 0.0;
ValueAvg = 0.0;
for (k=1;k<=10;k++) {
adcValue = (5.0/1024.0)*(float)read_adc();
Value += adcValue;
delay_ms(5);
}
ValueAvg = Value/10.0;
if (ValueAvg >= 2.5){
delay_ms(100);
output_high(DIR);
}
else{
delay_ms(100);
output_low(DIR);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
|