|
- 主函数
- #include <stdbool.h>
- #include <stdint.h>
- #include "nrf.h"
- #include "nrf_gpio.h"
- #include "nrf_delay.h"
- #include "nrf_pwm.h"
- #include "boards.h"
- #define FREQ_HALF_NOTE_FACTOR 1.059463f
- nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;
- void pwm_init(nrf_pwm_mode_t nrf_pwm_mode)
- {
- pwm_config.mode = nrf_pwm_mode;
- // Initialize the PWM library
- nrf_pwm_init(&pwm_config);
- nrf_delay_ms(300);
- }
-
- int main(void)
- {
- pwm_config.num_channels = 4;
- pwm_config.gpio_num[0] = 7;
- pwm_config.gpio_num[1] = 6;
- pwm_config.gpio_num[2] = 5;
- pwm_config.gpio_num[3] = 4;
-
- NRF_TIMER1->MODE =TIMER_MODE_MODE_Timer;
- // Start the external 16 MHz clock for a more accurate PWM frequency
- NRF_CLOCK->TASKS_HFCLKSTART = 1;
-
- //pwm_init(PWM_MODE_C);
-
- while (true)
- {
- pwm_init(PWM_MODE_C);
- pwm_init(PWM_MODE_D);
- pwm_init(PWM_MODE_E);
- pwm_init(PWM_MODE_F);
- // pwm_init(PWM_MODE_G);
- // pwm_init(PWM_MODE_A);
- // pwm_init(PWM_MODE_B);
- }
- }<div class="blockcode"><blockquote>
- uint32_t nrf_pwm_init(nrf_pwm_config_t *config)
- {
- if(config->num_channels == 0 || config->num_channels > PWM_MAX_CHANNELS) return 0xFFFFFFFF;
-
- switch(config->mode)
- {
- case PWM_MODE_C: // 8-bit resolution, 520Hz PWM frequency, 1us 1923
- PWM_TIMER->PRESCALER = 4;
- pwm_max_value = 1923;
- nrf_pwm_set_value(0, 960);
- break;
- case PWM_MODE_D: // 8-bit resolution, 585Hz PWM frequency, 1us 1709
- PWM_TIMER->PRESCALER = 4;
- pwm_max_value = 1709;
- nrf_pwm_set_value(1, 850);
- break;
- case PWM_MODE_E: // 0-1000 resolution, 650Hz PWM frequency, 1us 1538
- PWM_TIMER->PRESCALER = 4;
- pwm_max_value = 1538;
- nrf_pwm_set_value(2, 765);
- break;
- case PWM_MODE_F: // 0-100 resolution, 693Hz PWM frequency, 1us
- PWM_TIMER->PRESCALER = 4;
- pwm_max_value = 1443;
- nrf_pwm_set_value(3, 720);
- break;
- case PWM_MODE_G: // 8-bit resolution, 780Hz PWM frequency, 1us
- PWM_TIMER->PRESCALER = 4;
- pwm_max_value = 1282;
- nrf_pwm_set_value(0, 640);
- break;
- case PWM_MODE_A: // 8-bit resolution, 867Hz PWM frequency, 1us
- PWM_TIMER->PRESCALER = 4;
- pwm_max_value = 1153;
- nrf_pwm_set_value(0, 575);
- break;
- case PWM_MODE_B: // 8-bit resolution, 975Hz PWM frequency, 1us
- PWM_TIMER->PRESCALER = 4;
- pwm_max_value = 1026;
- nrf_pwm_set_value(0, 510);
- break;
- default:
- return 0xFFFFFFFF;
- }
- pwm_cc_update_margin_ticks = pwm_cc_margin_by_prescaler[PWM_TIMER->PRESCALER];
- pwm_num_channels = config->num_channels;
- for(int i = 0; i < pwm_num_channels; i++)
- {
- pwm_io_ch[i] = (uint32_t)config->gpio_num[i];
- nrf_gpio_cfg_output(pwm_io_ch[i]);
- pwm_running[i] = 0;
- pwm_gpiote_channel[i] = config->gpiote_channel[i];
- }
- PWM_TIMER->TASKS_CLEAR = 1;
- PWM_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
- PWM_TIMER->CC[2] = pwm_next_max_value = pwm_max_value;
- PWM_TIMER->MODE = TIMER_MODE_MODE_Timer;
- PWM_TIMER->SHORTS = TIMER_SHORTS_COMPARE2_CLEAR_Msk;
- PWM_TIMER->EVENTS_COMPARE[0] = PWM_TIMER->EVENTS_COMPARE[1] = PWM_TIMER->EVENTS_COMPARE[2] = PWM_TIMER->EVENTS_COMPARE[3] = 0;
-
- if(pwm_num_channels > 2)
- {
- PWM_TIMER2->TASKS_CLEAR = 1;
- PWM_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
- PWM_TIMER2->CC[2] = pwm_next_max_value = pwm_max_value;
- PWM_TIMER2->MODE = TIMER_MODE_MODE_Timer;
- PWM_TIMER2->SHORTS = TIMER_SHORTS_COMPARE2_CLEAR_Msk;
- PWM_TIMER2->EVENTS_COMPARE[0] = PWM_TIMER2->EVENTS_COMPARE[1] = PWM_TIMER2->EVENTS_COMPARE[2] = PWM_TIMER2->EVENTS_COMPARE[3] = 0;
- PWM_TIMER2->PRESCALER = PWM_TIMER->PRESCALER;
- }
- for(int i = 0; i < pwm_num_channels && i < 2; i++)
- {
- ppi_enable_channel(config->ppi_channel[i*2], &PWM_TIMER->EVENTS_COMPARE[i], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);
- ppi_enable_channel(config->ppi_channel[i*2+1],&PWM_TIMER->EVENTS_COMPARE[2], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);
- pwm_modified[i] = false;
- }
- for(int i = 2; i < pwm_num_channels; i++)
- {
- ppi_enable_channel(config->ppi_channel[i*2], &PWM_TIMER2->EVENTS_COMPARE[i-2], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);
- ppi_enable_channel(config->ppi_channel[i*2+1],&PWM_TIMER2->EVENTS_COMPARE[2], &NRF_GPIOTE->TASKS_OUT[pwm_gpiote_channel[i]]);
- pwm_modified[i] = false;
- }
- #if(USE_WITH_SOFTDEVICE == 1)
- sd_radio_session_open(nrf_radio_signal_callback);
- #else
- NVIC_SetPriority(PWM_IRQn, 0);
- NVIC_EnableIRQ(PWM_IRQn);
- #endif
- apply_pan73_workaround(PWM_TIMER, true);
- PWM_TIMER->TASKS_START = 1;
- if(pwm_num_channels > 2)
- {
- apply_pan73_workaround(PWM_TIMER2, true);
- PWM_TIMER2->TASKS_START = 1;
- }
- return 0;
- }
复制代码 ……设想使用蜂鸣器奏出乐音 但是最终配置出的四个通道频率相同占空比不同 若更改时钟的分频值和max_value 那么四个通道的值都会变成按照最大分频数 最大max_value设置 而出现的波形
求解……
|
|