Hi,
So I have already received help from Bob Crosby. Up until bob had helped me I was unable to get any constant reading for a known PWM. Now I do get a constant reading for PWMs with known pulse lengths but I have an irregularity - I thought to convert my pulse length into time I would have to divide by (120*10^6) to get my answer in seconds. This isn't the case, I have to use another constant to get my answer in seconds. It is a good amount off 120*10^6 - closer to 2 million.
If I use that constant I can get an accurate (within error range) pulse length reading when comparing it to oscilloscope (this includes PWMs with different pulse lengths) but I was wondering what could be causing my issue and if there is a possibility of this issue becoming a big problem in the future.
Here is my code relevant to the pulse width (I only want to read the pulse once at the moment):
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_pwm.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include <string.h>
#include <stdio.h>
#include "driverlib/sysctl.h"
// Global variable declaration for clock
uint32_t g_ui32SysClock;
// Global variable for Pulse
uint32_t start = 0, end = 0, length = 0;
void Pulse_Length (void)
{
//
// Enable GPIO and Timer 0
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//
//Configure GPIO Timer Pins in
//
GPIOPinConfigure(GPIO_PD0_T0CCP0);
GPIOPinConfigure(GPIO_PD1_T0CCP1);
GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize timer A and B to count up in edge time mode
//
TimerConfigure(TIMER0_BASE, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_TIME_UP | TIMER_CFG_B_CAP_TIME_UP));
TimerClockSourceSet(TIMER0_BASE, TIMER_CLOCK_SYSTEM);
//
//Timer Load Value
//
TimerLoadSet(TIMER0_BASE, TIMER_BOTH, 0xFFFFu);
//
// Timer A records positive edge time and Timer B records negative edge time
//
TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
TimerControlEvent(TIMER0_BASE, TIMER_B, TIMER_EVENT_NEG_EDGE);
// Registers a interrupt function to be called when timer as hits positive edge trig int
IntRegister(INT_TIMER0A, RisingEdge);
// Set interrupt to the highest priority
IntPrioritySet(INT_TIMER0A, 0);
// Makes sure the interrupt is cleared
TimerIntClear(TIMER0_BASE, TIMER_CAPA_EVENT);
// Enable the indicated timer interrupt source.
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT);
// The specified interrupt is enabled in the interrupt controller.
IntEnable(INT_TIMER0A);
// Registers a interrupt function to be called when timer as hits neg edge trig int
IntRegister(INT_TIMER0B, FallingEdge);
// Set interrupt to the highest priority
IntPrioritySet(INT_TIMER0B, 0);
// Makes sure the interrupt is cleared
TimerIntClear(TIMER0_BASE, TIMER_CAPB_EVENT);
// Enable the indicated timer interrupt source.
TimerIntEnable(TIMER0_BASE, TIMER_CAPB_EVENT);
// The specified interrupt is enabled in the interrupt controller.
//IntEnable(INT_TIMER0B);
TimerEnable(TIMER0_BASE, TIMER_BOTH); // needs to come after timer is configured
}
void RisingEdge (void)
{
// if (PULSE_START == PulseState)
// {
TimerIntClear(TIMER0_BASE, TIMER_CAPA_EVENT);
start = TimerValueGet(TIMER0_BASE, TIMER_A);
// PulseState = PULSE_POS;
IntDisable(INT_TIMER0A);
IntEnable(INT_TIMER0B);
// }
}
void FallingEdge (void)
{
// if (PULSE_POS == PulseState)
// {
TimerIntClear(TIMER0_BASE, TIMER_CAPB_EVENT);
end = TimerValueGet(TIMER0_BASE, TIMER_B);
if(end < start)
{
end = end + 0x10000;
}
length = end - start;
// PulseState=PULSE_NEG;
// }
//IntEnable(INT_TIMER0A); // This is disabled for acctual pulse
IntDisable(INT_TIMER0B);
}
int
main(void)
{
g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
FPUEnable();
FPURoundingModeSet(FPU_ROUND_POS_INF);
Pulse_Length();
IntMasterEnable();
while(1)
{
SysCtlDelay(40000000);
UARTprintf("Length = %d\n", length);
}
}