void PPMin_TIM_Init() { /* Enable TIM1 clock. */ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN); /* No Enable TIM1 interrupt. */ // nvic_enable_irq(NVIC_TIM1_IRQ); // nvic_set_priority(NVIC_TIM1_IRQ, 16); //High priority /* Reset TIM1 peripheral. */ timer_disable_counter(TIM1); timer_reset(TIM1); /* Timer global mode: * - No divider * - Alignment edge * - Direction up */ timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); /* Reset prescaler value. timer updates each uSecond */ timer_set_prescaler(TIM1, PPMIn_prescaler); // uSecond (72MHz / (35+1) = 2MHz = 0.5uSecond timer_set_period(TIM1, PPMIn_period); // 3300uSecond= 2MHz*6600times, TIM1_prescaler=0.5uSecond /* Disable preload. */ timer_disable_preload(TIM1); /* Continous mode. */ timer_continuous_mode(TIM1); /* Disable outputs. */ timer_disable_oc_output(TIM1, TIM_OC1); timer_disable_oc_output(TIM1, TIM_OC2); timer_disable_oc_output(TIM1, TIM_OC3); timer_disable_oc_output(TIM1, TIM_OC4); /* -- OC1 configuration -- */ /* Configure global mode of line 1. */ /* Enable CCP1 */ timer_disable_oc_clear(TIM1, TIM_OC1); timer_disable_oc_preload(TIM1, TIM_OC1); timer_set_oc_slow_mode(TIM1, TIM_OC1); timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_FROZEN); /* Enable commutation interrupt. */ // timer_enable_irq(TIM1, TIM_DIER_CC1IE); /* Disable CCP1 interrupt. */ timer_disable_irq(TIM1, TIM_DIER_CC1IE); /* Counter enable. */ timer_disable_counter(TIM1); }
void PPMin_Stop() { nvic_disable_irq(NVIC_EXTI9_5_IRQ); exti_disable_request(_PWM_EXTI); timer_disable_counter(TIM1); ppmSync = 0; }
static int command(char *command, size_t length) { size_t i; calculate_crc_and_ack(command, length); for (i = 0; i < length; i++) usart_send_blocking(USART2, command[i]); timer_set_counter(TIM5, 0); timer_clear_flag(TIM5, TIM_SR_UIF); timer_enable_counter(TIM5); for (i = 0; i < sizeof(expect_ack); ) { while (!timer_get_flag(TIM5, TIM_SR_UIF) && !usart_get_flag(USART2, USART_SR_RXNE)); if (timer_get_flag(TIM5, TIM_SR_UIF)) break; if (expect_ack[i] != usart_recv(USART2)) break; } timer_disable_counter(TIM5); return (i == sizeof(expect_ack)); }
/** * Initialize the DSM timer */ static void timer_dsm_init(void) { rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN); // Enable the timer NVIC nvic_enable_irq(TIMER_DSM_NVIC); nvic_set_priority(TIMER_DSM_NVIC, 1); // Setup the timer timer_disable_counter(TIMER_DSM); timer_reset(TIMER_DSM); timer_set_mode(TIMER_DSM, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_disable_preload(TIMER_DSM); timer_continuous_mode(TIMER_DSM); // Disable interrupts on Compare 1 timer_disable_irq(TIMER_DSM, TIM_DIER_CC1IE); // Clear the Output Compare of OC1 timer_disable_oc_clear(TIMER_DSM, TIM_OC1); timer_disable_oc_preload(TIMER_DSM, TIM_OC1); timer_set_oc_slow_mode(TIMER_DSM, TIM_OC1); timer_set_oc_mode(TIMER_DSM, TIM_OC1, TIM_OCM_FROZEN); // Set timer updates each 10 microseconds #if DEBUG && !DSM_RECEIVER && !DSM_MITM timer_set_prescaler(TIMER_DSM, 720000 - 1); #else timer_set_prescaler(TIMER_DSM, 720 - 1); #endif timer_set_period(TIMER_DSM, 65535); // Start the timer timer_enable_counter(TIMER_DSM); }
/* Function to stop a timer */ void timer_stop(void) { /* counter disable */ timer_disable_counter(TIM2); /* disable update interrupt */ timer_disable_irq(TIM2, TIM_DIER_UIE); }
void tim7_isr(void) { timer_clear_flag(TIM7, TIM_SR_UIF); state.rht_timeout = true; nvic_disable_irq(NVIC_TIM7_IRQ); timer_disable_irq(TIM7, TIM_DIER_UIE); timer_disable_counter(TIM7); }
void tim2_isr(void) { gpio_toggle(GPIOB, GPIO8); /* LED2 on/off. */ if (timer_get_flag(TIM2, TIM_SR_UIF)) timer_clear_flag(TIM2, TIM_SR_UIF); /* Clear interrrupt flag. */ timer_get_flag(TIM2, TIM_SR_UIF); /* Reread to force the previous write */ timer_disable_irq(TIM2, TIM_DIER_UIE); timer_disable_counter(TIM2); }
void BeepHandler(void) { static u32 clk1ms; //比较系统CLICK if(SysTimerClk==clk1ms) return; clk1ms=SysTimerClk; //短时鸣响计数器 if(BeepShortCnt) { BeepShortCnt--; if(BeepShortCnt==0) { Beep(1000,0);//解决很微小的嗡嗡声 timer_disable_counter(BEEP_TIM); } return; } //蜂鸣器分频,50ms为一个周期 if(BeepDivider++<50) return; BeepDivider=0; //如果没到末尾 if(pBeepMusic && pBeepMusic[0]) { if(BeepCnt++>=pBeepMusic[0]) { pBeepMusic+=4; BeepCnt=0; if(BeepMusicEnable) Beep(BeepMusicTable[pBeepMusic[1]],pBeepMusic[2]); if(BeepVibrator) PowerVibrate(pBeepMusic[3]); } } else { //到末尾停止声响 pBeepMusic=NULL; timer_disable_counter(BEEP_TIM); PowerVibrate(0); } }
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); } }
static int silence_nmea() { int attempt; size_t i; /* Configure the GPS */ for (attempt = 0; attempt < 4; attempt++) { calculate_crc_and_ack(set_port, sizeof(silence_nmea)); for (i = 0; i < sizeof(set_port); i++) usart_send_blocking(USART2, set_port[i]); timer_clear_flag(TIM5, TIM_SR_UIF); timer_set_counter(TIM5, 0); timer_enable_counter(TIM5); for (i = 0; i < sizeof(expect_ack); ) { while (!timer_get_flag(TIM5, TIM_SR_UIF) && !usart_get_flag(USART2, USART_SR_RXNE)); if (timer_get_flag(TIM5, TIM_SR_UIF)) break; if (expect_ack[i] == usart_recv(USART2)) i++; else i = 0; } timer_disable_counter(TIM5); if (i < sizeof(expect_ack)) continue; else break; } while (usart_get_flag(USART2, USART_SR_RXNE)) usart_recv(USART2); return attempt < 4; }
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 BACKLIGHT_Brightness(u8 brightness) { timer_disable_counter(TIM3); if (brightness == 0) { // Turn off Backlight gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO1); } else if(brightness > 9) { // Turn on Backlight full gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); gpio_set(GPIOB, GPIO1); } else { gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO1); u32 duty_cycle = 720 * brightness / 10 ; timer_set_oc_value(TIM3, TIM_OC4, duty_cycle); timer_enable_counter(TIM3); } }
/** @brief Timer3 callback * * callback function for Timer3 */ void tim3_isr(void) { uint8_t *bftemp; timer_clear_flag(TIM3, TIM_SR_UIF); /* wait an amount of time to trigger the WS2812 sync pulse */ if( waitcnt < NR_OF_WAITCOUNTS) { waitcnt++; } else { waitcnt = 0; timer_disable_counter(TIM3); timer_disable_irq(TIM3, TIM_DIER_UIE); /* if new framebuffer data is available * switch the unrolled buffer with the backbuffer */ if(update_flag == 1) { bftemp = actual_bitframe; actual_bitframe = drawing_bitframe; drawing_bitframe = bftemp; update_flag = 0; } /* restart the sending process */ ws2812_send(); } }
int main (void) { clock_setup(); //setup the main clock for 168MHz usart_setup(); //setup usart1 for debug messages debug_send("\n\n\n*************************************\n"); debug_send("* Lynx test starting up *\n"); debug_send("* Waveforms *\n"); debug_send("* Joe Roberts *\n"); debug_send("* UROP - Summer 2013 *\n"); debug_send("*************************************\n\n\n"); debug_send("ledpins_setup()\n"); ledpins_setup(); //setup the status led's gpio's debug_send("dac_setup()\n"); dac_setup(); //setup the dac gpio's debug_send("transmit_timer_setup(1)\n"); transmit_timer_setup(1); //setup the transmit timer and its interrupt debug_send("Starting the transmission timer\n"); timer_enable_counter(TIM2); while(1) { usart_wait_recv_ready(USART2); timer_disable_counter(TIM2); timer_set_counter(TIM2, 0); char message = usart_recv_blocking(USART2); debug_send("\nRecieved "); usart_send_blocking(USART2, message); bool done = false; char param; while(!done) { debug_send(": Send your parameter [0-9]\n"); usart_wait_recv_ready(USART2); param = usart_recv_blocking(USART2); if((param>47)&&(param<58)) { done = true; param = param-48; //ASCII to number } else debug_send("\nParameter must be [0-9] - try again\n"); } if(message=='n') n = 1000*param; if(message=='t') { transmit_timer_setup(10*param); timer_enable_counter(TIM2); } if(message=='w') set_waveform(param); } debug_send("We somehow escaped the for ever loop\n"); debug_send("..This is a little sad, there's not much you can do to fix this\n\n\n\n"); debug_send("goodbye, cruel world"); while(1); return 1; }
/* ----------------------- Disable timer -----------------------------*/ inline void vMBPortTimersDisable( ) { timer_disable_irq(TIM2, TIM_DIER_UIE); timer_disable_counter(TIM2); }