PDA

View Full Version : How do I read my ADC input only after the transition from a low to high occurs?



jessey
- 17th February 2006, 04:53
Hello Everyone,

I have a CMOS circuit that forms a square wave oscillator with approximately a 10/90 mark-space ratio and I'd like to be able to read the high pulses using an ADC input on my 18f452. I only want to take my AD reading after the voltage has gone from low to high each time. I don't have any problems on how to set up the ADC but I just can't think of the best way to only read the highs and ignore the lows from the transition of a low to a high signal.

I wrote some code below just off the top of my head but haven't tested it yet. Would this be a good approach to accomplishing this or is there a better way of approaching this? I'm not sure exactly what is the best way of doing this is. Maybe I should have some pauses in my While...Wend statement? The program will be running off Timer1 with a 32 KHz oscillator or I could switch it to the main oscillator while I'm reading the ADC.

Prepare_To_Read_ADC_Input:
WHILE a = 0 'A always equals 0
IF Read_Pin = 0 THEN
IF Read_Pin = 1 THEN Read_ADC_Input
ENDIF
WEND

Read_ADC_Input:
adcon1=%00000100
TRISA.0 = 1 'input to read ADC value
ADCON0 = $41 'Set A/D to Fosc/8, Channel 0, On
Pauseus 50 ' Wait for channel to setup
ADCON0.2 = 1 ' Start conversion
Pauseus 50 ' Wait for conversion
Moist = ADRESH
ADCON1 = 7 'shut off (Disable) ADC
Return

Thanks
jessey

sougata
- 17th February 2006, 08:35
Hi,

I would recommend that you tie the square wave input to the INT pin and set the AD conversion bit as soon as an interrupt occurs. Also set a software flag so that your program knows that a new sample is available. You can call this routine or just poll the software flag to update your results. However you should also introduce some delay before setting the ADON bit to ensure proper acquisition.

Regards

Sougata

jessey
- 17th February 2006, 09:52
Thanks Sougata,

I'll have to look into the manual more and try to absorb some of the information on how to do that. I've never used interrupts before so this will be a learning experience for me. After a quick look ay the manual, I take it I can connect my square wave output from the CMOS circuit to either pin INT0 (pin 33), INT1 (pin 34) or INT2 (pin 35).I guess I'll be using the PORTB Interrupt-on-Change feature.

Anyways it's getting late here so I'll have another look tomorrow evening and see what I can come up with between the manual and the archives. When I get a better understanding of what I'll have to do to use the Interrupt-on-Change feature (which will probably take me a few days but will be time well spent) then I'll come back with some questions.

Thanks Again
jessey