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
Bookmarks