void main (void) { uint32_t time; SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); timer0_ini(); UART0_init(); gpiob_ini(); time = TimerValueGet(WTIMER0_BASE, TIMER_B); UARTprintf("%d ", time); TimerEnable(WTIMER0_BASE, TIMER_B); LCD12864_Delay(10); TimerDisable(WTIMER0_BASE, TIMER_B); time = TimerValueGet(WTIMER0_BASE, TIMER_B); UARTprintf("%d", time); // while(1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); while(1) GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, ui8PrevCount); // while(1); }
void Timer0A_Handler(void){ static unsigned long Counter = 0; // may interrupt from timeouts multiple times before edge unsigned long ulIntFlags = TimerIntStatus(TIMER0_BASE, true); TimerIntClear(TIMER0_BASE, ulIntFlags);// acknowledge if(ulIntFlags & TIMER_TIMA_TIMEOUT) { Counter++; // keep track of timeouts } if(ulIntFlags & TIMER_CAPA_EVENT) { Period = Last - TimerValueGet(TIMER0_BASE, TIMER_A) + Counter*TimerLoadGet(TIMER0_BASE, TIMER_A); Last = TimerValueGet(TIMER0_BASE, TIMER_A); Counter = 0; } }
void IRIntHandler(void) { uint32_t ulTimerVal; ulTimerVal = TimerValueGet(IR_TIMER_BASE, IR_TIMER); //Read timer value // Reset the timer TimerLoadSet(IR_TIMER_BASE, IR_TIMER, g_ulIRPeriod); ir_timeout_flag = 0; if (ir_pulse_count == 0) { // Change the IO pin to trig on RISING, because after this we are // counting IR detector pulses, starting with RISING edge GPIOIntTypeSet(IR_PORT, IR_PIN, GPIO_RISING_EDGE); // Start the timer TimerEnable(IR_TIMER_BASE, IR_TIMER); } else { TimerEnable(IR_TIMER_BASE, IR_TIMER); if (ir_pulse_count < MAX_PULSE_COUNT) pulse_buf[ir_pulse_count - 1] = (int) (g_ulIRPeriod - ulTimerVal) / g_ulCountsPerMicrosecond; } ir_pulse_count++; }
// Summary: waits a specified amount of time // Parameters: // ms: the number of microseconds to wait // Note: Uses TIMER1. If you use Wait() or WaitUS(), you should not use TIMER1 elsewhere in your code; // If you use TIMER1 in your code, you should not use Wait() or WaitUS() void WaitUS(unsigned long us) { unsigned long ticks; static tBoolean isTimerInitialized = false; if(!isTimerInitialized) { // PRINT("init-ing timer"); NL; // first time initialization SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerConfigure(TIMER1_BASE, (TIMER_CFG_32_BIT_OS)); isTimerInitialized = true; } // PRINT("start-ing timer"); NL; // start the timer ticks = us*g_ulTicksPerUs; TimerLoadSet(TIMER1_BASE, TIMER_A, ticks); // set initial value to count down from TimerEnable(TIMER1_BASE, TIMER_A); // PRINT("wait-ing"); NL; // wait for the timer to count down // note: timer resets to and stops at the start value after it reaches 0, so check for that while(TimerValueGet(TIMER1_BASE, TIMER_A) != ticks); // spin TimerDisable(TIMER1_BASE, TIMER_BOTH); // PRINT("whoohoo!!!"); NL; }
unsigned long long vGetTimerValue( void ){ /* Currently we use the instruction counter from VarEMU */ //vemu_regs curr; //vemu_read_state(&curr); //return( curr.total_cycles ); return( 0 ); return( timer_num_overflows*MAX_32_BIT_VALUE + TimerValueGet( TIMER2_BASE, TIMER_A ) ); }
void oiltempRead() { // Oiltempset.oilTAlarm=EOiltemp_AlarmYes; Oiltempset.oilTAlarm=(PIN_Twarn==(uint8)GPIOPinRead(GPIO_PORTC_BASE,PIN_Twarn))?EOiltemp_AlarmYes:EOiltemp_AlarmNO; Oiltempset.oilTemp=0xffff-TimerValueGet(TIMER1_BASE, TIMER_A);// Oiltempset.oilTemp=HWREG(TIMER1_BASE + TIMER_O_TAR) ; TimerLoadSet(TIMER1_BASE, TIMER_A, 0xffff ); TimerEnable(TIMER1_BASE, TIMER_A); }
void freqfind(void) { TimerIntClear(TIMER4_BASE, TIMER_TIMA_TIMEOUT); edgecount=TimerValueGet(WTIMER0_BASE,TIMER_A); freq = (10000 - edgecount) * 10; if(mode==4||mode==5) TimerLoadSet(TIMER0_BASE, TIMER_A,SysCtlClockGet()/freq); //Set the Max Value of the timer TimerLoadSet(WTIMER0_BASE, TIMER_A,10000); }
/******** Timer_GetTime **************************************************** // Get the current value in timer // Input: timer is one of the Timer_T value ( Timer1A, Timer1B... ) // Output: n of the 20ns time slices. // Return 0xFFFFFFFF if the timer is not initialized // ------------------------------------------------------------------------*/ unsigned long Timer_GetTime ( Timer_T timer ) { if ( TIMER_ON(timer/2) ) { return TimerValueGet( Timer_Base[timer], get_half_timer(timer) ); } else { return 0xFFFFFFFF; } }
void SonarGPIOIntHandler(void) { GPIOPinIntClear(GPIO_PORTD_BASE, GPIO_PIN_3); if (GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_3)) { status = TIMING; TIME(MAX_SONAR_TIME); } else { Sonar_Value = TimerValueGet(TIMER2_BASE, TIMER_A); status = DELAY; (*callback)(Sonar_Value); TIME(MS(10)); } }
void Timer0IntHandler(void) { // Clear the timer interrupt. TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // Get the counter value. uint32_t delay = TimerValueGet(TIMER0_BASE, TIMER_A); // Execute the hooked function. (*periodic_task)(); // Reset the counter value. TimerLoadSet(TIMER0_BASE, TIMER_A, delay); }
void timer_delay() { TimerLoadSet(TIMER0_BASE, TIMER_A, frequency);//(SysCtlClockGet()/60)/2); TimerEnable(TIMER0_BASE, TIMER_A); while(1) { if(TimerValueGet(TIMER0_BASE, TIMER_A) == 0x0) { break; } } return; }
void main(void) { unsigned long Status = 0; /********************** Configure System clock *************************/ SysCtlClockSet(100000000, SYSCTL_OSC_INT | SYSCTL_XTAL_12_MHZ); SysCtlDelay(TICK_SLOW); // Enable LED PORT SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); // Configure LED(PC3) pin into output mode. xGPIOSPinTypeGPIOOutput(PC3); // Configure Timer Capture pin(PB26) GPIOPinFunCfg(GPIOB_BASE, GPIO_PIN_26, GPIO_PB26_TIMCCP0); TimerCounterCfg(TIMER0_BASE, TIMER_CFG_CNT_CAP0_BOTH); TimerReset(TIMER0_BASE); TimerStart(TIMER0_BASE); while (1) { if( (TimerValueGet(TIMER0_BASE) % 10) == 0) { if(Status) { // Turn on LED Status = 0; GPIOPinSet(GPIOC_BASE, GPIO_PIN_3); } else { // Turn off LED Status = 1; GPIOPinClr(GPIOC_BASE, GPIO_PIN_3); } } } while(1); }
void SonarInterrupt(void) { GPIOPinIntClear(GPIO_PORTD_BASE, GPIO_PIN_7); count++; if (Status==1) { GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, 0xFF); TimerLoadSet(TIMER2_BASE, TIMER_A,6000000); TimerEnable(TIMER2_BASE, TIMER_A); Status=2; } else if (Status==2) { GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, 0); //Distance=((5995500-TimerValueGet(TIMER2_BASE, TIMER_A))*17)/600000; Distance=TimerValueGet(TIMER2_BASE, TIMER_A); TimerDisable(TIMER2_BASE, TIMER_A); Status=3; } }
static void CaptureHandler(void) { static uint8_t channelIndex = 0; static uint32_t prev = 0; static bool last_edge = false; TimerIntClear(WTIMER1_BASE, TIMER_CAPA_EVENT); // Clear interrupt uint32_t curr = TimerValueGet(WTIMER1_BASE, TIMER_A); // Read capture value bool edge = GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_6); // Read the GPIO pin if (last_edge && !edge) { // Check that we are going from a positive to falling edge uint32_t diff = curr - prev; // Calculate diff uint32_t diff_us = 1000000UL / (SysCtlClockGet() / diff); // Convert to us if (diff_us > 2700) { // Check if sync pulse is received - see: https://github.com/multiwii/baseflight/blob/master/src/drv_pwm.c channelIndex = 0; // Reset channel index validRXData = true; for (uint8_t i = 0; i < RX_NUM_CHANNELS; i++) { if (rxChannel[i] == 0) // Make sure that all are above 0 validRXData = false; } if (validRXData) TimerLoadSet(WTIMER1_BASE, TIMER_B, timerLoadValue); // Reset timeout value to 100ms #if 0 && UART_DEBUG for (uint8_t i = 0; i < RX_NUM_CHANNELS; i++) { if (rxChannel[i] > 0) UARTprintf("%u\t", rxChannel[i]); else break; } UARTprintf("\n"); #endif } else if (channelIndex < RX_NUM_CHANNELS) rxChannel[channelIndex++] = diff_us; } prev = curr; // Store previous value last_edge = edge; // Store last edge }
// // returns idle timer remaining value in wall clock time // uint32_t idleTimerRemainingSecondsGet() { return (TimerValueGet(TIMER1_BASE, TIMER_A) + SysCtlClockGet() / 2) / SysCtlClockGet(); }
uint32 bsp_timer0_get_time(void) { return TimerValueGet(TIMER0_BASE, TIMER_A)/TIME0_DIVISION; }
// ******** OS_Time ************ // reads a timer value // Inputs: none // Outputs: time in 20ns units, 0 to max // The time resolution should be at least 1us, and the precision at least 12 bits // It is ok to change the resolution and precision of this function as long as // this function and OS_TimeDifference have the same resolution and precision unsigned long OS_Time(void){ return TimerValueGet(TIMER0_BASE, TIMER_A); //ERROR, this is TOTALLY INCORRECT but it approximates it, so bite me. }
/** * Gets the current value of the incrementing commutation timer counter * * @return - the current timer value */ uint32_t CommutationControllerClass::getValue() { return TimerValueGet(TIMER_BASE, TIMER_A); }
// ******** OS_Time ************ // reads a timer value // Inputs: none // Outputs: time in 100ns units, 0 to max // The time resolution should be at least 1us, and the precision at least 16 bits // It is ok to change the resolution and precision of this function as long as // this function and OS_TimeDifference have the same resolution and precision unsigned long OS_Time() { return TimerValueGet(TIMER0_BASE, TIMER_B); }
int main(void) { //Set the clock SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //Enable the peripherals SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); //Set the input type GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4); GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7); GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_2); GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6); //Timer Configuration TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC); TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); TimerControlEvent(TIMER1_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); TimerLoadSet(TIMER1_BASE, TIMER_A, 50000); TimerEnable(TIMER1_BASE, TIMER_A); while(1) { //Check the state of the timer if(TimerValueGet(TIMER1_BASE, TIMER_A) == 0x0) { number++; //reanable the timer. TimerLoadSet(TIMER1_BASE, TIMER_A, 50000); } int first_nible = (number & first_digit) >> 4; int second_nible = (number & second_digit); if(first_nible >= 10) { first_nible = first_nible + 7; } if(second_nible >=10) { second_nible = second_nible + 7; } //Turn off both displays GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, pin_4); GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, pin_2); //Make sure all data pins are low lower_pins(); //Send Data sevenSegWrite((char)(((int)'0')+first_nible)); //Turn on first 7-seg control signal GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x00); timer_delay(); //Turn off control signal for both 7-seg GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, pin_4); GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, pin_2); //Make sure all data pins are low lower_pins(); //Send Data sevenSegWrite((char)(((int)'0')+second_nible)); //Turn on second 7-seg control signal GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, 0x00); timer_delay(); } }
void the_taco_meter(void) { //Clear GPIOIntClear(GPIO_PORTF_BASE, GPIO_INT_PIN_2 | GPIO_INT_PIN_3); int codex[] = { 0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0}; int temp_binary; //Get a snapshot of the current state of the wheel positions if(GPIOPinRead(port_F, GPIO_PIN_2) == 0x0 && GPIOPinRead(port_F, GPIO_PIN_3) == 0x0) { current_state_a = 0; current_state_b = 0; } else if(GPIOPinRead(port_F, GPIO_PIN_2) == 0x4 && GPIOPinRead(port_F, GPIO_PIN_3) == 0x0) { current_state_a = 1; current_state_b = 0; } else if(GPIOPinRead(port_F, GPIO_PIN_2) == 0x0 && GPIOPinRead(port_F, GPIO_PIN_3) == 0x8) { current_state_a = 0; current_state_b = 1; } else if(GPIOPinRead(port_F, GPIO_PIN_2) == 0x4 && GPIOPinRead(port_F, GPIO_PIN_3) == 0x8) { current_state_a = 1; current_state_b = 1; } //Get the wheel direction if(previous_state_a != -1 && previous_state_b != -1) { //Convert binary to decimal and add to counter variable. //Proper order should be PrevA PrevB NewA NewB temp_binary = (pow(2, 3) * previous_state_a) + (pow(2, 2) * previous_state_b) + (pow(2, 1) * current_state_a) + (pow(2, 0) * current_state_b); if(codex[temp_binary] == 1) { //Direction is clockwise direction = 1; } else if(codex[temp_binary] == -1) { //Direction is counter-clockwise direction = -1; } if(direction != current_direction) { direction_change = 1; current_direction = direction; } //jump initial value t1 = t2; //Get the current speed t2 = TimerValueGet(TIMER0_BASE, TIMER_A)%60; //conversion t2 = t2 * (6.25) * pow(10, -8); delta = t2 - t1; if(delta < 0){ delta +=60; } current_rpm = (1.0/24) / (delta); current_rpm = current_rpm / 60.0; speed_change = 1; if(current_rpm > 9999.0){ current_rpm = 9999.0; } } flag = 1; previous_state_a = current_state_a; previous_state_b = current_state_b; }
/*---------------------------------------------------------------------------*/ rtimer_clock_t clock_counter(void) { return TimerValueGet(TIMER0_BASE,TIMER_A); }
int main() { //Set the Clock at 16MHZ SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //Enable Timer Peripheral SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //Enable Peripherals SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //Initialize Output ports GPIOPinTypeGPIOOutput(port_A, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4); GPIOPinTypeGPIOOutput(port_C, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7); GPIOPinTypeGPIOOutput(port_D, GPIO_PIN_6); GPIOPinTypeGPIOOutput(port_E, GPIO_PIN_0); GPIOPinTypeGPIOOutput(port_F, (GPIO_PIN_4 | GPIO_PIN_1 | GPIO_PIN_3)); //Input Pins GPIOPinTypeGPIOInput(port_F, GPIO_PIN_2 | GPIO_PIN_3); //Interrupt Enables GPIOIntEnable(GPIO_PORTF_BASE, (GPIO_INT_PIN_2 | GPIO_INT_PIN_3)); GPIOIntRegister(GPIO_PORTF_BASE, interrupt_handler); GPIOIntTypeSet(GPIO_PORTF_BASE, (GPIO_PIN_2 | GPIO_PIN_3) , GPIO_FALLING_EDGE); //Timer Setup TimerLoadSet(TIMER0_BASE, TIMER_A, 0xFFFF); TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_NEG_EDGE); TimerEnable(TIMER0_BASE, TIMER_A); //Initialize the display initializeDisplay(); while(1) { if(display_flag == 1) { //Software Debouncing display_flag = 0; SysCtlDelay(160000); //Clear scree clear_screen(); //RS High for writing GPIOPinWrite(port_C, GPIO_PIN_5, pin_5); write_char_to_pins((char)(((int)'0')+ (0x00FF & TimerValueGet(TIMER0_BASE, TIMER_A))% 10)); //Back to Low GPIOPinWrite(port_C, GPIO_PIN_5, 0x0); } } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(hello_world_process, ev, data) { static struct etimer timer; static int count; PROCESS_BEGIN(); /* i2c_init(I2C_SCL_PORT, I2C_SCL_PIN, I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_NORMAL_BUS_SPEED);*/ etimer_set(&timer, CLOCK_CONF_SECOND * 1); count = 0; relay_enable(PORT_D,LED_RELAY_PIN); while(1) { PROCESS_WAIT_EVENT(); if(ev == PROCESS_EVENT_TIMER) { if(count %2 == 0){ //relay_on(PORT_D,LED_RELAY_PIN); int delayIndex; unsigned int pwmDutyCycle = 0x0000; // // Initialize the interrupt counter. // int g_ui32Counter = 0; // // Set the clocking to run directly from the external crystal/oscillator. // (no ext 32k osc, no internal osc) // SysCtrlClockSet(false, false, 32000000); // // Set IO clock to the same as system clock // SysCtrlIOClockSet(32000000); // // The Timer0 peripheral must be enabled for use. // SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_GPT0); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer operation. // //InitConsole(); // // Display the example setup on the console. // UARTprintf("16-Bit Timer PWM ->"); UARTprintf("\n Timer = Timer0B"); UARTprintf("\n Mode = PWM with variable duty cycle"); // // Configure GPTimer0A as a 16-bit PWM Timer. // TimerConfigure(GPTIMER0_BASE, GPTIMER_CFG_SPLIT_PAIR | GPTIMER_CFG_A_PWM | GPTIMER_CFG_B_PWM); // // Set the GPTimer0B load value to 1sec by setting the timer load value // to SYSCLOCK / 255. This is determined by: // Prescaled clock = 16Mhz / 255 // Cycles to wait = 1sec * Prescaled clock TimerLoadSet(GPTIMER0_BASE, GPTIMER_A, SysCtrlClockGet() / 4000); TimerControlLevel(GPTIMER0_BASE, GPTIMER_A, false); // Configure GPIOPortA.0 as the Timer0_InputCapturePin.1 IOCPinConfigPeriphOutput(GPIO_A_BASE, GPIO_PIN_0, IOC_MUX_OUT_SEL_GPT0_ICP1); // Tell timer to use GPIOPortA.0 // Does Direction Selection and PAD Selection GPIOPinTypeTimer(GPIO_A_BASE, GPIO_PIN_0); // // Enable processor interrupts. // IntMasterEnable(); // // Enable GPTimer0B. // TimerEnable(GPTIMER0_BASE, GPTIMER_A); UARTprintf("\n"); // // Loop forever while the Timer0B runs. // while(1) { for (delayIndex = 0; delayIndex < 100000; delayIndex++); pwmDutyCycle += 0x0F; pwmDutyCycle &= 0xFFFF; TimerMatchSet(GPTIMER0_BASE, GPTIMER_A, pwmDutyCycle); UARTprintf("PWM DC Value: %04X -- %04X -- %04X\r", pwmDutyCycle, TimerValueGet(GPTIMER0_BASE, GPTIMER_A), TimerMatchGet(GPTIMER0_BASE, GPTIMER_A) ); } //SENSORS_ACTIVATE(cc2538_temp_sensor); // printf( "%d is temp\n",cc2538_temp_sensor.value); } else { //relay_off(PORT_D,LED_RELAY_PIN); } /* if(count %2 == 0) { relay_toggle(PORT_D,LED_RELAY_PIN); relay_status(PORT_D,LED_RELAY_PIN); } */ count ++; etimer_reset(&timer); } } PROCESS_END(); }
// // returns idle timer running status, indicates whether lights are still on or not // bool idleTimerRunning() { return TimerValueGet(TIMER1_BASE, TIMER_A) != TimerLoadGet(TIMER1_BASE, TIMER_A); }
unsigned long TimerTick(void) { return((TimerValueGet(TIMER3_BASE, TIMER_A)/TIMERDIV)); }