[DemoCode下载] PWM下的ADC采样

[复制链接]
1024|6
 楼主| zhuomuniao110 发表于 2019-5-26 22:44 | 显示全部楼层 |阅读模式
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V0.10
  4. * $Revision: 2 $
  5. * $Date: 14/01/28 11:43a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    ADC and PWM sample for M051 learning board
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"
  13. #include "lcd_driver.h"

  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/

  19.     /* Unlock protected registers */
  20.     SYS_UnlockReg();

  21.     /* Enable ADC and PWM23 clock */
  22.     CLK->APBCLK = CLK_APBCLK_PWM23_EN_Msk | CLK_APBCLK_ADC_EN_Msk;

  23.     /* ADC clock source is 22.1184MHz, set divider to (3 + 1), ADC clock is 22.1184/4 MHz */
  24.     CLK->CLKDIV |= (3 << CLK_CLKDIV_ADC_N_Pos);

  25.     /* Update System Core Clock */
  26.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CycylesPerUs automatically. */
  27.     SystemCoreClockUpdate();

  28.     /*---------------------------------------------------------------------------------------------------------*/
  29.     /* Init I/O Multi-function                                                                                 */
  30.     /*---------------------------------------------------------------------------------------------------------*/
  31.     /* Set P4.3 to PWM3 function */
  32.     SYS->P4_MFP = SYS_MFP_P43_PWM3;
  33.     /* Set P1.0 to AIN0 function */
  34.     SYS->P1_MFP = SYS_MFP_P10_AIN0;

  35.     /* Disable digital input path of analog pin AIN0 to prevent leakage */
  36.     P1->OFFD |= 1 << GPIO_OFFD_OFFD_Pos;

  37.     /* Lock protected registers */
  38.     SYS_LockReg();
  39. }


  40. void PWM_Init(void)
  41. {
  42.     /* Set PWM 3 clock prescale to 60, and divider to 1*/
  43.     PWMA->PPR = 60 << PWM_PPR_CP23_Pos;
  44.     PWMA->CSR = PWM_CLK_DIV_1 << PWM_CSR_CSR3_Pos;

  45.     /* Enable PWM3 and set to auto-reload mode.
  46.        Configure PWM mode before setting CNR, CMR. Otherwise CNR will be reset */
  47.     PWMA->PCR = PWM_PCR_CH3EN_Msk | PWM_PCR_CH3MOD_Msk;
  48.     /* PWM 3 frequency = HCLK / (prescale * divider * (CNR + 1))
  49.        PWM 3 duty ratio = (CMR + 1) / (CNR + 1) */
  50.     PWMA->CNR3 = 1843 - 1;
  51.     PWMA->CMR3 = 1833 - 1;

  52.     /* Enable PWM3 Output */
  53.     PWMA->POE = PWM_POE_PWM3_Msk;

  54. }

  55. void ADC_Init(void)
  56. {
  57.     /* Set to convert ADC channel 0 */
  58.     ADC->ADCHER = 0x1;
  59.     /* Enable the ADC converter */
  60.     ADC->ADCR = ADC_ADCR_ADEN_Msk;

  61. }

  62. /* Main function  */
  63. int main(void)
  64. {
  65.     uint32_t u32Data, u32Sum, u32Count;
  66.     char adc_value[20];

  67.     /* Init System, IP clock and multi-function I/O
  68.        In the end of SYS_Init() will issue SYS_LockReg()
  69.        to lock protected register. If user want to write
  70.        protected register, please issue SYS_UnlockReg()
  71.        to unlock protected register if necessary */
  72.     SYS_Init();

  73.     /* Initial LCD panel. SPI clock and interface is configured in this function. */
  74.     LCD_Init();
  75.     /* Clear LCD screen. Do this before turn back light on */
  76.     LCD_ClearScreen();
  77.     /* Enable LCD back light */
  78.     LCD_EnableBackLight();
  79.     /* Show messages on LCD */
  80.     LCD_Print(0, "ADC_PWM");
  81.     LCD_Print(1, "Sample code");
  82.     LCD_Print(2, "ADC Value:");

  83.     /* Init PWM channel 3 */
  84.     PWM_Init();

  85.     /* Init ADC to get the value of variable resistor */
  86.     ADC_Init();

  87.     u32Count = 0;
  88.     u32Sum = 0;

  89.     while(1)
  90.     {
  91.         /* Start convert */
  92.         ADC_START_CONV(ADC);

  93.         /* Waiting for convert complete */
  94.         while(ADC_IS_BUSY(ADC));

  95.         /* Read the result from ADC */
  96.         u32Data = ADC->ADDR[0] & ADC_ADDR_RSLT_Msk;

  97.         /* Determine the sum */
  98.         u32Sum += u32Data;
  99.         u32Count++;
  100.         if(u32Count == 128)
  101.         {
  102.             /* Determine the average */
  103.             u32Data = u32Sum / 128;
  104.             /* Update the average ADC conversion result to LCD */
  105.             sprintf(adc_value, "          %4d", u32Data);
  106.             LCD_Print(3, adc_value);
  107.             /* Adjust the duty cycle of PWM3 according to the average ADC value. */
  108.             PWMA->CMR3 = (u32Data * PWMA->CNR3) / 4096;
  109.             /* Reset count and sum */
  110.             u32Count = 0;
  111.             u32Sum = 0;
  112.         }
  113.     }
  114. }

  115. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/




 楼主| zhuomuniao110 发表于 2019-5-26 22:44 | 显示全部楼层
这个就是两者结合的参考例子,非常棒,新唐这个例子提供,比PIC单片机强多了。
 楼主| zhuomuniao110 发表于 2019-5-26 22:45 | 显示全部楼层
很符合中国人需求,哈哈。
21mengnan 发表于 2019-5-27 19:19 | 显示全部楼层
这个有什么现实意义
equivalent 发表于 2019-5-29 20:38 | 显示全部楼层
没太明白这是准备干什么
稳稳の幸福 发表于 2019-5-29 22:31 | 显示全部楼层
看懂了,是根据ADC设置PWM占空比。
稳稳の幸福 发表于 2019-5-29 22:32 | 显示全部楼层
PWMA->CMR3 = (u32Data * PWMA->CNR3) / 4096;
看这个是ADC的结果作为一个参数给占空比了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

233

主题

3529

帖子

11

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