| Nano100BN Series BSP V3.03.002 The Board Support Package for Nano100BN Series

NuEdu-Basic01_ADC_Knob.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include "Nano100Series.h"
3 #include "NuEdu-Basic01_Volume_Knob.h"
4
5 #define _ADC_Source_Clock 12000000 // from 12MHz XTAL
6 uint32_t ADC_Clock = 300000; // 300 kHz, ADC_F_Max = 42M
7 uint32_t Open_Volume_Knob_Fail = FALSE;
8
9 void Open_Volume_Knob(void)
10 {
11 uint32_t ADC_Source_Clock_DIV;
12
13 //Initial ADC Function Pin
14 SYS->PA_L_MFP = (SYS->PA_L_MFP & ~SYS_PA_L_MFP_PA0_MFP_Msk) | SYS_PA_L_MFP_PA0_MFP_ADC_CH0;
15 GPIO_DISABLE_DIGITAL_PATH(PA, BIT0);
16
17 //Initial ADC Clock Source
18 while(ADC_IS_BUSY(ADC)); //Safe for other unknown ADC device
19 SYS_UnlockReg();
20 ADC_Source_Clock_DIV = _ADC_Source_Clock / ADC_Clock;
21 if(ADC_Source_Clock_DIV>256)
22 {
23 Open_Volume_Knob_Fail = TRUE;
24 ADC_Source_Clock_DIV = 256;
25 ADC_Clock = _ADC_Source_Clock / 256;
26 printf("\nADC Clock Initial Fail!\n");
27 }
28 else
29 {
30 Open_Volume_Knob_Fail = FALSE;
31 // printf("\nADC Clock Initial Pass!\n");
32 }
33
34 // SYS_ResetModule(ADC_RST); //Donot Reset ADC, Safe for other unknown ADC device
35 CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HXT, CLK_ADC_CLK_DIVIDER(ADC_Source_Clock_DIV));
36 CLK_EnableModuleClock(ADC_MODULE);
37 SYS_LockReg();
38
39 //Initial ADC Peripheral
40 ADC_Open(ADC, ADC_INPUT_MODE_SINGLE_END, ADC_OPERATION_MODE_SINGLE_CYCLE, ADC_CH_0_MASK);
41 ADC_SET_RESOLUTION(ADC, ADC_RESSEL_12_BIT);
42 ADC_SET_REF_VOLTAGE(ADC, ADC_REFSEL_POWER);
43 ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
44 ADC_POWER_ON(ADC);
45 }
46
47 void Close_Volume_Knob(void)
48 {
49 //Close ADC Function Pin
50 SYS->PA_L_MFP &= ~SYS_PA_L_MFP_PA0_MFP_Msk;
51 GPIO_ENABLE_DIGITAL_PATH(PA, BIT0);
52 // _ADC_DISABLE_CHANNEL(7);
53
54 if(!(ADC->CHEN&ADC_CHEN_Msk)) //Donot Cloce ADC, Safe for other unknown ADC device
55 {
56 //Close ADC Clock Source
57 SYS_UnlockReg();
58 SYS_ResetModule(ADC_RST);
59 CLK_DisableModuleClock(ADC_MODULE);
60 SYS_LockReg();
61 }
62 }
63
64 uint32_t Get_Volume_Knob(void)
65 {
66 uint32_t ADC_Raw_Data;
67
68 //Start ADC Coversion
69 ADC_START_CONV(ADC);
70 while(!(ADC_GET_INT_FLAG(ADC, ADC_ADF_INT)));
71 ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
72 ADC_Raw_Data = ADC_GET_CONVERSION_DATA(ADC, 0);
73
74 return ADC_Raw_Data;
75 }
CLK_CLKSEL1_ADC_S_HXT
#define CLK_CLKSEL1_ADC_S_HXT
Definition: clk.h:132
ADC_POWER_ON
#define ADC_POWER_ON(adc)
Power on ADC module.
Definition: adc.h:166
ADC_GET_INT_FLAG
#define ADC_GET_INT_FLAG(adc, u32Mask)
Return the user-specified interrupt flags.
Definition: adc.h:104
ADC_RST
#define ADC_RST
Definition: sys.h:42
ADC_OPERATION_MODE_SINGLE_CYCLE
#define ADC_OPERATION_MODE_SINGLE_CYCLE
Definition: adc.h:66
CLK_EnableModuleClock
void CLK_EnableModuleClock(uint32_t u32ModuleIdx)
This function enable module clock.
Definition: clk.c:436
Close_Volume_Knob
void Close_Volume_Knob(void)
Definition: NuEdu-Basic01_ADC_Knob.c:47
ADC_ADF_INT
#define ADC_ADF_INT
Definition: adc.h:60
ADC_Clock
uint32_t ADC_Clock
Definition: NuEdu-Basic01_ADC_Knob.c:6
CLK_DisableModuleClock
void CLK_DisableModuleClock(uint32_t u32ModuleIdx)
This function disable module clock.
Definition: clk.c:478
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: Nano100Series.h:11863
ADC_SET_REF_VOLTAGE
#define ADC_SET_REF_VOLTAGE(adc, u32Ref)
Set the reference voltage selection.
Definition: adc.h:297
ADC_SET_RESOLUTION
#define ADC_SET_RESOLUTION(adc, u32Resolution)
Set the resolution of conversion result.
Definition: adc.h:285
ADC_Open
void ADC_Open(ADC_T *adc, uint32_t u32InputMode, uint32_t u32OpMode, uint32_t u32ChMask)
This API configures ADC module to be ready for convert the input from selected channel.
Definition: adc.c:41
Get_Volume_Knob
uint32_t Get_Volume_Knob(void)
Definition: NuEdu-Basic01_ADC_Knob.c:64
Nano100Series.h
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
BIT0
#define BIT0
Bit 0 mask of an 32 bit integer.
Definition: Nano100Series.h:11869
CLK_ADC_CLK_DIVIDER
#define CLK_ADC_CLK_DIVIDER(x)
Definition: clk.h:191
SYS_PA_L_MFP_PA0_MFP_ADC_CH0
#define SYS_PA_L_MFP_PA0_MFP_ADC_CH0
Definition: sys.h:147
Open_Volume_Knob_Fail
uint32_t Open_Volume_Knob_Fail
Definition: NuEdu-Basic01_ADC_Knob.c:7
SYS_UnlockReg
__STATIC_INLINE void SYS_UnlockReg(void)
Disable register write-protection function.
Definition: sys.h:826
NuEdu-Basic01_Volume_Knob.h
ADC_CLR_INT_FLAG
#define ADC_CLR_INT_FLAG(adc, u32Mask)
This macro clear the selected interrupt status bits.
Definition: adc.h:116
ADC_IS_BUSY
#define ADC_IS_BUSY(adc)
Get the busy state of ADC.
Definition: adc.h:128
Open_Volume_Knob
void Open_Volume_Knob(void)
Definition: NuEdu-Basic01_ADC_Knob.c:9
ADC_RESSEL_12_BIT
#define ADC_RESSEL_12_BIT
Definition: adc.h:73
ADC_REFSEL_POWER
#define ADC_REFSEL_POWER
Definition: adc.h:74
ADC_MODULE
#define ADC_MODULE
Definition: clk.h:305
ADC_INPUT_MODE_SINGLE_END
#define ADC_INPUT_MODE_SINGLE_END
Definition: adc.h:63
ADC_GET_CONVERSION_DATA
#define ADC_GET_CONVERSION_DATA(adc, u32ChNum)
Get the latest ADC conversion data.
Definition: adc.h:92
ADC_CH_0_MASK
#define ADC_CH_0_MASK
Definition: adc.h:33
ADC_START_CONV
#define ADC_START_CONV(adc)
Start the A/D conversion.
Definition: adc.h:253
PA
#define PA
Pointer to GPIO port A register structure.
Definition: Nano100Series.h:11695
_ADC_Source_Clock
#define _ADC_Source_Clock
Definition: NuEdu-Basic01_ADC_Knob.c:5
ADC_CHEN_Msk
#define ADC_CHEN_Msk
Definition: adc.h:51
SYS_LockReg
__STATIC_INLINE void SYS_LockReg(void)
Enable register write-protection function.
Definition: sys.h:843
ADC
#define ADC
Pointer to ADC register structure.
Definition: Nano100Series.h:11684
TRUE
#define TRUE
Boolean true, define to use in API parameters or return value.
Definition: Nano100Series.h:11862
SYS_ResetModule
void SYS_ResetModule(uint32_t u32ModuleIndex)
This function reset selected modules.
Definition: sys.c:129
GPIO_DISABLE_DIGITAL_PATH
#define GPIO_DISABLE_DIGITAL_PATH(gpio, u32PinMask)
Disable I/O Digital Input Path.
Definition: gpio.h:241
GPIO_ENABLE_DIGITAL_PATH
#define GPIO_ENABLE_DIGITAL_PATH(gpio, u32PinMask)
Enable I/O Digital Input Path.
Definition: gpio.h:253
CLK_SetModuleClock
void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
This function set selected module clock source and module clock divider.
Definition: clk.c:339
SYS_PA_L_MFP_PA0_MFP_Msk
#define SYS_PA_L_MFP_PA0_MFP_Msk
Definition: Nano100Series.h:3053
SYS
#define SYS
Pointer to SYS register structure.
Definition: Nano100Series.h:11692