Subversion Repositories Code-Repo

Rev

Rev 282 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
280 Kevin 1
#include "defines.h"
2
#include "INTERRUPTS.h"
281 Kevin 3
#include "I2C1.h"
280 Kevin 4
#include "NEOPIXEL.h"
281 Kevin 5
#include "DS3231.h"
6
#include "TSL2561.h"
280 Kevin 7
 
8
// <editor-fold defaultstate="collapsed" desc="Configuration Registers">
9
/* Config Register CONFIGL @ 0x8007 */
10
#pragma config CPD = OFF    // Data memory code protection is disabled
11
#pragma config BOREN = OFF  // Brown-out Reset disabled
12
#pragma config IESO = OFF   // Internal/External Switchover mode is disabled
13
#pragma config FOSC = INTOSC    // INTOSC oscillator: I/O function on CLKIN pin
14
#pragma config FCMEN = OFF  // Fail-Safe Clock Monitor is disabled
15
#pragma config MCLRE = ON   // MCLR/VPP pin function is MCLR
16
#pragma config WDTE = OFF   // WDT disabled
17
#pragma config CP = OFF     // Program memory code protection is disabled
282 Kevin 18
#pragma config PWRTE = ON   // PWRT enabled
280 Kevin 19
#pragma config CLKOUTEN = OFF   // CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
20
 
21
/* Config Register CONFIG2 @ 0x8008 */
22
#pragma config PLLEN = ON   // 4x PLL disabled
23
#pragma config WRT = OFF    // Write protection off
24
#pragma config STVREN = OFF // Stack Overflow or Underflow will not cause a Reset
25
#pragma config BORV = HI    // Brown-out Reset Voltage (Vbor), high trip point selected.
26
#pragma config LVP = OFF    // High-voltage on MCLR/VPP must be used for programming
27
// </editor-fold>
28
 
281 Kevin 29
I2C1_DATA i2c_data;
280 Kevin 30
NEOPIXEL_DATA neopixel_data;
281 Kevin 31
TSL2561_DATA tsl2561_data;
32
DS3231_TIME time;
280 Kevin 33
 
34
int main() {
35
    // Oscillator configuration (32Mhz HFINTOSC)
36
    OSCCONbits.SCS = 0b00;
37
    OSCCONbits.IRCF = 0b1110;
38
 
39
    ANSELA = 0x00;  // All pins set to digital I/O
40
    APFCONbits.CCP1SEL = 1; // Switch CCP1 from RA2 to RA5
282 Kevin 41
    WPUA = 0x00;    // Disable weak pull-ups
280 Kevin 42
 
43
    // Wait for HFINTOSC to be within 0.5% of target 32Mhz
44
    while (!OSCSTATbits.HFIOFS);
45
 
281 Kevin 46
    Interrupt_Enable();
282 Kevin 47
 
281 Kevin 48
    I2C1_Init();
49
    I2C1_Configure_Master(I2C_1MHZ);
280 Kevin 50
 
281 Kevin 51
    NeoPixel_Init();
52
    NeoPixel_Offet(30);
53
 
54
    DS3231_Init();
55
 
56
    TSL2561_Init(TSL2561_ADDR_FLOAT);
57
 
58
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
282 Kevin 59
//    TSL2561_Set_Gain(TSL2561_GAIN_0X);   // set no gain (for bright situtations)
60
    TSL2561_Set_Gain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
281 Kevin 61
 
62
    // Changing the integration time gives you a longer time over which to sense light
63
    // longer timelines are slower, but are good in very low light situtations!
64
//    TSL2561_Set_Timing(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright light)
65
    TSL2561_Set_Timing(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
66
//    TSL2561_Set_Timing(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
67
 
282 Kevin 68
    uint16_t full;
69
    uint8_t multiplier, multiplier_2;
281 Kevin 70
 
282 Kevin 71
//    time.sec = 0;
72
//    time.min = 16;
73
//    time.hour = 7;
74
//    time.h_mil = 0;
75
//    time.h_am_pm = 1;
76
//
77
//    DS3231_Set_Time(&time);
78
 
280 Kevin 79
    while(1) {
282 Kevin 80
        // ~37200 seems to be the max value at 16X gain, 101ms period
81
        full = TSL2561_Get_Luminosity(0);
82
        multiplier = (full / 3100) + 1;   // 12 levels (1-12)
83
        multiplier_2 = (multiplier / 7) + 1;    // 2 levels (1-2)
84
 
281 Kevin 85
//        NeoPixel_Clear();
282 Kevin 86
//        for (uint8_t i = 0; i < multiplier; i++) {
87
//            NeoPixel_Set(i, 0x10, 0x00, 0x00, 1);
281 Kevin 88
//        }
89
//        NeoPixel_Write_All();
282 Kevin 90
 
281 Kevin 91
        DS3231_Get_Time(&time);
92
        NeoPixel_Clear();
93
 
94
        // Draw markers
282 Kevin 95
        NeoPixel_Set(00, RED, multiplier_2);
96
        NeoPixel_Set(30, RED, multiplier_2);
97
        NeoPixel_Set(05, ORANGE, multiplier_2);
98
        NeoPixel_Set(35, ORANGE, multiplier_2);
99
        NeoPixel_Set(10, YELLOW, multiplier_2);
100
        NeoPixel_Set(40, YELLOW, multiplier_2);
101
        NeoPixel_Set(15, GREEN, multiplier_2);
102
        NeoPixel_Set(45, GREEN, multiplier_2);
103
        NeoPixel_Set(20, BLUE, multiplier_2);
104
        NeoPixel_Set(50, BLUE, multiplier_2);
105
        NeoPixel_Set(25, PURPLE, multiplier_2);
106
        NeoPixel_Set(55, PURPLE, multiplier_2);
281 Kevin 107
 
108
        // Draw time
282 Kevin 109
        NeoPixel_Or((time.hour * 5) % 60, BLUE, multiplier + 4);
110
        NeoPixel_Or(time.min, GREEN, multiplier + 4);
111
        NeoPixel_Or(time.sec, RED, multiplier + 4);
280 Kevin 112
        NeoPixel_Write_All();
113
    }
114
}