Breathing led in Tiva C series TM4C123G

-1

I have to write a C code so that the RGB LED on the board breaths. My code is blinking not breathing. My teacher said that varying brightness is achieved by varying duty-cycle so in that case I can't use pwm. Please help me to understand this code.

#include <stdint.h>
#include <stdlib.h>

#define SYSCTL_RCGC2_R          (*((volatile unsigned long *)0x400FE108))
#define SYSCTL_RCGC2_GPIOF       0x00000020  //port F clock gating control
#define GPIO_PORTF_DATA_R       (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R        (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_DEN_R        (*((volatile unsigned long *)0x4002551C))
   

void delay (double sec);
int cond;
int main(void){
   SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;
   
   GPIO_PORTF_DIR_R=0x0E;
   GPIO_PORTF_DEN_R=0x0E;
   cond=0;
   while(1){
       
       GPIO_PORTF_DATA_R = 0x02;
       delay(12.5);
           GPIO_PORTF_DATA_R = 0x00;
           delay(0);
           GPIO_PORTF_DATA_R = 0x02;
       delay(2.5);
           GPIO_PORTF_DATA_R = 0x00;
           delay(10);
   
           GPIO_PORTF_DATA_R = 0x02;
           delay(5);
           GPIO_PORTF_DATA_R = 0x00;
           delay(7.5);
           GPIO_PORTF_DATA_R = 0x02;
           delay(7.5);
           GPIO_PORTF_DATA_R = 0x00;
           delay(5);
           GPIO_PORTF_DATA_R = 0x02;
           delay(12.5);
           GPIO_PORTF_DATA_R = 0x00;
           delay(0);
           GPIO_PORTF_DATA_R = 0x02;
           delay(7.5);
           GPIO_PORTF_DATA_R = 0x00;
           delay(5);
           GPIO_PORTF_DATA_R = 0x02;
           delay(5);
           GPIO_PORTF_DATA_R = 0x00;
           delay(7.5);
       
       
   }
   return 0;
   
}
void delay(double sec){
   int c=1, d=1;
   for(c=1;c<=sec;c++)
       for(d=1;d<= 4000000;d++){}
           }

c
arm
embedded
cortex-m
keil
asked on Stack Overflow Apr 13, 2021 by Alexa

2 Answers

0

There are two ways you can drive LEDs: either with constant current through some general-purpose I/O, or with repeated duty cycle from PWM. PWM meaning pulse-width modulation and it will happen with pulses that are too fast for the human eye to notice, could be anywhere from some 100Hz up to 10kHz or so.

The main advantage of PWM is that you easily can control current. Which is case of RGB means color intensity of the 3 individual LEDs. Most smaller LEDs are rated at 20mA so that's usually the maximum current you are aiming for, corresponding to 100% duty cycle. The correct way to achieve this is to use PWM.

But what your current code does is to "bit bang" simulate PWM by pulling GPIO pins. That's very crude and inefficient. Normally microcontrollers have a timer and/or PWM hardware peripheral built in, where you just provide a duty cycle and the hardware takes care of everything from there. In this case you would set up 3 PWM hardware channels which should ideally be clocked at the same time.

LEDs are diodes with different forward voltage depending on chemistry. So you very likely have different forward voltages per each of the 3 colors. You have to check the datasheet of the RGB and look for luminous intensity experessed in candela. In this case very likely millicandela, mcd. Lets assume that your green led has 300mcd but the red and blue have 100mcd. They are somewhat linear, or you can probably get away with assuming they are. So a crude equation in this case is to give the green LED 3 times less current than the others, in order to get an even mix of colors. Once you have compensated for that, you can give your 3 PWM channels a RGB code and hopefully get the corresponding color.


As a side note, the delay function in your code is completely broken in many ways. The loop iterator for such busy-delays must be volatile or any half-decent compiler will simply remove the delay when optimizations are enabled. And there is no reason to use floating point either.

answered on Stack Overflow Apr 14, 2021 by Lundin
0

If you are doing it with your delay function and your delay resolution is in seconds as suggested in the code of course it will "blink" - the frequency needs to be faster than human visual perception - say for example about 50Hz, then to get a smooth variation you might divide that up into say 20 levels, requiring a millisecond delay.

In any case your delay() function defeats itself by taking a floating point number of seconds but comparing it with an integer loop counter - it will only ever work in whole seconds.

So given a function delayms( unsigned millisec ) (which I discuss later) then:

#define BREATHE_UPDATE_MS 100
#define BREATHE_MINIMUM 0
#define PWM_PERIOD_MS 20

unsigned tick = 0 ;
unsigned duty_cycle = 0 ;
unsigned cycle_start_tick= 0 ;

unsigned breath_update_tick = 0 ;
int breathe_dir = 1 ;

for(;;)
{
    // If in PWM "mark"...
    if( tick - cycle_start_tick < duty_cycle )
    {
        // LED on
        GPIO_PORTF_DATA_R |= 0x02 ;
    }
    // else PWM "space"
    else
    {
        // LED off
        GPIO_PORTF_DATA_R &= ~0x02 ;
    }

    // Update tick counter
    tick++ ;

    // If PWM cycle complete, restart
    if( tick - cycle_start_tick >= PWM_PERIOD_MS )
    {
        cycle_start_tick = tick ;
    }

    // If time to update duty-cycle...
    if( tick - breath_update_tick > BREATHE_UPDATE_MS )
    {
        breath_update_tick = tick ;
        duty_cycle += breathe_dir ;
        if( duty_cycle >= PWM_PERIOD_MS )
        {
            // Breathe in
            breathe_dir = -1 ;
        }
        else if( duty_cycle == BREATHE_MINIMUM )   
        {
            // Breathe out
            breathe_dir = 1 ;
        }
    }
 
    delayms( 1 ) ;
}

Change BREATHE_UPDATE_MS to breathe faster, change BREATHE_MINIMUM to "shallow breathe" - i.e. not dim to off.

If your delay function truly results in a delay resolution in seconds then approximately and rather crudely:

void delayms( unsigned millisec )
{
   for( int c = 0; c < millisec; c++ )
   {
       for( volatile int d = 0; d < 4000; d++ ) {}
   }
}

However that suggests to me a rather low core clock rate, so you may need to adjust that. Note the use of volatile to prevent the removal of the empty loop by the optimiser. The problem with this delay is that you will need to calibrate it to the clock speed of your target and its timing is likely to differ in any case depending on what compiler you use and what compiler options you use. It is generally a poor solution.

In practice using a "busy-loop" delay for this is ill-advised and crude and it would be better to use the Cortex-M SYSTICK:

volatile uint32_t tick = 0 ;
  
void SysTick_Handler(void)  
{
    tick++ ;
}

... removing the tick and tick++ from the original; code. Then you don't need a delay in the loop above because all the timing is pegged to the value of tick. However should you want a delay for other reasons then:

delayms( uint32_t millisec )
{
    uint32_t start = tick ;
    while( tick - start < millisec ) ;
}

Then you would initialise the SYSTICK at start-up thus:

int main (void)  
{
    SysTick_Config(SystemCoreClock / 1000) ;
 
    ...
}

This assumes that you are using the CMSIS, but your code suggests that you are not doing that (or even using a vendor supplied register header). You will in that case need to get down and dirty with the SYSTICK and NVIC registers if you (or your tutor) insists on that. The source for SysTick_Config() is as follows:

__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
  if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
  {
    return (1UL);                                                   /* Reload value impossible */
  }

  SysTick->LOAD  = (uint32_t)(ticks - 1UL);                         /* set reload register */
  NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
  SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */
  return (0UL);                                                     /* Function successful */
}
answered on Stack Overflow Apr 14, 2021 by Clifford • edited Apr 14, 2021 by Clifford

User contributions licensed under CC BY-SA 3.0