Saturday, 13 July 2013

PIC24, DsPIC33 and DsPIC30 (16bit PIC microcontoller) ADC module working code with Proteus Simulation.

This post will provide working code for built-in hardware ADC (Analog to Digital Conversion) module for PIC24, DsPIC33 and DsPIC30 with Proteus Simulation. You may download code and Proteus Simulation from the download section at the bottom of this post. I am using MPLAB X IDE, C30 Compiler and Proteus Simulation on v8.0 This code will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing the code.

In this post I supposed that you know following things if not then please go through that post before proceeding with this.

Proteus Simulation:

            In the following figure 1. I used a variable resistor to change the voltage applied to Channel 0 of ADC. And the value that ADC measured is displayed on LCD.

Figure 1. Proteus circuit

In the following figure 2. When I change value of voltage at channel 0 of ADC with the help of variable resistor. The potential divider at this pin has value measured by the voltage probe and ADC is perfectly same which means that our ADC is working well. For example in case of ADC value 511 the voltage probe reads 2.5volts (when variable resistor is at 50% position). So the controller I am using in this particular example has 10bit ADC and I use internal voltage reference which is 5 volts. So the voltage at channel zero is given by this formula voltage = (ADCvalue/ADC resolution) * Vref. Which is 2.495=(511/1024)*5volts. You may verify this by changing variable resistor value.

Figure 2. Proteus Simulation.


In the following figure 3. This is main function in which I first initialize LCD, made PIN RA0 as input for ADC, Configure ADC module and selecting the channel as “Channel 0”. And a loop which clear LCD screen, write a string to LCD “ADC Value: ” then we have ADC module measured value in “ADC_Value” variable which then displayed on LCD by passing it to a function WriteIntegerToLCD(ADC_Value).

Figure 3. Main Function.

In the following figure 4. We have ConfigModuleADC() which configure ADC module in following steps. For more details see datasheet.

  • ADC module OFF.
  • ADC clock is derived from system clock.
  • Auto sample Time at 31 TAD.
  • Conversion clock which is Tcy/2. (2.5MHz in this case)
  • Scaning inputs OFF.
  • Filling buffer 0.
  • Interrupt at completion of sampling and conversion. But I did not enabled ADC interrupt.
  • One 16bit buffer.
  • No alternate settings (MUX A).
  • Continue module in idle mode.
  • Data output type I selected integer type.
  • Conversion trigger at SAMP bit.
  • Sampling begins by setting SAMP bit.

Figure 4. ADC Configure.

In the following figure 5. We have channel selecting function as ChangeChannelADC(unsigned char CHANNEL) passing channel will set the sampling channel for ADC module. I this case I am using channel 0. You may use any other channel by using this function.

Figure 5. ADC Channel select.

In the following figure 6. We have a function which will return a value of ADC measured by ADC module named SamplingAndConversionADC() in this by setting SAMP bit ADC will start sampling. After which sampling is stopped by clearing this bit. After this ADC will start converting data into digital and when data is converted done bit is set. And this function will return measured value.

Figure 6. ADC Sampling and Conversion.

Reading Suggestions:


You can download Code (MPLAB X IDE and C30 compiler) and Proteus (v8) Simulation Click here

For all topics Click here.


All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 
© Copyright 2013 IbrahimLabs. All rights reserved.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...