Mired in code
Check-in [34668e0a73]
Not logged in
Public Repositories
mwm's Repositories

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the PWM input example code
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:34668e0a7367075134bc4d197501f5a1198aea4e
User & Date: mwm 2015-10-08 04:07:35
Context
2015-10-08
04:07
Add the PWM input example code Leaf check-in: 34668e0a73 user: mwm tags: trunk
2015-08-28
14:34
Add a note about running Calibrate to the calibration load failure message. check-in: bd54efccc5 user: mwm tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added Arduino/PWM-input/exti/Makefile.



















>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
ARDMK_DIR=/usr/local/arduino-mk
ARDUINO_DIR=/usr/local/arduino
ARDUINO_PORT=/dev/ttyU?
ifndef BOARD_TAG
BOARD_TAG=uno
endif

include $(ARDMK_DIR)/Arduino.mk

Added Arduino/PWM-input/exti/exti.ino.

































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define MY_PIN 3 // Must be pin 2 or 3

// Work around bug in Arduino 1.0.6
#define NOT_AN_INTERRUPT (-1)

uint16_t pwm_value = 0;

void change() {
  static unsigned long prev_time = 0;

  if (digitalRead(MY_PIN))
    prev_time = micros();
  else
    pwm_value = micros() - prev_time;
}

void setup() {
  pinMode(MY_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(MY_PIN), change, CHANGE);
  Serial.begin(115200);
}

void loop() {
  uint16_t pwmin;

  noInterrupts();
  pwmin = pwm_value;
  interrupts();  

  Serial.println(pwmin);
  delay(500);
}

Added Arduino/PWM-input/ici/Makefile.



















>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
ARDMK_DIR=/usr/local/arduino-mk
ARDUINO_DIR=/usr/local/arduino
ARDUINO_PORT=/dev/ttyU?
ifndef BOARD_TAG
BOARD_TAG=uno
endif

include $(ARDMK_DIR)/Arduino.mk

Added Arduino/PWM-input/ici/ici.ino.







































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define MY_PIN 8	 	// Must be pin 8 on 328P's.

static uint16_t pulse_length;	// in ticks

#define ICESB _BV(ICES1)

ISR(TIMER1_CAPT_vect) {
  if (TCCR1B & ICESB)			// On rising edge, start of pulse & frame
    TCNT1 = 0;			// Reset the counter
  else				// Falling edge
    pulse_length = ICR1;	// Save pulse length in ticks

  TCCR1B ^= ICESB;		// Detect other edge next time
  TIFR1 |= _BV(ICF1);
}

void setup() {
  TCCR1A = 0 ;			// Not doing anything here.
  TCCR1B = _BV(CS11) | ICESB;	// Enable with rising edge capture, prescaler 8.
  TIMSK1 = _BV(ICIE1);		// And unmask this interrupts.

  pinMode(MY_PIN, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {
  uint16_t pwm_value;

  TIMSK1 &= ~_BV(ICIE1);	// Turn off my interrupt to grab the value
  pwm_value = pulse_length;
  TIMSK1 |= _BV(ICIE1);

  Serial.println(clockCyclesToMicroseconds(pwm_value * 8));
  delay(500);
}

Added Arduino/PWM-input/pci/Makefile.







>
>
>
1
2
3
ARDUINO_LIBS = EnableInterrupt

include ../../Makefile

Added Arduino/PWM-input/pci/pci.ino.































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <EnableInterrupt.h>

#define MY_PIN 5 // we could choose any pin

uint16_t pwm_value = 0;

void change() {
  static unsigned long prev_time = 0;

  if (digitalRead(MY_PIN))
    prev_time = micros();
  else
    pwm_value = micros() - prev_time;
}

void setup() {
  pinMode(MY_PIN, INPUT_PULLUP);
  enableInterrupt(MY_PIN, &change, CHANGE);
  Serial.begin(115200);
}

void loop() {
  uint16_t pwmin;

  noInterrupts();
  pwmin = pwm_value ;
  interrupts();  

  Serial.println(pwmin);
  delay(500);
}