void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable HIRC clock (Internal RC 48MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Wait for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Select HCLK clock source as HIRC and HCLK source divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable UART0 clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(UART1_MODULE);
CLK_EnableModuleClock(TMR0_MODULE);
CLK_EnableModuleClock(ADC_MODULE);
/* Switch UART0 clock source to HIRC */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_HIRC, CLK_CLKDIV0_UART0(1));
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HIRC, 0);
CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1));
/* Update System Core Clock */
SystemCoreClockUpdate();
SYS->GPA_MFPH = (SYS->GPA_MFPH & ~(SYS_GPA_MFPH_PA15MFP_Msk | SYS_GPA_MFPH_PA14MFP_Msk \
| SYS_GPA_MFPH_PA8MFP_Msk | SYS_GPA_MFPH_PA9MFP_Msk))| \
(SYS_GPA_MFPH_PA15MFP_UART0_RXD | SYS_GPA_MFPH_PA14MFP_UART0_TXD \
| SYS_GPA_MFPH_PA8MFP_UART1_RXD | SYS_GPA_MFPH_PA9MFP_UART1_TXD);
SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk | SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk|SYS_GPB_MFPL_PB4MFP_Msk | SYS_GPB_MFPL_PB5MFP_Msk | SYS_GPB_MFPL_PB6MFP_Msk | SYS_GPB_MFPL_PB7MFP_Msk)) |
(SYS_GPB_MFPL_PB0MFP_ADC0_CH0 | SYS_GPB_MFPL_PB1MFP_ADC0_CH1 | SYS_GPB_MFPL_PB2MFP_ADC0_CH2 | SYS_GPB_MFPL_PB3MFP_ADC0_CH3|SYS_GPB_MFPL_PB4MFP_ADC0_CH4 | SYS_GPB_MFPL_PB5MFP_ADC0_CH5 | SYS_GPB_MFPL_PB6MFP_ADC0_CH6 | SYS_GPB_MFPL_PB7MFP_ADC0_CH7);
GPIO_SetMode(PB, BIT0, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT1, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT2, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT3, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT4, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT5, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT6, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT7, GPIO_MODE_INPUT);
GPIO_DISABLE_DIGITAL_PATH(PB, BIT0|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6|BIT7);
/* Lock protected registers */
SYS_LockReg();
}
void adc_conversion(void)
{
ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE,0xff);
ADC_START_CONV(ADC);
ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
ADC_START_CONV(ADC);
delay_1ms(20);
ConversionData = ADC_GET_CONVERSION_DATA(ADC, 0);
printf("Conversion result of channel : 0x%X (%d)\n", ConversionData, ConversionData);
ADC_STOP_CONV(ADC);
ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
}
|