Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 341 → Rev 342

/PIC Projects/PICX_12F1840_Clock/main.c
0,0 → 1,114
#include "defines.h"
#include "INTERRUPTS.h"
#include "I2C1.h"
#include "NEOPIXEL.h"
#include "DS3231.h"
#include "TSL2561.h"
 
// <editor-fold defaultstate="collapsed" desc="Configuration Registers">
/* Config Register CONFIGL @ 0x8007 */
#pragma config CPD = OFF // Data memory code protection is disabled
#pragma config BOREN = OFF // Brown-out Reset disabled
#pragma config IESO = OFF // Internal/External Switchover mode is disabled
#pragma config FOSC = INTOSC // INTOSC oscillator: I/O function on CLKIN pin
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor is disabled
#pragma config MCLRE = ON // MCLR/VPP pin function is MCLR
#pragma config WDTE = OFF // WDT disabled
#pragma config CP = OFF // Program memory code protection is disabled
#pragma config PWRTE = ON // PWRT enabled
#pragma config CLKOUTEN = OFF // CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
 
/* Config Register CONFIG2 @ 0x8008 */
#pragma config PLLEN = ON // 4x PLL disabled
#pragma config WRT = OFF // Write protection off
#pragma config STVREN = OFF // Stack Overflow or Underflow will not cause a Reset
#pragma config BORV = HI // Brown-out Reset Voltage (Vbor), high trip point selected.
#pragma config LVP = OFF // High-voltage on MCLR/VPP must be used for programming
// </editor-fold>
 
I2C1_DATA i2c_data;
NEOPIXEL_DATA neopixel_data;
TSL2561_DATA tsl2561_data;
DS3231_TIME time;
 
int main() {
// Oscillator configuration (32Mhz HFINTOSC)
OSCCONbits.SCS = 0b00;
OSCCONbits.IRCF = 0b1110;
ANSELA = 0x00; // All pins set to digital I/O
APFCONbits.CCP1SEL = 1; // Switch CCP1 from RA2 to RA5
WPUA = 0x00; // Disable weak pull-ups
 
// Wait for HFINTOSC to be within 0.5% of target 32Mhz
while (!OSCSTATbits.HFIOFS);
 
Interrupt_Enable();
 
I2C1_Init();
I2C1_Configure_Master(I2C_1MHZ);
 
NeoPixel_Init();
NeoPixel_Offet(30);
 
DS3231_Init();
 
TSL2561_Init(TSL2561_ADDR_FLOAT);
 
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
// TSL2561_Set_Gain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
TSL2561_Set_Gain(TSL2561_GAIN_16X); // set 16x gain (for dim situations)
 
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
// TSL2561_Set_Timing(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
TSL2561_Set_Timing(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
// TSL2561_Set_Timing(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)
 
uint16_t full;
uint8_t multiplier, multiplier_2;
 
// time.sec = 0;
// time.min = 16;
// time.hour = 7;
// time.h_mil = 0;
// time.h_am_pm = 1;
//
// DS3231_Set_Time(&time);
 
while(1) {
// ~37200 seems to be the max value at 16X gain, 101ms period
full = TSL2561_Get_Luminosity(0);
multiplier = (full / 3100) + 1; // 12 levels (1-12)
multiplier_2 = (multiplier / 7) + 1; // 2 levels (1-2)
 
// NeoPixel_Clear();
// for (uint8_t i = 0; i < multiplier; i++) {
// NeoPixel_Set(i, 0x10, 0x00, 0x00, 1);
// }
// NeoPixel_Write_All();
 
DS3231_Get_Time(&time);
NeoPixel_Clear();
 
// Draw markers
NeoPixel_Set(00, RED, multiplier_2);
NeoPixel_Set(30, RED, multiplier_2);
NeoPixel_Set(05, ORANGE, multiplier_2);
NeoPixel_Set(35, ORANGE, multiplier_2);
NeoPixel_Set(10, YELLOW, multiplier_2);
NeoPixel_Set(40, YELLOW, multiplier_2);
NeoPixel_Set(15, GREEN, multiplier_2);
NeoPixel_Set(45, GREEN, multiplier_2);
NeoPixel_Set(20, BLUE, multiplier_2);
NeoPixel_Set(50, BLUE, multiplier_2);
NeoPixel_Set(25, PURPLE, multiplier_2);
NeoPixel_Set(55, PURPLE, multiplier_2);
 
// Draw time
NeoPixel_Or((time.hour * 5) % 60, BLUE, multiplier + 4);
NeoPixel_Or(time.min, GREEN, multiplier + 4);
NeoPixel_Or(time.sec, RED, multiplier + 4);
NeoPixel_Write_All();
}
}