Hi, I'm using the EK-TM4C123GXL with CCS version 6.1 and Tivaware version 2.1.0 to try and measure the duty cycle of a 1kHz pwm signal from a Senseair S8 CO2 sensor connected to pin PB6. I'm using timer0A and timer0B in edge time mode with A on positive edges and B on negative edges. What I'm trying to do is have an interrupt that records the time of the positive edge, then the negative edge(using TimerValueGet), then finds the difference and outputs through UART
Originally I had the interrupt occur on TIMER_CAPB_EVENT, but when that was the case, I wasn't receiving values for the positive edge(timer A) or the negative edge(timer B) and there was no output through UART at all. I changed the interrupt source to TIMER_CAPA_EVENT, and now I'm getting values for only the positive edge on timer A. It seems that timer B isn't recording a value for the negative edge, but I am getting a UART output (which is negative, because the timer B register is 0)
Am I messing something up somewhere? I would appreciate some help.
Thanks,
Here's my code:
//includes #include <stdint.h> // Variable definitions for the C99 standard. #include <stdio.h> // Input and output facilities for the C99 standard. #include <stdbool.h> // Boolean definitions for the C99 standard. #include "inc/tm4c123gh6pm.h" // Definitions for the interrupt and register assignments. #include "inc/hw_memmap.h" // Memory map definitions of the Tiva C Series device. #include "inc/hw_types.h" // Definitions of common types and macros. #include "driverlib/sysctl.h" // Definitions and macros for System Control API of DriverLib. #include "driverlib/interrupt.h" // Defines and macros for NVIC Controller API of DriverLib. #include "driverlib/gpio.h" // Definitions and macros for GPIO API of DriverLib. #include "driverlib/timer.h" // Defines and macros for Timer API of DriverLib. #include "driverlib/pin_map.h" //Mapping of peripherals to pins for all parts. #include "driverlib/uart.h" // Definitions and macros for UART API of DriverLib. #include "driverlib/adc.h" // Definitions for ADC API of DriverLib. #include "driverlib/fpu.h" // Prototypes for the FPU manipulation routines. #include "utils/uartstdio.h" // Prototypes for the UART console functions. // Needs to add "utils/uartstdio.c" through a relative link. //definitions #define UART1_BAUDRATE 115200 // UART baudrate in bps // function prototypes void init_timer(void); void duty_cycle(void); void init_UART(void); // global variables uint32_t sys_clock; uint32_t start = 0, end = 0, length = 0; int main(void) { // Configure system clock at 40 MHz. SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); sys_clock = SysCtlClockGet(); // Enable the processor to respond to interrupts. IntMasterEnable(); init_UART(); init_timer(); TimerEnable(TIMER0_BASE, TIMER_BOTH); while(1); } void init_timer(void) { // Enable and configure Timer0 peripheral. SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // 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)); // Timer a records pos edge time and Timer b records neg edge time TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); TimerControlEvent(TIMER0_BASE, TIMER_B, TIMER_EVENT_NEG_EDGE); //set the value that the timers count to (0x9C3F = 39999) //CO2 sensor outputs 1khz pwm so with mcu at 40Mhz, timers should stay in sync with CO2 output TimerLoadSet(TIMER0_BASE, TIMER_BOTH, 0x9C3F); //Configure the pin that the timer reads from (PB6) SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinConfigure(GPIO_PB6_T0CCP0); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6); // Registers a interrupt function to be called when timer b hits a neg edge event IntRegister(INT_TIMER0A, duty_cycle); // 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); } //When negative edge is hit, record the values and find the difference, output to putty void duty_cycle(void) { TimerIntClear(TIMER0_BASE, TIMER_CAPA_EVENT); start = TimerValueGet(TIMER0_BASE, TIMER_A); end = TimerValueGet(TIMER0_BASE, TIMER_B); length = end - start; UARTprintf("\nLENGTH = %d\n", length); } void init_UART(void) { // Enable and configure UART0 for debugging printouts. SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinTypeUART(GPIO_PORTA_BASE, (GPIO_PIN_0 | GPIO_PIN_1)); UARTStdioConfig(0, UART1_BAUDRATE, sys_clock); }