ADC_ContinuousScan (void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SysInit(); /* Init UART0 for printf */ UART0_Init(); printf("\nThis sample code demonstrate ADC continuous scan conversion\n"); printf("It convert channel 0,1,2 continuously and print conversion result\n"); // Enable channel 0,1,2 ADC_Open(ADC, ADC_INPUT_MODE_SINGLE_END, ADC_OPERATION_MODE_CONTINUOUS, (ADC_CH_0_MASK | ADC_CH_1_MASK | ADC_CH_2_MASK)); // Power on ADC ADC_POWER_ON(ADC); // Enable ADC ADC_IF interrupt ADC_EnableInt(ADC, ADC_ADF_INT); NVIC_EnableIRQ(ADC_IRQn); ADC_START_CONV(ADC); while(1); }
void initialize() { IO::pinMode(MUX_ADR0_PIN, OUTPUT); IO::pinMode(MUX_ADR1_PIN, OUTPUT); IO::pinMode(MUX_ADR2_PIN, OUTPUT); IO::pinMode(V_IN_PIN, ANALOG_INPUT); IO::pinMode(OUTPUT_VOLTAGE_MINUS_PIN, ANALOG_INPUT); IO::pinMode(SMPS_CURRENT_PIN, ANALOG_INPUT); IO::pinMode(OUTPUT_VOLTAGE_PLUS_PIN, ANALOG_INPUT); IO::pinMode(DISCHARGE_CURRENT_PIN, ANALOG_INPUT); IO::pinMode(MUX0_Z_D_PIN, ANALOG_INPUT_DISCHARGE); IO::digitalWrite(MUX0_Z_D_PIN, 0); //initialize internal temperature sensor SYS->TEMPCR |= 1; //initialize TIMER 1 (mux ADC capacitor discharge) CLK_EnableModuleClock(TMR1_MODULE); CLK_SetModuleClock(TMR1_MODULE,CLK_CLKSEL1_TMR1_S_HCLK,CLK_CLKDIV_UART(1)); //TODO: 50kHz ?? TIMER_Open(TIMER1, TIMER_ONESHOT_MODE, 1000000 / ADC_CAPACITOR_DISCHARGE_DELAY_US); TIMER_EnableInt(TIMER1); NVIC_EnableIRQ(TMR1_IRQn); NVIC_SetPriority(TMR1_IRQn, ADC_C_DISCHARGE_IRQ_PRIORITY); //initialize ADC //init clock CLK_EnableModuleClock(ADC_MODULE); CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HCLK, CLK_CLKDIV_ADC(CLK_GetHCLKFreq()/ADC_CLOCK_FREQUENCY)); //__HXT/ADC_CLOCK_FREQUENCY)); /* Set the ADC operation mode as burst, input mode as single-end and enable the analog input channel 2 */ ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_BURST, 0x1 << 2); ADC_SET_DMOF(ADC, ADC_ADCR_DMOF_UNSIGNED_OUTPUT); /* Power on ADC module */ ADC_POWER_ON(ADC); /* clear the A/D interrupt flag for safe */ ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Enable the ADC interrupt */ ADC_EnableInt(ADC, ADC_ADF_INT); NVIC_EnableIRQ(ADC_IRQn); NVIC_SetPriority(ADC_IRQn, ADC_IRQ_PRIORITY); current_input_ = 0; startConversion(); }
int32_t main (void) { uint32_t u32Result; /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SYS_Init(); /* Init UART0 for printf */ UART0_Init(); printf("\n\nCPU @ %dHz\n", SystemCoreClock); printf("\nThis sample code demonstrate ADC single mode conversion\n"); printf("It convert channel 0 and print conversion result\n"); // Enable channel 0 ADC_Open(ADC, 0, ADC_OPERATION_MODE_SINGLE, ADC_CH_0_MASK); // Set reference voltage to AVDD ADC_SET_REF_VOLTAGE(ADC, ADC_REFSEL_POWER); // Power on ADC ADC_POWER_ON(ADC); // Enable ADC ADC_IF interrupt ADC_EnableInt(ADC, ADC_ADF_INT); NVIC_EnableIRQ(ADC_IRQn); u8ADF = 0; ADC_START_CONV(ADC); while (u8ADF == 0); u32Result = ADC_GET_CONVERSION_DATA(ADC, 0); printf("Channel 0 conversion result is 0x%x\n",u32Result); ADC_DisableInt(ADC, ADC_ADF_INT); while(1); }
int32_t main (void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); printf("\nThis sample code demonstrate PWM channel 0 trigger ADC function\n"); /* Enable channel 5 */ ADC_Open(ADC, 0, 0, 0x01 << 5); /* Power on ADC */ ADC_POWER_ON(ADC); /* Enable PWM trigger */ ADC_EnableHWTrigger(ADC, ADC_TRIGGER_BY_PWM, ADC_FALLING_EDGE_TRIGGER); /* Enable ADC convert complete interrupt */ ADC_EnableInt(ADC, ADC_ADIF_INT); NVIC_EnableIRQ(ADC_IRQn); /* PWM frequency is 100Hz, duty 30% */ PWM_ConfigOutputChannel(PWM, 0, 100, 30); /* Enable output PWM channel 0 */ PWM_EnableOutput(PWM, 0x1); /* Set PWM channel 0 to center-aligned mode */ PWM_SET_ALIGNED_TYPE(PWM, 0, PWM_CENTER_ALIGNED); /* Enable PWM channel 0 center-triggered ADC */ PWM_EnableADCTrigger(PWM, 0, PWM_TRIGGER_ADC_CNTR_IS_CNR); /* PWM Start */ PWM_Start(PWM, 0x1); while(1); }