Hello,
can anyone help me,how can I program the AD-Converter of the MCB4300. The program works with one channel (AD0 - Channel 1), but I need 2 channels (AD0 - Channel 1 and AD0 Channel 0).
int main(void) { int adcVal;
ADC_Initialize(); ADC_StartConversion();
while(ADC_ConversionDone () < 0);
adcVal = ADC_GetValue(); }
/* This is the program code from the Example of Keil for the MCB4300 */ #include "LPC43xx.h" #include "SCU_LPC43xx.h" #include "Board_ADC.h"
#define ADC_RESOLUTION 10 /* Number of A/D converter bits */
/* Clock Control Unit register bits */ #define CCU_CLK_CFG_RUN (1 << 0) #define CCU_CLK_CFG_AUTO (1 << 1) #define CCU_CLK_STAT_RUN (1 << 0)
static volatile uint16_t AD_last; /* Last converted value */ static volatile uint8_t AD_done; /* AD conversion done flag */
int32_t ADC_Initialize (void) {
/* Enable ADC0 clock */ LPC_CCU1->CLK_APB3_ADC0_CFG = CCU_CLK_CFG_AUTO | CCU_CLK_CFG_RUN; while (!(LPC_CCU1->CLK_APB3_ADC0_STAT & CCU_CLK_STAT_RUN));
/* Configure ADC0_1 */ LPC_ADC0->CR = (1 << 1) | /* Select ADC0_1 pin for conversion */ (2 << 8) | /* 12MHz / (2+1) = 4MHz */ (1 << 21) ; /* ADC is operational */
/* Enable ADC0 Channel 1 interrupt */ LPC_ADC0->INTEN |= (1 << 1); NVIC_EnableIRQ (ADC0_IRQn);
return 0; }
int32_t ADC_Uninitialize (void) {
/* Disable ADC0 Channel 1 interrupt */ NVIC_DisableIRQ (ADC0_IRQn); LPC_ADC0->INTEN &= ~(1 << 1);
/* Disable ADC0 clock */ LPC_CCU1->CLK_APB3_ADC0_CFG = 0;
int32_t ADC_StartConversion (void) {
LPC_ADC0->CR |= (1 << 24); /* Start conversion */
int32_t ADC_ConversionDone (void) { return (AD_done ? 0 : -1); }
int32_t ADC_GetValue (void) {
if (AD_done) { AD_done = 0; return AD_last; } return -1; }
uint32_t ADC_GetResolution (void) { return ADC_RESOLUTION; }
void ADC0_IRQHandler (void) {
LPC_ADC0->GDR; /* Clear IRQ request flag */ AD_last = (LPC_ADC0->DR[1] >> 6) & 0x3FF; /* Read value and clear IRQ */ AD_done = 1; }
-----> LPC_ADC0->CR = (1 << 1) | (2 << 8) | (1 << 21) ; <-----
It says in the datasheet that for ADC0-CR where I can select the Channel for the AD0: "In software-controlled mode, only one of these bits should be 1. In hardware scan mode, any value containing 1 t0 8 ones is allowed"
My problem is, if I want to use Channel 0 and 1 of the ADC0 at the same time, how can I do this.
Thank you in advance for your help.
Why do you initialize the ADC again and again?
It's enough that you initialize it once and then just start a new conversion when the timer ticks.
By the way - if you take a closer look at the information directly above the message input box when you are about to make new posts, there are clear instructions how to post source code.
It's much easier to read code problerly tagged:
#include <stdio.h> void main(void) { printf("Hello world!\n"); while (1) ; }
Sorry, with the source code, i will do it next time.
Yes you're right, I can initialize the ADC only once in the main-function. I will correct it.
By the way, I have an 10-Bit ADC. If I get the converted value from my ADC, I have to convert this value in voltage. The formula is below:
Voltage = (ADC-Value * Uref) / 1024
But where can I get the value of the reference voltage Uref (in datasheet?)? Or rather, which means Uref?
Have you taken a closer look at the pins on your processor? And the schematics how your processor is interfaced on the board?
Or looked closer at the datasheet information?
Just identifying all processor pins relevant for the ADC should represent a significant hint how to figure ut that Uref.