/** * @brief External INT0 IRQ * * @param None * * @return None * * @details The External INT0(P3.2) default IRQ, declared in startup_M051Series.s. */ void EINT0_IRQHandler(void) { U8 OK = 0; //判断是否接收到1个编码位 U8 RODATA = 0; //用于记录判断高低电平的编码是0还是1 /* For P3.2, clear the INT flag */ GPIO_CLR_INT_FLAG(P3, BIT2); if(0 == HONGWAI) { TIMER_Stop(TIMER0); //接收到低电平关闭定时器 TIMER_Stop(TIMER1); //接收到低电平关闭定时器 if(Width == 75) //接收到高电平时间超过1.5ms时 接收到的不是所需的数据 { Remote_Rdy = 0; //红外没接收到数据 index = 0; //红外编码位计数清零 } else if((Width >= 40) && (Width < 75)) {OK=1;RODATA = 0;}//高电平时间是0.5ms,是“0” else if((Width >= 5) && (Width < 40)) {OK=1;RODATA = 1;}//高电平时间是1.5s,是“1” else {} if(OK) { OK=0; index++; code >>= 1; if(RODATA) code += 0x800; else {} } else {} if(11 == index)
void lp_ticker_set_interrupt(timestamp_t timestamp) { uint32_t now = lp_ticker_read(); wakeup_tick = timestamp; TIMER_Stop((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); /** * FIXME: Scheduled alarm may go off incorrectly due to wrap around. * Conditions in which delta is negative: * 1. Wrap around * 2. Newly scheduled alarm is behind now */ //int delta = (timestamp > now) ? (timestamp - now) : (uint32_t) ((uint64_t) timestamp + 0xFFFFFFFFu - now); int delta = (int) (timestamp - now); if (delta > 0) { cd_major_minor_clks = (uint64_t) delta * US_PER_TICK * TMR3_CLK_PER_SEC / US_PER_SEC; lp_ticker_arm_cd(); } else { cd_major_minor_clks = cd_minor_clks = 0; /** * This event was in the past. Set the interrupt as pending, but don't process it here. * This prevents a recurive loop under heavy load which can lead to a stack overflow. */ NVIC_SetPendingIRQ(timer3_modinit.irq_n); } }
void DEBUG_LEDCallback() { *ATLYS_GPIO_DATA_PTR ^= ATLYS_GPIO_LED1; if (--dwBlinkCnt == 0) { *ATLYS_GPIO_DATA_PTR &= ~ATLYS_GPIO_LED1; TIMER_Stop(dwDebugTimer); } }
void lp_ticker_set_interrupt(timestamp_t timestamp) { uint32_t delta = timestamp - lp_ticker_read(); wakeup_tick = timestamp; TIMER_Stop((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); cd_major_minor_clks = (uint64_t) delta * US_PER_TICK * TMR3_CLK_PER_SEC / US_PER_SEC; lp_ticker_arm_cd(); }
void lp_ticker_set_interrupt(uint32_t now, uint32_t time) { wakeup_tick = time; TIMER_Stop((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); int delta = (time > now) ? (time - now) : (uint32_t) ((uint64_t) time + 0xFFFFFFFFu - now); // NOTE: If this event was in the past, arm an interrupt to be triggered immediately. cd_major_minor_ms = delta * MS_PER_TICK; lp_ticker_arm_cd(); }
void us_ticker_set_interrupt(timestamp_t timestamp) { TIMER_Stop((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname)); int delta = (int) (timestamp - us_ticker_read()); if (delta > 0) { cd_major_minor_us = delta * US_PER_TICK; us_ticker_arm_cd(); } else { cd_major_minor_us = cd_minor_us = 0; /** * This event was in the past. Set the interrupt as pending, but don't process it here. * This prevents a recurive loop under heavy load which can lead to a stack overflow. */ NVIC_SetPendingIRQ(timer1hires_modinit.irq_n); } }
/******************************************************************************* * Function Name: TIMER_Sleep ******************************************************************************** * * Summary: * Stop and Save the user configuration * * Parameters: * void * * Return: * void * * Global variables: * TIMER_backup.TimerEnableState: Is modified depending on the * enable state of the block before entering sleep mode. * *******************************************************************************/ void TIMER_Sleep(void) { #if(!TIMER_UDB_CONTROL_REG_REMOVED) /* Save Counter's enable state */ if(TIMER_CTRL_ENABLE == (TIMER_CONTROL & TIMER_CTRL_ENABLE)) { /* Timer is enabled */ TIMER_backup.TimerEnableState = 1u; } else { /* Timer is disabled */ TIMER_backup.TimerEnableState = 0u; } #endif /* Back up enable state from the Timer control register */ TIMER_Stop(); TIMER_SaveConfig(); }
/** * @brief Application specific processing * @param None * @retval None */ static void TS_Application_Process(void) { switch(BLE_Profile_Read_DeviceState()) { case APPL_INIT_DONE: { if(TS_Start_Timer == 1) { /** * Make sure to stop the timer only when it has been started at least once * The Timer ID must be existing to call TIMER_Stop(); */ TIMER_Stop(profileApplContext.profileTimer_Id); TS_Start_Timer = 0; } Advertize(); BLE_Profile_Write_DeviceState(APPL_ADVERTISING); } break; case APPL_TERMINATE_LINK: { /* on disconnection complete, the state will be changed to INIT_DONE */ BLE_Profile_Disconnect(); BLE_Profile_Write_DeviceState(APPL_UNINITIALIZED); } break; case APPL_CONNECTED: { if (TS_Start_Timer == 0) { TIMER_Start(profileApplContext.profileTimer_Id, (uint16_t)(TS_TIMER_VALUE/TIMERSERVER_TICK_VALUE)); TS_Start_Timer = 1; } } break; default: break; } return; }
/* ----------------------- Start implementation -----------------------------*/ BOOL xMBPortTimersInit(USHORT usTim1Timerout50us) { uint16_t PrescalerValue = 0; CLK_EnableModuleClock(TMR0_MODULE); CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK, 0); // Set timer frequency to 20KHZ (50us) TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, usTim1Timerout50us); // Enable timer interrupt TIMER_EnableInt(TIMER0); NVIC_SetPriority(TMR0_IRQn, 10 ); //定时器的中断优先级应该比串口的低 NVIC_EnableIRQ(TMR0_IRQn); TIMER_SET_PRESCALE_VALUE(TIMER0, 0); // Stop Timer 0 TIMER_Stop(TIMER0); return TRUE; }
VOID CALLBACK TIMER_TimeoutProc(PVOID lpParameter, BOOLEAN TimerOrWaitFired) { ULONG ulTimerID; TIMER_MSG_S stMsg; ulTimerID = (ULONG)lpParameter; stMsg.ulName = g_pstTimerCB[ulTimerID].ulName; stMsg.ulPara = g_pstTimerCB[ulTimerID].ulPara; SYS_SendMsg(SYS_MODULE_TIMER, g_pstTimerCB[ulTimerID].ulModuleID, &stMsg, sizeof(TIMER_MSG_S)); if (g_pstTimerCB[ulTimerID].eType == TIMER_TYPE_NOLOOP) { TIMER_Stop(ulTimerID); } return; }
void vMBPortTimersDisable() { TIMER_Stop(TIMER0); }
//========================================================================= // Additional initialisations //------------------------------------------------------------------------- __myevic__ void CustomStartup() { //------------------------------------------------------------------------- // EADC test if ( 0 ) { uint32_t s1, s2, s3; SetADCState( 0, 1 ); SetADCState( 4, 1 ); do { ClearScreenBuffer(); CLK_SysTickDelay( 10 ); s3 = ADC_Read( 4 ); CLK_SysTickDelay( 10 ); s1 = ADC_Read( 18 ); CLK_SysTickDelay( 10 ); s2 = ADC_Read( 0 ); DrawValue( 8, 0, s1, 0, 0x29, 4 ); DrawValue( 8, 20, s2, 0, 0x29, 4 ); DrawValue( 8, 40, s3, 0, 0x29, 4 ); DisplayRefresh(); WaitOnTMR2( 1000 ); } while ( PD3 ); } //------------------------------------------------------------------------- // Timer test 1 if ( 0 ) { TIMER_Stop( TIMER3 ); TIMER_Close( TIMER3 ); MemClear( gPlayfield.uc, 256 ); CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_LIRC, 0 ); gPlayfield.ul[1] = TIMER_Open( TIMER3, TIMER_PERIODIC_MODE, 10 ); TIMER_EnableInt( TIMER3 ); TIMER_Start( TIMER3 ); } //------------------------------------------------------------------------- // Timer test 2 if ( 0 ) { TIMER_Close( TIMER2 ); TIMER_Close( TIMER3 ); MemClear( gPlayfield.uc, 256 ); CLK_SetModuleClock( TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HXT, 0 ); CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_LIRC, 0 ); CLK_EnableModuleClock( TMR2_MODULE ); CLK_EnableModuleClock( TMR3_MODULE ); __set_PRIMASK(1); TIMER3->CTL |= TIMER_CTL_RSTCNT_Msk; TIMER2->CTL |= TIMER_CTL_RSTCNT_Msk; TIMER3->CMP = 1000; TIMER3->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE; TIMER2->CTL = TIMER_CTL_CNTEN_Msk | TIMER_CONTINUOUS_MODE; while(!(TIMER3->INTSTS & TIMER_INTSTS_TIF_Msk)); TIMER2->CTL = 0; gPlayfield.ul[0] = TIMER2->CNT; __set_PRIMASK(0); TIMER_Close( TIMER2 ); TIMER_Close( TIMER3 ); CLK_SetModuleClock( TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HIRC, 0 ); CLK_EnableModuleClock( TMR2_MODULE ); TIMER_Open( TIMER2, TIMER_PERIODIC_MODE, 1000 ); TIMER_EnableInt( TIMER2 ); TIMER_Start( TIMER2 ); CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_HXT, 0 ); CLK_EnableModuleClock( TMR3_MODULE ); TIMER_Open( TIMER3, TIMER_PERIODIC_MODE, 10 ); TIMER_EnableInt( TIMER3 ); TIMER_Start( TIMER3 ); } return; }
/*---------------------------------------------------------------------------------------------------------*/ int main(void) { volatile uint32_t u32InitCount; /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, peripheral clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); printf("\n\nCPU @ %d Hz\n", SystemCoreClock); printf("+-------------------------------------------------+\n"); printf("| Timer0 Power-down and Wake-up Sample Code |\n"); printf("+-------------------------------------------------+\n\n"); printf("# Timer Settings:\n"); printf(" Timer0: Clock source is LIRC(10 kHz); Toggle-output mode and frequency is 1 Hz; Enable interrupt and wake-up.\n"); printf("# System will enter to Power-down mode while Timer0 interrupt count is reaching 3;\n"); printf(" And be waken-up while Timer0 interrupt count is reaching 4.\n\n"); /* To program PWRCON register, it needs to disable register protection first. */ SYS_UnlockReg(); /* Open Timer0 frequency to 1 Hz in toggle-output mode */ TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1); /* Enable Timer0 interrupt and wake-up function */ TIMER_EnableInt(TIMER0); TIMER_EnableWakeup(TIMER0); /* Enable Timer0 NVIC */ NVIC_EnableIRQ(TMR0_IRQn); /* Start Timer0 counting */ TIMER_Start(TIMER0); u32InitCount = g_u8IsTMR0WakeupFlag = g_au32TMRINTCount[0] = 0; while(g_au32TMRINTCount[0] < 10) { if(g_au32TMRINTCount[0] != u32InitCount) { printf("Timer0 interrupt count - %d\n", g_au32TMRINTCount[0]); if(g_au32TMRINTCount[0] == 3) { PowerDownFunction(); /* Check if Timer0 time-out interrupt and wake-up flag occurred */ while(1) { if(((CLK->PWRCON & CLK_PWRCON_PD_WU_STS_Msk) == CLK_PWRCON_PD_WU_STS_Msk) && (g_u8IsTMR0WakeupFlag == 1)) break; } printf("System has been waken-up done. (Timer0 interrupt count is %d)\n\n", g_au32TMRINTCount[0]); } u32InitCount = g_au32TMRINTCount[0]; } } /* Stop Timer0 counting */ TIMER_Stop(TIMER0); printf("*** PASS ***\n"); while(1); }