Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analog-to-Digital Converter (ADC)

Similar presentations


Presentation on theme: "Analog-to-Digital Converter (ADC)"— Presentation transcript:

1 Analog-to-Digital Converter (ADC)
1/20/2017 Richard Kuo Assistant Professor

2 Outline 6.0 NuMicro_ADC.ppt Analog-to-Digital Converter Introduction
Lab. ADC Single_Cycle mode (ADC) Lab. ADC Continuous mode (ADC_4ch) Lab. To read Variable Resistor (ADC_VR1) Lab. To read Photoresistor (ADC_Photoresistor) Lab. To read Thermistor (ADC_Thermistor) Lab. To read Joystick (ADC_Joystick) Lab. ADC Triggered by PWM (ADC_Trigger)

3 Signal Conversion ADC DAC Filter

4 Analog to Digital Conversion

5 Digital Conversion Vin is input voltage for ADC to sample
Vref is reference voltage for ADC to compare with Vin Vref is connected to 3.3V on NUC140 learning board 12-bit ADC : output is a 12-bit binary code, so its value = 0 ~ 4095 Decimal Binary Voltage (Vref=3.3V) 0000_0000_0000 0/4096 * 3.3V 1 0000_0000_0001 1/4096 * 3.3V 2048 1000_0000_0000 2048/4096 * 3.3V 4095 1111_1111_1111 4095/4096 * 3.3V

6 ADC Architectures Direct-Conversion ADC
SAR (Successive Approximation Register) ADC ΔΣ (Sigma-Delta) ADC Sigma-Delta ADC SAR ADC Successive Approximation Register Direct-Conversion ADC

7 SAR (Successive Approximation Register) ADC
End-Of-Conversion clock N-bit DAC Vref Comparator - + Sample & Hold Vin

8 ADC specification 12-bit SAR ADC (10-bit accuracy)
Analog input voltage range: 0~Vref (Max to 5.0V). Operation voltage: AVDD=3.0V~5.5V; Input channel: Up to 8 single-end analog input channels 4 pairs of differential analog input channel. NUC140 = Up to 800KSPS, NANO102 up to 1MSPS The maximum ADC operating frequency is 16M Hz Three ADC operation modes Single mode Single-cycle mode Continuous mode

9 ADC pins configuration
The ADC input pins share with GPIO group A The ADC input pins must be configured in input type Single-end input mode ADC0 ~ ADC7 = PA0~PA7 Differential input mode ADC0 ~ ADC3 Differential input channels are the paired channels Differential input paired channel Single-end input channel 1 2 3 4 5 6 7

10 ADC Clock Source NUC100

11 ADC Clock Selection (in SYS_init.c)
void CLK_SetModuleClock( uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv) ADC Clock Source CLK_CLKSEL1_ADC_S_HXT CLK_CLKSEL1_ADC_S_LXT CLK_CLKSEL1_ADC_S_PLL CLK_CLKSEL1_ADC_S_HCLK CLK_CLKSEL1_ADC_S_HIRC ADC Clock Divider CLK_CLKDIV_ADC(x) Module Index = ADC Clock Source Clock Divider ADC clock 8-bit divider = x

12 ADC Sample Modes Single Mode Single-Cycle Mode Continuous Mode

13 ADC Single mode

14 ADC Single-Cycle mode

15 ADC Continuous mode

16 ADC Initialization (in adc.c)
void ADC_Open(ADC_T *adc, uint32_t u32InputMode, uint32_t u32OpMode, uint32_t u32ChMask) ADC input mode ADC_INPUT_MODE_SINGLE_END ADC_INPUT_MODE_DIFFERENTIAL ADC operation mode ADC_OPERATION_MODE_SINGLE ADC_OPERATION_MODE_SINGLE_CYCLE ADC_OPERATION_MODE_CONTINUOUS ADC struct pointer ADC input mode ADC operation mode ADC channel

17 NuMicro Cortex-M0 Learning Board
ADC reference voltage is VCC33 (3.3V) ADC0~7(GPA0~7) VR1

18 ADC Sample Codes ADC : Single Cycle Conversion
ADC_8ch : Continuous Conversion ADC_Photoresistor : ADC0 reading sensors (GL5516, MQ7, UVM30) ADC_Thermistor : ADC1 reading thermistor & calculate temperature ADC_Joystick : ADC0 & ADC1 reading Joystick

19 ADC (Single Cycle Mode)
MCU_init.h (chip initialization setting) //Define Clock source #define MCU_CLOCK_SOURCE #define MCU_CLOCK_SOURCE_HIRC // HXT, LXT, HIRC, LIRC #define MCU_CLOCK_FREQUENCY //Hz //Define MCU Interfaces #define MCU_INTERFACE_ADC #define ADC_CLOCK_SOURCE_HIRC // HXT, LXT, PLL, HIRC, HCLK #define ADC_CLOCK_DIVIDER 1 #define PIN_ADC_0 #define PIN_ADC_1 #define PIN_ADC_2 #define PIN_ADC_3 #define ADC_CHANNEL_MASK ADC_CH_0_MASK | ADC_CH_1_MASK | ADC_CH_2_MASK | ADC_CH_3_MASK #define ADC_INPUT_MODE ADC_INPUT_MODE_SINGLE_END // SINGLE_END, DIFFERENTIAL #define ADC_OPERATION_MODE ADC_OPERATION_MODE_SINGLE_CYCLE // SINGLE, SINGLE_CYCLE, CONTINUOUS CPU clock maximum frequency ADC Single Cycle Conversion

20 ADC volatile uint8_t u8ADF; void ADC_IRQHandler(void) {
ADC Flag for main loop to detect when ADC sample finished and ADC interrupt is generated volatile uint8_t u8ADF; void ADC_IRQHandler(void) { uint32_t u32Flag; // Get ADC conversion finish interrupt flag u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT); if(u32Flag & ADC_ADF_INT) u8ADF = 1; ADC_CLR_INT_FLAG(ADC, u32Flag); } Set ADC software flag when IRQ ocurrs

21 ADC void Init_ADC(void) {
ADC_Open(ADC, ADC_INPUT_MODE, ADC_OPERATION_MODE, ADC_CHANNEL_MASK); // set ADC modes & channels ADC_POWER_ON(ADC); // Power on ADC ADC_EnableInt(ADC, ADC_ADF_INT); // Enable ADC ADC_IF interrupt NVIC_EnableIRQ(ADC_IRQn); // Enable CPU NVIC } Define in MCU_init.h

22 ADC int32_t main (void) { int i; uint32_t u32Result; SYS_Init();
Init_ADC(); %dHz\n", SystemCoreClock); u8ADF = 0; while(1) { ADC_START_CONV(ADC); // start ADC conversion while (u8ADF == 0); // wait for ADC sampling finished & generate interrupt to set this flag to 1 // read ADC result & print it for (i=0; i<4; i++) { u32Result = ADC_GET_CONVERSION_DATA(ADC, i); printf("ADC%d = %d, ",i, u32Result); } printf("\n"); u8ADF =0; // clear the flag start ADC conversion Check flag from ADC IRQHandler clear ADC software flag when ADC result is printed

23 ADC_4ch (Continuous Mode)
MCU_init.h (chip initialization setting) //Define Clock source #define MCU_CLOCK_SOURCE #define MCU_CLOCK_SOURCE_HIRC // HXT, LXT, HIRC, LIRC #define MCU_CLOCK_FREQUENCY //Hz //Define MCU Interfaces #define MCU_INTERFACE_ADC #define ADC_CLOCK_SOURCE_HIRC // HXT, LXT, PLL, HIRC, HCLK #define ADC_CLOCK_DIVIDER 1 #define PIN_ADC_0 #define PIN_ADC_1 #define PIN_ADC_2 #define PIN_ADC_3 #define ADC_CHANNEL_MASK ADC_CH_0_MASK | ADC_CH_1_MASK | ADC_CH_2_MASK | ADC_CH_3_MASK #define ADC_INPUT_MODE ADC_INPUT_MODE_SINGLE_END // SINGLE_END, DIFFERENTIAL #define ADC_OPERATION_MODE ADC_OPERATION_MODE_CONTINUOUS // SINGLE, SINGLE_CYCLE, CONTINUOUS CPU clock maximum frequency ADC Continuous Conversion Mini51/54/58 only support Single mode conversion

24 ADC_4ch volatile uint32_t u32ADCvalue[4]; void ADC_IRQHandler(void) {
uint8_t i; uint32_t u32Flag; // Get ADC conversion finish interrupt flag u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT); if(u32Flag & ADC_ADF_INT) { for (i=0; i<4; i++) { u32ADCvalue[i] = ADC_GET_CONVERSION_DATA(ADC, i); printf("ADC%d = %4d, ",i, u32ADCvalue[i]); } printf("\n"); ADC_CLR_INT_FLAG(ADC, u32Flag); check ADC interrupt flag read ADC value clear ADC interrupt flag

25 ADC_4ch void Init_ADC(void) {
ADC_Open(ADC, ADC_INPUT_MODE, ADC_OPERATION_MODE, ADC_CHANNEL_MASK); ADC_POWER_ON(ADC); ADC_EnableInt(ADC, ADC_ADF_INT); NVIC_EnableIRQ(ADC_IRQn); ADC_START_CONV(ADC); } int32_t main (void) SYS_Init(); Init_ADC(); while(1); start ADC conversion once when using Continous Mode When ADC finish its conversion, ADC interrupt will occur, ADC_IRQHandler will print out ADC value

26 Gas Sensor

27 CO gas sensor - MQ7

28 CO gas sensor - MQ7 CO

29 UV Sensor - UVM-30

30 IR Ranger - GP2Y0A41SK0F 0.4V ~ 3V 30cm ~ 3cm every 5ms

31 IR Ranger : Distance vs Output voltage

32 Photoresistor (GL5516)

33 Photoresistor Connection Diagram
Learning Board Vcc3.3V pullup resistor 10Kohm Measure the resistance for calibration in main.c ADC6 (GPA6) Photoresistor 0~10Kohm Gnd Sample Code : ADC_Photoresistor

34 ADC_Photoresistor get ADC value & print it
void ADC_IRQHandler(void) { uint32_t u32Flag; uint32_t u32ADCvalue; // Get ADC conversion finish interrupt flag u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT); if(u32Flag & ADC_ADF_INT) { u32ADCvalue = ADC_GET_CONVERSION_DATA(ADC, 0); printf("ADC0= %4d\n",u32ADCvalue); } ADC_CLR_INT_FLAG(ADC, u32Flag); get ADC value & print it

35 ADC_Photoresistor define in MCU_init.h
void Init_ADC(void) { ADC_Open(ADC, ADC_INPUT_MODE, ADC_OPERATION_MODE, ADC_CHANNEL_MASK); ADC_POWER_ON(ADC); ADC_EnableInt(ADC, ADC_ADF_INT); NVIC_EnableIRQ(ADC_IRQn); ADC_START_CONV(ADC); } int32_t main (void) SYS_Init(); Init_ADC(); printf("ADC ch0 reading sensor\n"); while(1); define in MCU_init.h

36 Thermistor NTC Thermistor : Negative Temperature Coefficient
temperature rising will decrease resistance B or β parameter equation log(R) = log(R0) + B * (1/T – 1/T0) 1/T = 1/T0 + (log(R) – log(R0)) / B T = 1 / (1/T0 + (log(R) – log(R0)) /B) Temperature Calculation

37 Thermistor Connection Diagram
Learning Board Vcc3.3V pullup resistor 10Kohm Measure the resistance for calibration in main.c ADC6 (GPA6) thermistor 0~10Kohm Gnd Sample Code : smpl_ADC_Thermistor

38 ADC_Thermistor thermistor pullup resistor

39 ADC_Thermistor Resistance to Temperature calculation
void Thermistor(int16_t ADCvalue) { double T, Temp; double T0 = ; // double lnR; int16_t R; // Thermistor resistence int16_t R0 = 8805; // calibrated by reading R at 28 degree celsius int16_t B = 3950; int16_t Pullup = 9930; // 10K ohm // R / (Pullup + R) = adc / 4096 R = (Pullup * ADCvalue) / ( ADCvalue); // B = (log(R) - log(R0)) / (1/T - 1/T0) T = 1 / (1/T0 + (log(R)-log(R0)) / B ); Temp = T ; printf("ADC:%4d, R=%d, Temp.=%f\n", ADCvalue, R, Temp); } Resistance to Temperature calculation

40 ADC_Joystick

41 ADC_Joystick using 2 channels AD0 & ADC1 to read joystick X & Y value
void ADC_IRQHandler(void) { uint32_t u32Flag; u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT); if(u32Flag & ADC_ADF_INT) { X = ADC_GET_CONVERSION_DATA(ADC, 0); Y = ADC_GET_CONVERSION_DATA(ADC, 1); } ADC_CLR_INT_FLAG(ADC, u32Flag); void Init_ADC(void) ADC_Open(ADC, ADC_INPUT_MODE, ADC_OPERATION_MODE, ADC_CHANNEL_MASK ); ADC_POWER_ON(ADC); ADC_EnableInt(ADC, ADC_ADF_INT); NVIC_EnableIRQ(ADC_IRQn); ADC_START_CONV(ADC); using 2 channels AD0 & ADC1 to read joystick X & Y value

42 PWM Trigger ADC Sampling

43 ADC Conversion Trigger Sources (Nano100)

44 ADC Driver function call (adc.c)
void ADC_EnableHWTrigger(ADC_T *adc, uint32_t u32Source, uint32_t u32Param) ADC HW Trigger Source ADC_ADCR_TRGS_STADC ADC_ADCR_TRGS_PWM ADC STADC Trigger Mode ADC_ADCR_TRGCOND_LOW_LEVEL ADC_ADCR_TRGCOND_HIGH_LEVEL ADC_ADCR_TRGCOND_FALLING_EDGE ADC_ADCR_TRGCOND_RISING_EDGE ADC struct pointer ADC HW Trigger Source ADC STADC Trigger Mode Triggered by External pin STADC Triggered by PWM

45 Project : Plant Care Function : use ADC to read Moisture & pH
Platform : Example : Demo :

46 Important Notice ! This educational material are neither intended nor warranted for usage in systems or equipment, any malfunction or failure of which may cause loss of human life, bodily injury or severe property damage. Such applications are deemed, “Insecure Usage”. Insecure usage includes, but is not limited to: equipment for surgical implementation, atomic energy control instruments, airplane or spaceship instruments, the control or operation of dynamic, brake or safety systems designed for vehicular use, traffic signal instruments, all types of safety devices, and other applications intended to support or sustain life. All Insecure Usage shall be made at user’s own risk, and in the event that third parties lay claims to the author as a result of customer’s Insecure Usage, the user shall indemnify the damages and liabilities thus incurred by using this material. Please note that all lecture and sample codes are subject to change without notice. All the trademarks of products and companies mentioned in this material belong to their respective owners.

47 References Analog-to-digital converter - Wikipedia, the free encyclopedia Flash ADC - Wikipedia, the free encyclopedia Understanding SAR ADCs: Their Architecture and Comparison with ... TI High Speed Analog to Digital Converter Basics

48 Digital to Analog Converter (DAC)
Binary-Weighted Resistor DAC ΔΣ DAC (Sigma-Delta DAC) R-2R Ladder DAC Vout =   - (Vmsb + Vn + Vlsb) = - (Vref + Vref/2 + Vref/ 4)


Download ppt "Analog-to-Digital Converter (ADC)"

Similar presentations


Ads by Google