/* * Phase Offset Detection Example Code * Written by Matthew Bon for Digikey Electronics */ #include #include #include #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_nvic.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" #include "driverlib/systick.h" #include "driverlib/rom.h" #include "driverlib/pin_map.h" #include "driverlib/uart.h" #include "grlib/grlib.h" #include "utils/uartstdio.h" #include "driverlib/gpio.h" #include "driverlib/gpio.c" #include "driverlib/timer.h" uint32_t DeltaT = 0; uint32_t Phase_offset = 0; int status = 0; /*Check if Driver Library Encounters an error */ #ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endif /*Functions*/ extern void IntHandler_Phase(void); void System_Init(); void IntHandler_Phase() { //Clear Interrupt status = GPIOIntStatus(GPIO_PORTC_BASE,true); GPIOIntClear(GPIO_PORTC_BASE,status); if (GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_7) == 128) //Check if it was a rising edge. { TimerEnable(TIMER0_BASE, TIMER_A); //If it was a rising edge, start timer. } else //If falling edge, perform measurment. { /*Calculate DeltaT*/ DeltaT = (65535 - TimerValueGet(TIMER0_BASE, TIMER_A)); /*Calculate Phase Offset, Hard Coded for a 60 Hz signal T = 16.67 ms, 1 timer count = 0.32 us, Thus the period = 52083 counts */ Phase_offset = (((DeltaT * 100000)/(52083)) * 360 )/10000; /*Reset and Reload Timer*/ TimerDisable(TIMER0_BASE, TIMER_A); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_ONE_SHOT); TimerLoadSet(TIMER0_BASE, TIMER_A, 65535); } } //***************************************************************************** // // Configure the UART // //***************************************************************************** void Configure_UART(void) { /*Enable GPIO and UART*/ ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); /*Configure GPIO A0 and A1 as UARTS*/ ROM_GPIOPinConfigure(GPIO_PA0_U0RX); ROM_GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); /*Designate UART Clock Source*/ UARTClockSourceSet(UART0_BASE, UART_CLOCK_SYSTEM); UARTStdioConfig(0, 115200, 3125000); } //***************************************************************************** // //Initiate System, wait for interrupts and print out phase offset value // //***************************************************************************** int main(void) { System_Init(); while(1) { UARTprintf("Phase offset = %i \n", Phase_offset); // Display three significant figures..aka..a phase offset of 89.2 will be printed as 892 } } void System_Init() { SysCtlClockSet(SYSCTL_SYSDIV_64 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // 200 MHz / sysdiv = 3.125 MHZ /*Init Port C*/ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlDelay(3); GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_INT_PIN_7); GPIOPadConfigSet(GPIO_PORTC_BASE ,GPIO_INT_PIN_7,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPD); GPIOIntTypeSet(GPIO_PORTC_BASE,GPIO_PIN_7,GPIO_BOTH_EDGES); //Set Interrupt Type on PORT C, pin 7 /*Register Interrupt*/ GPIOIntRegister(GPIO_PORTC_BASE,IntHandler_Phase); /*Configure UART */ Configure_UART(); /*Configure Timer*/ SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_ONE_SHOT); TimerLoadSet(TIMER0_BASE, TIMER_A, 65535); IntPrioritySet(INT_GPIOC_TM4C123, 0); //Set Interrupt at highest priority. /*Enable Interrupt*/ GPIOIntEnable(GPIO_PORTC_BASE, GPIO_INT_PIN_7); }