Professional Documents
Culture Documents
CoE115 1s1920 Lab4
CoE115 1s1920 Lab4
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.
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.
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.
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.
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
}
}
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.