Today,I want to use the TImer and Interrupt to flip the GPIO In a certain period,and my code follow as:
/**************************************************************************************************************************/
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //Configure SysCtlClock 40Mhz
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // GPIOF_pin2
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_2);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //Configure Timer
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
ui32Period = (SysCtlClockGet()/10)/2; //Configure the load value for TIMER0 -> TIMER_A
TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1); //Trigger the interrupt function in 20hz
IntRegister(INT_TIMER0A,Timer0IntHandler); // Register a function : Timer0IntHandler()
IntMasterEnable(); //Enables the processor interrupt
TimerEnable(TIMER0_BASE, TIMER_A); //Enables TIMER0 -> TIMER_A
while(1)
{
}
}
void Timer0IntHandler(void) // flip the GPIOF_PIN2
{
// Clear the timer interrupt
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
// Read the current state of the GPIO pin and
// write back the opposite state
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2))
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
}
}
/***********************************************************************************/