Discuz! Board

标题: 用pwm控制无源蜂鸣器出现的问题 [打印本页]

作者: kallyshall    时间: 2016-5-7 09:30
标题: 用pwm控制无源蜂鸣器出现的问题
  1. 主函数
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include "nrf.h"
  5. #include "nrf_gpio.h"
  6. #include "nrf_delay.h"
  7. #include "nrf_pwm.h"
  8. #include "boards.h"

  9. #define FREQ_HALF_NOTE_FACTOR 1.059463f
  10. nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;

  11. void pwm_init(nrf_pwm_mode_t nrf_pwm_mode)
  12. {
  13.     pwm_config.mode             = nrf_pwm_mode;
  14.     // Initialize the PWM library
  15.     nrf_pwm_init(&pwm_config);   
  16.           nrf_delay_ms(300);
  17. }
  18.    
  19. int main(void)
  20. {

  21.           pwm_config.num_channels     = 4;
  22.     pwm_config.gpio_num[0]      = 7;
  23.           pwm_config.gpio_num[1]      = 6;
  24.           pwm_config.gpio_num[2]      = 5;
  25.           pwm_config.gpio_num[3]      = 4;
  26.        
  27.     NRF_TIMER1->MODE =TIMER_MODE_MODE_Timer;
  28.     // Start the external 16 MHz clock for a more accurate PWM frequency
  29.     NRF_CLOCK->TASKS_HFCLKSTART = 1;
  30.    
  31.     //pwm_init(PWM_MODE_C);
  32.    
  33.     while (true)
  34.     {
  35.                         pwm_init(PWM_MODE_C);
  36.                         pwm_init(PWM_MODE_D);
  37.                         pwm_init(PWM_MODE_E);
  38.                         pwm_init(PWM_MODE_F);
  39. //                        pwm_init(PWM_MODE_G);
  40. //                        pwm_init(PWM_MODE_A);
  41. //                        pwm_init(PWM_MODE_B);
  42.     }
  43. }<div class="blockcode"><blockquote>
  44. uint32_t nrf_pwm_init(nrf_pwm_config_t *config)
  45. {
  46.     if(config->num_channels == 0 || config->num_channels > PWM_MAX_CHANNELS) return 0xFFFFFFFF;
  47.    
  48.     switch(config->mode)
  49.     {
  50.         case PWM_MODE_C:   // 8-bit resolution, 520Hz PWM frequency, 1us  1923
  51.             PWM_TIMER->PRESCALER = 4;
  52.             pwm_max_value = 1923;
  53.                                     nrf_pwm_set_value(0, 960);
  54.             break;
  55.         case PWM_MODE_D:   // 8-bit resolution, 585Hz PWM frequency, 1us   1709
  56.             PWM_TIMER->PRESCALER = 4;
  57.             pwm_max_value = 1709;  
  58.                                     nrf_pwm_set_value(1, 850);
  59.             break;
  60.         case PWM_MODE_E:  // 0-1000 resolution, 650Hz PWM frequency, 1us   1538
  61.             PWM_TIMER->PRESCALER = 4;
  62.             pwm_max_value = 1538;
  63.                                     nrf_pwm_set_value(2, 765);
  64.             break;
  65.         case PWM_MODE_F:   // 0-100 resolution, 693Hz PWM frequency, 1us
  66.             PWM_TIMER->PRESCALER = 4;
  67.             pwm_max_value = 1443;
  68.                                     nrf_pwm_set_value(3, 720);
  69.             break;
  70.         case PWM_MODE_G:    // 8-bit resolution, 780Hz PWM frequency, 1us
  71.             PWM_TIMER->PRESCALER = 4;
  72.             pwm_max_value = 1282;
  73.                                      nrf_pwm_set_value(0, 640);
  74.             break;
  75.         case PWM_MODE_A:  // 8-bit resolution, 867Hz PWM frequency, 1us
  76.             PWM_TIMER->PRESCALER = 4;
  77.             pwm_max_value = 1153;
  78.                                     nrf_pwm_set_value(0, 575);
  79.             break;
  80.                                 case PWM_MODE_B:  // 8-bit resolution, 975Hz PWM frequency, 1us
  81.             PWM_TIMER->PRESCALER = 4;
  82.             pwm_max_value = 1026;
  83.                                     nrf_pwm_set_value(0, 510);
  84.             break;
  85.         default:
  86.             return 0xFFFFFFFF;
  87.     }
  88.     pwm_cc_update_margin_ticks = pwm_cc_margin_by_prescaler[PWM_TIMER->PRESCALER];
  89.     pwm_num_channels = config->num_channels;
  90.     for(int i = 0; i < pwm_num_channels; i++)
  91.     {
  92.         pwm_io_ch[i] = (uint32_t)config->gpio_num[i];
  93.         nrf_gpio_cfg_output(pwm_io_ch[i]);
  94.         pwm_running[i] = 0;      
  95.         pwm_gpiote_channel[i] = config->gpiote_channel[i];        
  96.     }
  97.     PWM_TIMER->TASKS_CLEAR = 1;
  98.     PWM_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
  99.     PWM_TIMER->CC[2] = pwm_next_max_value = pwm_max_value;
  100.           PWM_TIMER->MODE = TIMER_MODE_MODE_Timer;
  101.     PWM_TIMER->SHORTS = TIMER_SHORTS_COMPARE2_CLEAR_Msk;
  102.     PWM_TIMER->EVENTS_COMPARE[0] = PWM_TIMER->EVENTS_COMPARE[1] = PWM_TIMER->EVENTS_COMPARE[2] = PWM_TIMER->EVENTS_COMPARE[3] = 0;     
  103.    
  104.     if(pwm_num_channels > 2)
  105.     {
  106.         PWM_TIMER2->TASKS_CLEAR = 1;
  107.         PWM_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
  108.         PWM_TIMER2->CC[2] = pwm_next_max_value = pwm_max_value;
  109.         PWM_TIMER2->MODE = TIMER_MODE_MODE_Timer;
  110.         PWM_TIMER2->SHORTS = TIMER_SHORTS_COMPARE2_CLEAR_Msk;
  111.         PWM_TIMER2->EVENTS_COMPARE[0] = PWM_TIMER2->EVENTS_COMPARE[1] = PWM_TIMER2->EVENTS_COMPARE[2] = PWM_TIMER2->EVENTS_COMPARE[3] = 0;            
  112.         PWM_TIMER2->PRESCALER = PWM_TIMER->PRESCALER;
  113.     }

  114.     for(int i = 0; i < pwm_num_channels && i < 2; i++)
  115.     {
  116.         ppi_enable_channel(config->ppi_channel[i*2],  &PWM_TIMER->EVENTS_COMPARE[i], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);
  117.         ppi_enable_channel(config->ppi_channel[i*2+1],&PWM_TIMER->EVENTS_COMPARE[2], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);  
  118.         pwm_modified[i] = false;        
  119.     }
  120.     for(int i = 2; i < pwm_num_channels; i++)
  121.     {
  122.         ppi_enable_channel(config->ppi_channel[i*2],  &PWM_TIMER2->EVENTS_COMPARE[i-2], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);
  123.         ppi_enable_channel(config->ppi_channel[i*2+1],&PWM_TIMER2->EVENTS_COMPARE[2], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);  
  124.         pwm_modified[i] = false;        
  125.     }
  126. #if(USE_WITH_SOFTDEVICE == 1)
  127.     sd_radio_session_open(nrf_radio_signal_callback);
  128. #else
  129.     NVIC_SetPriority(PWM_IRQn, 0);
  130.     NVIC_EnableIRQ(PWM_IRQn);
  131. #endif
  132.     apply_pan73_workaround(PWM_TIMER, true);
  133.     PWM_TIMER->TASKS_START = 1;
  134.     if(pwm_num_channels > 2)
  135.     {
  136.         apply_pan73_workaround(PWM_TIMER2, true);
  137.         PWM_TIMER2->TASKS_START = 1;
  138.     }
  139.     return 0;
  140. }
复制代码
  1. <p><p></p>
复制代码
……设想使用蜂鸣器奏出乐音  但是最终配置出的四个通道频率相同占空比不同   若更改时钟的分频值和max_value  那么四个通道的值都会变成按照最大分频数  最大max_value设置 而出现的波形

求解……  




作者: 18942328508    时间: 2018-11-26 18:17
请问解决了吗

作者: 18942328508    时间: 2018-11-26 18:19
18942328508 发表于 2018-11-26 18:17
请问解决了吗

刚好我也用52832控制无源蜂鸣器遇到了问题
作者: admin    时间: 2018-11-26 19:35
哪个芯片?




欢迎光临 Discuz! Board (http://qfv8.com/) Powered by Discuz! X3