Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CoE 115 Lab 4: Analog to Digital Conversion

October 2019

Topics/Objectives
• To understand the general functionality of Analog-to-Digital Converters.
• To be able to properly setup the Analog-to-Digital Converter functionality of the PIC microcontroller.

• To interface an analog peripheral to the PIC Microcontroller.

Pre-lab:
• Read the ”10-bit High-Speed A/D Converter” section of the PIC24FJ64GB002 Family Reference Man-
ual

Overview
The PICs 10-bit A/D converter allows us to use the PIC microcontroller to read analog voltages as inputs,
as opposed to just HIGH and LOW values of digital inputs.
For this exercise, we will be interfacing an analog device as our input and use an LED as an indicator if
the input has crossed a certain threshold. We will use AN9 as the analog input pin. For the analog signal
we need, set up a voltage divider circuit using a potentiometer and the 3.3V supply. Lastly, we will revisit
Lab 1 and apply the ADC functionality accordingly.

Setting Up the ADC


Refer to the ”10-Bit High-Speed A/D Converter” section of the PIC24FJ64GB002 as your guide for setting
up the ADC registers. The PIC ADC Module for this exercise is congured according to the table bellow.

1
• PCFG<12:0> - Set up AN9 as
A/D Port Conguration Register
analog input, all other pins as dig-
The AD1PCFG register is used to
AD1PCFG ital inputs.
congure which pins are digital inputs
and which are analog inputs. • PCFG<15:13> - Set to 1.

• Set up the ADC to discontinue


operation if device enters Idle
A/D Control Register 1 mode.
AD1CON1, like AD1CON2 and
AD1PCFG AD1CON3, is a collection of multi- • Output the data in integer form.
ple control elds to congure certain
• Use the internal counter as trigger
parts/functionality of the ADC.
for conversion and automatically
begin sampling.

• Do not scan inputs.

• Interrupts should be at the com-


A/D Control Register 2
pletion of each sample/convert.
AD1CON2, like AD1CON1 and
AD1PCFG AD1CON3, is a collection of multi- • Always use MUX A input set-
ple control elds to congure certain tings.
parts/functionality of the ADC.
• Buer is congured as one 16-word
buer.

A/D Control Register 3 • Use system clock


AD1CON3, like AD1CON1 and • AD1CON3<12:8> - set to 2 *
AD1PCFG AD1CON2, is a collection of multi- TAD
ple control elds to congure certain
parts/functionality of the ADC. • AD1CON<7:0> - set to 2 * TCY

• AD1CHS<4:0> - Channel 0 pos-


A/D Input Select
itive input is AN9 for MUX A.
AD1PCFG This is to set which inputs are the pos-
itive or negative inputs to channel 0. • All other bits 0.

Input Scan Select Register


AD1PCFG This is to set which inputs are the pos- • Set to 0
itive or negative inputs to channel 0.
Do not forget to also set up your inputs and outputs using GPIO bits, including AN9.

Setting up your ADC this way will set it to continuously convert the value it reads at AN9, and store it
in a buer, ADC1BUF0. We will need to set up a ISR to be able to utilize this value and store it whenever
the microcontroller completes a sample/convert. Similar to our Input Change Notication interrupt enable
and interrupt ag.

EC0bits.AD1IE A/D Interrupt Enable


IFS0bits.AD1IF A/D Interrupt Flag

Lab Proper
Part 1:
Write a code that will turn the LED ON when the voltage level read by the ADC (use a potentiometer of any
value) is greater than 1.6V and OFF otherwise. The middle pin of the potentiometer should be interfaced
to AN9 while the two other pins are connected to the output of the 3.3V regulator and ground. Modify the
basic code below.

2
#i n c l u d e ” xc . h”
CONFIG1 (FWDTEN OFF & JTAGEN OFF)
CONFIG2 (POSCMOD NONE & OSCIOFNC ON & FCKSM CSDCMD & FNOSC FRCPLL & PLL96MHZ OFF & P
CONFIG3 ( SOSCSEL IO )

i n t adcvalue ;

v o i d ADC init ( ) {
// s e t u p ADC c o n f i g u r a t i o n b i t s and TRISB
}

v o i d main ( v o i d ) {
ADC init ( ) ; // i n i t i a l i z e ADC
// e n a b l e I n t e r r u p t
// c l e a r I n t e r r u p t f l a g
AD1CON1bits .ADON = 1 ; // t u r n on ADC

while (1){
// Turn LED on/ o f f based on a d c v a l u e
}
}

void attribute (( interrupt , no auto psv )) ADC1Interrupt ( v o i d ) {


// D i s a b l e i n t e r r u p t
// C l e a r f l a g
//Copy ADC output t o a d c v a l u e
// Enable i n t e r r u p t
// C l e a r f l a g
}
Demonstrate this to your instructor.

Part 2:
Using the same setup in part 1, change the threshold to 0.5V. Demonstrate this to your instructor.

Challenge
Print the measured voltage from the ADC to the LCD.

You might also like