Created
April 22, 2020 15:59
-
-
Save nmk456/6f993de48f0b5170e32a80fb8f93466d to your computer and use it in GitHub Desktop.
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
#include <Arduino.h> | |
#define clk 10 // PWM Clock Pin - First timer, first channel | |
#define testpin 9 | |
uint32_t count = 0; | |
// Timer stuff | |
IRQ_NUMBER_t irq = IRQ_QTIMER1; | |
IMXRT_TMR_t* tmr = &IMXRT_TMR1; | |
IMXRT_TMR_CH_t* ch0 = &tmr->CH[0]; | |
void isr() { | |
digitalWrite(testpin, HIGH); | |
count++; | |
delayMicroseconds(250); | |
digitalWrite(testpin, LOW); | |
ch0->CSCTRL &= ~TMR_CSCTRL_TCF1; | |
} | |
void timerSetup() { | |
ch0->CTRL = 0x0000; | |
attachInterruptVector(irq, isr); | |
NVIC_ENABLE_IRQ(irq); | |
int period = 1'000; // Period in us | |
int psc = 2; // Prescaler 2^(n-1) | |
float pscValue = 1 << (psc & 0b0111); | |
uint32_t pscBits = 0b1000 | (psc & 0b0111); | |
float t = period * (150.0f / pscValue); // Number of clock cycles per period | |
uint16_t reload; | |
if(t > 0xFFFF) reload = 0xFFFE; | |
else reload = (uint16_t)t - 1; | |
ch0->CTRL = 0x0000; | |
ch0->LOAD = 0x0000; | |
ch0->COMP1 = reload; | |
ch0->CMPLD1 = reload; | |
ch0->CNTR = 0x0000; | |
ch0->CSCTRL &= ~TMR_CSCTRL_TCF1; | |
ch0->CSCTRL |= TMR_CSCTRL_TCF1EN; | |
ch0->CTRL = TMR_CTRL_CM(1) | TMR_CTRL_PCS(pscBits) | | |
TMR_CTRL_LENGTH; | |
} | |
void setup() { | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(testpin, OUTPUT); | |
digitalWrite(LED_BUILTIN, HIGH); | |
delay(1000); | |
Serial.begin(115200); | |
while(!Serial); | |
Serial.println("Serial initialized."); | |
timerSetup(); | |
Serial.println("Setup done"); | |
digitalWrite(LED_BUILTIN, LOW); | |
interrupts(); noInterrupts(); | |
} | |
void loop() { | |
count = 0; | |
interrupts(); | |
while(count < 100) { | |
//Serial.print(" "); | |
} | |
noInterrupts(); | |
Serial.println(); | |
Serial.print("Count: "); | |
Serial.println(count); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment