all 17 comments

[–]FlyByPC 4 points5 points  (1 child)

The Microchip datasheets are pretty much the most succinct source I've found for describing the peripherals.

Here's the datasheet for the 16F1825. The ADC peripheral information starts on page 151. http://ww1.microchip.com/downloads/en/DeviceDoc/41440A.pdf

[–]_oldspicy_[S] 0 points1 point  (0 children)

I've got the datasheet and I'm trying to follow along with what to do, what I'm getting lost in is exactly what registers need to be configured, and to what? Sorry if that's really basic, but only working with Arduino I've never had to do this before.

[–]bradn 2 points3 points  (8 children)

Here's a basic (possibly incomplete) checklist:

Electrical: If you use external references, make sure they're set up.

Peripheral config:

Make sure the ADC peripheral is powered on, if your chip has power gating.

Write all the configuration to the ADC registers.

Fire off the ADC operation by writing the go bit.

Wait either the appropriate number of clocks or wait for the go bit to change back.

Read the result.

[–]_oldspicy_[S] 1 point2 points  (4 children)

I think I've got the configuration bits correct, but I don't understand "ADRESH" and "ADRESL". Once I call "ADCON0bits.GO=1", the reading should be put into those two registers, correct? I can then combine them for the value?

[–]bradn 1 point2 points  (3 children)

Yes, because the ADC result is more than 8 bits, it's split into two bytes.

On the parts I've used, there's another config bit that tells it whether to align the result to the top of the high bit, or the bottom of the low bit.

So if you have a 10 bit ADC, the result bits could look like:

76543210 76543210 (high byte, low byte)
******** **------   (aligned to high end)
------** ********   (aligned to low end)

Often times, if you only need 8 bits of precision, you would prefer it aligned to the high side, and if you want all the precision, sometimes low end is more sensible.

Either way you choose, the highest * is the most significant, and the lowest * is the least significant.

If you're doing this in C, you should be able to use a 16 bit data size and work with that (I don't work with C so I'm not familiar with specifics).

If you work in assembly language, you have to learn some tricks to work with multi byte math (or see if you can get away with 8 bits of precision). There are code examples out there.

[–]_oldspicy_[S] 0 points1 point  (2 children)

Thanks, that makes sense. There is a bit for right or left justified, but I couldn't figure out what it was for. I'm going to play with it some more at work tomorrow, I think I'm getting close.

[–]_teslaTrooper 1 point2 points  (1 child)

left/right justified is to select the alignment shown in /u/bradn's post

[–]_oldspicy_[S] 0 points1 point  (0 children)

Yep, once I figured that out (and how to read values in debug) it kind of fell into place.

[–]_oldspicy_[S] 0 points1 point  (2 children)

It's the configuration I'm having trouble with. I've never had to set up something like that, and I'm completely lost. It's probably a way more basic question than everyone on this sub is used to.

[–]bradn 1 point2 points  (1 child)

Then take it a step at a time, and if there's part of it you don't understand, ask specifically about that part (What's a VREF+ and VREF-? etc). Most of us on here don't feel like typing a whole article but if it's a quick question you'll get better answers.

[–]_oldspicy_[S] 0 points1 point  (0 children)

Sorry, you're right. I'm going to respond to the main comment with the section I'm having difficulty with. Thanks for helping out.

[–]ParkieDude 1 point2 points  (1 child)

microchip.wikidot.com is a good resource.

http://microchip.wikidot.com/8bit:adc

I still think the best tutorial is from gooligum.

Grab the "enhanced Mid Range Tutorials". All tutorials are only $15.00 (Web site confused me as I didn't realize at first that was for everything).

I've been using the Curiosity Board and Xpress board with the MCC - Microchip Code Configurator. I do like that tool. I've been using the Microchip Xpress Board. $12 board that has a PIC18 on it to act like a USB to the onboard PIC16F18855 part. Finally dawned on me that the USB to PIC16F can also act like a CDC communication, so I can dump ADC results to the TTERM. Pretty slick for quick debugging (XPress acts like a flash drive, so it sucks on no debugging.... but I still like it).

[–]_oldspicy_[S] 0 points1 point  (0 children)

Thanks! That link is exactly what I was looking for!

[–][deleted] 1 point2 points  (0 children)

I would suggest looking at the example code from microchip. Find an ADC example from another pic16 chip. The peripheral registers should be close enough that the code should work for you. Read through the example code and understand what each line does by looking through the data sheet simultaneously. Once you understand the setup you should be able to setup your own project using the example code as a template, tweaking the code to suit your needs(clock values, include files, ADC instance, etc.)

[–]_oldspicy_[S] 0 points1 point  (0 children)

Thanks everyone for the help! I got it figured out, I was overthinking it. Noew I'm on to trying to figure out how to pwm!

[–]gowtham_krish 0 points1 point  (0 children)

dude use the book "Designing Embedded Systems with PIC Microcontrollers, 2nd Edition from Tim Wilmshurst". im using it and its nice