Hello,
Im trying to generate a PWM with a code that I got the community but the PWM interrupt routine is not handled. Attached the code. Request to review and let know what might be wrong.
I have updated the start up file too.
Please dont consider the comments since they are not related properly with code.
//*****************************************************************************
//
// reload_interrupt.c - Example demonstrating the PWM interrupt.
//
// Copyright (c) 2010-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This is part of revision 2.1.0.12573 of the Tiva Firmware Development Package.
//
//*****************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include "C:/ti/TivaWare_C_Series-2.1.4.178/inc/hw_gpio.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/inc/hw_ints.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/inc/hw_memmap.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/inc/hw_types.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/driverlib/gpio.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/driverlib/interrupt.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/driverlib/pin_map.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/driverlib/pwm.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/driverlib/sysctl.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/driverlib/uart.h"
#include "C:/ti/TivaWare_C_Series-2.1.4.178/utils/uartstdio.h"
uint32_t ui32SysClock;
//*****************************************************************************
//
//! \addtogroup pwm_examples_list
//! <h1>PWM Reload Interrupt (reload_interrupt)</h1>
//!
//! This example shows how to setup an interrupt on PWM0. This example
//! demonstrates how to setup an interrupt on the PWM when the PWM timer is
//! equal to the configurable PWM0LOAD register.
//!
//! This example uses the following peripherals and I/O signals. You must
//! review these and change as needed for your own board:
//! - GPIO Port B peripheral (for PWM0 pin)
//! - PWM0 - PB6
//!
//! The following UART signals are configured only for displaying console
//! messages for this example. These are not required for operation of the
//! PWM.
//! - UART0 peripheral
//! - GPIO Port A peripheral (for UART0 pins)
//! - UART0RX - PA0
//! - UART0TX - PA1
//!
//! This example uses the following interrupt handlers. To use this example
//! in your own application you must add these interrupt handlers to your
//! vector table.
//! - INT_PWM0_0 - PWM0IntHandler
//
//*****************************************************************************
//*****************************************************************************
//
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//
//*****************************************************************************
void
InitConsole(void)
{
//
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
//
// Enable UART0 so that we can configure the clock.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
}
//*****************************************************************************
//
// Prints out 5x "." with a second delay after each print. This function will
// then backspace, clear the previously printed dots, backspace again so you
// continuously printout on the same line. The purpose of this function is to
// indicate to the user that the program is running.
//
//*****************************************************************************
void
PrintRunningDots(void)
{
UARTprintf(". ");
SysCtlDelay(ui32SysClock / 3);
UARTprintf(". ");
SysCtlDelay(ui32SysClock / 3);
UARTprintf(". ");
SysCtlDelay(ui32SysClock / 3);
UARTprintf(". ");
SysCtlDelay(ui32SysClock / 3);
UARTprintf(". ");
SysCtlDelay(ui32SysClock / 3);
UARTprintf("\b\b\b\b\b\b\b\b\b\b");
UARTprintf(" ");
UARTprintf("\b\b\b\b\b\b\b\b\b\b");
SysCtlDelay(ui32SysClock / 3);
}
//*****************************************************************************
//
// The interrupt handler for the for PWM0 interrupts.
//
//*****************************************************************************
void
PWM0IntHandler(void)
{
// Clear the PWM0 LOAD interrupt flag. This flag gets set when the PWM
// counter gets reloaded.
//
PWMGenIntClear(PWM0_BASE, PWM_GEN_0, PWM_INT_CNT_LOAD);
//SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOQ);
//GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_7, GPIO_PIN_7);
//
// If the duty cycle is less or equal to 75% then add 0.1% to the duty
// cycle. Else, reset the duty cycle to 0.1% cycles. Note that 64 is
// 0.01% of the period (64000 cycles).
//
if((PWMPulseWidthGet(PWM0_BASE, PWM_OUT_1) + 64) <=
((PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0) * 3) / 4))
{
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1,
PWMPulseWidthGet(PWM0_BASE, PWM_OUT_1) + 64);
}
else
{
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 64);
}
}
//*****************************************************************************
//
// Configure PWM0 for a load interrupt. This interrupt will trigger everytime
// the PWM0 counter gets reloaded. In the interrupt, 0.1% will be added to
// the current duty cycle. This will continue until a duty cycle of 75% is
// received, then the duty cycle will get reset to 0.1%.
//
//*****************************************************************************
int
main(void)
{
//
// Run from the PLL at 120 MHz.
//
ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// Set up the serial console to use for displaying messages. This is
// just for this example program and is not needed for PWM0 operation.
//
InitConsole();
//
// Display the setup on the console.
//
UARTprintf("PWM ->\n");
UARTprintf(" Module: PWM0\n");
UARTprintf(" Pin: PD0\n");
UARTprintf(" Duty Cycle: Variable -> ");
UARTprintf("0.1%% to 75%% in 0.1%% increments.\n");
UARTprintf(" Features: ");
UARTprintf("Variable pulse-width done using a reload interrupt.\n\n");
UARTprintf("Generating PWM on PWM0 (PF0) -> ");
/////////////////////////////////////////////////////////////////////////////
// GPIO set up //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOQ);
//
// Check if the peripheral access is enabled.
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOQ))
{
}
//
// Enable the GPIO pin for the LED (PQ7). Set the direction as output, and
// enable the GPIO pin for digital function.
//
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_7, GPIO_PIN_7);
//
// The PWM peripheral must be enabled for use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
//
// For this example PWM0 is used with PortB Pin6. The actual port and pins
// used may be different on your part, consult the data sheet for more
// information. GPIO port B needs to be enabled so these pins can be used.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_PWM0))
{
}
SysCtlDelay(10);
//
// Configure the GPIO pin muxing to select PWM00 functions for these pins.
// This step selects which alternate function is available for these pins.
// This is necessary if your part supports GPIO pin function muxing.
// Consult the data sheet to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
//
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x01;
GPIOPinConfigure(GPIO_PF1_M0PWM1);
//
// Configure the PWM function for this pin.
// Consult the data sheet to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1);
//
// Set the PWM clock to the system clock.
//
PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_1);
//
// Configure the PWM0 to count down without synchronization.
//
PWMGenConfigure(PWM0_BASE, PWM_GEN_0,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
//
// Set the PWM period to 250Hz. To calculate the appropriate parameter
// use the following equation: N = (1 / f) * SysClk. Where N is the
// function parameter, f is the desired frequency, and SysClk is the
// system clock frequency.
// In this case you get: (1 / 250Hz) * 16MHz = 64000 cycles. Note that
// the maximum period you can set is 2^16.
//
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 64000);
//
// For this example the PWM0 duty cycle will be variable. The duty cycle
// will start at 0.1% (0.01 * 64000 cycles = 640 cycles) and will increase
// to 75% (0.5 * 64000 cycles = 32000 cycles). After a duty cycle of 75%
// is reached, it is reset to 0.1%. This dynamic adjustment of the pulse
// width is done in the PWM0 load interrupt, which increases the duty
// cycle by 0.1% everytime the reload interrupt is received.
//
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 64);
IntEnable(INT_PWM0_1);
//
// Allow PWM0 generated interrupts. This configuration is done to
// differentiate fault interrupts from other PWM0 related interrupts.
//
PWMIntEnable(PWM0_BASE, PWM_INT_GEN_0);
IntMasterEnable();
//
// Enable processor interrupts.
//
PWMGenIntTrigEnable(PWM0_BASE, PWM_GEN_0, PWM_INT_CNT_LOAD);
//
// Enable the PWM0 interrupts on the processor (NVIC).
//
//
// Enable the PWM0 LOAD interrupt on PWM0.
//
//
// Enable the PWM0 output signal (PD0).
//
PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true);
//
// Enables the PWM generator block.
//
PWMGenEnable(PWM0_BASE,PWM_GEN_0);
//UARTprintf("17/n");
//
// Loop forever while the PWM signals are generated and PWM0 interrupts
// get received.
//
while(1)
{
//
// Print out indication on the console that the program is running.
//
//PWMIntEnable(PWM0_BASE, PWM_INT_GEN_0);
//UARTprintf("Interrupt\n");
PrintRunningDots();
}
}
Thanks for help
Shijin