# PIC18F45K20 A/D Setup

#### blah2222

Joined May 3, 2010
582
Hello, I am going over Lesson 7: ADC for the PICKIT3 Debug Express demo board (PIC18F45K20), and I am just trying to understand how the timing for the ADC conversion works.

They tell you to choose a TAD and TACQ, but how can you tell how long the whole conversion will take, including attributed delays?

The reason I ask is because I am wanting to read from an instrumentation amplifier which holds the value for tens of microseconds and I want to make sure that I am not taking too long to sample and convert.

Can anyone tell me how to calculate the whole time conversion?

Thanks,
JP

**MCLRE = OFF***

Last edited:

#### ErnieM

Joined Apr 24, 2011
7,991
You should spend some time reading section 19 of the PIC18F45K20 Data Sheet. Table 19.1 you pick the conversion clock to get a Tad between 1 to 4 uS, and a conversion takes (by figure 19.4) something like 4 + 12 Tad cycles, or between 16 and 58 uS.

10mS is an eternity compared to the whole conversion process.

#### blah2222

Joined May 3, 2010
582
Okay for instance, their example Figure 19-4, they set TACQ = 4 TAD.

So the TAD is the A/D clock rate. The voltage to be converted is given 4 TAD cycles to be stored onto the holding capacitors? From then, the holding capacitors are disconnected and the conversion goes on bit by bit for 11 TAD cycles and then finally the capacitors fully discharge for 2 TAD cycles. Then it starts up again with no delay?

Did I get that example down correctly?

#### ErnieM

Joined Apr 24, 2011
7,991
Yes I believe you did.

#### blah2222

Joined May 3, 2010
582
Cool! Okay, so I guess one other thing that is getting me confused. The voltages that I am dealing with are negative from the amplifier IC. How does that work when converting with the A/D?

Also, I am trying to follow what is happening with the circuit in Figure 19-5, but I am not sure how the signal isn't lost and if the diodes are tunnel or zeners. The 1/2 LSB thing is tripping me up for how to calculate the appropriate TACQ as well.

Thanks again!

Last edited:

#### ErnieM

Joined Apr 24, 2011
7,991
Well... don't do that.

TABLE 26-24: A/D CONVERTER CHARACTERISTICS: PIC18F2XK20/4XK20
Rich (BB code):
Param No.     Symbol    Characteristic         Min     Typ     Max         Units
A21         VREFH    Reference Voltage High     VDD/2          VDD + 0.3     V
A22         VREFL    Reference Voltage Low     VSS          0.3V  VDD/2     V
(Not bad tabulation, not perfect but...)

The A2D is only intended for positive voltages. Ground (Vss) to the + rail Vdd. You can up the lower voltage to Vdd/2, or lower the higher voltage to the same, but both are within the rails of the device.

So invert the last amplifier stage and get a positive signal.

TACQ is the time that the cap is given to change. The "1/2 LSB" of EQUATION 19-1 is there as a limit of how close you have to charge the cap: once it is withing 1/2 LSB then it need be no closer as to not affect the conversion result.

The result of their calculation is based on a 10K source impedance (which is also their recommended max), other terms boost total resistance to just 11.7K (1oK dominates). If you are driving direct from the op amp output you have a zero source Z and a net resistance of only 1.7K, so you don't need such a very long TACQ. The 7.45μs they show is the max time you should ever need.

By setting the ACQT bits you get a built-in "soak" time when hitting the A2D GO bit. Saves you the trouble of inserting a software delay there.

#### blah2222

Joined May 3, 2010
582
Well... don't do that.

TABLE 26-24: A/D CONVERTER CHARACTERISTICS: PIC18F2XK20/4XK20
Rich (BB code):
Param No.     Symbol    Characteristic         Min     Typ     Max         Units
A21         VREFH    Reference Voltage High     VDD/2          VDD + 0.3     V
A22         VREFL    Reference Voltage Low     VSS          0.3V  VDD/2     V
(Not bad tabulation, not perfect but...)

The A2D is only intended for positive voltages. Ground (Vss) to the + rail Vdd. You can up the lower voltage to Vdd/2, or lower the higher voltage to the same, but both are within the rails of the device.

So invert the last amplifier stage and get a positive signal.

TACQ is the time that the cap is given to change. The "1/2 LSB" of EQUATION 19-1 is there as a limit of how close you have to charge the cap: once it is withing 1/2 LSB then it need be no closer as to not affect the conversion result.

The result of their calculation is based on a 10K source impedance (which is also their recommended max), other terms boost total resistance to just 11.7K (1oK dominates). If you are driving direct from the op amp output you have a zero source Z and a net resistance of only 1.7K, so you don't need such a very long TACQ. The 7.45μs they show is the max time you should ever need.

By setting the ACQT bits you get a built-in "soak" time when hitting the A2D GO bit. Saves you the trouble of inserting a software delay there.
Awesome! That really cleared up the confusion. Again, ErnieM saves the day.

Just curious, did you gain experience with PICs through hobbies or through work?

#### ErnieM

Joined Apr 24, 2011
7,991
Well, both kinda.

I started playing with PICs on my own somewhere in the mid 90's. Back then they only came with EPROM mamory so you needed a UV light to reprogram them. No ICSP, no in circuit debugging. I made a parallel device programmer and started hacking at them as they looked really useful, and MPLAB was a freebie!

My first project got stuck inside a test fixture to make some timing delays. I already had one made from timers (never a 555!) but this was a 1-chip (plus a resonator, no internal RC then yet either) replacement.

That fixture was for a timing board which went into some military relays. It was a "build to print" job, meaning the customer supplied us the design, and the design was terrible! A double sided ceramic board with printed resistors and both gold and silver traces, some of it was "chip and wire," bare IC's wirebonded down and covered in epoxy, some was solder SMD, some parts went on the back side too. There were some 55 different versions of the board depending on what the time delay was and what other features were needed.

I looked at it and swore a mighty oath to redesign it! I got it down to a single sided board, SMD a PIC and an op amp and a few resistors, and you built the same board for everything and just programmed for the time and other features. But... we had to make their version work first so mine got shoved to the side, and I left the company before I could complete it in a layoff.

So at my next job they had a circuit the size of a credit card running a sensor the size of an ice cube. 7 or 8 IC's, random CMOS stuff, not one part designed after 1980. All their standard products used it too. After another oath and over the objections of my supervisor I built a PIC version after hours to demo the concept. It was half their board (3MHz 60dB amp) and my PIC running the show. After I showed that to him (it worked just fine) I got real approval to continue on company time. It took some effort to redesign that amp as I needed something smaller.

The final version used 2 chips, was the size of a postage stamp, and fit up into the sensor. No longer a separate board.

Yippie. Got a raise after that one.

Designed a couple of other PIC projects for them while I was there too. Seems the PICs got better and features got added at a pace I could keep up with them.

Left that place about 2006, again their choice as this was a tiny company owned by a big company and the two main managers are complete tools and didn't like anyone on the staff smarter then they were... and my old boss found I was looking for a job and hired me back at near twice my former salary.

Guess what job they drop me into? Yep, timers! PICs were better, I was better so I designed it again from scratch. Getting a delay timer in a PIC is simple, but this also needed an external resistor (0 to 900K) to set the time from 1 to 10 times the nominal. That took me a few iterations to work out as I was resisting using an op amp over a dual transistor. The op amp won when I found some from Microchip for about a quarter that worked rather well for this.

Along the way I had discovered alphanumeric LCD displays and found them useful even for just debugging. When I had to make the test fixture for the timers I started out with an alphanumeric display but the first inexpensive graphic touch screen ($26!) came my way and I used that in the final fixture. Previous fixture only did 1 test (the time) on 10 units, had a switch for every device 4 thumbnail switched for the time, and a few other switches too. My replacement fixture had an ON/OFF switch and a touchscreen, and made several voltage and current measurements too. 11 PICs in there, 1 master and a slave for each unit being tested. Did a few other jobs with PICs here, but nothing hit production yet. One is a LED controller so you can run a LED off 10 to 28VDC and have it be the same intensity as the incandescent bulb it will replace. Next they want a 10 segment bar graph display with similar intensity control but 1.5-5V supply. Another is an isolated solid state relay, 2 PICs in there, one running a transformer to do isloated power (there are dedicated chips for this but they drew too much quiescent power took too much) plus another PIC that controls a FET, reads the current and can do an instant trip or an I-squared-t type trip too. Today I have discovered the joys of color LCD graphic displays, and have 1$150 kit on my desk as I type this I can make sing (literally, it can play wav files) and sorta dance (there is a demo of 15 FPS video but I have no need for that). Microchip's graphic libraries let me make some cool stuff on it. I am just beginning with it, do have a boot loader so I can make it load apps from the SD card on it, plus a "Program Manager" app that lets you pick the next app while giving you USB access to the SD card so you can update things without removing the card.

#### blah2222

Joined May 3, 2010
582
I actually did read your whole post, and my do you have a colourful experience with microcontrollers. That is the kind of stuff that I would really like to get into job-wise, and there are a number of projects that I would like to get started on once I really nail the operation of this PIC18. Thanks for sharing!

At the moment, I am in university for EE and am working as a Research Assistant. Judging by my other threads you might be able to get a feel for what my project is about, but since I haven't taken the Intro to Microcontrollers course yet, I've had to pretty much just learn everything from scratch. Good for when it comes to taking the course, but at the moment it eats away at the amount of design time I have.

I am a heck of a lot more comfortable with ADC and timing with your help, but I have yet to cover Interrupts or UART which eventually I need to learn because I want to have my PCB linked up through USB to send data to MATLAB... Hrm, take it piece by piece I guess... lol

#### ErnieM

Joined Apr 24, 2011
7,991
Actually, hooking up a PIC18F45K20 thru the USB to the computer isn't very hard at all. Microchip has a sample app called "USB Device - CDC - Serial Emulator" that makes the USB look like a serial port to any program, and drives the UART to boot.

Lots of great code to explore there. I've used it to be the core code of a IEEE-488 bus controller (an old but not obsolete standard).

USB is simple if you can reuse one of their examples.

You do know Microchip gives away nice C compilers, right?

#### blah2222

Joined May 3, 2010
582
Actually, hooking up a PIC18F45K20 thru the USB to the computer isn't very hard at all. Microchip has a sample app called "USB Device - CDC - Serial Emulator" that makes the USB look like a serial port to any program, and drives the UART to boot.

Lots of great code to explore there. I've used it to be the core code of a IEEE-488 bus controller (an old but not obsolete standard).

USB is simple if you can reuse one of their examples.

You do know Microchip gives away nice C compilers, right?
Whoa, that's awesome, I'd love to check that example out. That's exactly what I've been looking for haha!

I know of the different C compilers but I am hesitant to use the Hi-Tech C or C30 because I am just becoming comfortable with the C18 compiler and I have no idea when to use the others or if they are compatible with C18. I would eventually like to learn Assembly programming as well.

#### blah2222

Joined May 3, 2010
582
From what I have seen there are four C compilers offered by Microchip, and correct me if I am wrong but here are the compatibilities:

C18 = PIC18
C30 = dsPIC30F, dsPIC33E, dsPIC33F, PIC24E, PIC24F, and PIC24H
C32 = PIC32
HI-TECH C = dsPIC3x, PIC10, PIC12, PIC16, PIC18, PIC24, and PIC32

I have also heard of PIC14s and PIC17s, are they obsolete now? When would you use one compiler over another, if you have a choice for your PIC?

#### ErnieM

Joined Apr 24, 2011
7,991
You need to use a compiler for your target. Can't use C18 on PIC32's.

I've used the C18 with 18F devices and the C32 with PIC32 devices.

For baseline devices I use the SourceBoost BoostC compiler since when I started with it the Microchip offering did not have anything that included my target. I never used the HiTech compiler.

AFAIK the PIC14's and 17's are EPROM based (one E), meaning one time program unless you buy it in the package with a window for UV erasure. Basically now obsolete but good ole Microchip keeps supporting their legacy devices.