void TIM_Config(void) { TimHandle3.Instance = TIM3x; TIM_Init(&TimHandle3); TimHandle4.Instance = TIM4x; TIM_Init(&TimHandle4); }
float TimerCapture(void){ PINSEL_CFG_Type PinCfg; PinCfg.Funcnum = 3; PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinCfg.Pinmode = PINSEL_PINMODE_PULLDOWN; PinCfg.Portnum = 0; PinCfg.Pinnum = 4; PINSEL_ConfigPin(&PinCfg); TIM_CAPTURECFG_Type captcfg; captcfg.CaptureChannel = 0; captcfg.IntOnCaption = ENABLE; captcfg.FallingEdge = ENABLE; captcfg.RisingEdge = ENABLE; TIM_TIMERCFG_Type timercfg; timercfg.PrescaleOption = TIM_PRESCALE_USVAL; timercfg.PrescaleValue = 1; TIM_Init(LPC_TIM2, TIM_TIMER_MODE, &timercfg); TIM_ConfigCapture(LPC_TIM2, &captcfg); NVIC_SetPriority(TIMER2_IRQn, ((0x01<<3)|0x01)); NVIC_EnableIRQ(TIMER2_IRQn); TIM_Cmd(LPC_TIM2, ENABLE); return ultradist; }
/*********************************************************************//** * @brief Delay millisecond * @param[in] time (ms) * @return None **********************************************************************/ void Timer_Wait(uint32_t time) { // Initialize timer 0, prescale count time of 1ms TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1000; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //do no thing for external output TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_NOTHING; // Set Match value, count value is time (timer * 1000uS =timer mS ) TIM_MatchConfigStruct.MatchValue = time; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct); // To start timer 0 TIM_Cmd(LPC_TIM0,ENABLE); }
void timer_init(int frequency) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct; PINSEL_CFG_Type PinCfg; PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 10; TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; // Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; // Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; // Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value, count value of 100000 (100000 * 10uS = 1000000us = 1s --> 1 Hz) TIM_MatchConfigStruct.MatchValue = (int) 100000.0 / frequency; TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0, &TIM_MatchConfigStruct); // preemption = 1, sub-priority = 1 NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); }
static void TIM2_CFG( ) { TIM_TIMERCFG_Type TMR2_Cfg; TIM_MATCHCFG_Type TMR2_Match; /* On reset, Timer0/1 are enabled (PCTIM0/1 = 1), and Timer2/3 are disabled (PCTIM2/3 = 0).*/ /* Initialize timer 0, prescale count time of 100uS */ TMR2_Cfg.PrescaleOption = TIM_PRESCALE_USVAL; TMR2_Cfg.PrescaleValue = 10000; /* Use channel 0, MR0 */ TMR2_Match.MatchChannel = 0; /* Enable interrupt when MR0 matches the value in TC register */ TMR2_Match.IntOnMatch = ENABLE; /* Enable reset on MR0: TIMER will reset if MR0 matches it */ TMR2_Match.ResetOnMatch = FALSE; /* Don't stop on MR0 if MR0 matches it*/ TMR2_Match.StopOnMatch = TRUE; /* Do nothing for external output pin if match (see cmsis help, there are another options) */ TMR2_Match.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; /* Set Match value, count value of 2 (2 * 100uS = 200us ) */ TMR2_Match.MatchValue = 250; /* Set configuration for Tim_config and Tim_MatchConfig */ TIM_Init(LPC_TIM2, TIM_TIMER_MODE, &TMR2_Cfg); TIM_ConfigMatch(LPC_TIM2, &TMR2_Match); NVIC_SetPriorityGrouping(3); /* Preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER2_IRQn, 2); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER2_IRQn); /* Start timer 0 */ TIM_Cmd(LPC_TIM2, ENABLE); }
void InitTimerProcessSensorRMSValue(void) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Initialize timer 1, prescale count time of tickval TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.MatchValue = 20000; //20000us TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM1, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM1,&TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 0 */ NVIC_SetPriority(TIMER1_IRQn, ((0x01<<3)|PRIORITY_TIMER1)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER1_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM1,ENABLE); }
int platform_init() { unsigned i; TIM_InitTypeDef tim; TIM_TypeDef* base; // System configuration platform_config_scu(); // PIO setup for( i = 0; i < 10; i ++ ) GPIO_DeInit( ( GPIO_TypeDef* )port_data[ i ] ); // Initialize VIC VIC_DeInit(); // UART setup (only STR9_UART is used in this example) platform_uart_setup( CON_UART_ID, CON_UART_SPEED, 8, PLATFORM_UART_PARITY_NONE, PLATFORM_UART_STOPBITS_1 ); // Initialize timers for( i = 0; i < 4; i ++ ) { base = ( TIM_TypeDef* )timer_data[ i ]; TIM_DeInit( base ); TIM_StructInit( &tim ); tim.TIM_Clock_Source = TIM_CLK_APB; tim.TIM_Prescaler = 255; TIM_Init( base, &tim ); TIM_CounterCmd( base, TIM_START ); } cmn_platform_init(); return PLATFORM_OK; }
/*********************************************************************//** * @brief Initialize external NOR FLASH memory * @param[in] None * @return None **********************************************************************/ void NORFLASHInit( void ) { TIM_TIMERCFG_Type TIM_ConfigStruct; EMC_STATIC_MEM_Config_Type config; /************************************************************************** * Initialize EMC for NOR FLASH **************************************************************************/ config.CSn = 0; config.AddressMirror = 0; config.ByteLane = 1; config.DataWidth = 16; config.ExtendedWait = 0; config.PageMode = 0; config.WaitWEn = 2; config.WaitOEn = 2; config.WaitWr = 0x1f; config.WaitPage = 0x1f; config.WaitRd = 0x1f; config.WaitTurn = 0x1f; StaticMem_Init(&config); // init timer TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_Waitms(100); //delay time TIM_Waitms(10); return; }
int Timer_delay_ms(int count) { // Initialize timer 0, prescale count time of 100uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1000; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Disable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; // Set Match value, count value of 1000 (1000 * 100uS = 100mS --> 10Hz) TIM_MatchConfigStruct.MatchValue = count; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct); TIM_Cmd(LPC_TIM0,ENABLE); // Wait for 1000 millisecond while(!(TIM_GetIntStatus(LPC_TIM0,TIM_MR0_INT))); TIM_ClearIntPending(LPC_TIM0,TIM_MR0_INT); return 0; }
void Timer(uint32_t period){ TIM_MATCHCFG_Type matchcfg; matchcfg.MatchChannel = 0; matchcfg.IntOnMatch = ENABLE; matchcfg.ResetOnMatch = DISABLE; matchcfg.StopOnMatch = DISABLE; matchcfg.MatchValue = period; matchcfg.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; TIM_TIMERCFG_Type timercfg; timercfg.PrescaleOption = TIM_PRESCALE_USVAL; timercfg.PrescaleValue = 1; TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &timercfg); TIM_ConfigMatch(LPC_TIM0, &matchcfg); tim0_flag = 0; NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); NVIC_EnableIRQ(TIMER0_IRQn); TIM_Cmd(LPC_TIM0, ENABLE); while(!tim0_flag); TIM_DeInit(LPC_TIM0); NVIC_DisableIRQ(TIMER0_IRQn); }
//===========================================================// // But: Init Timer0 pour generer 1 signal à 1ms //===========================================================// void T0_Init(void) { TIM_TIMERCFG_Type Timer_Config_Structure; // structures pour passer les valeurs d'initialisation d'un timer TIM_MATCHCFG_Type Timer_MatchConfig_Structure; // Timer0 Initialization Timer_Config_Structure.PrescaleOption = TIM_PRESCALE_USVAL; // Timer en Mode us Timer_Config_Structure.PrescaleValue = TIMER_TIME_STEP; // TC incrementé de 1 chaque 10us TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&Timer_Config_Structure); // Configuration of Match channels Timer_MatchConfig_Structure.MatchChannel = 0; // Use channel 0, MR0 for both Timers Timer_MatchConfig_Structure.IntOnMatch = FALSE; // En Timer IT qd MR0 = TC Register Timer_MatchConfig_Structure.ResetOnMatch = TRUE; // En Timer RST qd MR0 = TC Register Timer_MatchConfig_Structure.StopOnMatch = FALSE; // No Stop Timer qd MR0 = TC Register Timer_MatchConfig_Structure.ExtMatchOutputType = TIM_EXTMATCH_TOGGLE; // Toggle MATx.y pin qd MR0 = TC Register Timer_MatchConfig_Structure.MatchValue = TIMER_MATCH_VALUE_1ms; // Match Value Timer0 TIM_ConfigMatch(LPC_TIM0,&Timer_MatchConfig_Structure); // Config Timer0 TIM_Cmd(LPC_TIM0,ENABLE); // Start Timer0 }
static void prvSetupTimerInterrupt( void ) { unsigned char a; unsigned short b; unsigned long n = configCPU_PERIPH_HZ / configTICK_RATE_HZ; TIM_InitTypeDef timer; SCU_APBPeriphClockConfig( __TIM23, ENABLE ); TIM_DeInit(TIM2); TIM_StructInit(&timer); prvFindFactors( n, &a, &b ); timer.TIM_Mode = TIM_OCM_CHANNEL_1; timer.TIM_OC1_Modes = TIM_TIMING; timer.TIM_Clock_Source = TIM_CLK_APB; timer.TIM_Clock_Edge = TIM_CLK_EDGE_RISING; timer.TIM_Prescaler = a-1; timer.TIM_Pulse_Level_1 = TIM_HIGH; timer.TIM_Pulse_Length_1 = s_nPulseLength = b-1; TIM_Init (TIM2, &timer); TIM_ITConfig(TIM2, TIM_IT_OC1, ENABLE); /* Configure the VIC for the WDG interrupt. */ VIC_Config( TIM2_ITLine, VIC_IRQ, 10 ); VIC_ITCmd( TIM2_ITLine, ENABLE ); /* Install the default handlers for both VIC's. */ VIC0->DVAR = ( unsigned long ) prvDefaultHandler; VIC1->DVAR = ( unsigned long ) prvDefaultHandler; TIM_CounterCmd(TIM2, TIM_CLEAR); TIM_CounterCmd(TIM2, TIM_START); }
// setup hardware timer timerNum // timerCallback: if not-NULL, will be called on any timer interrupt. Default is Match0 // For convenience various default values are setup: a default reload/Match0 period of // 10us is set, this can be changed by setHwTimerInterval. void setupHwTimer (uint16_t timerNum, tHwTimerCallback timerCallback) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Prescale in absolute value TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; TIM_Init (pTimerRegs[timerNum], TIM_TIMER_MODE, &TIM_ConfigStruct); /* Configure Timer to have the same clock as CPU: 100MHz */ CLKPWR_SetPCLKDiv (TimerConfig[timerNum].ClkPwr_PClkSel, CLKPWR_PCLKSEL_CCLK_DIV_1); // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = 1; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = 1; //Do not stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = 0; //Do not toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value, count value of 100000000 (100000000 * 10ns = 100000000ns = 1s --> 1 Hz) TIM_MatchConfigStruct.MatchValue = 1000; TIM_ConfigMatch (pTimerRegs[timerNum], &TIM_MatchConfigStruct); /* Set to have highest priority = 0 */ NVIC_SetPriority(TimerConfig[timerNum].TimerIrq, 0); // store the callback address for later use HwTimer[timerNum].timerCallback = timerCallback; }
int platform_init() { unsigned i; TIM_InitTypeDef tim; TIM_TypeDef* base; // System configuration platform_config_scu(); // PIO setup for( i = 0; i < 10; i ++ ) GPIO_DeInit( ( GPIO_TypeDef* )port_data[ i ] ); // UART setup (only STR9_UART is used in this example) platform_uart_setup( 0, 115200, 8, PLATFORM_UART_PARITY_NONE, PLATFORM_UART_STOPBITS_1 ); // Initialize timers for( i = 0; i < 4; i ++ ) { base = ( TIM_TypeDef* )timer_data[ i ]; TIM_DeInit( base ); TIM_StructInit( &tim ); tim.TIM_Clock_Source = TIM_CLK_APB; tim.TIM_Prescaler = 255; TIM_Init( base, &tim ); TIM_CounterCmd( base, TIM_START ); } // Set the send/recv functions std_set_send_func( uart_send ); std_set_get_func( uart_recv ); return PLATFORM_OK; }
// timerNumber 1,2,3 uses Timer1,Timer2,Timer3. Timer0 is used for initializeHOPE() sequence static void enableTimer(int timerNumber, uint32_t time){ TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Initialize timer 0, prescale count time of 1ms TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1000; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if interrupt triggered TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Continue running after interrupt has occurred TIM_MatchConfigStruct.StopOnMatch = FALSE; //do no thing for external output TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set Match value, count value is time (timer * 1000uS =timer mS ) TIM_MatchConfigStruct.MatchValue = time; LPC_TIM_TypeDef *TIMx; // select Timer1, Timer2 or Timer3 if (timerNumber == 1) { TIMx = LPC_TIM1; } else if (timerNumber == 2) { TIMx = LPC_TIM2; } else if (timerNumber == 3) { TIMx = LPC_TIM3; } // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(TIMx,TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(TIMx,&TIM_MatchConfigStruct); TIM_Cmd(TIMx, ENABLE); }
/**************************************************************************//** * * @brief Starts the I2C monitor * * @retval CMD_STATUS_OK If successfully started * @retval CMD_STATUS_ERR_* If the I2C monitor could not be started * *****************************************************************************/ cmd_status_t monitor_i2c_Start(void) { if (!validConfiguration) { // no point in arming if the configuration is invalid return CMD_STATUS_ERR_MON_I2C_NOT_CONFIGURED; } CLR_MEAS_PIN_1(); TIM_Init(LPC_TIMER3, TIM_TIMER_MODE, &timerCfg); TIM_Cmd(LPC_TIMER3, ENABLE); circbuff_Reset(pSampleBuffer); bytesToCapture = pSampleBuffer->size / SAMPLE_SIZE; pSampleData = (uint32_t*)pSampleBuffer->data; done = FALSE; I2C_IntCmd(LPC_I2C0, TRUE); I2C_MonitorModeCmd(LPC_I2C0, ENABLE); while (!done) { TIM_Waitms(10); } return CMD_STATUS_OK; }
void hal_tick_init(void){ //initialize timer struct TTC0.PrescaleOption = TIM_PRESCALE_TICKVAL; TTC0.PrescaleValue = 1; //intialize match channel struct MATC0.MatchChannel = 0; MATC0.IntOnMatch = ENABLE; MATC0.StopOnMatch = DISABLE; MATC0.ResetOnMatch = ENABLE; MATC0.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; MATC0.MatchValue = TIMER_COUNTDOWN; // this should give approximately 1/4 of a sec //initialize TIMER0 TIM_Init((LPC_TIM_TypeDef *) LPC_TIM0, TIM_TIMER_MODE, &TTC0); //initialize MAT0 TIM_ConfigMatch((LPC_TIM_TypeDef *) LPC_TIM0, &MATC0); //reset Timer0 TIM_ResetCounter((LPC_TIM_TypeDef *) LPC_TIM0); //enable Timer0 TIM_Cmd((LPC_TIM_TypeDef *) LPC_TIM0, ENABLE); NVIC_EnableIRQ(TIMER0_IRQn); }
void tim_init(int dev, uint32_t preval) { tim_cfg.PrescaleOption = TIM_PRESCALE_USVAL; //use microseconds tim_cfg.PrescaleValue = preval; TIM_Init(tim_devs[dev], TIM_TIMER_MODE, &tim_cfg);//Allow setting of mode one day }
void InitTimerADCRead(void) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct ; // Initialize timer 1, prescale count time of tickval TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Enable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.MatchValue = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER0)/ADC_GET_FREQUENCY; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM3, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM3,&TIM_MatchConfigStruct); /* preemption = 1, sub-priority = 0 */ NVIC_SetPriority(TIMER3_IRQn, ((0x01<<3)|PRIORITY_TIMER3)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER3_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM3,ENABLE); }
void Timer::initialize() { callbackFunction = new FunctionPointer<void, void>; callbackActive = false; if(!timerInitialized) { timerInitialized = true; //configure timer for 25MHz, overflow at 2^32 / 25000000 = ~171.798 seconds TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; TIM_Init(TIMER_PERIPHERAL, TIM_TIMER_MODE, &TIM_ConfigStruct); TIM_MATCHCFG_Type TIM_MatchConfigStruct; TIM_MatchConfigStruct.MatchChannel = 0; TIM_MatchConfigStruct.IntOnMatch = TRUE; TIM_MatchConfigStruct.ResetOnMatch = TRUE; TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; TIM_MatchConfigStruct.MatchValue = 0xffffffff; TIM_ConfigMatch(TIMER_PERIPHERAL,&TIM_MatchConfigStruct); TIM_ResetCounter(TIMER_PERIPHERAL); TIM_Cmd(TIMER_PERIPHERAL,ENABLE); } }
/*********************************************************************//** * @brief c_entry: Main program body * @param[in] None * @return int **********************************************************************/ int c_entry (void) { PINSEL_CFG_Type PinCfg; TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct; // Conifg P1.28 as MAT0.0 PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); /* P3.26 as STCLK */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 3; PinCfg.Pinnum = 26; PINSEL_ConfigPin(&PinCfg); // Initialize timer 0, prescale count time of 10uS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 10; // use channel 0, MR0 TIM_MatchConfigStruct.MatchChannel = 0; // Disable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; //Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; //Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; //Toggle MR0.0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; // Set Match value, count value of 10 (10 * 10uS = 100uS --> 10KHz) TIM_MatchConfigStruct.MatchValue = 10; TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct); TIM_Cmd(LPC_TIM0,ENABLE); GPIO_SetDir(0, (1<<0), 1); //Set P0.0 as output //Use P0.0 to test System Tick interrupt /* Initialize System Tick with 10ms time interval * Frequency input = 10kHz /2 = 5kHz * Time input = 10ms */ SYSTICK_ExternalInit(5000, 10); //Enable System Tick interrupt SYSTICK_IntCmd(ENABLE); //Enable System Tick Counter SYSTICK_Cmd(ENABLE); while(1); return 1; }
// -------------------------------------------------- // Initialize external SDRAM memory Micron K4S561632J, 256Mbit(8M x 32) void Init_SDRAM( void ) { volatile uint32_t i; volatile unsigned long Dummy; EMC_DYN_MEM_Config_Type config; TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); config.ChipSize = 256; config.AddrBusWidth = 32; config.AddrMap = EMC_ADD_MAP_ROW_BANK_COL; config.CSn = 0; config.DataWidth = 16; config.TotalSize = SDRAM_SIZE; config.CASLatency= 3; config.RASLatency= 3; config.Active2ActivePeriod =EMC_NS2CLK( SDRAM_TRC); config.ActiveBankLatency =EMC_NS2CLK( SDRAM_TRRD); config.AutoRefrehPeriod = EMC_NS2CLK( SDRAM_TRFC); config.DataIn2ActiveTime = SDRAM_TDAL + EMC_NS2CLK( SDRAM_TRP); config.DataOut2ActiveTime = SDRAM_TAPR; config.WriteRecoveryTime = SDRAM_TWR; config.ExitSelfRefreshTime = EMC_NS2CLK( SDRAM_TXSR); config.LoadModeReg2Active = SDRAM_TMRD; config.PrechargeCmdPeriod = EMC_NS2CLK( SDRAM_TRP); config.ReadConfig = 1; // Command delayed strategy, using EMCCLKDELAY config.RefreshTime = EMC_NS2CLK( SDRAM_REFRESH) >> 4; config.Active2PreChargeTime = EMC_NS2CLK( SDRAM_TRAS); config.SeftRefreshExitTime = EMC_NS2CLK( SDRAM_TXSR); DynMem_Init(&config); EMC_DynCtrlSDRAMInit(EMC_DYNAMIC_CTRL_SDRAM_NOP); // Issue NOP command TIM_Waitms(100); // wait 200ms EMC_DynCtrlSDRAMInit(EMC_DYNAMIC_CTRL_SDRAM_PALL); // Issue Pre-charge command for(i = 0; i < 0x80; i++); // wait 128 AHB clock cycles TIM_Waitms(100); EMC_DynCtrlSDRAMInit(EMC_DYNAMIC_CTRL_SDRAM_MODE); // Issue MODE command Dummy = *((volatile uint32_t *)(SDRAM_BASE_ADDR | (0x32<<13))); // Mode Register Setting //Timing for 48/60/72MHZ Bus EMC_DynCtrlSDRAMInit(EMC_DYNAMIC_CTRL_SDRAM_NORMAL); // Issue NORMAL command //enable buffers EMC_DynMemConfigB(0, EMC_DYNAMIC_CFG_BUFF_ENABLED); for(i = 100000; i;i--); TIM_DeInit(LPC_TIM0); }
/* Initialise general purpose timer 2 (TIM2). */ void timer_initialise_tim(void) { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 10; TIM_Init(LPC_TIM2, TIM_TIMER_MODE, &TIM_ConfigStruct); //Now call timer_configure_TIM_capture to configure and enable timer 2. }
/*********************************************************************//** * @brief c_entry: Main TIMER program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { PINSEL_CFG_Type PinCfg; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); //Config P1.26 as CAP0.0 PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 1; PinCfg.Pinnum = 26; PINSEL_ConfigPin(&PinCfg); // Initialize timer 0, prescale count time of 1000000uS = 1S TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1000000; // use channel 0, CAPn.0 TIM_CaptureConfigStruct.CaptureChannel = 0; // Enable capture on CAPn.0 rising edge TIM_CaptureConfigStruct.RisingEdge = ENABLE; // Enable capture on CAPn.0 falling edge TIM_CaptureConfigStruct.FallingEdge = ENABLE; // Generate capture interrupt TIM_CaptureConfigStruct.IntOnCaption = ENABLE; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigCapture(LPC_TIM0, &TIM_CaptureConfigStruct); TIM_ResetCounter(LPC_TIM0); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER0_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM0,ENABLE); while (1); return 1; }
void timer_init() { TIM_TIMERCFG_Type TIM_ConfigStruct; TIM_MATCHCFG_Type TIM_MatchConfigStruct; //Set timer prescaler to 0 (increment every tick) TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 1; //Init timer 0 TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &TIM_ConfigStruct); //Init timer 1 TIM_Init(LPC_TIM1, TIM_TIMER_MODE, &TIM_ConfigStruct); //Configure match 0 TIM_MatchConfigStruct.MatchChannel = 0; TIM_MatchConfigStruct.IntOnMatch = TRUE; TIM_MatchConfigStruct.ResetOnMatch = TRUE; TIM_MatchConfigStruct.StopOnMatch = FALSE; TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; //Set match 0 on timer 0 (10uS @ 24Mhz) TIM_MatchConfigStruct.MatchValue = 240; TIM_ConfigMatch(LPC_TIM0, &TIM_MatchConfigStruct); //Set match 0 on timer 1 (12.5uS @ 24Mhz) TIM_MatchConfigStruct.MatchValue = 300; TIM_ConfigMatch(LPC_TIM1, &TIM_MatchConfigStruct); //Set timer 0 interrupt priority NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); //Set timer 1 interrupt priority NVIC_SetPriority(TIMER1_IRQn, ((0x01<<3)|0x02)); //Enable interrupt for timer 0 NVIC_EnableIRQ(TIMER0_IRQn); //Enable interrupt for timer 1 NVIC_EnableIRQ(TIMER1_IRQn); }
/******************************************************************************* * Function Name : Target_Init * Description : STM32各个模块初始化 * Input : None * Output : None * Return : None *******************************************************************************/ void Target_Init(void) { RCC_Configuration(); GPIO_config(); NVIC_Configuration(); TIM_Init(); Init_PWM(3600); //USART1_Configuration(); //USART2_Configuration(); //USART3_Configuration(); //I2C1_Configuration(); }
void Install_Timer(uint32_t ms, uint32_t timer, uint32_t prio, uint8_t mtchstop){ #ifndef AVR #if defined(STM32F10X_MD) || defined(STM32F30X) TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, DISABLE); EnableClk(timer+6); ///< Just to use created functions // RCC_APB1PeriphClockCmd(1<<(timer-2), ENABLE); TIM_TimeBaseStructure.TIM_Prescaler = (ms-1); TIM_TimeBaseStructure.TIM_Period = ((SystemCoreClock) / 1000) - 1;//(ms); TIM_TimeBaseInit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), &TIM_TimeBaseStructure); TIM_ClearITPendingBit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update); //TODO: Install Interrupt here? Well it actually SHOULD be initialized before activating TIM_IT //but that would force me to create more doubles. Keep an eye on this TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, ENABLE); #else // configure timer TIM_TIMERCFG_Type tc; TIM_ConfigStructInit(TIM_TIMER_MODE, &tc); TIM_Init((( TIM_TypeDef *) timers[timer]), TIM_TIMER_MODE, &tc); // set up match register TIM_MATCHCFG_Type mc; mc.MatchChannel = 0; mc.IntOnMatch = ENABLE; mc.StopOnMatch = mtchstop; mc.ResetOnMatch = ENABLE; mc.ExtMatchOutputType = 0; #ifdef _LPC23XX_ //Yes it actually IS running at one Mhz. This isn't right but I don't have time to spend days in the lpc23xx bible again. mc.MatchValue = (((1000000/1000)*ms)-1); #else mc.MatchValue = (ms * CLKPWR_GetPCLK(CLKPWR_PCLKSEL_TIMER0)) / 1000; #endif TIM_ConfigMatch((( TIM_TypeDef *) (timers[timer])), &mc); // InstallINT(timer, prio); // TIM_Cmd((( TIM_TypeDef *) (timers[timer])), ENABLE);// enable timer #endif //Must really do something neater than this if(mtchstop==0) mtchstop=1; else mtchstop=0; InstallINT(timer, prio); TIM_Cmd((( TIM_TypeDef *) (timers[timer])), mtchstop); #endif }
static void init_TIM() { TIM_TIMERCFG_Type TIM_ConfigStruct; /* Initialize timer 0, prescale count time of 1uS */ TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL; TIM_ConfigStruct.PrescaleValue = 204; /* 204MHz */ /* Set configuration for Tim_config and Tim_MatchConfig */ TIM_Init(LPC_TIMER2, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ResetCounter(LPC_TIMER2); /* To start timer 2 */ TIM_Cmd(LPC_TIMER2,ENABLE); }
void RC5_Init(void) { reseteo_general=0; // Esto estaba en el main() antes. //Config P1.26 as CAP0.0 PinCfg.Funcnum = PINSEL_FUNC_3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = PINSEL_PORT_1; PinCfg.Pinnum = PINSEL_PIN_26; PINSEL_ConfigPin(&PinCfg); //Config P0.24 como GPIO PinCfg.Funcnum=PINSEL_FUNC_0; PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinCfg.Pinmode = PINSEL_PINMODE_PULLUP; PinCfg.Pinnum = PINSEL_PIN_24; PinCfg.Portnum = PINSEL_PORT_0; PINSEL_ConfigPin(&PinCfg); // Initialize timer 0, prescale count time of 1000000uS = 1S lo cambio yo TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 1; // use channel 0, CAPn.0 TIM_CaptureConfigStruct.CaptureChannel = 0; // Enable capture on CAPn.0 rising edge TIM_CaptureConfigStruct.RisingEdge = ENABLE; // Enable capture on CAPn.0 falling edge TIM_CaptureConfigStruct.FallingEdge = ENABLE; // Generate capture interrupt TIM_CaptureConfigStruct.IntOnCaption = ENABLE; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); TIM_ConfigCapture(LPC_TIM0, &TIM_CaptureConfigStruct); TIM_ResetCounter(LPC_TIM0); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); /* Enable interrupt for timer 0 */ NVIC_EnableIRQ(TIMER0_IRQn); // To start timer 0 TIM_Cmd(LPC_TIM0,ENABLE); } // --> RC5
/********************************************************************//** * @brief Configures the TIM1 peripheral according to the specified * parameters. * @param[in] None * @return None *********************************************************************/ void TIM1_Config (void) { // TIM Configuration structure variable TIM_TIMERCFG_Type TIM_ConfigStruct; // TIM Match configuration Structure variable TIM_MATCHCFG_Type TIM_MatchConfigStruct; // TIM Capture configuration Structure variable TIM_CAPTURECFG_Type TIM_CaptureConfigStruct; // Initialize timer, prescale count time of 1mS TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; TIM_ConfigStruct.PrescaleValue = 100; // use channel 0, CAPn.0 TIM_CaptureConfigStruct.CaptureChannel = 0; // Enable capture on CAPn.0 rising edge TIM_CaptureConfigStruct.RisingEdge = ENABLE; // Enable capture on CAPn.0 falling edge TIM_CaptureConfigStruct.FallingEdge = DISABLE; // Generate capture interrupt TIM_CaptureConfigStruct.IntOnCaption = ENABLE; // Use channel PCfg TIM_MatchConfigStruct.MatchChannel = 0; // Disable interrupt when MR0 matches the value in TC register TIM_MatchConfigStruct.IntOnMatch = TRUE; // Enable reset on MR0: TIMER will reset if MR0 matches it TIM_MatchConfigStruct.ResetOnMatch = TRUE; // Stop on MR0 if MR0 matches it TIM_MatchConfigStruct.StopOnMatch = FALSE; // Toggle MR0 pin if MR0 matches it TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; // Set Match value, count value of 1000 (1000 * 100uS = 100mS --> 10Hz) TIM_MatchConfigStruct.MatchValue = 1000; // Set configuration for Tim_config and Tim_MatchConfig TIM_Init(LPC_TIM1, TIM_TIMER_MODE,&TIM_ConfigStruct); // TIM_ConfigMatch(LPC_TIM1, &TIM_MatchConfigStruct); TIM_ConfigCapture(LPC_TIM1, &TIM_CaptureConfigStruct); TIM_ResetCounter(LPC_TIM1); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(TIMER1_IRQn, 1); /* Enable interrupt for timer 1 */ NVIC_EnableIRQ(TIMER1_IRQn); TIM_Cmd(LPC_TIM1, ENABLE); }