- Published: Saturday, 04 April 2009
- Written by Digital DIY
- Hits: 9935
This Analogue to Digital (ADC) PIC micro example is not only great for learning how to use ADC with Swordfish, but also to learn how a Function works. The Function will perform the required task/commands and then return with a Word variable called Result. you don't need to declare the variable "Result", as its the sole purpose of using a Function.
In this case, the Function is called "ADInAsVolt". It gets the ADC value on channel 0, and converts it to volts, and stores it in the Functions Result register. Just because the Function is at the start of the program does not mean that it is executed at the start - the first line to be executed is "ADCON1 = $07", and the program follows on from there. The command ADVal = ADInAsVolt instructs the program to call the function, and store the result in ADVal. Handy!
Voltages over 5V
For voltages that exceed 5V that you wish to sample, simply use a voltage divider network like the following;
For best ADC performance of the PIC micro, the input impedance should not exceed 2.5K as the PIC's internal capacitors will take too long too charge/discharge. This isn't really an issue for most projects though, as you can just change the sample time with a couple of settings - see the help file of your compiler for more information. I generally use 10K resistors without any issues.
Using a PIC Datasheet to initialize ADC settings
- Explores the different settings for the 18F452 that setup the ADC settings
18F452 ADC Example
- Simulation of the code and circuit shown throughout the above example.