P89LPC935 ADC/DAC Tutorial The P89LPC935 microcontroller has 2 on-board analog to digital modules Each module contains a 4-channel 8-bit successive approximation ADC 89LPC935 ADC/DAC Tutorial 1
ADC Module Features 2 8-bit 4-channel multiplexed input A/D converters Separate result registers for each channel Six operating modes Single or continuous conversion Scan mode 4 conversion start modes Conversion time of 13 ADC clocks (3.9usec for a 3.3MHz clock) May be polled or interrupt driven Each ADC module may be configured in DAC mode to produce a single analog output 89LPC935 ADC/DAC Tutorial 2
ADC Input/Output Pins ADC Input Channel Port Pin Result Register DAC Output Channel AD00 P1.7 ADODAT0 AD01 P0.0 ADODAT1 AD02 P2.1 ADODAT2 AD03 P2.0 ADODAT3 DAC0 AD10 P0.1 AD1DAT0 AD11 P0.2 AD1DAT1 AD12 P0.3 AD1DAT2 AD13 P0.4 AD1DAT3 DAC1 89LPC935 ADC/DAC Tutorial 3
DAC Operation 2 DACs are available for use on the LPC935 The DAC0 output pin is P2.0 The DAC0 input register is AD0DAT3 Writing to this register will update the DAC analog output The DAC1 output pin is P0.4 The DAC1 input register is AD1DAT3 Writing to this register will update the DAC analog output DAC Channel 0 DAC Channel 1 AD0DAT3 DAC0 P2.0 AD1DAT3 DAC1 P0.4 Write data to register Write data to register 89LPC935 ADC/DAC Tutorial 4
Configuring a DAC Ensure that the DAC output is configured as an output Configure for quasi-bidirectional mode Enable the ADC module in the ADCON0 or the ADCCON1 register ENADC0 = 1 in ADCON0 register ENADC1 = 1 in ADCON1 register Enable the DAC in the ADMODB register ENDAC0 = 1 for DAC0 ENDAC1 = 1 for DAC1 Program Example Write a C program to generate a sawtooth waveform using DAC0. 89LPC935 ADC/DAC Tutorial 5
Generating a sawtooth wave using DAC1 #include <REG935.H> void main() { unsigned char data x ; //configure Port 2 as quasi-bidirectional P2M1 = 0x00; P2M2 = 0x00; //Configure the DAC ADCON1 = 0x04; ADMODB = 0x08; //AD1 enabled //DAC mode for ADC1 } //Sawtooth wave generation while(1) { for(x=0; x<256; x++) { AD1DAT3 = x; } } //write to conversion register 89LPC935 ADC/DAC Tutorial 6
A/D Control Register 89LPC935 ADC/DAC Tutorial 7
A/D Mode Register B 89LPC935 ADC/DAC Tutorial 8
ADC Operation The 89LPC935 micro ADC operation can be quite complex with 6 conversion modes and 4 trigger modes This tutorial will demonstrate the use of the ADC in it s simplest format Single channel conversion with immediate start Refer to the LPC935 user guide for multi-channel conversions with different trigger modes We will write a program to measure the voltage on ADC1 channel 2 (pin P0.3) and to display the digital value on the LEDs connected to Port 2 3.3V LPC935 P0.3 P2 89LPC935 ADC/DAC Tutorial 9
Configuring the ADC Configure the ADINS register to enable the relevant ADC channel For ADC1 channel 2, ADINS = 40H Configure the ADC conversion mode in the ADMODA register SCAN bit = 1 for fixed channel single conversion mode ADMODA = 10H Enable ADC channel 1 ADCON1 = 04H Configure the ADC clock in the ADMODB register Max ADC clock is 3.3MHz The ADCON1 control register is used to start a conversion and contains an end of conversion flag To start a conversion set the ENADC1 bit (bit 0) ADCON1 = 0x01; The ADCI1 bit (bit 3) goes high when a conversion is complete The ADCI1 bit must be cleared by software 89LPC935 ADC/DAC Tutorial 10
#include <REG935.H> void main() { //configure Port 2 as quasi-bidirectional P2M1 = 0x00; P2M2 = 0x00; C Program //Configure the ADC ADINS = 0x40; ADMODA = 0x10; ADCON1 = 0x04; ADMODB = 0xE0; //select AD12 as ADC input (pin P0.3) //AD1 in fixed channel single conversion mode //ADC channel 1 enabled //divide by 8 ADC clock } while(1) { ADCON1 = 0x01; while((adcon1 & 0x08) == 0); ADCON1 &= 0xF7; P2 = AD1DAT2; } //start conversion immediately //wait for end of conversion //clear EOC flag //display result on port 2 LEDs 89LPC935 ADC/DAC Tutorial 11
ADC Simulation After starting the debugger, select the A/D Converter channel 1 from the Peripherals pull-down menu An analog input voltage may be simulated by changing the AD12 field in the bottom right-hand corner of the peripheral window 89LPC935 ADC/DAC Tutorial 12
ADINS Register 89LPC935 ADC/DAC Tutorial 13
ADMODA Register 89LPC935 ADC/DAC Tutorial 14
ADCON1 Register 89LPC935 ADC/DAC Tutorial 15
ADMODB Register 89LPC935 ADC/DAC Tutorial 16