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

Pulse width modulation nRF52832

Hi all! I used PWM module of nRF52832 to create the PWM but i don't know how to update new value to duty_cycle. This is my code. I used Button (Pin 13) to set a new DUTY_CYCLE. Help me, pls.

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nrf.h"
#define NRF_LOG_MODULE_NAME "APP"
#include "SEGGER_RTT.h"
uint16_t DUTY_CYCLE = 15000;
static void PWM_config(void)
{
uint16_t pwm_seq[4] = {DUTY_CYCLE};
NRF_PWM0->PSEL.OUT[0] = (20 << 0) | (0 << 31);
NRF_PWM0->ENABLE      = (1 << 0);
NRF_PWM0->MODE        = (2 << 0);
NRF_PWM0->PRESCALER   = (7 << 0);
NRF_PWM0->COUNTERTOP  = (30000 << 0);
NRF_PWM0->LOOP        = (0 << 0);
NRF_PWM0->DECODER   = (2 << 0) | (0 << 8);
NRF_PWM0->SEQ[0].PTR  = (uint32_t)(pwm_seq);
NRF_PWM0->SEQ[0].CNT  =((sizeof(pwm_seq) / sizeof(uint16_t)) << 0);
NRF_PWM0->SEQ[0].REFRESH  = 0;
NRF_PWM0->SEQ[0].ENDDELAY = 0;
NRF_PWM0->TASKS_SEQSTART[0] = 1;
}
int main(void)
{
NRF_GPIO -> PIN_CNF[13] = ((0<<0)|(0<<1)|(3<<2)|(2<<8)|(3<<16));
NRF_GPIO -> PIN_CNF[17] = ((1<<0)|(1<<1)|(0<<2)|(2<<8)|(0<<16));
SEGGER_RTT_WriteString(0, "Hello World!\n");
PWM_config();
while (1)
{
	if (((NRF_GPIO -> IN >> 13) & 1) == 0)
	{
		DUTY_CYCLE = 30000;
		NRF_GPIO -> OUT = 0<<17;
		//NRF_PWM0-> TASKS_STOP = 1;
		//while (((NRF_GPIO -> IN >> 13) & 1) == 0);
		//NRF_PWM0->COUNTERTOP  = (15000 << 0);
		
	}
	else
	{
		//DUTY_CYCLE = 30000;
		//NRF_PWM0->TASKS_SEQSTART[0] = 1;
		NRF_GPIO -> OUT = 1<<17;
	}
}
			
}
Related