View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
can use analog pin for digital |
Posted: Tue Nov 23, 2010 4:56 am |
|
|
Hi,
i am using pic18f252.
i want to use analog pin as an interrupt but the signal coming to the pin is digital not analoge.
i wanna ask that can i use analog pin as an interrrupt and read the state of the pin,
i want this because all my interrupts are consumed and only analoge interrupt is left.
Thanks |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Nov 23, 2010 6:11 am |
|
|
short answer... yes
long answer ... use a high value pullup R to reduce pin current if the circuit is power sensitive( ie battery operated)
Otherwise yes, done it for years.... |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Tue Nov 23, 2010 6:33 am |
|
|
Thanks
Initially high signal is coming. I want to interrupt when signal goes high to low. Is it possible?
Can show me a sample program on how to define analog interrupt routine? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Nov 23, 2010 12:13 pm |
|
|
There are plenty of examples in the 'examples' folder of the compiler. Best to check there. Be sure to reconfigure any 'header' info to reflect the PIC that you're using as peripherals are different anongst PICs.
Obviously you have to be sure the pin you select can have an interrupt routine attched to it ! I only work with 16F series so I can't offer any real help with the 18F series, only general information. |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Fri Nov 26, 2010 1:15 am |
|
|
No such examples in the ccs folder regarding A/D interrupt,
can anyone share the code? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Nov 26, 2010 3:19 am |
|
|
No.
You can't use an analog pin to trigger an interrupt.
I think you misunderstand 'what' the analog interrupt does.
There is no automatic triggering of an 'event' depending on the input.
The ADC interrupt triggers, when the ADC conversion _completes_. Normally, the conversion takes so little time (typically about 20uSec), that the ADC interrupt is useless, since it takes longer to get into the interrupt routine, and out again, than it takes to simply wait for the reading. The only time the ADC interrupt is useful, is if you use the CCP, to trigger an automatic ADC reading at a point in time. Then the reading takes place without any code involved, and the interrupt 'fires' when the reading completes. However the interrupt will fire, whatever the voltage on the pin is.
A series of questions/comments:
1) Has your chip got a comparator as well as the ADC?. If so, _these_ pins _can_ be setup to trigger a 'level' event.
2) You talk about 'analog pins'. On most PICs, even if a pin is assigned as being available for ADC use, it can still be read/written as a digital pin. This has some slight "caveat's" with potentially introducing extra noise to the other ADC sources, but these apply if a digital signal is being connected to the pin, even if it is not being read as digital. If you have a 'timer' interrupt, why not just 'poll' the pin in this?.
3) The only way to have a 'level' event on an ADC input pin, would be to program the CCP to trigger the ADC at intervals, then read the voltage in the ADC interrupt, and compare it with your required level yourself.
With both the above, you do not get an event _when_ a voltage changes, but at up to the polling interval later.
4) If you have a CCP and timer3 (or 1 depending on the chip), on the chip not being used, you can generate an interrupt with this anyway. Simply set the 'trigger' count to be one greater than the current timer count, connect the signal to the timer input, and when it 'counts', you will get an interrupt.
5) Remember that there is no reason you can't potentially 'OR' interrupt sources together (by using open collector drives to the line). Then your code can poll the potential sources, to give you more effective interrupt sources.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Nov 26, 2010 7:02 am |
|
|
His first post says"i want to use analog pin as an interrupt but the signal coming to the pin is digital not analoge. " So it sounds like he has a valid logic level input. I think he does not realize the pin can be set up for either analog or digital. I have never worked with the PIC18F's but any PIC analog input I have ever seen can also function as a digital pin. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Nov 26, 2010 8:19 am |
|
|
Exactly my comment '2'.
This has been discussed in the past. If you setup the ADC, so that (say) pins A0 to A5, are available as analog pins, for use by the ADC, they are still readable, and writable as digital pins. There is a 'caveat', in that you can get a fractional amount of extra 'crosstalk' through the analog multiplexer, but this is going to happen if he applies a digital signal to a pin available to the multiplexer anyway, whether or not the pin is read as a digital input or not.
Problem is that he appears to want an 'event' (interrupt) to occur on a change on the signal. This is not possible, using either the ADC, or the pin as a digital input, unless he happens to have available a timer input, or CCP pin (both of which can be 'bodged' to give an interrupt event....).
It really would help if the original poster said:
What PIC
What pins are in use and immovable.
Frequency of the incoming signal.
Latency acceptable.
What other features are needed.
What clock frequency.
What interrupts are in use.
etc. etc..
Best Wishes |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Fri Nov 26, 2010 11:34 pm |
|
|
Thanks guys for your replies.
Quote: |
It really would help if the original poster said:
What PIC
What pins are in use and immovable.
Frequency of the incoming signal.
Latency acceptable.
What other features are needed.
What clock frequency.
What interrupts are in use.
|
-> I am using pic18f252.
-> Almost all the pins are used (pins are left: two pins B6 and B7 for ICD but can be used, 1 analog pin).
-> The signal is not giving pulses so frequency does not exit. I want to monitor, Is the circuit is powered by battery or through power supply. Between this I have a circuit based on opto-coupler. It normally gives logic level high. When power supply is off and the microcontroller operates on battery then the logic level goes low. I want to interrupt the controller when power supply will be OFF.
-> I am using 20MHz crystal.
-> Currently I am using INT0, INT1, INT2, Timer0, Timer1 interrupts.
I have remaining 1 analog pin that is pin no 7 (RA5/AN4).
In case if I have to use CCP pin then I have option to switch that pin with pin no 7.
Now tell me what are the options, which I can use? |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Nov 27, 2010 3:00 am |
|
|
I think the intended function can be easily achieve by polling the respective input.
I didn't yet hear a plausible reason, why an interrupt should be necessary. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Nov 27, 2010 3:24 am |
|
|
Back to this 'one analog pin'. Understand _there is no such thing as an analog pin on a PIC_......
You have pins that can be _connected_ to the analog multiplexor, or to digital input latches, or to a variety of similar I/O circuits, but there is no such thing as a dedicated analog pin.
I presume you are using the ADC?.
If so, what config statement are you using for the ADC?. What Vref?.
What are you connecting to the other pins being used by the ADC?.
As FvM says, it does not sound as if you need an interrupt, you just need to poll the pin. Possibly in one of your timers, or in your main loop.
Best Wishes |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Sat Nov 27, 2010 3:47 am |
|
|
I want interrupt because whenever the power goes then microcontroller stop doing all the functions immediately and indicates that microcontroller is on battery.
I am using analog pins as a digital not as a analog.
I thought i have one pin left which is analog and i can use it as an interrupt,that's why i asked you guys Can i use that pin as an interrupt. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Nov 27, 2010 5:06 am |
|
|
No, you cannot
As has been explained several times now, the _only_ sources that can generate 'change' interrupts, are:
1) The interrupt pins.
2) Interrupt on change.
3) Comparator inputs.
4) Timer input pins
5) CCP pins.
If you are not using analog inputs at all, why on earth do you keep talking about 'analog pins'?.
Port A5, has _no_ ability to generate an interrupt.
Are you actually using the ICD?.
If not, then you have interrupt on change available on the top four pins of portB. So long as nothing too fast is going on with the other pins in this 'set', then you can just poll when the interrupt occurs, and return if the pin is not the one you want. So:
Code: |
static int8 old_port; //preload this with the value of PORTB in the main
//before enabling the interrupt.
#define MASK (0x80) //For whatever pins in port B you want -B7 here
#INT_RB
void input_changed(void) {
int8 temp;
temp=input_b();
if ((old_port ^ temp) & MASK) {
//Here B7 has changed
if (temp & MASK) {
//Here B7 has gone high - test for zero if you want low
//Code here
}
}
old_port=temp;
}
|
This will be called if any input pin in the top four pins of PortB changes, but will only execute the section at 'Code here', for the one defined by MASK.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Nov 27, 2010 9:02 am |
|
|
Quote: | I want interrupt because whenever the power goes then microcontroller stop doing all the functions immediately and indicates that microcontroller is on battery. |
Normally, a microcontroller can act "immediately" (within a few milliseconds) without using interrupts, assuming a reasonable organisation of the application. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat Nov 27, 2010 9:27 am |
|
|
For what you are doing, I think you might want to look at the HLVD feature of the PIC (if it has one) or use one that does.
There's the HLVD that can monitor a range of voltage levels and trigger an interrupt on crossing a threshold.
Normally the source is Vdd, so no external pins are needed.. but the PIC can monitor input voltage and do exactly what you describe you want.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|