1. Introduction to NUC442
NUC442 is a Cortex ® -M4F core processor with DSP and floating point unit, working frequency up to 84 MHz, embedded 256/512K /flash, 64K/SRAM. There are abundant peripheral resources on the chip. Because the official BSP package is too confusing and complicated, I studied ADC 1 in EADC. The PID of the NUC442JI8AE development board used is 0x00044201.
Second, about EADC
NUC442/NUC472 series includes a 12-bit successive approximation analog-to-digital converter (SAR A/ D converter). The A/D converter can be triggered by software trigger, PWM trigger, timer0~3 overflow pulse trigger, ADINT0, ADINT1 interrupt EOC (conversion end) pulse trigger, and external pin (STADC) input signal to trigger conversion.
3. Module code implementation
This time I quoted library functions and sorted out the usage in detail.
(1) First is the initialization function of ADC1, the code is as follows:
1 void ADC1_Init(void)
2 {
3 unsigned char i=0;
4 SYS_UnlockReg();
5 CLK_EnableModuleClock(EADC_MODULE); //Enable EADC clock
6 CLK_SetModuleClock(EADC_MODULE, CLK_CLKSEL1_ADCSEL_PCLK, CLK_CLKDIV0_ADC(5));
7 //PE8~PE15 ADC1->Channel 0~7
8 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE8MFP_Msk) | SYS_GPE_MFPH_PE8MFP_ADC1_0;
9 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE9MFP_Msk) | SYS_GPE_MFPH_PE9MFP_ADC1_1;
10 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE10MFP_Msk) | SYS_GPE_MFPH_PE10MFP_ADC1_2;
11 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE11MFP_Msk) | SYS_GPE_MFPH_PE11MFP_ADC1_3;
12 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE12MFP_Msk) | SYS_GPE_MFPH_PE12MFP_ADC1_4;
13 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE13MFP_Msk) | SYS_GPE_MFPH_PE13MFP_ADC1_5;
14 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE14MFP_Msk) | SYS_GPE_MFPH_PE14MFP_ADC1_6;
15 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE15MFP_Msk) | SYS_GPE_MFPH_PE15MFP_ADC1_7;
16 PE->DINOFF = PE->DINOFF | (GPIO_DINOFF_DINOFF8_Msk | GPIO_DINOFF_DINOFF9_Msk | GPIO_DINOFF_DINOFF10_Msk |
17 GPIO_DINOFF_DINOFF11_Msk | GPIO_DINOFF_DINOFF12_Msk | GPIO_DINOFF_DINOFF13_Msk |
18 GPIO_DINOFF_DINOFF14_Msk | GPIO_DINOFF_DINOFF15_Msk); //Close PE8~15 digital input channel
19
20 SYS->VREFCTL = SYS_VREFCTL_VREF_AVDD; //Set the reference voltage to VDD
21 SYS_LockReg();
22 //--------------------------------------------- -------------------------------------
23 /* Enable the A/D converter */
24 EADC_Open(EADC,0); //EADC enable;
25 /* Configure the EADC1 sample module 0~7 for analog input channel 0~7 and software trigger source.*/
26 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE0, EADC_SOFTWARE_TRIGGER, EADC1_CH0);
27 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE1, EADC_SOFTWARE_TRIGGER, EADC1_CH1);
28 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE2, EADC_SOFTWARE_TRIGGER, EADC1_CH2);
29 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE3, EADC_SOFTWARE_TRIGGER, EADC1_CH3);
30 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE4, EADC_SOFTWARE_TRIGGER, EADC1_CH4);
31 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE5, EADC_SOFTWARE_TRIGGER, EADC1_CH5);
32 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE6, EADC_SOFTWARE_TRIGGER, EADC1_CH6);
33 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE7, EADC_SOFTWARE_TRIGGER, EADC1_CH7);
34
35 EADC_CLR_INT_FLAG(EADC, 0x02); //ADINT1 interrupt flag is cleared to 0
36 EADC_ENABLE_INT(EADC, 0x02); //Enable ADINT1
37 for(i=8;i<16;i++) //< span style="color: #008000;">AD1SPIE0:AD1SPIE7=[8:15]
38 {
39 EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 1, 1<//ADC Interrupt 1 source enable; ADC1-SAMPLE10~17 interrupt enable;
40 }
41 NVIC_EnableIRQ(EADC1_IRQn);
42 }
(2) Then there is the interrupt service function, as follows:< /p>
1 void EADC1_IRQHandler( void) //EADC1 interrupt Service function
2 {
3 EADC1_ConversionFlag = 1;
4 EADC_CLR_INT_FLAG(EADC, 0x02); //Clear the interrupt flag
5 }
(4) Since ADC1 is used, it also needs to be configured as EADC;< /p>
1 void ADC_Select(ADC_Source source)
2 {
3 SYS_UnlockReg();
4 if(source == ADC1_Select) //Select EADC1
5 {
6 /* Set to EADC mode */
7 SYS->VREFCTL = (SYS->VREFCTL & ~SYS_VREFCTL_ADCMODESEL_EADC) | SYS_VREFCTL_ADCMODESEL_EADC;
8 }
9 else //Select ADC0
10 {
11 SYS->VREFCTL = (SYS->VREFCTL & ~SYS_VREFCTL_ADCMODESEL_EADC) | SYS_VREFCTL_ADCMODESEL_ADC;
12 }
13 SYS_LockReg();
14 }
(5) Finally, you can write your own ADC processing function ;
1 unsigned int ADC1_Conversion_Process(ADC1_Channel ch) //EADC1 conversion processing
2 {
3 unsigned int EADC1_Conversion_Result;
4 EADC_START_CONV(EADC,1<<(ch+ 8)); // Start converting module 10~17
5 while(!EADC1_ConversionFlag); //Wait for the conversion to complete
6 EADC1_ConversionFlag = 0;
7 EADC1_Conversion_Result=EADC1_GET_CONV_DATA(EADC,ch); // span>Read the conversion result;
8 return EADC1_Conversion_Result;
9 }
(6) Finally put the header file;
1 #ifndef _MY_EADC_H
2 #define _MY_EADC_H
3 #include "NUC472_442.h"
4 //------------------------------------------------- ---------------------
5 #define ADC1_VREF 3300
6 #define ADC0_VREF 3300
7 //------------------------------------------------- ---------------------
8 typedef enum _ADC1_Channel{ADC1_CH0,ADC1_CH1,ADC1_CH2,ADC1_CH3,ADC1_CH4,ADC1_CH5,ADC1_CH6,ADC1_CH7}ADC1_Channel;
9 typedef enum _ADC_Source{ADC0_Select,ADC1_Select}ADC_Source;
10 //--------------------------------------------- -------------------------
11 void ADC1_Init(void);
12 unsigned int ADC1_Conversion_Process(ADC1_Channel ch);
13 void ADC_Select(ADC_Source source);
14 //--------------------------------------------- -------------------------
15 extern unsigned char EADC1_ConversionFlag;
16 //--------------------------------------------- -------------------------
17 #endif
(7) This time the software trigger conversion is used, of course, other trigger methods can also be used;
(8) In summary, the use of NUC442 is a bit simpler than STM32, and the functions are also compared. powerful.
1 void ADC1_Init( void)
2 {
3 unsigned char i=0;
4 SYS_UnlockReg();
5 CLK_EnableModuleClock(EADC_MODULE); //Enable EADC clock
6 CLK_SetModuleClock(EADC_MODULE, CLK_CLKSEL1_ADCSEL_PCLK, CLK_CLKDIV0_ADC(5));
7 //PE8~PE15 ADC1->Channel 0~7
8 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE8MFP_Msk) | SYS_GPE_MFPH_PE8MFP_ADC1_0;
9 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE9MFP_Msk) | SYS_GPE_MFPH_PE9MFP_ADC1_1;
10 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE10MFP_Msk) | SYS_GPE_MFPH_PE10MFP_ADC1_2;
11 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE11MFP_Msk) | SYS_GPE_MFPH_PE11MFP_ADC1_3;
12 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE12MFP_Msk) | SYS_GPE_MFPH_PE12MFP_ADC1_4;
13 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE13MFP_Msk) | SYS_GPE_MFPH_PE13MFP_ADC1_5;
14 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE14MFP_Msk) | SYS_GPE_MFPH_PE14MFP_ADC1_6;
15 SYS->GPE_MFPH = (SYS->GPE_MFPH & ~SYS_GPE_MFPH_PE15MFP_Msk) | SYS_GPE_MFPH_PE15MFP_ADC1_7;
16 PE->DINOFF = PE->DINOFF | (GPIO_DINOFF_DINOFF8_Msk | GPIO_DINOFF_DINOFF9_Msk | GPIO_DINOFF_DINOFF10_Msk |
17 GPIO_DINOFF_DINOFF11_Msk | GPIO_DINOFF_DINOFF12_Msk | GPIO_DINOFF_DINOFF13_Msk |
18 GPIO_DINOFF_DINOFF14_Msk | GPIO_DINOFF_DINOFF15_Msk); //Close PE8~15 digital input channel
19
20 SYS->VREFCTL = SYS_VREFCTL_VREF_AVDD; //Set the reference voltage to VDD
21 SYS_LockReg();
22 //--------------------------------------------- -------------------------------------
23 /* Enable the A/D converter */
24 EADC_Open(EADC,0); //EADC enable;
25 /* Configure the EADC1 sample module 0~7 for analog input channel 0~7 and software trigger source.*/
26 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE0, EADC_SOFTWARE_TRIGGER, EADC1_CH0);
27 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE1, EADC_SOFTWARE_TRIGGER, EADC1_CH1);
28 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE2, EADC_SOFTWARE_TRIGGER, EADC1_CH2);
29 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE3, EADC_SOFTWARE_TRIGGER, EADC1_CH3);
30 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE4, EADC_SOFTWARE_TRIGGER, EADC1_CH4);
31 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE5, EADC_SOFTWARE_TRIGGER, EADC1_CH5);
32 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE6, EADC_SOFTWARE_TRIGGER, EADC1_CH6);
33 EADC_ConfigSampleModule(EADC, EADC1_SAMPLE_MODULE7, EADC_SOFTWARE_TRIGGER, EADC1_CH7);
34
35 EADC_CLR_INT_FLAG(EADC, 0x02); //ADINT1 interrupt flag is cleared to 0
36 EADC_ENABLE_INT(EADC, 0x02); //Enable ADINT1
37 for(i=8;i<16;i++) //< span style="color: #008000;">AD1SPIE0:AD1SPIE7=[8:15]
38 {
39 EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 1, 1<//ADC Interrupt 1 source enable; ADC1-SAMPLE10~17 interrupt enable;
40 }
41 NVIC_EnableIRQ(EADC1_IRQn);
42 }
1 void EADC1_IRQHandler(void) //EADC1 interrupt service function
2 {
3 EADC1_ConversionFlag = 1;
4 EADC_CLR_INT_FLAG(EADC, 0x02); //Clear the interrupt flag
5 }
1< /span> void ADC_Select(ADC_Source source)
2 {
3 SYS_UnlockReg();
4 if(source == ADC1_Select) //Select EADC1
5 {
6 /* Set to EADC mode */
7 SYS->VREFCTL = (SYS->VREFCTL & ~SYS_VREFCTL_ADCMODESEL_EADC) | SYS_VREFCTL_ADCMODESEL_EADC;
8 }
9 else //Select ADC0
10 {
11 SYS->VREFCTL = (SYS->VREFCTL & ~SYS_VREFCTL_ADCMODESEL_EADC) | SYS_VREFCTL_ADCMODESEL_ADC;
12 }
13 SYS_LockReg();
14 }
1 unsigned int ADC1_Conversion_Process(ADC1_Channel ch) //EADC1 conversion processing
2 {
3 unsigned int EADC1_Conversion_Result;
4 EADC_START_CONV(EADC,1<<(ch+ 8)); // Start converting module 10~17
5 while(!EADC1_ConversionFlag); //Wait for the conversion to complete
6 EADC1_ConversionFlag = 0;
7 EADC1_Conversion_Result=EADC1_GET_CONV_DATA(EADC,ch); // span>Read the conversion result;
8 return EADC1_Conversion_Result;
9 }
1 #ifndef _MY_EADC_H
2 #define _MY_EADC_H
3 #include "NUC472_442.h"
4 //------------------------------------------------- ---------------------
5 #define ADC1_VREF 3300
6 #define ADC0_VREF 3300
7 //------------------------------------------------- ---------------------
8 typedef enum _ADC1_Channel{ADC1_CH0,ADC1_CH1,ADC1_CH2,ADC1_CH3,ADC1_CH4,ADC1_CH5,ADC1_CH6,ADC1_CH7}ADC1_Channel;
9 typedef enum _ADC_Source{ADC0_Select,ADC1_Select}ADC_Source;
10 //--------------------------------------------- -------------------------
11 void ADC1_Init(void);
12 unsigned int ADC1_Conversion_Process(ADC1_Channel ch);
13 void ADC_Select(ADC_Source source);
14 //--------------------------------------------- -------------------------
15 extern unsigned char EADC1_ConversionFlag;
16 //--------------------------------------------- -------------------------
17 #endif