void delay_nus2(uint32_t nDelay) { // remember current timer value: volatile uint16_t TIMCounter_0 = timer_get_counter(SOFTI2C_TIMER_1); volatile uint16_t TIMCounter_1; uint16_t deltaCounter; uint16_t nIteration = (nDelay / 65535); while (((nIteration * 65535) + nDelay) > 0) { TIMCounter_1 = timer_get_counter(SOFTI2C_TIMER_1); if ((TIMCounter_1 - TIMCounter_0) < 0) { if (nIteration > 0) { nIteration--; } deltaCounter = TIMCounter_0 - TIMCounter_1; } else { deltaCounter = TIMCounter_1 - TIMCounter_0; } if (deltaCounter >= nDelay) { break; } else { nDelay = nDelay - deltaCounter; } TIMCounter_0 = TIMCounter_1; } }
void sleep(uint32_t microsec) { uint64_t start_time = timer_get_counter(); do { /* Nothing */ } while (timer_get_counter() - start_time < microsec); }
/** * Set the DSM timer to interrupt * @param[in] us The time in microseconds divided by 10 */ void timer_dsm_set(u16 us) { #if DEBUG && !DSM_RECEIVER && !DSM_MITM u16 new_t = (us*2 + timer_get_counter(TIMER_DSM)) & 65535; #else u16 new_t = (us + timer_get_counter(TIMER_DSM)) & 65535; #endif // Update the timer compare value 1 timer_set_oc_value(TIMER_DSM, TIM_OC1, new_t); // Clear the interrupt flag and enable the interrupt of compare 1 timer_clear_flag(TIMER_DSM, TIM_SR_CC1IF); timer_enable_irq(TIMER_DSM, TIM_DIER_CC1IE); }
PUBLIC float HCSR04_read(uint8_t index) { float fl_distancia = 0.0; UWord count_timer; //limpa contador timer_set_counter(timerM, 0); //Pulso digitalWrite(ultrason[index].trig, 1); Delay_us(10); digitalWrite(ultrason[index].trig, 0); //ECO while(!digitalRead(ultrason[index].eco)); timer_start(timerM,1);//start while(digitalRead(ultrason[index].eco)); timer_start(timerM,0);//stop //Calculo de distancia. count_timer.value = timer_get_counter(timerM); //fl_distancia = (float)distancia.value*0.011328; fl_distancia = (float)count_timer.value*k; return (fl_distancia); }
void tim2_isr(void) { if (timer_get_flag(TIM2, TIM_SR_CC1IF)) { /* Clear compare interrupt flag. */ timer_clear_flag(TIM2, TIM_SR_CC1IF); /* * Get current timer value to calculate next * compare register value. */ compare_time = timer_get_counter(TIM2); /* Calculate and set the next compare value. */ frequency = frequency_sequence[frequency_sel++]; new_time = compare_time + frequency; timer_set_oc_value(TIM2, TIM_OC1, new_time); if (frequency_sel == 18) frequency_sel = 0; /* Toggle LED to indicate compare event. */ gpio_toggle(GPIOC, GPIO12); } }
/** * Update our last capture time * */ void comm_tim_update_capture(void) { comm_tim_data.last_capture_time = timer_get_counter(TIM2); timer_set_oc_value(TIM2, TIM_OC1, comm_tim_data.last_capture_time + comm_tim_data.freq); OFF(DP_EXT_SCL); }
void tim1_cc_isr(void) { if((TIM2_SR & TIM_SR_CC3IF) != 0) { timer_clear_flag(TIM1, TIM_SR_CC3IF); uint32_t now = timer_get_counter(TIM1) + timer_rollover_cnt; DecodePpmFrame(now); } }
/** * Record the time of the last commutation */ void comm_tim_capture_time(void) { u16 new_time = timer_get_counter(TIM2); comm_tim_data.prev_time = comm_tim_state.next_prev_time; comm_tim_data.curr_time = new_time; comm_tim_state.next_prev_time = new_time; comm_tim_data.update_count = comm_tim_state.update_count; comm_tim_state.update_count = 0; }
static uint32_t tim6_get_time_us(void) { uint32_t cnt = timer_get_counter(TIM6); // convert to 1MHz less precise timer value -> units: microseconds uint32_t time_us = cnt * 100; return time_us; }
/****************************************************************************** Return the elapsed time to tell the Stack how much time is spent since last call. INPUT void OUTPUT value TIMEVAL (unsigned long) the elapsed time ******************************************************************************/ TIMEVAL getElapsedTime(void) { TIMEVAL timer = timer_get_counter(TIM3); /* Read the value of the running timer */ if (timer > last_time_set) /* In case the timer value is higher than the last time. */ return (timer - last_time_set); /* Calculate the time difference */ else if (timer < last_time_set) /* Timer has cycled around */ return (timer + (TIMEVAL_MAX - last_time_set)); /* Calculate the time difference */ else return TIMEVAL_MAX; /* timer has gone around a full cycle.*/ }
void tim1_cc_isr(void) { static int last_value = 0; int value; if (timer_get_flag(TIM1, TIM_SR_CC1IF) != 0) { // Timer channel 1 interrupt -> First edge (outcoming signal). timer_clear_flag(TIM1, TIM_SR_CC1IF); last_value = timer_get_counter(TIM1); } if (timer_get_flag(TIM1, TIM_SR_CC2IF)) { // Timer channel 2 interrupt -> response (incoming signal). timer_clear_flag(TIM1, TIM_SR_CC2IF); value = timer_get_counter(TIM1) - last_value; if (value < 0) value = value + 0xffff; latency = value; if (latency > latency_max) latency_max = latency; } }
/** Returns current angle of motor shaft to some arbitrary 0-axis * @return angle in digit (2Pi=65536) */ uint16_t Encoder::GetAngle() { uint32_t time_since_last_pulse = timer_get_counter(REV_CNT_TIMER); uint16_t interpolated_angle = (angle_per_pulse * time_since_last_pulse) / last_pulse_timespan; if (ignore > 0) { interpolated_angle = 0; } return angle + interpolated_angle; }
/** * Update our last capture time and curr time * */ void comm_tim_update_capture_and_time(void) { comm_tim_data.last_capture_time = timer_get_counter(TIM2); timer_set_oc_value(TIM2, TIM_OC1, comm_tim_data.last_capture_time + comm_tim_data.freq); comm_tim_data.prev_time = comm_tim_state.next_prev_time; comm_tim_data.curr_time = comm_tim_data.last_capture_time; comm_tim_state.next_prev_time = comm_tim_data.last_capture_time; comm_tim_data.update_count = comm_tim_state.update_count; comm_tim_state.update_count = 0; }
/****************************************************************************** Interruptserviceroutine Timer 3 Compare 1 for the CAN timer Pull in the timer counter value to represent the last time an alarm was set. ******************************************************************************/ void tim3_isr(void) { /* Clear interrrupt flag. */ if (timer_get_flag(TIM3, TIM_SR_CC1IF)) { timer_clear_flag(TIM3, TIM_SR_CC1IF); /* Reread to force the previous write before leaving (a side-effect of hardware pipelining)*/ timer_get_flag(TIM3, TIM_SR_CC1IF); last_time_set = timer_get_counter(TIM3); /* Call the time handler of the stack to adapt the elapsed time */ TimeDispatch(); } }
void tim2_isr(void) { if((TIM2_SR & TIM_SR_CC2IF) != 0) { timer_clear_flag(TIM2, TIM_SR_CC2IF); uint32_t now = timer_get_counter(TIM2) + timer_rollover_cnt; DecodePpmFrame(now); } else if((TIM2_SR & TIM_SR_UIF) != 0) { timer_rollover_cnt+=(1<<16); timer_clear_flag(TIM2, TIM_SR_UIF); } }
void exti9_5_isr(void) { u16 t1 = 0; u16 t = 0; t1 = timer_get_counter(TIM1); // get the counter(TIM1) value exti_reset_request(_PWM_EXTI); // reset(clean) the IRQ t = (t1>=t0) ? (t1-t0) : (65536+t1-t0); // none-stop TIM1 counter, compute ppm-signal width (2MHz = 0.5uSecond) t0 = t1; if (!ppmSync) { // ppm-in status : not Sync /* (1) capture pmSync (for ppm-timing > MIN_PPMin_Sync : 3300uSecond) */ if (t>MIN_PPMin_Sync) { // ppm-input Sync-signal if (j<3) { // set 3-times for count total channels number, k[0], k[1], k[2] j++; // set for next count ppm-in total channels number k[j] = 0; // initial ppm-in total channels number =0 } else { // accumulate 3-times total channels number k[0], k[1], k[2] /* (2) count channels and set to "ppmin_num_channels" */ j = 0; // initial ppm-in Sync counter=0 (or missed signal) k[0] = 0; // set ppm-in signal beginning k[0]=0, ignore the first count total channels number if (k[1]>1 && k[1]==k[2]) { // compare total channels number k[1], k[2] ppmin_num_channels = k[1]; // save number of channels found ppmSync = 1; // in-sync i = 0; } } } else { // t<MIN_PPMin_Sync, ppm-input each Channel-signal k[j]++; // conut 3-times for total channels number, k[0], k[1], k[2]. // ignore the first count total channels number k[0] } } else { // ppm-in status : Sync, /* (3) get each channel value and set to "Channel[i]" , [0...ppmin_num_channels-1] for each Channel-signal */ int ch = (t - (Model.ppmin_centerpw * 2))*10000 / (Model.ppmin_deltapw * 2); //Convert input to channel value ppmChannels[i] = ch; i++; // set for next count ppm-signal width /* (4) continue count channels and compare "num_channels", if not equal => disconnect(no-Sync) and re-connect (re-Sync) */ if (t>MIN_PPMin_Sync) { // Got the ppm-input Sync-signal if ((i-1) != ppmin_num_channels) { // Trainer disconnect (coach-trainee disconnect or noise) ppmSync = 0; // set ppm-in status to "Not Sync" } i = 0; // initial counter for capture next period } } }
void delay_nus(sGrpDev* pGrpDev, uint32_t nCount) { volatile uint16_t TIMCounter = nCount; if (pGrpDev->pTimer) { /* Counter enable. */ /* Reset prescaler value. */ timer_set_prescaler(pGrpDev->pTimer, 72); timer_direction_down(pGrpDev->pTimer); timer_enable_counter(pGrpDev->pTimer); timer_set_counter(pGrpDev->pTimer, TIMCounter); /* Start timer. */ TIM_CR1(pGrpDev->pTimer) |= TIM_CR1_CEN; while (TIMCounter > 1) { TIMCounter = timer_get_counter(pGrpDev->pTimer); } timer_disable_counter(pGrpDev->pTimer); } }
void tim1_up_isr(void) { #elif defined(STM32F4) void tim1_up_tim10_isr(void) { #endif if((TIM1_SR & TIM_SR_UIF) != 0) { timer_rollover_cnt+=(1<<16); timer_clear_flag(TIM1, TIM_SR_UIF); } } void tim1_cc_isr(void) { if((TIM1_SR & PPM_CC_IF) != 0) { timer_clear_flag(TIM1, PPM_CC_IF); uint32_t now = timer_get_counter(TIM1) + timer_rollover_cnt; DecodePpmFrame(now); } }
void delay_ms(sGrpDev* pGrpDev, uint32_t nCount) { volatile uint16_t TIMCounter;// = nCount; uint16_t cnt2; /* Counter enable. */ /* Reset prescaler value. */ timer_set_prescaler(pGrpDev->pTimer, 7200); timer_direction_down(pGrpDev->pTimer); timer_enable_counter(pGrpDev->pTimer); for (cnt2 = 0; cnt2 < 750; cnt2++) { TIMCounter = nCount; timer_set_counter(pGrpDev->pTimer, TIMCounter); /* Start timer. */ TIM_CR1(pGrpDev->pTimer) |= TIM_CR1_CEN; while (TIMCounter > 1) { TIMCounter = timer_get_counter(pGrpDev->pTimer); } } timer_disable_counter(pGrpDev->pTimer); }
void Encoder::UpdateRotorAngle(int dir) { if (encMode == SINGLE) { int16_t numPulses = GetPulseTimeFiltered(); angle += (int16_t)(dir * numPulses * anglePerPulse); } else if (encMode == RESOLVER) { GetAngleResolver(); } else { static uint16_t lastAngle = 0; uint16_t angleDiff; if (encMode == SPI) { angle = GetAngleSPI(); //Gets 12-bit angle <<= 4; uint16_t diffPos = angle - lastAngle; uint16_t diffNeg = lastAngle - angle; angleDiff = MIN(diffNeg, diffPos); } else { uint32_t cntVal = timer_get_counter(REV_CNT_TIMER); cntVal *= TWO_PI; cntVal /= pulsesPerTurn * 4; angle = (uint16_t)cntVal; angleDiff = (angle - lastAngle) & 0xFFFF; if ((TIM_CR1(REV_CNT_TIMER) & TIM_CR1_DIR_DOWN)) angleDiff = (lastAngle - angle) & 0xFFFF; } lastAngle = angle; turnsSinceLastSample += angleDiff; //Param::SetDig(Param::tm_meas, angle); } }
/** * Timer 2 interrupt handler */ void tim2_isr(void) { if (timer_get_flag(TIM2, TIM_SR_CC1IF)) { timer_clear_flag(TIM2, TIM_SR_CC1IF); /* prepare for next comm */ comm_tim_data.last_capture_time = timer_get_counter(TIM2); //comm_tim_data.last_capture_time = timer_get_ic_value(TIM2, TIM_OC1); //comm_tim_data.last_capture_time = TIM2_CCR1; /* triggering commutation event */ if (comm_tim_trigger_comm || comm_tim_trigger_comm_once) { timer_generate_event(TIM1, TIM_EGR_COMG); //timer_generate_event(TIM1, TIM_EGR_COMG | TIM_EGR_UG); } /* (re)setting "semaphors" */ comm_tim_trigger_comm_once = false; comm_tim_trigger = true; /* Set next comm time */ timer_set_oc_value(TIM2, TIM_OC1, comm_tim_data.last_capture_time + (comm_tim_data.freq * 2)); //timer_set_oc_value(TIM2, TIM_OC1, // comm_tim_data.last_capture_time + // 65535); TOGGLE(DP_EXT_SCL); } if (timer_get_flag(TIM2, TIM_SR_UIF)) { timer_clear_flag(TIM2, TIM_SR_UIF); comm_tim_state.update_count++; } }
void tim1_cc_isr(void) { if(timer_get_flag(TIM1, TIM_SR_CC2IF)) { if (timer_get_flag(TIM1, TIM_SR_CC2OF)) overcapture++; uint32_t current_counter = timer_get_counter(TIM1); uint32_t current_overflow = overflow_counter; timer_clear_flag(TIM1, TIM_SR_CC2IF); //gpio_toggle(GPIOE, GPIO13); /* toggle pin for 'scope debugging */ if (m_state == PENDING) { // start measurement edges=0; start_counter = current_counter; overflow_counter = 0; m_state = RUNNING; } if (edges == NUM_EDGES) { // end measurement cycles_during_measurement = (current_overflow*TIMER1_PERIOD) + current_counter - start_counter; m_state = IDLE; } else { edges += 1; } } }
/** * Get the counter value of Timer * @return Counter value */ uint16_t tim_get_counter(void) { return timer_get_counter(TIM6); }
timeval_t current_time() { return timer_get_counter(TIM2); }
/** * Update the next prev time to the current time */ void comm_tim_update_next_prev(void) { comm_tim_state.next_prev_time = timer_get_counter(TIM2); }