自己修改的新唐例子程序,但发现无法采集,程序如下 ,求大神解答
#include <stdio.h>
#include "M051Series.h"
#include "lcd_driver.h"
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable ADC and PWM23 clock */
CLK->APBCLK = CLK_APBCLK_PWM23_EN_Msk | CLK_APBCLK_ADC_EN_Msk;
/* ADC clock source is 22.1184MHz, set divider to (3 + 1), ADC clock is 22.1184/4 MHz */
CLK->CLKDIV |= (3 << CLK_CLKDIV_ADC_N_Pos);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CycylesPerUs automatically. */
SystemCoreClockUpdate();
/*---------------------------------------------------------------------------------------------------------*/
/* Set P1.0 to AIN0 function */
SYS->P1_MFP = SYS_MFP_P10_AIN0;
/* Disable digital input path of analog pin AIN0 to prevent leakage */
P1->OFFD |= 1 << GPIO_OFFD_OFFD_Pos;
/* Lock protected registers */
SYS_LockReg();
}
void ADC_Init(void)
{
/* Set to convert ADC channel 0 */
ADC->ADCHER = 0x1;
/* Enable the ADC converter */
ADC->ADCR = ADC_ADCR_ADEN_Msk;
}
/* Main function */
int main(void)
{
uint32_t u32Data, u32Sum, u32Count;
// char adc_value[20];
/* 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 ADC to get the value of variable resistor */
ADC_Init();
u32Data = 0 ;
u32Count = 0;
u32Sum = 0;
while(1)
{
/* Start convert */
ADC_START_CONV(ADC);
/* Waiting for convert complete */
while(ADC_IS_BUSY(ADC));
/* Read the result from ADC */
u32Data = ADC->ADDR[0] & ADC_ADDR_RSLT_Msk;
/* Determine the sum */
u32Sum += u32Data;
u32Count++;
if(u32Count == 128)
{
/* Determine the average */
u32Data = u32Sum / 128;
/* Reset count and sum */
u32Count = 0;
u32Sum = 0;
}
}
} |