This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Cannot drive LED using PWM

I want to drive LED using NRF_PWM0, but It is not working.

I want to increase brightness of LED but it is alyway on.

Please help me.

/*
 *  Copyright 2019. Seonguk Jeong All rights reserved
 *  uAHRS Project
 *  Example : Pulse Width Modulation(PWM)
 *  Created at Dec. 11, 2019
 *  Author : Seonguk Jeong
 */
#include <nrf_drv_pwm.h>
#include <nrf_drv_timer.h>
#include <nrf_delay.h>

void Initialize();
void setDuty(int16_t duty);

int main(void){
	// Initialize PWM
	Initialize();

	int16_t test = 0;
	while(1){
		if(test >= 32000){
			test = 0;
		}
		setDuty(test);
		test++;
		nrf_delay_ms(2);
	}
	return 0;
}

void Initialize(){
	NRF_PWM0->PSEL.OUT[0] = 23;
	NRF_PWM0->PSEL.OUT[1] = 24;
	NRF_PWM0->ENABLE = 0x01;			// Enable PWM
	NRF_PWM0->MODE = 0x00;				// Up mode
	NRF_PWM0->PRESCALER = 0x00	;		// No prescaler
	NRF_PWM0->COUNTERTOP = 32000;		// Set 2ms
	NRF_PWM0->LOOP = 0x00;				// Disable loop
	NRF_PWM0->DECODER = 0x00;			// Common Mode
	NRF_PWM0->SEQ[0].REFRESH = 0;
	NRF_PWM0->SEQ[0].ENDDELAY = 0;
}

void setDuty(int16_t duty){
	int16_t dutyMemory[4] = {duty};

	NRF_PWM0->SEQ[0].PTR = (uint32_t)dutyMemory;
	NRF_PWM0->SEQ[0].CNT = (sizeof(dutyMemory) / sizeof(uint16_t));

	// PWM TASK START
	NRF_PWM0->TASKS_SEQSTART[0] = 1;
	while(!NRF_PWM0->EVENTS_SEQSTARTED[0]){}
	NRF_PWM0->EVENTS_SEQSTARTED[0] = 0;
}

Parents
  • Hello,

    Have you tried to analyze the PWM signal using e.g. a saleae logic analyzer? Is it actually a PWM signal on the LED? Or is it simply ON?

    Note that on the LEDs on the DK it may be difficult to tell the difference between 90% and 100%, while it is easy to tell the difference between 0% and 10%, or 10% and 20%

  • Hello.

    Thank you for reply.

    I've solved problem.

    void pwmInit(void)
    {
    	NRF_PWM0->PSEL.OUT[0] = 23 ;
    	NRF_PWM0->PSEL.OUT[1] = 24 ; 
    	NRF_PWM0->ENABLE = 0x00000001;
    	NRF_PWM0->MODE = 0x00000000;
    	NRF_PWM0->PRESCALER = 0x00000000;
    	NRF_PWM0->COUNTERTOP = 32000;
    	NRF_PWM0->DECODER = 0x00000002;
    	NRF_PWM0->LOOP = 0x00000000;
    	NRF_PWM0->SEQ[0].REFRESH = 0;
    	NRF_PWM0->SEQ[0].ENDDELAY = 0;
    	NRF_PWM0->SEQ[0].PTR = (uint32_t)(pwmDuty);
    	NRF_PWM0->SEQ[0].CNT = 4;
    	NRF_PWM0->TASKS_SEQSTART[0] = 1;
    }

    it works after remove <nrf_drv_pwm.h>

    Thank you.

Reply
  • Hello.

    Thank you for reply.

    I've solved problem.

    void pwmInit(void)
    {
    	NRF_PWM0->PSEL.OUT[0] = 23 ;
    	NRF_PWM0->PSEL.OUT[1] = 24 ; 
    	NRF_PWM0->ENABLE = 0x00000001;
    	NRF_PWM0->MODE = 0x00000000;
    	NRF_PWM0->PRESCALER = 0x00000000;
    	NRF_PWM0->COUNTERTOP = 32000;
    	NRF_PWM0->DECODER = 0x00000002;
    	NRF_PWM0->LOOP = 0x00000000;
    	NRF_PWM0->SEQ[0].REFRESH = 0;
    	NRF_PWM0->SEQ[0].ENDDELAY = 0;
    	NRF_PWM0->SEQ[0].PTR = (uint32_t)(pwmDuty);
    	NRF_PWM0->SEQ[0].CNT = 4;
    	NRF_PWM0->TASKS_SEQSTART[0] = 1;
    }

    it works after remove <nrf_drv_pwm.h>

    Thank you.

Children
No Data
Related