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

ADC problem ... that burned my sensor

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



Joined: 13 Sep 2007
Posts: 14

View user's profile Send private message

ADC problem ... that burned my sensor
PostPosted: Thu Sep 20, 2007 6:26 am     Reply with quote

Hi all, i have a small problem on a adc issue. I have a proximity sensor(sharp gp2d12) and i tried to make a simple adc conversion, something like when an object is in front of my sensor led should turn on. My code is:



#include <16F877.h>
#device adc=10
#fuses XT // Oscillator mode
#fuses NOLVP, NOWDT // No Low Voltage Program, No Watchdog timer
#fuses NOPROTECT // Code no protection
#use delay(clock=20000000)
#use fast_io(D)


void main()
{
long c;
setup_port_a( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
setup_adc_ports(ra0_analog);

setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);


set_tris_b(0x00);
set_tris_d(0x00);

output_high(pin_d0); //leds are off
output_high(pin_d1);
output_high(pin_d2);
output_high(pin_d3);
output_high(pin_d4);
while(1){
c=read_adc();
delay_m(1);
delay_ms(150);
if(c<150){
output_high(pin_d0); //leds are off
output_high(pin_d1);
output_high(pin_d2);
output_high(pin_d3);
output_high(pin_d4);
}
if(c>150){
output_low(pin_d0); //leds are on
output_low(pin_d1);
output_low(pin_d2);
output_low(pin_d3);
output_low(pin_d4);
}

}
}


Ok.The adc works because i put a variable voltage on RA0 and the leds ar doing what they should, BUT the problem is that the input pin A0 is outputing a 5V voltage and this ruined my sensor.
From what i know an input pin shouldn`t output anything !!!!

Please help me with this small issue.
Best regards,
Vodka
Guest








PostPosted: Thu Sep 20, 2007 9:00 am     Reply with quote

Question...

If you put a pot on RA0 and you can move the voltage on RA0 around (as I think you said), how can RA0 also be stuck at +5V?

Steve H.
Ttelmah
Guest







PostPosted: Thu Sep 20, 2007 9:14 am     Reply with quote

There are lots of faults here:
1) XT oscillator, is not legitimate for 20MHz operation.
2) setup_port_a, and setup_adc_ports, are the _same_ function. This is what 'aka' means (also known as). You cannot set the port to be 'all_analog', and also only have an0 as analog...
3) There are some typing errors (no such thing as 'delay_m'. So, what you have posted, is not the running code, making it impossible to be sure that you have not done something else wrong...

Now, the pin won't actually output 5v (unless you have blown the input pin with your drive voltage...). The fact that applying a variable voltage to it, gives correct operation, suggests this has not happened. The input presents a load to a capacitor inside the chip. If this capacitor has already been charged to 5v, then attach a device to it, and the device needs to be able to discharge this capacitor, and will initially 'see' the 5v on this capacitor. The capacitance though is relatively low. The GP2D12, can drive the PIC ADC input fine. You really want to be running using an external Vref, of about 2.5v though, otherwise the accuracy will be 'pits', since the sensor gives a fairly non-linear output.
If you have blown the sensor, it is because of a wiring error, or because you have blown the PIC's pin, not because of the code as shown.

Best Wishes
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Thu Sep 20, 2007 9:15 am     Reply with quote

Anyway (especially when testing/learning/experimenting, and no high-speed communication needed) I suggest to always use current-limiting safety resistors in series on all input pins of the pic (especially those connected to output pins of a sensitive device), so you cannot mistakenly destroy something. Same protecting resistors can be used on output pins of the PIC used for signalling only. (not driving a load) For signaling a current-limit below 1mA will be more that enough (5-10kOhms), and with such a small current even when wired or programmed wrongly, it is not likely to destroy anything.
So a programming or a wiring error (beyond the resistors) will not cost you that much.

Another good practice is to use a current limiting power supply with a current measuring display. (or a multimeter is series measuring the supply current will also do)
I see the 'normal' current an experimenting circuit takes, and set the current limit slightly above this 'normal' level. Having done that - always when I apply power to my circuit - I watch for the current limiting red led, and disconnect the wire immediately if it lights up due to a wrong wiring (or in most of the cases even conflicting ports are detectable). A slight overcurrent for a short time usually does not destroy anything. Ususally it takes up to 2-3 secs of overcurrent to destroy something, they die because of the thermal shock, so watching the current and being quick usually saves them.
Doing so I have saved the life of many little things in the past years. Smile
Vodka



Joined: 13 Sep 2007
Posts: 14

View user's profile Send private message

PostPosted: Thu Sep 20, 2007 10:12 am     Reply with quote

Thank you very much libor for you`re nice suggestions and what about Ttelmah said :
1. " XT oscillator, is not legitimate for 20MHz operation. " i don`t really understand what you tried to said there, because i saw a lot of examples with XT oscillator for 20Mhz.
2. "There are some typing errors (no such thing as 'delay_m'. ", yes it`s may bad because the code is much longer and i pull off as much as i though it whould be needed for you to understand my problem.(the code works,but if making RA0 high !!!!)

if setup_port_a and setup_adc_ports are the same functions i deleted setup_adc_ports, wiring is good, but my problem still exists!!!
Vodka



Joined: 13 Sep 2007
Posts: 14

View user's profile Send private message

PostPosted: Thu Sep 20, 2007 10:23 am     Reply with quote

[censored] my mistake, wiring is wrong !!! Thank you all and please excuse me Embarassed Embarassed Embarassed Embarassed
Ttelmah
Guest







PostPosted: Thu Sep 20, 2007 3:40 pm     Reply with quote

Glad you have found it.
XT, is only designed to work up to 4MHz. If you have seen 'lots of examples with XT, and 20MHz', then they were faulty...
XT will sometimes work at higher rates, but what is more likely to happen, is that though you use a 20MHz crystal, you end up running at an undertone, not the 20MHz rate. You can use 'XT', if you have a complete external oscillator, as an alternative to 'EC'.
HS, is the proper fuse to use if the clock is above 4MHz. Read the data sheeton this.

Best Wishes
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