From ffd1f29d91d4762d8c27aff6e3e6fbdac25dba41 Mon Sep 17 00:00:00 2001
From: Sam Calisch <s.calisch@gmail.com>
Date: Sun, 1 Oct 2017 15:11:14 -0400
Subject: [PATCH] added pwm

---
 modules/pwm/pwm.ino | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 modules/pwm/pwm.ino

diff --git a/modules/pwm/pwm.ino b/modules/pwm/pwm.ino
new file mode 100644
index 0000000..708c5e3
--- /dev/null
+++ b/modules/pwm/pwm.ino
@@ -0,0 +1,26 @@
+//sec 2017
+
+
+uint16_t pwms[1] = {0}; //duty cycles.  can put more to loop through a sequence
+const uint8_t pwm_pin = 26; //p26
+
+void setup() {
+  //Use PWM module.  Can also use a timer and ppi.
+  NRF_GPIO->DIRSET = (1<<pwm_pin); //set ref pin as output
+  NRF_GPIO->OUTCLR = (1<<pwm_pin); //set ref pin low
+  NRF_PWM0->PSEL.OUT[0] = (pwm_pin << PWM_PSEL_OUT_PIN_Pos) | (PWM_PSEL_OUT_CONNECT_Connected << PWM_PSEL_OUT_CONNECT_Pos);
+  NRF_PWM0->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
+  NRF_PWM0->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos);
+  NRF_PWM0->PRESCALER = (PWM_PRESCALER_PRESCALER_DIV_1 << PWM_PRESCALER_PRESCALER_Pos); //16MHz tick
+  NRF_PWM0->COUNTERTOP = (1600 << PWM_COUNTERTOP_COUNTERTOP_Pos); //10 kHz pwm freq.
+  NRF_PWM0->LOOP = (PWM_LOOP_CNT_Disabled << PWM_LOOP_CNT_Pos);
+  NRF_PWM0->DECODER = (PWM_DECODER_LOAD_Common << PWM_DECODER_LOAD_Pos) | (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);
+  pwms[0] = 800;
+  NRF_PWM0->SEQ[0].PTR = ((uint32_t)(pwms) << PWM_SEQ_PTR_PTR_Pos);
+  NRF_PWM0->SEQ[0].CNT = (1 << PWM_SEQ_CNT_CNT_Pos);
+  NRF_PWM0->SEQ[0].REFRESH = 0;
+  NRF_PWM0->SEQ[0].ENDDELAY = 0;
+  NRF_PWM0->TASKS_SEQSTART[0] = 1;
+}
+
+void loop() {}
-- 
GitLab