Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This paste provided by Chris Troutner from http://thesolarpowerexpert.com
- *
- * This code is based on publicly available code available at the following sources:
- * http://pastebin.com/NQtbVCFh
- * http://forums.leaflabs.com/topic.php?id=1038
- *
- * It's inteded for a Maple Rev 5 from leaflabs.com
- */
- #include <timer.h>
- int value = 0;
- int count = 0;
- int overflow_flag = 0;
- HardwareTimer timer4 = HardwareTimer(4);
- //quick method to return if there's anyone on the other end of the serialusb link yet.
- boolean isConnected(){
- return (SerialUSB.isConnected() && (SerialUSB.getDTR() || SerialUSB.getRTS()));
- }
- void setup()
- {
- SerialUSB.begin();
- while(!isConnected()); //wait till console attaches.
- SerialUSB.println("Hello!");
- //Setup Timer4 Channel 1 (pin 5) as an input
- pinMode(5, INPUT);
- //Configure pin 7 as an output
- pinMode(7, OUTPUT);
- setup_timer();
- }
- void __measurePulse_irq()
- {
- //For some reason, the interrupt is triggred as soon as
- //the device is turned on. This gets around that bug.
- if( count == 0 )
- return;
- else {
- if(~overflow_flag)
- value = TIMER4_BASE->CCR1; //read the captured value
- else {
- value = 0;
- overflow_flag = 0;
- }
- timer4.setCount(0); //Zero timer
- }
- }
- void handle_overflow() {
- overflow_flag = 1;
- }
- void loop() {
- //Display the value very 500 mS
- if( count == 100 ) {
- //delay(500);
- SerialUSB.println(value);
- //value = 0;
- count = 0;
- }
- //Pulse pin 7 at a rate of 100 Hz @ 50% DC
- digitalWrite(7, HIGH);
- delay(5);
- digitalWrite(7, LOW);
- delay(5);
- count++;
- }
- //Halt processor and flash LED - used for debugging
- void HaltAll(void) {
- //Timer4.pause();
- timer4.pause();
- pinMode(BOARD_LED_PIN, OUTPUT);
- while(1) {
- toggleLED();
- delay(1000);
- }
- }
- /*
- This is a mis-mash of code from these two sources:
- http://pastebin.com/NQtbVCFh
- http://forums.leaflabs.com/topic.php?id=1038
- */
- void setup_timer(void) {
- //Create a pointer to the timer (why?)
- timer_dev *t = TIMER4;
- //Initialize timer
- timer4.pause();
- /*
- A prescaler of 288 means that at 72MHz, I get the following:
- 100 Hz = 2510
- 10 Hz = 25084
- */
- timer4.setPrescaleFactor(288);
- timer4.setOverflow(65535);
- timer4.setCount(0);
- //New code to set overflow timer
- timer4.setMode(2, TIMER_OUTPUT_COMPARE); //Set this channel to compare
- timer4.setCompare(2, 65534); //Set the compare for the channel to the overflow vale
- timer4.attachInterrupt(2, handle_overflow); //Assign the interrupt handler
- timer4.refresh();
- //Create a pointer to the timer registers (why?)
- timer_reg_map r = t->regs;
- //capture compare regs TIMx_CCRx used to hold val after a transition on corresponding ICx
- //when cap occurs, flag CCXIF (TIMx_SR register) is set,
- //and interrupt, or dma req can be sent if they are enabled.
- //if cap occurs while flag is already high CCxOF (overcapture) flag is set..
- //CCIX can be cleared by writing 0, or by reading the capped data from TIMx_CCRx
- //CCxOF is cleared by writing 0 to it.
- //Capture/Compare 1 Selection
- // set CC1S bits to 01 in the capture compare mode register.
- // 01 selects TI1 as the input to use. (page 336 stm32 reference)
- // (assuming here that TI1 is D6, according to maple master pin map)
- //CC1S bits are bits 0,1
- bitSet(r.adv->CCMR1, 0);
- bitClear(r.adv->CCMR1, 1);
- //Input Capture 1 Filter.
- // need to set IC1F bits according to a table saying how long
- // we should wait for a signal to be 'stable' to validate a transition
- // on the input.
- // (page 336 stm32 reference)
- //IC1F bits are bits 7,6,5,4
- bitClear(r.adv->CCMR1, 7);
- bitClear(r.adv->CCMR1, 6);
- bitSet(r.adv->CCMR1, 5);
- bitSet(r.adv->CCMR1, 4);
- //sort out the input capture prescaler..
- //00 no prescaler.. capture is done as soon as edge is detected
- bitClear(r.adv->CCMR1, 3);
- bitClear(r.adv->CCMR1, 2);
- //This command does everything above to the CCMR1 register.
- //TIMER4_BASE->CCMR1 = 0x00F1; //channel 1 configure as input, IC1 mapped on TI1
- //select the edge for the transition on TI1 channel using CC1P in CCER
- //CC1P is bit 1 of CCER (page 339)
- // 0 = rising
- // 1 = falling
- bitClear(r.adv->CCER,1);
- //set the CC1E bit to enable capture from the counter.
- //CCE1 is bit 0 of CCER (page 339)
- //Enable capture
- bitSet(r.adv->CCER,0);
- //TIMER4_BASE->CCMR2 = 0x0000; //channels 3&4 disabled
- //TIMER4_BASE->CCER = 0x0001; //configure channel 1 as an input
- //TIMER4_BASE->SMCR = 0x0000; //all slave modes disabled, CC transitions on clock
- //TIMER4_BASE->CR1 = 0x0001; //enable the counter
- //TIMER4_BASE->CR2 = 0x0000; //connect TIM4_CH1 pin to TI1 input
- //TIMER4_BASE->PSC = 0x00FF; //set a random large prescalar just for testing
- //timer_attach_interrupt(TIMER4, TIMER_CC1_INTERRUPT, __measurePulse_irq);
- timer4.attachInterrupt(1, __measurePulse_irq);
- //timer_resume(TIMER4);
- timer4.resume();
- /*value = TIMER4_BASE->CCMR2;
- SerialUSB.println("Value is: ");
- SerialUSB.println(value);
- HaltAll();
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement