]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - rgb.c
Closer to a full featured setup
[rgblamp.git] / rgb.c
diff --git a/rgb.c b/rgb.c
new file mode 100644 (file)
index 0000000..5fb2849
--- /dev/null
+++ b/rgb.c
@@ -0,0 +1,60 @@
+/*
+ * File:   rgb.c
+ *
+ * RGB led control
+ */
+
+#include <htc.h>
+
+#define PINS_PORTA  0b10011000
+#define PINS_PORTB  0b00001000
+
+void rgb_init()
+{
+    /* Initialize rgb
+     * CCP1 on RB3, CCP2 on RA7, CCP3 on RA3, CCP4 on RA4
+     * - Fosc = 32MHz
+     * - Prescale = 16
+     * - PRx value = 0xff
+     * = f(rgb) = 1.95 kHz
+     */
+
+    /* Set rgb Rxn pins as outputs. */
+    PORTA &= ~PINS_PORTA;
+    TRISA &= ~PINS_PORTA;
+    PORTB &= ~PINS_PORTB;
+    TRISB &= ~PINS_PORTB;
+
+    /* Configure ECCP1 */
+    CCP1CON = 0b00001100;
+    CCPR1L = 0; /* Initial rgb value; only using 8 LSBs */
+
+    /* Configure ECCP2 */
+    APFCON0 |= 0b00001000; /* Use alternate output pin RA7 */
+    CCP2CON = 0b00001100;
+    CCPR2L = 0; /* Initial rgb value; only using 8 LSBs */
+
+    /* Configure CCP3 */
+    CCP3CON = 0b00001100;
+    CCPR3L = 0; /* Initial rgb value; only using 8 LSBs */
+
+    /* Configure CCP4 */
+    CCP4CON = 0b00001100;
+    CCPR4L = 0; /* Initial rgb value; only using 8 LSBs */
+
+    /* Configure Timer2.  Start it to set 0 outputs, then stop it. */
+    CCPTMRS = 0; /* All CCPx use Timer 2 */
+    TMR2IF = 0;
+    PR2 = 0xff;
+    T2CON = 0b00000011;
+}
+
+void rgb_off()
+{
+    if (TMR2ON) {
+        CCPR1L = CCPR2L = CCPR3L = CCPR4L = 0;
+        TMR2IF = 0;
+        while (!TMR2IF);
+        TMR2ON = 0;
+    }
+}