//***************************************************************************** // // Configure the timer and its pins for measuring the length of // ultrasonic sensor echo pulse. // //***************************************************************************** void ConfigureDistancePulseTimer() { // // Enable Timer 4 // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER4); // // Configure timer 4A as a 16-bit event capture up-counter // ROM_TimerConfigure(TIMER4_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_TIME_UP); // // Set prescaler to 255. This essentially makes // the 16-bit timer a 24-bit timer. // ROM_TimerPrescaleSet(TIMER4_BASE, TIMER_A, 0xFF); // // The timer should capture events on both rising and falling edges // ROM_TimerControlEvent(TIMER4_BASE, TIMER_A, TIMER_EVENT_BOTH_EDGES); }
//***************************************************************************** // // This example application demonstrates the use of the timers to generate // periodic interrupts. // //***************************************************************************** int main(void) { #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) uint32_t ui32SysClock; #endif // // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. // ROM_FPULazyStackingEnable(); // // Set the clocking to run directly from the crystal. // TODO: Set the system clock appropriately for your application. // #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_OSC), 25000000); #else ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); #endif // // Initialize the display on the board. This is not directly related // to the timer operation but makes it easier to see what's going on // when you run this on an EK-LM4F232 board. // // TODO: Remove or replace this call with something appropriate for // your hardware. // InitDisplay(); // // Enable the peripherals used by this example. // // TODO: Update this depending upon the general purpose timer and // CCP pin you intend using. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER4); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM); // // Configure PM0 as the CCP0 pin for timer 4. // // TODO: This is set up to use GPIO PM0 which can be configured // as the CCP0 pin for Timer4 and also happens to be attached to // a switch on the EK-LM4F232 board. Change this configuration to // correspond to the correct pin for your application. // ROM_GPIOPinTypeTimer(GPIO_PORTM_BASE, GPIO_PIN_0); GPIOPinConfigure(GPIO_PM0_T4CCP0); // // Set the pin to use the internal pull-up. // // TODO: Remove or replace this call to correspond to the wiring // of the CCP pin you are using. If your board has an external // pull-up or pull-down, this will not be required. // MAP_GPIOPadConfigSet(GPIO_PORTM_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // // Enable processor interrupts. // ROM_IntMasterEnable(); // // Configure the timers in downward edge count mode. // // TODO: Modify this to configure the specific general purpose // timer you are using. The timer choice is intimately tied to // the pin whose edges you want to capture because specific CCP // pins are connected to specific timers. // ROM_TimerConfigure(TIMER4_BASE, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_COUNT)); ROM_TimerControlEvent(TIMER4_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); ROM_TimerLoadSet(TIMER4_BASE, TIMER_A, 9); ROM_TimerMatchSet(TIMER4_BASE, TIMER_A, 0); // // Setup the interrupt for the edge capture timer. Note that we // use the capture match interrupt and NOT the timeout interrupt! // // TODO: Modify to enable the specific timer you are using. // ROM_IntEnable(INT_TIMER4A); ROM_TimerIntEnable(TIMER4_BASE, TIMER_CAPA_MATCH); // // Enable the timer. // // TODO: Modify to enable the specific timer you are using. // ROM_TimerEnable(TIMER4_BASE, TIMER_A); // // At this point, the timer will count down every time a positive // edge is detected on the relevant pin. When the count reaches // 0, the timer count reloads, the interrupt fires and the timer // is disabled. The ISR can then restart the timer if required. // MainLoopRun(); }
//***************************************************************************** // // This example application demonstrates the use of the timers to generate // periodic interrupts. // //***************************************************************************** int main(void) { // // Set the clocking to run directly from the crystal at 120MHz. // g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); // Setup for ultrasonic ranger // // Enable GPIO M // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM); // // Enable GPIO pin for timer event capture (M4). // ROM_GPIOPinTypeTimer(GPIO_PORTM_BASE, GPIO_PIN_4); // // Configure 7 segment display gpio pins // sevenSegSetup(); // // Initialize timer for distance pulse measurement. // ConfigureDistancePulseTimer(); ROM_FPULazyStackingEnable(); //Enable lazy stacking for faster FPU performance ROM_FPUEnable(); //Enable FPU TimerACount = 0; TimerBCount = 0; TimerCCount = 0; TimerDCount = 0; g_temp_index = 0; g_prox_index = 0; g_light_index = 0; // // Initialize the UART and write status. // ConfigureUART(); UARTprintf("\033[2J"); //Clear screen //UARTprintf("\033[2JFinal Project Timers Example\n"); configureADC(); // // Enable the peripherals used by this example. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //Real Time Clock ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); //Temperature ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); //Proximity ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3); //Light ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER5); //Timer5 is used to measure the leading edge pulses from the signal generator // Enable the A peripheral used by the Timer3 pin PA6, PA7 // Enable the B peripheral used by the Timer5 pin PB2, PB3 // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //Enable GPIO A ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //Enable GPIO B //Configure the pins for its Timer functionality ROM_GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_2); //Enable Timer5 on PB2 //Configures the alternate function of a GPIO pin for Timer5 ROM_GPIOPinConfigure(GPIO_PB2_T5CCP0); // // Enable processor interrupts. // ROM_IntMasterEnable(); // // Configure the four 32-bit periodic timers. // ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC); ROM_TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC); ROM_TimerConfigure(TIMER3_BASE, TIMER_CFG_PERIODIC); ROM_TimerConfigure(TIMER5_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_COUNT); // Timer5 ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, g_ui32SysClock / 10); //RTC 100ms ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, g_ui32SysClock / 5); //Temp 200ms ROM_TimerLoadSet(TIMER2_BASE, TIMER_A, g_ui32SysClock / 2); //Proximity 500ms ROM_TimerLoadSet(TIMER3_BASE, TIMER_A, g_ui32SysClock / 10); //Light 100ms ROM_TimerLoadSet(TIMER5_BASE, TIMER_A, 65000); // Timer5 //Configure the signal edges that triggers the timer when in capture mode ROM_TimerControlEvent(TIMER5_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); // // Setup the interrupts for the timer timeouts. // ROM_IntEnable(INT_TIMER0A); //RTC ROM_IntEnable(INT_TIMER1A); //Temp ROM_IntEnable(INT_TIMER2A); //Proximity ROM_IntEnable(INT_TIMER3A); //Light ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); //RTC ROM_TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT); //Temp ROM_TimerIntEnable(TIMER2_BASE, TIMER_TIMA_TIMEOUT); //Proximity ROM_TimerIntEnable(TIMER3_BASE, TIMER_TIMA_TIMEOUT); //Light // // Enable the timers. // ROM_TimerEnable(TIMER0_BASE, TIMER_A); //RTC ROM_TimerEnable(TIMER1_BASE, TIMER_A); //Temp ROM_TimerEnable(TIMER2_BASE, TIMER_A); //Proximity ROM_TimerEnable(TIMER3_BASE, TIMER_A); //Light ROM_TimerEnable(TIMER5_BASE, TIMER_A); //Timer5 // // Loop forever while the timers run. // while(1) { //Process data (Busy-Wait Loop) //If a flag hasn't been set by the interrupt the calc functions will simply exit //For performance it makes more sense to check flags here instead of at the beginning of the functions calcTemp(); //Calculates Temperature reading calcProx(); //Calculates Proximity reading calcLight(); //Calculates Light reading UARTSendData(); //Sends read data through UART } }
void ping0Int( void ) { // clear interrupts. ROM_TimerIntClear( TIMER_BASE_PING0, TIMER_PING0_MATCH|TIMER_PING0_EVENT|TIMER_PING0_TIMEOUT ); switch( ping0state ) { case RECHARGE: // We are now done recharging: ping0state = INIT_PULSE; // Now we should start again // set IO pin as output ROM_GPIOPinTypeGPIOOutput( GPIO_BASE_PING0, GPIO_PIN_PING0 ); ROM_GPIOPinConfigure( GPIO_MUX_PING0 ); // drive IO pin high ROM_GPIOPinWrite( GPIO_BASE_PING0, GPIO_PIN_PING0, GPIO_PIN_PING0 ); // stop timer ROM_TimerDisable( TIMER_BASE_PING0, TIMER_PING0 ); // set timer for 2-5µs, single shot (start of with 5µs) ROM_TimerConfigure( TIMER_BASE_PING0, TIMER_CFG_SPLIT_PAIR | TIMER_PING0_ONE_SHOT ); ROM_TimerLoadSet( TIMER_BASE_PING0, TIMER_PING0, INIT_PULSE_N ); // set timer interrupt for end of count ROM_TimerIntDisable( TIMER_BASE_PING0, TIMER_PING0_MATCH|TIMER_PING0_EVENT ); ROM_TimerIntEnable( TIMER_BASE_PING0, TIMER_PING0_TIMEOUT ); // enable timer ROM_TimerEnable( TIMER_BASE_PING0, TIMER_PING0 ); break; case INIT_PULSE: // We are now done sending the init pulse: ping0state = HOLDOFF; // Now we should wait some // drive low ROM_GPIOPinWrite( GPIO_BASE_PING0, GPIO_PIN_PING0, 0 ); // set IO pin as input ROM_GPIOPinTypeGPIOInput( GPIO_BASE_PING0, GPIO_PIN_PING0 ); ROM_GPIOPinConfigure( GPIO_MUX_PING0 ); // stop timer ROM_TimerDisable( TIMER_BASE_PING0, TIMER_PING0 ); // set timer for 500µs, single shot (holdoff is ~750µs) ROM_TimerConfigure( TIMER_BASE_PING0, TIMER_CFG_SPLIT_PAIR | TIMER_PING0_ONE_SHOT ); ROM_TimerLoadSet( TIMER_BASE_PING0, TIMER_PING0, HOLDOFF_N ); // set timer interrupt for end of count ROM_TimerIntDisable( TIMER_BASE_PING0, TIMER_PING0_MATCH|TIMER_PING0_EVENT ); ROM_TimerIntEnable( TIMER_BASE_PING0, TIMER_PING0_TIMEOUT ); // enable timer ROM_TimerEnable( TIMER_BASE_PING0, TIMER_PING0 ); break; case HOLDOFF: // We are now done wating for the ping))) to start ping0state = WAIT_RISING; // Now we should wait for the begining of its reply // set IO pin as CCP ROM_GPIOPinTypeTimer( GPIO_BASE_PING0, GPIO_PIN_PING0 ); ROM_GPIOPinConfigure( TIMER_MUX_PING0 ); // stop timer ROM_TimerDisable( TIMER_BASE_PING0, TIMER_PING0 ); // set timer to edge time ROM_TimerConfigure( TIMER_BASE_PING0, TIMER_CFG_SPLIT_PAIR | TIMER_PING0_TIME_UP ); ROM_TimerLoadSet( TIMER_BASE_PING0, TIMER_PING0, 0xffffffff ); // set count edge to rising ROM_TimerControlEvent( TIMER_BASE_PING0, TIMER_PING0, TIMER_EVENT_POS_EDGE ); // set timer interrupt for event ROM_TimerIntDisable( TIMER_BASE_PING0, TIMER_PING0_TIMEOUT|TIMER_PING0_MATCH ); ROM_TimerIntEnable( TIMER_BASE_PING0, TIMER_PING0_EVENT ); // enable timer ROM_TimerEnable( TIMER_BASE_PING0, TIMER_PING0 ); break; case WAIT_RISING: // We are now done wating for the ping))) to begin its relpy ping0state = WAIT_FALLING; // Now we should wait for the end of its reply ping0start = HWREG( TIMER_BASE_PING0 + TIMER_PING0_TR ); ping0now = HWREG( TIMER_BASE_PING0 + TIMER_PING0_TV ); // IO pin already CCP // stop timer ROM_TimerDisable( TIMER_BASE_PING0, TIMER_PING0 ); // set count edge to falling ROM_TimerControlEvent( TIMER_BASE_PING0, TIMER_PING0, TIMER_EVENT_NEG_EDGE ); // enable timer ROM_TimerEnable( TIMER_BASE_PING0, TIMER_PING0 ); // Check if falling edge already occured, if it did: fall trough if ( ROM_GPIOPinRead( GPIO_BASE_PING0, GPIO_PIN_PING0 ) ) break; case WAIT_FALLING: // We are now done wating for the ping))) to end its relpy ping0state = RECHARGE; // Now we should wait for the end of its reply ping0time = HWREG( TIMER_BASE_PING0 + TIMER_PING0_TR ) + ( ping0now - ping0start ); ping0updated = true; int32_t pauseTime = PAUSE_TIME_N - ping0time; if ( pauseTime < PAUSE_TIME_MIN_N ) pauseTime = PAUSE_TIME_MIN_N; // set IO pin as output ROM_GPIOPinTypeGPIOOutput( GPIO_BASE_PING0, GPIO_PIN_PING0 ); ROM_GPIOPinConfigure( GPIO_MUX_PING0 ); // drive IO pin low ROM_GPIOPinWrite( GPIO_BASE_PING0, GPIO_PIN_PING0, 0 ); // stop timer ROM_TimerDisable( TIMER_BASE_PING0, TIMER_PING0 ); // set timer for 200µs, single shot ROM_TimerConfigure( TIMER_BASE_PING0, TIMER_PING0_ONE_SHOT ); ROM_TimerLoadSet( TIMER_BASE_PING0, TIMER_PING0, pauseTime ); // set timer interrupt for end of count ROM_TimerIntDisable( TIMER_BASE_PING0, TIMER_PING0_MATCH|TIMER_PING0_EVENT ); ROM_TimerIntEnable( TIMER_BASE_PING0, TIMER_PING0_TIMEOUT ); // enable timer ROM_TimerEnable( TIMER_BASE_PING0, TIMER_PING0 ); break; } }