This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi,
I have tried several way and checked the examples but could not make the code below work. I have a fan plugged to my output but could not make it work with PWM, also the fan LED does not even light up. Thanks for help.
{code}
SysCtlClockSet(
SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
SysCtlPWMClockSet( SYSCTL_PWMDIV_64);
SysCtlPeripheralEnable( SYSCTL_PERIPH_PWM0);
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOD);
GPIOPinConfigure( GPIO_PD0_T0CCP0);
GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PD0_T0CCP0);
PWMGenConfigure( PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN);
PWMGenPeriodSet( PWM1_BASE, PWM_GEN_0, 2500);
PWMPulseWidthSet( PWM1_BASE, PWM_OUT_0,
(PWMGenPeriodGet(PWM1_BASE, PWM_GEN_0) / 2));
PWMOutputState( PWM1_BASE, PWM_OUT_0_BIT, true);
PWMGenEnable( PWM1_BASE, PWM_GEN_0);
while (1)
{
;
}
{code}
Hello Veruma,
I don't understand the full scope of the problem from your description, but I can give some starting pointers. Please try and explain more about what you see with the code, such as if it runs correctly or hits a fault, and if you see any output on the GPIO's.
First comment, you are using the wrong clock configuration for TM4C129x MCU's. The one you have is for TM4C123x.
This is the right API:
g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_OSC), 25000000);
Next, you should place this before the PWM Clock Set API:
SysCtlPeripheralEnable( SYSCTL_PERIPH_PWM0);
Next, GPIO_PD0_T0CCP0 is not a PWM output, so you need to figure out the right pin for PWM output. I often use Pin F1 for this which is associated with PWM0, but you can use others.
Last, for GPIOPinTypePWM, you need to give a parameter like GPIO_PIN_0, GPIO_PIN_1 etc., not the GPIO_PD0_T0CCP0 or GPIO_PF1_M0PWM1 for GPIOPinConfigure.
Thanks for the reply. But in the documentation it says " T0CCP0 -PD0 (3)-16/32-Bit Timer 0 Capture/Compare/PWM 0." that is why I used T0CCP0. I am trying to start/stop/adjust the speed of a fan with the pwm using the code above. But it does not even start the fan.
Thanks for the clarification. Please see the code below. I tried to apply what you just explained but not sure if I am missing anything.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOD)) ; SysCtlDelay(3); GPIOPinConfigure(GPIO_PD0_T0CCP0); SysCtlDelay(3); GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_0); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM); TimerLoadSet(TIMER0_BASE, TIMER_BOTH, period); TimerMatchSet(TIMER0_BASE, TIMER_BOTH, dutyCycle); TimerEnable(TIMER0_BASE, TIMER_A); TimerEnable(TIMER0_BASE, TIMER_B); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, dutyCycle); PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true); PWMGenEnable(PWM0_BASE, PWM_GEN_0);
Hi Ralph,
I read the documentation that you mentioned above. It helped a lot but I still have some confusions. in the tm4c1294ncpdt.pdf it says "T0CCP0-PD0 (3)-16/32-Bit Timer 0 Capture/Compare/PWM 0." so why does it say PWM0 if PD0 is not a PWM pin. Also I have to use GPIO_PD0_T0CCP0 for pwm purpose how can I accomplish this? Please see my code below.
Best,
GPIOPinTypePWM( GPIO_PORTD_BASE, GPIO_PIN_0 ); GPIOPinConfigure( GPIO_PD0_M1PWM0 );// here I have to say GPIOPinConfigure(GPIO_PD0_T0CCP0); PWMGenConfigure( PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN ); PWMGenPeriodSet( PWM1_BASE, PWM_GEN_0, 2500 ); //Total Pulse Period PWMPulseWidthSet( PWM1_BASE, PWM_OUT_0, ( PWMGenPeriodGet(PWM1_BASE, PWM_GEN_0) / 2) ); PWMOutputState( PWM1_BASE, PWM_OUT_0_BIT, true ); PWMGenEnable( PWM1_BASE, PWM_GEN_0 ); while( 1 ) { ; }
Hello Veruma,
As far as the datasheet goes, the nomenclature of "16/32-Bit Timer 0 Capture/Compare/PWM 0" is to say that the specified pin is a 16 or 32 bit timer and that the timer support both Capture and Compare mode and Timer PWM mode.
The PWM module pins are labeled like M0PWM0 or M0PWM1 instead, and those pins are used by the PWM module and require PWM API's.
If you are using the LaunchPad then you can use other pins, and therefore use the PWM module which would be best for your application.
But if you are using a custom board and have no flexibility to change pins then see the following code which should get you going with basic functionality that you can modify. The code was based of our Timer PWM example in TivaWare, but I stripped out needless things like the UART and used a pin configuration for the EK-TM4C1294XL. You can change that pin configuration if needed.
//***************************************************************************** // // pwm.c - Example demonstrating timer-based PWM on a 16-bit CCP. // // Copyright (c) 2010-2017 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.4.178 of the Tiva Firmware Development Package. // //***************************************************************************** #include <stdbool.h> #include <stdint.h> #include "inc/hw_gpio.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_timer.h" #include "inc/hw_types.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/timer.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" //***************************************************************************** // //! \addtogroup timer_examples_list //! <h1>PWM using Timer (pwm)</h1> //! //! This example shows how to configure Timer0A to generate a PWM signal on the //! timer's CCP pin. // //***************************************************************************** //***************************************************************************** // // Configure Timer1B as a 16-bit PWM with a duty cycle of 66%. // //***************************************************************************** int main(void) { uint32_t g_ui32SysClock; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_OSC), 25000000); #else SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); #endif // // The Timer1 peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // // For this example T1CCP1 is used with port B pin 5. // 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_GPIOL); // // Configure the GPIO pin muxing for the Timer/CCP function. // This is only necessary if your part supports GPIO pin function muxing. // Study the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using // GPIOPinConfigure(GPIO_PL4_T0CCP0); // // Configure the ccp settings for CCP pin. This function also gives // control of these pins to the SSI hardware. Consult the data sheet to // see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypeTimer(GPIO_PORTL_BASE, GPIO_PIN_4); // // Configure Timer1B as a 16-bit periodic timer. // TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM); // // Set the Timer1B load value to 50000. For this example a 66% duty cycle // PWM signal will be generated. From the load value (i.e. 50000) down to // match value (set below) the signal will be high. From the match value // to 0 the timer will be low. // TimerLoadSet(TIMER0_BASE, TIMER_A, 50000); // // Set the Timer1B match value to load value / 3. // TimerMatchSet(TIMER0_BASE, TIMER_A, TimerLoadGet(TIMER0_BASE, TIMER_A) / 3); // // Enable Timer1B. // TimerEnable(TIMER0_BASE, TIMER_A); // // Loop forever while the Timer1B PWM runs. // while(1) { } }