Last active
August 29, 2015 14:16
-
-
Save facchinm/5815f83b55b1ef86dc6b to your computer and use it in GitHub Desktop.
pulseIn example for Arduino Due
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int pwmPin = 13; | |
const int inPin = 3; | |
const int pulseLenIn = 50; | |
int pulseLen; | |
volatile boolean l; | |
void TC3_Handler() | |
{ | |
TC_GetStatus(TC1, 0); | |
digitalWrite(pwmPin, l = !l); | |
} | |
void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) { | |
pmc_set_writeprotect(false); | |
pmc_enable_periph_clk((uint32_t)irq); | |
TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4); | |
uint32_t rc = VARIANT_MCK/128/frequency; //128 because we selected TIMER_CLOCK4 above | |
TC_SetRA(tc, channel, rc/2); //50% high, 50% low | |
TC_SetRC(tc, channel, rc); | |
TC_Start(tc, channel); | |
tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS; | |
tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS; | |
NVIC_EnableIRQ(irq); | |
} | |
void setup(void) | |
{ | |
pinMode(pwmPin,OUTPUT); | |
startTimer(TC1, 0, TC3_IRQn, 1000000/pulseLenIn); //TC1 channel 0, the IRQ for that channel and the desired frequency | |
Serial.begin(115200); | |
pinMode(inPin,INPUT); | |
} | |
void loop(void) | |
{ | |
pulseLen = pulseIn(inPin, HIGH, 1000000); | |
/* should print pulseLenIn */ | |
Serial.print("PulseIn: "); | |
Serial.println(pulseLen); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment