0% found this document useful (0 votes)
84 views26 pages

Welcome To Turbofuture!: Internet Computers Cell Phones Consumer Electronics

Uploaded by

Moyukh Mandal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views26 pages

Welcome To Turbofuture!: Internet Computers Cell Phones Consumer Electronics

Uploaded by

Moyukh Mandal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

INTERNET COMPUTERS CELL PHONES CONSUMER ELECTRONICS

Welcome to TurboFuture!
Welcome to TurboFuture!

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
DEC 28, 2020

How to Use ADCs in


dspic30f and dspic33f
STORMSHALTED

The author completed his final year engineering project with the dsPic micro-
controllers, gaining extensive insight in these devices.

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
dsPic30f4011 micro-controller along with its programmer.
Own work

The dsPic devices offer a powerful ADC (Analogue to Digital Converter)


module which can sample at speeds up to 1 Msps. It can be used in a variety
of different ways and modes to suit the needs of your project.

This tutorial is aimed at teaching the basics of A/D conversion modules to all
those who are beginning their projects with the dsPics.

How to Give Input to the ADC


The first step is to set up the required hardware to give input to the ADC.

Sampling a DC Signal

For initial testing, you may use a potentiometer connected across a 5 V supply
and give input to the ADC analogue pins directly.

Sampling an AC Signal With ADC


INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
To sample an AC signal (line voltages or currents), you will have to:

1. Step down the signal to the required voltage level with the help of
transformers.

2. Level-shift the signal so that negative values below zero are shifted up. The
ADCs in Pics are not able to sample negative signals.

This reference circuit design may help you with the level shifting circuits:

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Sampling an AC signal with ADC. (Gain of Op-amp may be tuned through the variable
resistor to achieve an output in the range of 0 - 5V, I however use a value of 2.7 kOhms
in its place.)(Click to enlarge).
Own Work

Testing the ADC


After setting up the required code and hardware, there must be a way to test
if the ADC is actually sampling correctly in the required way or not. Since you
cannot peek inside the MCU to see if everything is going correctly, I suggest
two ways:

1. You can connect a debugger with the Pic like the one pictured above. In
debug mode, you can check the value in registers ADCBUF0 through 10 to
INTERNET COMPUTERS CELL PHONES
see if they are being loaded with correctly sampled values.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
2. You can send the values of ADCBUF register thorough UART and monitor
them on a PC with the help of serial monitor.

ADC Example Code for dsPic


#include "xc.h"
#define FCY 20000000
#define FPWM 3600
#include <xc.h>
#include <stdio.h>
#include <delay.h>
#include <libpic30.h>
#include <dsp.h>
#include <math.h>
_FOSC( CSW_FSCM_OFF & XT_PLL8 ); // External Oscillator,
PLLx8
_FWDT( WDT_OFF ); // Watchdog timer off
_FBORPOR( MCLR_DIS ); // Disable reset
int ADCValue;
void init_ADC( void )
{
TRISB = 0xFFFF; //Set as Input Port
ADPCFG = 0x0000; //Selecting all analogue pins to
analogue mode
ADCHSbits.CH0SA = 1;
ADCHSbits.CH0NA = 0;
//ADCHSbits.CH123SA = 0;
//ADCHSbits.CH123NA = 0;
IEC0bits.ADIE = 1; //Enable ADC Interrupt
IPC2bits.ADIP = 1; //set interrupt priorty-6
ADCSSLbits.CSSL0 = 0; //Skip input scan for analoge
INTERNET COMPUTERS CELL PHONES
pin AN0,AN1,AN2
ADCSSLbits.CSSL1 = 0;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
ADCSSLbits.CSSL2 = 0;
ADCON3bits.SAMC = 0; //Auto sample time 6TAD//max
sample time
ADCON3bits.ADRC = 0; //selecting Conversion clock
source derived from system clock
ADCON3bits.ADCS = 9; //Selecting conversion clock
6Tcy
ADCON1bits.ADSIDL = 0; //Selecting continue mode
operation in idle mode
ADCON1bits.FORM = 1; //Selecting data output in
signed integer format
ADCON1bits.SSRC = 0; //Selecting Motor Control PWM
interval ends sampling and starts conversion
ADCON1bits.SIMSAM = 0; // Samples CH0, CH1, CH2, CH3
simultaneously
ADCON1bits.ASAM = 0; //Selecting Sampling begins
immediately after last conversion completes. SAMP bit is auto
set
ADCON1bits.SAMP = 0; // At least one A/D sample/hold
amplifier is sampling
ADCON2bits.VCFG = 0; //Voltage Reference
Configuration bits
ADCON2bits.CSCNA = 0; //Disable input scan
ADCON2bits.CHPS = 0; //Selecting conversion channel
CH0
ADCON2bits.SMPI = 0; //Selecting 1 conversion sample
per interrupt
ADCON2bits.ALTS = 0; //Uses MUX A input multiplexer
settings
ADCON2bits.BUFM = 0; // Buffer configured as one 16-
word buffer ADCBUF(15...0)
ADCON1bits.ADON = 1; //A/D converter is ON
}
void readADC( )
{
INTERNET COMPUTERS
//ADCON1bits.SAMP CELL
= 1; // start PHONES
sampling, automatic
conversion will follow

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
//__delay_ms( 100 );
ADCON1bits.SAMP = 0; // start sampling, automatic
conversion will follow
while ( !ADCON1bits.DONE ); // wait to complete the
conversion
ADCValue = ADCBUF0; // read the conversion result
}
void main( void )
{
init_ADC();
int data;
while( 1 )
{
readADC();
data = ADCValue;
}
}

Configuring the ADC Module


The ADC module may be initialized and configured with the help of the code
given above. A step-by-step guide follows.

Steps 1 through 7 are essential. After 7, any or all of them may be skipped.

1. Set the Port as Input and in


Analogue Mode
INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
1. Port B, on which the ADC input pins are present, must be set as an input
port. (line - 21).

2. They must be set in the analogue input mode by the ADPCFG register. (line -
22).

2. Select Analogue Inputs for


Sampling
Analogue inputs must be connected to an 'ADC channel' for sampling. There
are usually more analogue inputs than channels; for example, dsPic30f4011
has 09 analogue inputs but only 04 ADC channels. Therefore you must now
select which analogue input pin is connected to which ADC channel. This is
done with the help of ADCHS register.

Here AN1 is selected as the analogue pin input to channel 0. (line - 2).

Number of channels determine the number of simultaneous samples


possible.

The controller can sample alternately between Mux A and Mux B.

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Possible
Resulting Analouge Input Pin
Configurations Code Line
Configuration
of ADCHS

Channel 0 -> Any anlouge pin from AN0 - ADCHSbits.CH0SA


Mux A AN8 can be selected to give = 1; or any other
input to channel 0 through number depending
Mux A. on pin

Channel 1, 2 Channel 1 input is AN3, ADCHSbits.CH123SA


and 3 -> Mux A Channel 2 -> AN4, Channel 3 - = 1;
> AN5

Channel 0 -> Any anlouge pin from AN0 - ADCHSbits.CH0SB =


Mux B AN8 can be selected to give 1; or any other
input to channel 0 through number depending
Mux B on pin

Channel 1, 2 Channel 1 input is AN3, ADCHSbits.CH123SB


and 3 -> Mux B Channel 2 -> AN4, Channel 3 - = 1;
> AN5

Selecting different combinations of ADC analogue inputs and channels for dspic30f
devices.
(dspic33f devices may have more channels and inputs available)

3. Select the Voltage Reference


INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
A voltage reference must be selected to match the input range of the
analogue inputs. There are two options for this:

1. The Vcc and Gnd can be made upper and lower reference limits.

2. An external reference voltage may be applied on the AVref pin.

This is done with the help of VCFG bit in the ADCON2 register. (line - 47)

VCFG Vref (Higher) Vref (Lower)

0 AVdd AVss

1 External Vref + AVss

2 AVdd External Vref -

3 External Vref + External Vref -

4. Select the Output Format of


Data
The output format of the data sampled through ADC can also be specified
with the helpINTERNET
of FORM bit inCOMPUTERS CELLaccording
the ADCON1 register, PHONESto the table

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
below:

FORM Data Output Format

0 integer

1 signed integer

2 fractional

3 signed fractional

5. Select the Conversion Trigger


Source
Analogue to digital conversion inside the ADC module may be initiated by a
number of trigger sources, determined by the SSRC bits in the ADCON1 register
according to the following table:

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
SSRC Conversion Trigger Source

0 Manually clearing the SAMP bit ends sampling and starts


conversion.

1 Active transition on INT0 pin ends sampling and starts


conversion.

2 Timer 3 compare match ends sampling and starts conversion.

3 Ending of PWM cycle ends sampling and starts conversion.

7 Automatic conversion triggered by internal counter.

6. Select the Mode of Sampling


Sampling on ADC channels can be initiated with either of the following:

1. Setting the SAMP bit manually in the code.

2. Automatically after the end


INTERNET of last conversion.
COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
This is decided by the ASAM bit in ADCON1 register.

ASAM Mode

0 Sampling begins when SAMP bit set

1 Sampling begins immediately after the end of last conversion.

7. Select the Conversion Clock


Any Analogue to Digital conversion with the A/D module requires 12 clock
periods. The period of a single clock cycle can be configured with the help of
ADCS bits in the ADCON3 register. The value placed in ADCS is a six bit value.
This can be calculated from the following formulas:

The minimumINTERNET
value of Tad that can be used is
COMPUTERS 154 nanoseconds.
CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
8. Decide if Simultaneous Sampling
Is Required or Not (Optional)
Simultaneous sampling can be enabled by the SIMSAM bit in the ADCON1
register. Simultaneous sampling captures the samples of all input channels at
precisely the same instant. If Simultaneous sampling is disabled, the channels
will be sampled one after the other.

9. Select the Conversion Clock


Source (Optional)
In most cases, the conversion clock source is selected to be the system clock
by setting the ADRC bit in the ADCON3 register to 0.

10. Determine if Input Scanning Is


Required (Optional)
This step can be skipped in most applications.

Input scanning can be enabled


INTERNET by the CSNA
COMPUTERS bit inPHONES
CELL the ADCON2 register.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Channel 0 of the ADC module can be configured to scan multiple analogue
inputs. This can be used if their are multiple input sources and not all of them
are active at the same time.

The ADC Interrupt in dspic30f and


dspic33f
Instead of using the readADC() function in main body to read the values from
the ADC module, an interrupt is used to perform the same function in most
advanced applications.

For more information on interrupts refer to this detailed tutorial: How to Use
INTERNET COMPUTERS CELL PHONES
Interrupts in Pic MicroControllers

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Interrupt Based Code for ADCs in
dspic30f and dspic33f
#define FCY 30000000
#include <xc.h>
#include <stdio.h>
#include <delay.h>
#include <libpic30.h>
#include <math.h>
#include <p30F4011.h>
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30
MIPS
_FWDT(WDT_OFF); // Watchdog timer off
_FBORPOR(MCLR_DIS);
void Interrupt_Init( void )
{
IEC0bits.ADIE = 1; //Enable ADC Interrupt
IPC2bits.ADIP = 6; //set interrupt priority = 6
}
void __attribute__((interrupt, auto_psv))_ADCInterrupt (void)
{
while ( !ADCON1bits.DONE ); // wait to complete the
conversion
sample.Va = ADCBUF0;
sample.Vb = ADCBUF1;
IFS0bits.ADIF = 0;
}
int main( void )
{
// Make RD0 a digital output
_TRISD0 = 0;
Interrupt_Init();
ADC_Init();
while(INTERNET
1 ) COMPUTERS CELL PHONES
{

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
_LATD0 = 1;
__delay32(15000000);
_LATD0 = 0;
__delay32(15000000);
}
}

A conversion trigger source should be selected for the ADC interrupt to


function properly as directed in step 5.

Sampling should be done in automatic mode. (ASAM bit)

Frequency of the triggering of ADC interrupt may be controlled with the


help of SMPI bits in the ADCON2 register. (line 50).

This article is accurate and true to the best of the author’s knowledge.
Content is for informational or entertainment purposes only and does not
substitute for personal counsel or professional advice in business, financial,
legal, or technical matters.

© 2017 StormsHalted

Comments
StormsHalted (author) from Karachi, Pakistan on May 08, 2020:
INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
The code lines displayed in this article are tested to work, if you copy and
paste it, it will be the same as if I send you the code.

You might want to recheck the MCU model and its configuration bits. This one
is for dsPic30f4011.

Lucas Felfoldi on May 28, 2019:

Hello!

First, thank you for sharing your knowledge!

Could you explain a little bit about the code [ADCS = 9] (on "ADC Example
Code for dsPic") ?

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In the comments, you said that the clock conversion is 6Tcy, but according to
the datasheet, Conversion time = (Tcy/2) * (ADCS + 1)..

So,

Conversion time = (Tcy/2) * (9 + 1) = 5Tcy...

Am I wrong?

Thank you

AS73 on April 28, 2019:

Thank you so much. You really saved my day!!

StormsHalted (author) from Karachi, Pakistan on June 08, 2018:

Try debugging with MplabX. It will give a much better interface.

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
You will have to disconnect and plugin pickit back. This type of issue is not
uncommon with pickit.

Pramod singh on June 06, 2018:

Hello,

I am facing an issue with pickit3 where I'm trying to debug a dspic30f4011 on


which I could debug it only once. If I try it once again The ID says no device ID
found.

BY STORMSHALTED

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Related Articles

INDUSTRIAL TECHNOLOGY MISCELLANEOUS MISCELLANEOUS

How to Send and


How to Generate PWM Receive Data From
How to Use Timers in on dspic30f and UART in dspic30f and
dsPic Microcontrollers dspic33f dspic33f
BY STORMSHALTED · JAN 28, 2021 BY STORMSHALTED · JAN 7, 2021 BY STORMSHALTED · DEC 28, 2020

COMPUTERS MISCELLANEOUS

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
How to Use Interrupts
in Pic and dsPic Code Optimization in
MicroControllers Microcontrollers
BY STORMSHALTED · DEC 29, 2020 BY STORMSHALTED · DEC 25, 2020

SOFTWARE & OPERATING INDUSTRIAL TECHNOLOGY INDUSTRIAL TECHNOLOGY


SYSTEMS

How to Set a Schneider Fundamentals of MV


Convolution in Matlab Electric's Sepam S20 Transformer Protection
With Code Examples Relay Using Relays
BY STORMSHALTED · MAR 26, 2021 BY STORMSHALTED · JAN 1, 2021 BY STORMSHALTED · OCT 5, 2020
INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
SOFTWARE & OPERATING SOFTWARE & OPERATING MISCELLANEOUS
SYSTEMS SYSTEMS

How to Convert Word Solution to Slow MP4


Documents Into Images Playback on VLC and The Ultimate Arduino
(jpg, png, gif, tiff) Other Players Tutorial
BY STORMSHALTED · FEB 3, 2021 BY STORMSHALTED · JAN 30, 2021 BY CHRISCAMARO · JAN 6, 2021

SOFTWARE & OPERATING COMPUTERS


SYSTEMS

INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
How to Convert PSD & C++ Program to
PSB to JPG in Estimate the Value of
Photoshop 'e' and e^x
BY STORMSHALTED · JAN 31, 2021 BY STORMSHALTED · JAN 20, 2021

COMPUTERS MISCELLANEOUS

EcoStruxure Power What's the Difference


Monitoring Expert: An Between Analog and
Engineer's Review Digital?
BY STORMSHALTED · FEB 3, 2021 BY EUGENE BRENNAN · OCT 9, 2020

SEE MORE
INTERNET COMPUTERS CELL PHONES

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Privacy Policy · Terms of Use · About Us · Editorial Policy

© 2021 Maven Media Brands, LLC and respective content providers on this website. Other product and company names shown may be
trademarks of their respective owners. Maven Media Brands, LLC and respective content providers to this website may receive compensation
for some links to products and services on this website.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

You might also like