设为首页收藏本站

Discuz! Board

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 98|回复: 0
打印 上一主题 下一主题

nrf52832单片机 P0.12 P0.13输出PWM,P0.12不输出P0.13正常,请版主帮指点一下!感谢

[复制链接]

2

主题

2

帖子

12

积分

新手上路

Rank: 1

积分
12
跳转到指定楼层
楼主
chaotian 发表于 2026-1-5 22:44:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"

APP_PWM_INSTANCE(PWM1,1);                   // 创建一个使用定时器1产生PWM波的实例
#define BSP_PWM_0      12
#define BSP_PWM_1      13

static volatile bool ready_flag;            // 使用一个标志位表示PWM状态

void pwm_ready_callback(uint32_t pwm_id)    // PWM回调功能
{
    ready_flag = true;
}

int main(void)
{
    ret_code_t err_code;

    /* 2-个频道的 PWM, 200Hz(5000us=5ms), 通过 开发板LED 管脚输出. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_PWM_0, BSP_PWM_1);
        /* 切换两个频道的极性 */
    pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW;
    /* 切换两个频道的极性 */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;

    /* 初始化和使能PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);//使能PWM
    app_pwm_channel_duty_set(&PWM1, 0, 2500);
          app_pwm_channel_duty_set(&PWM1, 1, 2500);
    uint32_t value;
    while(true)
    {
//        for (uint8_t i = 0; i < 40; ++i)
//        {
//            value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);
//            
//            ready_flag = false;
//            /* 设置占空比 - 不停设置直到PWM准备好. */
//            while (app_pwm_channel_duty_set(&PWM1, 0, value) == NRF_ERROR_BUSY);  
//            /* 等待回调 */
//            while(!ready_flag);
//            APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
//            nrf_delay_ms(25);
//        }
    }

}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖
回复

使用道具 举报

Archiver|手机版|小黑屋|Comsenz Inc.   

GMT+8, 2026-1-17 21:55 , Processed in 4.155749 second(s), 30 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表