In this tutorial, we will cover the basic aspects of controlling the RGB LEDs present on the PICxie development kit.
You will need the PICxie development kit, a USB cable for programming, Microchips MPLABX and XC8 software, and Mid-Ohio Area Robotics MOARProg software and PICxie Application Libraries installed on your PC. All of this will have came with your PICxie development kit.
You should at least know the difference between a bit and a byte, and know what hexadecimal and binary numbers are.
What are RGB LEDs?
RGB (Red/Green/Blue) LEDs look and function much like a typical LED, however, inside the LED package, there are actually three LEDs representing the primary colors, red, green, and blue. By controlling the brightness of each individual LED you can create pretty much any color you want.
We create colors just like you would mix paints together on a palette, by adjusting the brightness you control how much color (paint) is added to the palette! The reason that varying the amount of red, green, and blue light creates different colors is that your eye has three types of light receptors in it (red, green, and blue). Your eye and brain work together to convert the amount of each type of light into a color of the spectrum.
In order to vary the amount of light we need to send a variable amount of power to each color inside the LED.
This is accomplished with Pulse-Width Modulation, a very simple technique for controlling power. We are going to use it to control the brightness of the RGB LEDs. The diagram below shows a typical PWM signal, exactly like the one we’ll be using.
We will be writing a program that produces a PWM pulse about every .5 milliseconds. The length of this pulse will be controlled by 3 variables representing the red, green, and blue colors. These variables have a range of 0-255, so ‘R = 0” will not produce any pulse at all and 'R=255’ will produce a pulse that lasts all the way until the end of the pulse, effectively turning that color on completely. If we set that variable to something in between 0 and 255, we can vary the brightness of the LED. A value of 13 will turn the LED on about ~5%, and a value of 230 will deliver about 90% of the power. The human eye cannot see the LED turning on and off that fast, so to our brain it appears as if the brightness is changing. Getting Started with MPLabX and PICxie
Before we dive into coding, the project needs to be created in MPLabX.
1. Start by opening MPLabX, and clicking File > New Project…
2. Choose 'Microchip Embedded’ for the project category, and 'Standalone Project’ for the type of project. Click next
3. The next screen lets us specify the type of device we will be programming. PICxie uses a PIC18F26J50 and that is the device you’ll want to enter into the Device field in this window.
4. Click next through the 'Select Tool’ window, ICD3 should be selected already (although we will not be using it)
5. For the compiler, select the XC8 compiler and click next
6. Enter the project name of your choosing, we will be calling ours Tutorial_1. Leave the other options alone, and click Finish. Your screen should look very similar to following image
7. Almost done! In the left hand list, make sure the top panel is set to the 'Project’’ tab, then right click on “Tutorial_1” and click 'Properties…’
8. Go into the “XC8 Linker” category, and from the drop down select 'Memory model’, and in the 'ROM ranges’ field, enter 'default,-0-FFF,-FC00-FFF7’ and click apply
9. Click the drop down again, and select 'Additional options…’ and in the 'Codeoffset’ field enter '0x1000’ and click OK.
We are now ready to start coding programs for PICxie!
Coding the Basics
Now that we have everything setup, lets write a program that will cycle the RGB LEDs through a...
Read more »