]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - rgb.c
Add README
[rgblamp.git] / rgb.c
diff --git a/rgb.c b/rgb.c
index 43fec9ef21aae0071e75ccca3f461eac13e8424f..cdee44902a93950e680989a21e1c5d7d6ced02e0 100644 (file)
--- a/rgb.c
+++ b/rgb.c
@@ -1,3 +1,32 @@
+/*
+ * Copyright © 2011, 2012, Titanium Mirror, Inc..
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright notice,
+ *   this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - Neither the name of Titanium Mirror, Inc. nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 /*
  * File:   rgb.c
  *
  */
 
 #include <htc.h>
+#include "rgb.h"
+
+#define PINS_PORTB  0b00101001
+#define PINS_PORTC  0b00000100
+
+/* To get a perceived linear output, the PWM value must be adjusted according
+ * to CIE Lightness.  The human eye perceives changes at lower intensities
+ * more readily that at higher intensities.
+ *
+ * L* = 116(Y/Yn)^1/3 - 16 , Y/Yn > 0.008856
+ * L* = 903.3(Y/Yn), Y/Yn <= 0.008856
+*/
 
-#define PINS_PORTA  0b10011000
-#define PINS_PORTB  0b00001000
+#ifdef CEIL256
+const unsigned char pwm_table[] = {
+  0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 1, 1, 1, 1, 1, 1,
+  1, 1, 1, 2, 2, 2, 2, 2,
+  2, 2, 2, 3, 3, 3, 3, 3,
+  3, 3, 4, 4, 4, 4, 4, 5,
+  5, 5, 5, 5, 6, 6, 6, 6,
+  6, 7, 7, 7, 7, 8, 8, 8,
+  8, 9, 9, 9, 10, 10, 10, 11,
+  11, 11, 12, 12, 12, 13, 13, 13,
+  14, 14, 14, 15, 15, 16, 16, 17,
+  17, 17, 18, 18, 19, 19, 20, 20,
+  21, 21, 22, 22, 23, 23, 24, 24,
+  25, 25, 26, 27, 27, 28, 28, 29,
+  30, 30, 31, 31, 32, 33, 33, 34,
+  35, 35, 36, 37, 38, 38, 39, 40,
+  41, 41, 42, 43, 44, 45, 45, 46,
+  47, 48, 49, 50, 51, 51, 52, 53,
+  54, 55, 56, 57, 58, 59, 60, 61,
+  62, 63, 64, 65, 66, 67, 68, 69,
+  70, 71, 73, 74, 75, 76, 77, 78,
+  80, 81, 82, 83, 84, 86, 87, 88,
+  90, 91, 92, 93, 95, 96, 98, 99,
+  100, 102, 103, 105, 106, 107, 109, 110,
+  112, 113, 115, 116, 118, 120, 121, 123,
+  124, 126, 128, 129, 131, 133, 134, 136,
+  138, 139, 141, 143, 145, 146, 148, 150,
+  152, 154, 156, 157, 159, 161, 163, 165,
+  167, 169, 171, 173, 175, 177, 179, 181,
+  183, 185, 187, 189, 192, 194, 196, 198,
+  200, 203, 205, 207, 209, 212, 214, 216,
+  218, 221, 223, 226, 228, 230, 233, 235,
+  238, 240, 243, 245, 248, 250, 253, 255
+};
+#else
+#ifdef CEIL32
+const unsigned char pwm_table[] = {
+    0, 1, 2, 3, 4, 5, 7, 9,
+    12, 15, 18, 22, 27, 32, 38, 44,
+    51, 58, 67, 76, 86, 96, 108, 120,
+    134, 148, 163, 180, 197, 216, 235, 255
+};
+#endif
+#endif
 
 void rgb_init()
 {
     /* Initialize rgb
-     * CCP1 on RB3, CCP2 on RA7, CCP3 on RA3, CCP4 on RA4
-     * - Fosc =     8 MHz,    4 MHz
-     * - Prescale = 4,        4
+     * red on CCP3/RB5, grn on CCP2/RB3, blu on CCP4/RB0, wht on CCP1/RC2
+     * - Fosc =     8 MHz,    4 MHz,    2 MHz
+     * - Prescale = 4,        1,        1
      * - PRx value = 0xff
-     * = f(rgb) =   1.95 kHz, 0.98 kHz
+     * = f(rgb) =   1.95 kHz, 3.90 kHz, 1.95 kHz
      */
 
     /* Set rgb Rxn pins as outputs. */
-    PORTA &= ~PINS_PORTA;
-    TRISA &= ~PINS_PORTA;
     PORTB &= ~PINS_PORTB;
     TRISB &= ~PINS_PORTB;
+    PORTC &= ~PINS_PORTC;
+    TRISC &= ~PINS_PORTC;
 
-    /* Configure ECCP1 */
+    /* Configure CCP1 */
     CCP1CON = 0b00001100;
-    CCPR1L = 0; /* Initial rgb value; only using 8 LSBs */
+    CCPR1L = 0; /* Initial wht value; only using 8 LSBs */
 
-    /* Configure ECCP2 */
-    APFCON0 |= 0b00001000; /* Use alternate output pin RA7 */
+    /* Configure CCP2 */
+    CCP2SEL = 1; /* CCP2 is on RB3 */
     CCP2CON = 0b00001100;
-    CCPR2L = 0; /* Initial rgb value; only using 8 LSBs */
+    CCPR2L = 0; /* Initial grn value; only using 8 LSBs */
 
     /* Configure CCP3 */
+    CCP3SEL = 1; /* CCP3 is on RB5 */
     CCP3CON = 0b00001100;
-    CCPR3L = 0; /* Initial rgb value; only using 8 LSBs */
+    CCPR3L = 0; /* Initial red value; only using 8 LSBs */
 
     /* Configure CCP4 */
     CCP4CON = 0b00001100;
-    CCPR4L = 0; /* Initial rgb value; only using 8 LSBs */
+    CCPR4L = 0; /* Initial blu value; only using 8 LSBs */
 
     /* Configure Timer2.  Start it to set 0 outputs, then stop it. */
-    CCPTMRS = 0; /* All CCPx use Timer 2 */
+    CCPTMRS0 = 0; /* CCP[1-4] use Timer 2 */
     TMR2IF = 0;
     PR2 = 0xff;
 
-    T2CON = 0b00000001;
+    T2CON = 0b00000000;
 }
 
 void rgb_off()