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.

TM4C1231H6PM: Use different frequency to turn on LED by timer

Part Number: TM4C1231H6PM

I want to let the LED flash with 1 second by 1Hz timer, and use 5kHz timer to save the  current, but it doesn't turn on as I think. My finally goal is use 5kHz timer to save half of current then use 50kHz timer to save 60% current. It might use 3 timer, but when I use 2 timer, it doesn't work. Do I have something wrong ?

My code as follow :

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_uart.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/interrupt.h"
#include "driverlib/timer.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/gpio.h"
#include "driverlib/gpio.c"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/sysctl.c"
#include "driverlib/systick.h"
#include "driverlib/uart.h"
#include "driverlib/uart.c"
#include "driverlib/adc.h"

int sys_status;
int flash_status;
int press_status;
int stable_time = 0;

int flash_status_5k;
int On_count = 0;

void IRQ_Timer0AIntHandler(void)
{
    MAP_IntMasterDisable();
    MAP_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    
    if (flash_status == 0)
    {
        flash_status = 1;
    }
    else if (flash_status == 1)
    {
        flash_status = 0;
    }
    
    MAP_IntMasterEnable();
}

void IRQ_Timer0BIntHandler(void)
{
    MAP_IntMasterDisable();
    MAP_TimerIntClear(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
    
    if (flash_status == 0)
    {
        MAP_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, 0);
    }
    if (flash_status == 1)
    {
        if (flash_status_5k == 0)
        {
            MAP_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, 0);
            flash_status_5k = 1;
        }
        else if (flash_status_5k == 1)
        {
            MAP_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, GPIO_PIN_4);
            flash_status_5k = 0;
        }
    }
    MAP_IntMasterEnable();
}

int
main(void)
{ 
    MAP_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);

    // 
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    // 
    MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
    MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
    // 
    HWREG(GPIO_PORTF_BASE + 0x520) = 0x4C4F434B;
    HWREG(GPIO_PORTF_BASE + 0x524) = 0x00FF;
    HWREG(GPIO_PORTF_BASE + 0x420) = 0x0000;
    HWREG(GPIO_PORTF_BASE + 0x51C) = 0x001F;
    // 
    MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
    
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_4);
    // 
    MAP_UARTConfigSetExpClk(UART0_BASE, MAP_SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));
    
    // Switch
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_4, GPIO_PIN_4);
    MAP_GPIOPadConfigSet(GPIO_PORTD_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
    MAP_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_4);

    // Timer0A 1Hz
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    
    MAP_TimerDisable(TIMER0_BASE, TIMER_A);
    MAP_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC | TIMER_CFG_A_PERIODIC);
    MAP_TimerLoadSet(TIMER0_BASE, TIMER_A, MAP_SysCtlClockGet());
    MAP_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    MAP_IntEnable(INT_TIMER0A);
    
    // Timer0B 5kHz
    MAP_TimerDisable(TIMER0_BASE, TIMER_B);
    MAP_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC | TIMER_CFG_B_PERIODIC);
    MAP_TimerLoadSet(TIMER0_BASE, TIMER_B, MAP_SysCtlClockGet() / 5000);
    MAP_TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
    MAP_IntEnable(INT_TIMER0B);
    
    sys_status = 0;
    flash_status = 0;
    flash_status_5k = 0;
    uint32_t Key1status;

    while(1)
    {
        Key1status = MAP_GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_4);
        
        if (Key1status != GPIO_PIN_4)
        {
            press_status = 1;
            stable_time = 0;
        }
        
        if (press_status == 1)
        {
            if (Key1status == GPIO_PIN_4)
            {
                stable_time++;
            }
        }
        
        if (stable_time >= 100)
        {
            if (sys_status == 0)
            {
              sys_status = 1;
            }
            else if (sys_status == 1)
            {
              sys_status = 2;
            }
            else if (sys_status == 2)
            {
              sys_status = 3;
            }
            else if (sys_status == 3)
            {
              sys_status = 1;
            }
            press_status = 0;
            stable_time = 0;
        }
        
        switch(sys_status)
        {
          case 0:
              break;
          case 1:
              MAP_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, GPIO_PIN_4);
              break;
          case 2:
              MAP_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, 0);
              break;
          case 3:
              MAP_TimerEnable(TIMER0_BASE, TIMER_A);
              MAP_TimerEnable(TIMER0_BASE, TIMER_B);
              break;
          default:
              break;
        }
    }
}

  • I just solved the problem, I didn't know that TIMER0A and TIMER0B can't use different frequency. After I change the 5kHz timer to TIMER2A, it works !
  • I want to let the LED flash with 1 second by 1Hz timer, and use 5kHz timer to save the  current, but it doesn't turn on as I think.

    I would have suggested the SysTick timer as alternative. It's simpler, and there are plenty of usage example on the net.

    My finally goal is use 5kHz timer to save half of current then use 50kHz timer to save 60% current.

    "Saved current" means reduced brightness as well ...

    But for switching fast enough to be invisible to the human eye, a frequency of 100Hz is sufficient. PAL/SECAM/NTSC worked fine with 50/60Hz, albeit not really "invisible" ...

  • Hello Yang,

    f.m. has a very valid point on what you meant by "saved current"?
  • Hi f.m, Amit,
    Thanks for answering me, I learn a lot.
    I'm new to ti and learning how to use it, 50kHz is just the question that my manager gave me to solve. And I've solved it already. Thanks.
  • I wouldn't be too pleased w/50KHz - we're told that "Yang Tesla" has achieved 100KHz! (although his employed some "alternating.")