Menu

RGB LED Code

Anonymous
2014-05-05
2014-05-12
  • Anonymous

    Anonymous - 2014-05-05

    Here's my next contribution, code to modulate the colors of an RGB LED. With it, 4096 different colors are possible. I've written this so it does not require any PWM module, so it should work on all processors PIC and AVR alike, supposing a Timer 0 is available. Given how ubiquitous RGB has become, some enterprising person might want to work this up into an include file which would make it easy for newcomers to get started with it.

    This also makes a nice demo of Timer 0 and interrupts. For that matter, it is also a good way to generate multiple independent PWM channels which doesn't tie up the processor babysitting, supposing 16 levels are sufficient.

    As usual, there are instructions and comments in the listing.

    ;RGB LED Driver
    ;Thomas Henry --- 5/5/2014
    
    ;This program demonstrates how to drive an RGB LED
    ;to create 4096 different colors. Each of the three
    ;elements (red, green and blue) responds to 16
    ;different levels. A value of 0 means the element
    ;never turns on, while a value of 15 means the
    ;element never shuts off. Values in between these
    ;two extremes vary the pulse width.
    
    ;This is essentially an interrupt driven three
    ;channel PWM implementation. The basic carrier
    ;frequency depends upon the microcontroller clock
    ;speed. For example, with an 8 MHz clock, the LED
    ;elements are modulated at about 260 Hz.
    
    ;The interrupts are generated by Timer 0. With an
    ;8 MHz clock they occur about every 256 uS. The
    ;interrupt routine consumes about 20 uS.
    
    ;Don't forget the current limiting resistors to the
    ;LED elements. A value of around 470 ohms is good, 
    ;but you may want to adjust the individual values,
    ;to balance the color response.
    
    ;In this demonstration, three potentiometers are used
    ;to set the color levels.
    
    ;----- Configuration
    
    #chip 16F88, 8                ;PIC16F88 running at 8 MHz
    #config mclr=off              ;reset handled internally
    #config osc=int               ;use internal clock
    
    ;----- Constants
    
    #define LED_R PortB.0         ;pin to red element
    #define LED_G PortB.1         ;pin to green element
    #define LED_B PortB.2         ;pin to blue element
    
    ;----- Variables
    
    dim redValue, greenValue, blueValue, ticks as byte
    
    ;----- Program
    
    dir PortA in                  ;three pots for inputs
    dir PortB out                 ;the LED outputs    
    
    on interrupt Timer0Overflow call update
    initTimer0 Osc, PS0_1/2
    
    do
      redValue = readAD(AN0)/16   ;red -- 0 to 15
      greenValue = readAD(AN1)/16 ;green -- 0 to 15
      blueValue = readAD(AN2)/16  ;blue -- 0 to 15
    loop
    
    sub update                    ;interrupt routine
      ticks++                     ;increment master timekeeper
      if ticks = 15 then          ;start of the count
        ticks = 0
        if redValue <> 0 then     ;only turn on if nonzero
          set LED_R on
        end if
        if greenValue <> 0 then
          set LED_G on
        end if
        if blueValue <> 0 then
          set LED_B on
        end if
      end if  
      if ticks = redValue then    ;time to turn off red?
        set LED_R off
      end if
      if ticks = greenValue then  ;time to turn off green?
        set LED_G off
      end if
      if ticks = blueValue then   ;time to turn off blue?
        set LED_B off
      end if
    end sub  
    
     
    • Anobium

      Anobium - 2014-05-12

      Included in the new help file!

      J

      From: Thomas Henry [mailto:bachlover@users.sf.net]
      Sent: 05 May 2014 22:17
      To: [gcbasic:discussion]
      Subject: [gcbasic:discussion] RGB LED Code

      Here's my next contribution, code to modulate the colors of an RGB LED. With
      it, 4096 different colors are possible. I've written this so it does not
      require any PWM module, so it should work on all processors PIC and AVR
      alike, supposing a Timer 0 is available. Given how ubiquitous RGB has
      become, some enterprising person might want to work this up into an include
      file which would make it easy for newcomers to get started with it.

      This also makes a nice demo of Timer 0 and interrupts. For that matter, it
      is also a good way to generate multiple independent PWM channels which
      doesn't tie up the processor babysitting, supposing 16 levels are
      sufficient.

      As usual, there are instructions and comments in the listing.

      ;RGB LED Driver
      ;Thomas Henry --- 5/5/2014

      ;This program demonstrates how to drive an RGB LED
      ;to create 4096 different colors. Each of the three
      ;elements (red, green and blue) responds to 16
      ;different levels. A value of 0 means the element
      ;never turns on, while a value of 15 means the
      ;element never shuts off. Values in between these
      ;two extremes vary the pulse width.

      ;This is essentially an interrupt driven three
      ;channel PWM implementation. The basic carrier
      ;frequency depends upon the microcontroller clock
      ;speed. For example, with an 8 MHz clock, the LED
      ;elements are modulated at about 260 Hz.

      ;The interrupts are generated by Timer 0. With an
      ;8 MHz clock they occur about every 256 uS. The
      ;interrupt routine consumes about 20 uS.

      ;Don't forget the current limiting resistors to the
      ;LED elements. A value of around 470 ohms is good,
      ;but you may want to adjust the individual values,
      ;to balance the color response.

      ;In this demonstration, three potentiometers are used
      ;to set the color levels.

      ;----- Configuration

      chip 16F88, 8 ;PIC16F88 running at 8 MHz

      config mclr=off ;reset handled internally

      config osc=int ;use internal clock

      ;----- Constants

      define LED_R PortB.0 ;pin to red element

      define LED_G PortB.1 ;pin to green element

      define LED_B PortB.2 ;pin to blue element

      ;----- Variables

      dim redValue, greenValue, blueValue, ticks as byte

      ;----- Program

      dir PortA in ;three pots for inputs
      dir PortB out ;the LED outputs

      on interrupt Timer0Overflow call update
      initTimer0 Osc, PS0_1/2

      do
      redValue = readAD(AN0)/16 ;red -- 0 to 15
      greenValue = readAD(AN1)/16 ;green -- 0 to 15
      blueValue = readAD(AN2)/16 ;blue -- 0 to 15
      loop

      sub update ;interrupt routine
      ticks++ ;increment master timekeeper
      if ticks = 15 then ;start of the count
      ticks = 0
      if redValue <> 0 then ;only turn on if nonzero
      set LED_R on
      end if
      if greenValue <> 0 then
      set LED_G on
      end if
      if blueValue <> 0 then
      set LED_B on
      end if
      end if
      if ticks = redValue then ;time to turn off red?
      set LED_R off
      end if
      if ticks = greenValue then ;time to turn off green?
      set LED_G off
      end if
      if ticks = blueValue then ;time to turn off blue?
      set LED_B off
      end if
      end sub


      RGB LED Code
      https://sourceforge.net/p/gcbasic/discussion/629990/thread/e6e65702/?limit= 25#b093


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/gcbasic/discussion/629990/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       

Log in to post a comment.