PDA

View Full Version : 18F1220 A/D Converter



BobEdge
- 26th May 2011, 16:07
Hi,

I am struggling to use the 18F1220 A/D converter. I know the answer is RTFDS, but I have, & I cant figure out why it's not working. I have managed to use the A/D on other chips with no problems. Here is how I have set it up:-

'channel 0 analog all others digital
ADCON0 = %00000001
ADCON1 = %11111110
ADCON2 = %10000111
define adc_bits 10
define adc_sampleus 50

Anyone know what I'm doing wrong?

Regards
Bob...

mtripoli
- 26th May 2011, 17:34
I'll take a couple of shots in the dark... don't be offended...

You know for certain that the TRIS is all set correctly? You only show the ADCON registers...

You have ADCS2:ADCS0 set for "000". If I'm reading the data sheet correctly the maximum device frequency is 1.25MHz. If you're using, say a 4Mhz. clock these have to be set to "001"...

You're not using an "LF" version? Numbers are different for these...

Are you using 8 bits or 10? You have the result right shifted...

Edit: Looking again it also says under Table 17-1 when using the ADC RC clock:


3: For device frequencies above 1 MHz, the device must be in Sleep for the entire conversion or the A/D
accuracy may be out of specification.

BobEdge
- 27th May 2011, 13:06
Hi,

Thanks for your reply.

I am using a 20MHz resonator, but I have ADCS2:ADCS0 set to 111, internal RC oscillator.
I am also wanting a 10 bit result so I think it should be right justified.

I have tried setting TRISA = 255, but this made no difference.

There must be something else I am missing.

Regards
Bob...

aratti
- 27th May 2011, 13:38
The following is the setting I used last time for reading three ADC channels .



CMCON = 7 ' comparator off

DEFINE OSC 32

PortA = 0 ' set portA all zero
PortB = 0 ' set portB all zero
PortC = 0 ' set portC all zero

TrisA = %00101111 ' set portA as Inp/Outputs
TrisB = %00000000 ' set portB as inputs
TrisC = %10000000 ' set portC as inputs/Outputs

'--------------------------------------------------------------------------
' set ADC registers
'--------------------------------------------------------------------------
ADCON1 = %00001010 'Vref- = Vss + Vref+ = Vdd + An0 to An4 analog rest Digital
ADCON2 = %10100010 'R.Justified + 8 TAD + FOSC/32



Then for reading ADC I don't use the PBP instruction but the following code:



Chan = Chan + 1
If Chan > 4 then Chan = 0

'if Chan = 0 then gosub Adjust_S_Point ' Adjust setpoint with external temp.

ADCON0 = AdcN[Chan]
ADCON0.0 = true ' ADC on
ADCON0.1 = True ' GoDone True
while ADCON0.1 = true
wend
ADCON0.0 = False ' ADC off
ADvalue.lowbyte = ADRESL
ADvalue.highbyte = ADRESH



See if you can use it.

Cheers

Al.

BobEdge
- 27th May 2011, 15:02
Hi,

I'm not sure what chip you were using here, but I dont think it was the 1220, I cant find any refference to CMCON in the data sheet.

Bob.

mtripoli
- 27th May 2011, 15:41
Well, I'm intrigued. This has also raised some questions as to my ability to read a data sheet...

Going back to Table 17-1 in the datasheet. MC refers to "TAD vs. DEVICE OPERATING FREQUENCY". In this case I assume "DEVICE" to be the IC. If this is the case then with ADCS2:ADCS0 set to "111" then the max you can clock the part at is 1 MHz. Am I missing something here?

In ADCON2 you have ACQT2:ACQT0 set to "000" = 0TAD. Is this correct? I'd have a look at section "17.3 Selecting and Configuring Automatic Acquisition Time"

There is are two notes regarding using Frc as the clock and putting the part to SLEEP before starting a conversion. There was the note about the accuracy may be off if not put to sleep but unclear to me what happens if you don't.

When you say it's not working, what exactly do you mean? The result is wrong, or there is no conversion at all? You may already do these things, but when I'm testing a section of code I'll do things like blink an led after a conversion and such just to make sure that the routine is even running...

What is the source impedance? Max is 2.5K...

I agree with Al in the way that he handles the Go/Done bits and not use the defines...

Lastly; what versions of PBP and MPLAB are you using? I recently was having a problem with a program and knew the program was correct but wouldn't run. For giggles I upgraded both PBP 2.60A and MPLAB to the latest and low and behold it started working. I wish I hadn't done them both at the same time so I knew which one it was (frankly I don't care) but it solved the problem.

aratti
- 27th May 2011, 15:47
I'm not sure what chip you were using here, but I dont think it was the 1220, I cant find any refference to CMCON in the data sheet.Just to complete the information, I was using a 18F2620.

You should ignore the CMCON register since 18F1220 has no comparators.

Al.

Charles Linquis
- 27th May 2011, 16:50
I can't tell you if your chip behaves the same as mine, but I use a lot of 18F2321's and I found that ADCIN didn't work.

Try this.




ADCON0 = %00000001 ; Turn it on
ADCON1 = %00010011 ; Chan 0 -11, Vref, GND (note that I use a precision reference on AN3)
ADCON2 = %10001001 ; Rt jus, 2Tad, /8 clk


Main:
Chan = 4 ;Pick the channel you want to read
NumSamples = 10 ; Do multiple reads for averaging or scaling
Gosub DOADC
; resut is in ADCSTOR (WORD)
GoTo Main


.....

DOADC:

ADCSTOR = 0
For X = 1 to NumSamples
ADCON0 = ((Chan << 2) | %00000011)
While ADCON0.1:WEND
ADCVAR.highbyte = ADRESH
ADCVAR.lowbyte = ADRESL
ADCSTOR = ADCSTOR + ADCVAR
Pauseus 20
Next X

Return

BobEdge
- 31st May 2011, 16:50
Well I have it working now, thanks for all of your help.

I decided to take PBP out of the equation. I no am using:

TRISA = 255
ADCON0 = %00000001 'Vref = Vss Vdd, turn on A/D
ADCON1 = %11111110 'Only Channel 0 analog
ADCON2 = %10001010 'Right justified, 2 TAD, TOSC/32 (for 20MHz)

For the A/D conversion:

ADCON0.1 = 1 'Start A/D conversion
while ADCON0.1 = 1 'Wait for it to finish
wend
adresult.highbyte = ADRESH
adresult.lowbyte = ADRESL

This works a treat. Now on to the next problem, trying to get the thiristors firing in the correct way.

Regards
Bob...