[应用方案] 八月新唐+在NuEDU上通过VCP上传显示ADC

[复制链接]
1795|11
 楼主| ysdx06010302 发表于 2017-8-19 18:15 | 显示全部楼层 |阅读模式
在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,主要是按照例程修改的,对发送和接收重新封装了几个函数,用起来更方便了。
zhuotuzi 发表于 2017-8-19 18:16 | 显示全部楼层
这贴新鲜的还没有上图呢。
 楼主| ysdx06010302 发表于 2017-8-19 18:18 | 显示全部楼层
我还不会上图,我再看看
 楼主| ysdx06010302 发表于 2017-8-19 18:18 | 显示全部楼层
 楼主| ysdx06010302 发表于 2017-8-19 18:21 | 显示全部楼层
使用VCP发送ADC
RTX截jjj.png
RTX截jjj.png
 楼主| ysdx06010302 发表于 2017-8-19 18:21 | 显示全部楼层
呼吸灯1
看绿色的灯
 楼主| ysdx06010302 发表于 2017-8-19 18:27 | 显示全部楼层
绿色的呼吸灯。
呼吸灯1.JPG
呼吸灯2.JPG
 楼主| ysdx06010302 发表于 2017-8-19 18:29 | 显示全部楼层
  1. int32_t main(void)
  2. {
  3.     uint8_t VCPReceive[16] = {0};
  4.     uint8_t VCPSend[16] = {0};
  5.    
  6.     SYS_Init();

  7.     USBD_Open(&gsInfo, VCOM_ClassRequest, NULL);

  8.     /* Endpoint configuration */
  9.     VCOM_Init();
  10.     USBD_Start();
  11.     NVIC_EnableIRQ(USBD_IRQn);

  12.     PWM_Init();
  13.     EADC_Init();
  14.     Timer1_Init();
  15.    
  16.     while(1)
  17.     {
  18.         if( VCom_Read( VCPReceive, 16 ) )
  19.         {
  20.             if( VCPReceive[0] == 0x5a )
  21.             {
  22.                 VCom_Write("八月,新唐你好!\r\n", 20);
  23.                 VCom_Write("Hello, 21ic.com !\r\n", 20);
  24.                 VCom_Write("MyID:ysdx06010302\r\n", 20);
  25.                
  26.                 VCom_Write("Start\r\n", 8);
  27.                
  28.                 gStart = 1;
  29.             }
  30.         }
  31.         
  32.         if( gStart )
  33.         {
  34.             if( nTimerCnt > 100 )  //100ms,读ADC,调整PWM占空比
  35.             {
  36.                 nTimerCnt = 0;
  37.                 if( EADC_GET_INT_FLAG(EADC, (1<<0)) )
  38.                 {
  39.                     /* Get the conversion result of the sample module 0 */
  40.                     i32ConversionData = EADC_GET_CONV_DATA(EADC, 0);
  41.                     
  42.                     i32ConversionData *= 3300;
  43.                     i32ConversionData /= 4096;
  44.                     
  45.                     /* trigger sample module 0 to start A/D conversion */
  46.                     EADC_START_CONV(EADC, (1<<0));
  47.                     
  48.                     // 发送AD值
  49.                     VCom_Write("AD Value:", 9);
  50.                     Int2Str(i32ConversionData, VCPSend);
  51.                     VCom_Write(VCPSend, 8);
  52.                 }
  53.                
  54.                 //调整占空比
  55.                 SetPwmPara(1, 125, (DutyBuf[DutyIndex] * 100) >> 8 );
  56.                
  57.                 DutyIndex++;
  58.                 if( DutyIndex >= 40 )
  59.                 {
  60.                     DutyIndex = 0;
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }
wanduzi 发表于 2017-8-19 22:15 | 显示全部楼层
带数字的圆盘是什么元件,没见过,电位器还是编码器?
huahuagg 发表于 2017-8-19 22:19 | 显示全部楼层
VCP是指的什么?
 楼主| ysdx06010302 发表于 2017-8-21 08:15 | 显示全部楼层

Virtual com port
huahuagg 发表于 2017-8-25 17:00 | 显示全部楼层
多谢赐教。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

39

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部