在NuEDU开发板上,使用VCP上传ADC值,同时还有呼吸灯,下面是图片
开发过程中,用到了,定时器、adc、pwm输出还有VCP。
发送间隔是有Timer1定时控制的,下面是设置
//定时器初始化
void Timer1_Init(void)
{
//Enable Timer0 clock and select Timer0 clock source
CLK_EnableModuleClock(TMR1_MODULE);
CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_PCLK0, 0);
//Initial Timer0 to periodic mode with 100Hz(10ms)
TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 100);
//Enable Timer0 interrupt
TIMER_EnableInt(TIMER1);
NVIC_EnableIRQ(TMR1_IRQn);
//Start Timer0
TIMER_Start(TIMER1);
}
Adc,通道6,连续采样,软件触发
//初始化 AD(PB9 EADC_CH6)
void EADC_Init(void)
{
/* Enable EADC module clock */
CLK_EnableModuleClock(EADC_MODULE);
/* EADC clock source is HCLK(72MHz), set divider to 8, ADC clock is 72/8 MHz */
CLK_SetModuleClock(EADC_MODULE, 0, CLK_CLKDIV0_EADC(4));
/* Configure the GPB9 for ADC analog input pins. */
SYS->GPB_MFPH &= ~SYS_GPB_MFPH_PB9MFP_Msk;
SYS->GPB_MFPH |= SYS_GPB_MFPH_PB9MFP_EADC_CH6;
/* Disable the GPB9 digital input path to avoid the leakage current. */
GPIO_DISABLE_DIGITAL_PATH(PB, BIT9);
/* Set the ADC internal sampling time, input mode as single-end and enable the A/D converter */
EADC_Open(EADC, EADC_CTL_DIFFEN_SINGLE_END);
EADC_SetInternalSampleTime(EADC, 12);
/* Configure the sample module 0 for analog input channel 6 and software trigger source.*/
EADC_ConfigSampleModule(EADC, 0, EADC_SOFTWARE_TRIGGER, 6);
EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, (1<<0));//Enable sample module 0 interrupt.
/* trigger sample module 0 to start A/D conversion */
EADC_START_CONV(EADC, (1<<0));
}
呼吸灯,原理就是定时去调整PWM的占空比,下面是PWM设置
/*---------------------------------------------------------------------------------------------------------*/
/* Init PWM */
/*---------------------------------------------------------------------------------------------------------*/
void PWM_Init(void)
{
/* Set PC10 multi-function pins for PWM1 Channel 1 */
SYS->GPC_MFPH &= ~SYS_GPC_MFPH_PC10MFP_Msk;
SYS->GPC_MFPH |= SYS_GPC_MFPH_PC10MFP_PWM1_CH1;
/* Enable PWM module clock */
CLK_EnableModuleClock(PWM1_MODULE);
/* Select PWM module clock source */
CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0);
/* Reset PWM1 channel 1 */
SYS_ResetModule(PWM1_RST);
}
//设置PWM的占空比
void SetPwmPara(unsigned Enable, unsigned int Frequence, unsigned int Duty)
{
/* set PWM1 channel 1 output configuration */
PWM_ConfigOutputChannel(PWM1, 1, Frequence, Duty);
// Start
PWM_Start(PWM1, 1 << 1);
if(Enable == 1)
/* Enable PWM Output path for PWM1 channel 1 */
PWM_EnableOutput(PWM1, 1 << 1);
else
PWM_DisableOutput(PWM1, 1 << 1);
}
VCP,主要是按照例程修改的,对发送和接收重新封装了几个函数,用起来更方便了。
|