EE Give the code necessary to initialize the operation of th
EE
Give the code necessary to initialize the operation of the ADC. State any assumptions. Divide your answer into two: (i) Operations required for any or all ADC channels, and (ii) Operations to control/format/select the specific channel in use. Using your answers to the previous questions give the firmware to implement the design of figure 3. On the application board there are 6 pins that might be used as general purpose output P2.0 ..P2.5 (repeated as figure 4 for convenience). To allow performance measurements one or more of the LEDs may be used. Include the code to initialize the LED(s) and to toggle about each reading. The ADC is 12 bits. What is the maximum reading with 12 bits?Solution
Multiple questions are asked here. Answer of Question.2 is as below:
Answer of Question 2:
Before answering your question, there are two important things you should be aware of.
1) Deveopment environment: I suggest that you use MBED compiler for coding for MBED application board.
2) LPC 1768 Hardware Configuration: This hardware configuration is very important to write a code for it. It includes the details of the pin configiration for ADC channels & Register details for ADC configuration. Find those details as below:
Pin Configuration for ADC Channels:
Register Configuration for ADC:
Please refer datasheet for bit details of each register.
Now coming to your answers:
Answer of part (i) : Operations required to enable all or any ADC Channel
Answer of part (ii) : Operations to control a specific channel
Code for LPC1768:
#include<lpc17xx.h>
#include \"delay.h\" // delay library which contains the delay routines
/* Bit positions of ADCR and ADGR registers */
#define SBIT_BURST 16u
#define SBIT_START 24u
#define SBIT_PDN 21u
#define SBIT_EDGE 27u
#define SBIT_DONE 31u
#define SBIT_RESULT 4u
#define SBIT_CLCKDIV 8u
void main()
{
uint16_t adc_result;
SystemInit(); //Clock and PLL configuration
LPC_SC->PCONP |= (1 << 12); /* Enable CLOCK for internal ADC controller */
LPC_ADC->ADCR = ((1<<SBIT_PDN) | (10<<SBIT_CLCKDIV)); /* Set the clock and Power ON ADC module */
LPC_PINCON->PINSEL1|= 0x01<<14; /* Select the P0_23 AD0[0] for ADC function */
while(1)
{
LPC_ADC->ADCR |= 0x01; /* Select Channel 0 by setting 0th bit of ADCR */
DELAY_us(10); /* allow the channel voltage to stabilize*/
util_BitSet(LPC_ADC->ADCR,SBIT_START); /*Start ADC conversion*/
while(util_GetBitStatus(LPC_ADC->ADGDR,SBIT_DONE)==0); /* wait till conversion completes */
adc_result = (LPC_ADC->ADGDR >> SBIT_RESULT) & 0xfff; /*Read the 12bit adc result*/
}
}
| Adc Channel | Port Pin | Associated PINSEL Register |
|---|---|---|
| AD0 | P0.23 | 14,15 bits of PINSEL1 |
| AD1 | P0.24 | 16,17 bits of PINSEL1 |
| AD2 | P0.25 | 18,19 bits of PINSEL1 |
| AD3 | P0.26 | 20,21 bits of PINSEL1 |
| AD4 | P1.30 | 28,29 bits of PINSEL3 |
| AD5 | P1.31 | 30,31 bits of PINSEL3 |
| AD6 | P0.3 | 6,7 bits of PINSEL0 |
| AD7 | P0.2 | 4,5 bits of PINSEL0 |

