Code:
ASM
__CONFIG _CONFIG1H, _OSC_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
DEFINE OSC 20 ; 48 was old config settings 18F4520, 20mhz crystal
ADCON1 = $0F
clear
DEFINE WRITE_USED 1
DEFINE LCD_DREG PORTB ' LCD Data port
DEFINE LCD_DBIT 0 ' starting Data bit (0 or 4)
DEFINE LCD_EREG PORTB ' LCD Enable port
DEFINE LCD_EBIT 5 ' Enable bit (on EasyPIC 5 LCD)
DEFINE LCD_RSREG PORTB ' LCD Register Select port
DEFINE LCD_RSBIT 4 ' Register Select bit (on EasyPIC 5 LCD)
DEFINE LCD_BITS 4 ' LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 4 ' number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Command delay time in us
DEFINE LCD_DATAUS 50 ' Data delay time in us
DecButton var PORTA.0 ' Press to Decrement Button
SetButton var PORTA.1 ' Press to Set/memorise Button
IncButton var PORTA.2 ' Press to Increment Button
H_butt VAR PORTA.2
M_butt VAR PORTA.0
S_butt VAR PORTA.1
CounterA var byte ' General purpose Variable
CounterB var byte ' General purpose Variable
CounterC var byte ' General purpose Variable
CounterD var byte ' General purpose Variable
'****************************************************************
' menu variables - used to store menu options
lightsetHR VAR WORD[2]
lightsetHR1 VAR lightsetHR[0]
lightsetHR2 VAR lightsetHR[1]
lightsetMN VAR WORD[2]
lightsetMN1 VAR lightsetMN[0]
lightsetMN2 VAR lightsetMN[1]
lightoffHR VAR WORD[2]
lightoffHR1 VAR lightoffHR[0]
lightoffHR2 VAR lightoffHR[1]
lightoffMN VAR WORD[2]
lightoffMN1 VAR lightoffMN[0]
lightoffMN2 VAR lightoffMN[1]
fadesetHR VAR WORD[2]
fadesetHR1 VAR fadesetHR[0]
fadesetHR2 VAR fadesetHR[1]
fadesetMN VAR WORD[2]
fadesetMN1 VAR fadesetMN[0]
fadesetMN2 VAR fadesetMN[1]
fadeoutHR VAR WORD[2]
fadeoutHR1 VAR fadeoutHR[0]
fadeoutHR2 VAR fadeoutHR[1]
fadeoutMN VAR WORD[2]
fadeoutMN1 VAR fadeoutMN[0]
fadeoutMN2 VAR fadeoutMN[1]
maxbright var byte
'****************************************************************
' LED variables
' Whites:
W_Min var word 'Min Light value
W_Max var word 'Max Light value
W_Fade_In var word 'Fade in duration
W_Fade_Out var word 'Fade out duration
W_On_Time_H var word 'What time to turn the lights on
W_On_Time_M var word 'What time to turn the lights on
W_Off_Time_H var word 'Time to turn lights off
W_Off_Time_M var word 'Time to turn lights off
' Blues:
B_Min var word 'Min Light value
B_Max var word 'Max Light value
B_Fade_In var word 'Fade in duration
B_Fade_Out var word 'Fade out duration
B_On_Time_H var word 'What time to turn the lights on
B_On_Time_M var word 'What time to turn the lights on
B_Off_Time_H var word 'Time to turn lights off
B_Off_Time_M var word 'Time to turn lights off
'Running fade variables
B_FadeIn_Time var word 'LIVE fade delay time seconds
B_FadeOut_Time var word
W_FadeIn_Time var word
W_FadeOut_Time var word
B_Cnt var word 'Seconds counter for the timing of adjusting the PWM
W_Cnt var word
B_Tick var word
W_Tick var word
'PWM Variables where the current output values are stored
W_PWM var word 'Whites
B_PWM var word 'Royal Blues
'****************************************************************
Blue_Day_Cycle var word 'Store for which part of the day cycle we are in
White_Day_Cycle var word
' Constants for the current running mode cycle for the lights
DAWN con 0
DAY con 1
DUSK con 2
NIGHT con 3
'****************************************************************
'switches
tog1 var byte
'****************************************************************
'Expendable variables used for misc calculations etc
Vb_1 var word
Vb_2 Var word
Vb_3 var word
Vb_4 var word
Power var bit
'****************************************************************
' Include files and configure timers
DEFINE WRITE_INT 1
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas"
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _MyTimer, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
endasm
T0CON = %11000111
@ INT_ENABLE TMR1_INT ; enable Timer1 interrupts
'T2CON = %00000001 ; free-running, 1:1 prescaler
ENABLE DEBUG
'****************************************************************
'DS18B20 setting
DQ VAR PORTA.5 ' One-wire data pin
temperature VAR WORD ' Temperature storage
count_remain VAR BYTE ' Count remaining
count_per_c VAR BYTE ' Count per degree C
'****************************************************************
'analog settings
ADCON0 = 0 'Set ADCON0
ADCON1 = %00001111 'Set D i/o
CMCON = 7 'Disable Comparators
PR2 = 249
'****************************************************************
'Port settings
CCP1CON = %00001100 '
CCP2CON = %00001100 '
TRISA = %11101111 'set PORTA as all output apart from 0,1,2
TRISB = %00000011
TRISD = %00000011 'set PORTB as all output apart from 0&1
'****************************************************************
'Varibles
GIE VAR INTCON.7
TempWD VAR WORD ' temporary WORD variable
Bvar VAR BYTE ' temporary BYTE variable
TempStr VAR BYTE[8]
EditChannel VAR BYTE
option var byte
Hours var byte
Minutes var byte
nitetemp var word
fn var byte
cn var byte
AllZeros VAR Bit
Tick_Tmr var byte
Value VAR WORD
Sensor VAR BYTE
viv var byte
counterX var byte
Counter1 var word
set1 var word
set2 var word
ButtonRepeat con 200 ' Timeperiod (in mS for Edit Buttons auto-repeat
' should you want to hold them down...
'****************************************************************
'Data presets
data @0
data 14 'Whites ON Time HOURS
data 00 'Whites ON Time MINS
data 20 'Whites OFF Time HOURS
data 30 'Whites OFF Time MINS
data 0 'Whites Fade IN duration HOURS
data 10 'Whites Fade IN duration MINS
data 0 'Whites Fade OUT duration HOURS
data 15 'Whites Fade OUT duration MINS
data 0 'Whites MIN Intensity %
data 80 'Whites MAX Intensity %
data 14 'Blues ON Time HOURS
data 05 'Blues ON Time MINS
data 20 'Blues OFF Time HOURS
data 45 'Blues OFF Time MINS
data 0 'Blues Fade IN duration HOURS
data 05 'Blues Fade IN duration MINS
data 0 'Blues Fade OUT duration HOURS
data 10 'Blues Fade OUT duration MINS
data 0 'Blues MIN Intensity %
data 100 'Blues MAX Intensity %
'****************************************************************
'Read EEPROM on Power-Up
' gosub Read_eeprom
'****************************************************************
;Initialization
init2:
LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250 ' Initialize LCD
LCDOUT $FE,1
lcdout $FE,$C0," Version T "
pause 2000 'wait 2 seconds so they can see it
lcdout $FE,1 'Clear the screen
'gosub read_eeprom 'Read in the data needed for the lighting periods
option=1
tog1 =0
init:
b_cnt = 0
w_cnt = 0
'gosub Calc_Fade
'gosub check_lighting
HzTimer VAR Word '1/2 second counter (2 Hz)
HH VAR Byte ' Hours 0-23
MM VAR ByTE ' Minutes 0-59
SS VAR Byte ' Seconds 0-59
col VAR Byte ' colon 1=on, 0=0ff
S1H var byte
S1M var byte
S1S var byte
S2H var byte
S2M var byte
S2S var byte
HzTimer=$7A12 'for 1/2 Sec
HH=19:MM=58:SS=58:col=0 'initial conditions (12:00 O'clock noon)
S1H=20:S1M=0:S1S=0
S2H=20:S2M=30:S2S=0
'****************************************************************
;************ Main Program Loop *************
'****************************************************************
Main:
ClockLoop: IF intcon.2=0 THEN ClockLoop 'Poll for TMRO overflow
INTCON.2=0 ' Clear TMRO overflow flag
HzTimer=HzTimer-$1000 'decrement timer count
IF HzTimer < $1000 THEN
IF Col=10 THEN ' update time'
SS=SS+1
IF SS=60 THEN ' minute
SS=0
MM=MM+1
IF MM=60 THEN ' hour
MM=0
HH=HH+1
IF HH=24 THEN HH=0
ENDIF
counter1 = (HH*60)+MM
ENDIF
ENDIF
Col=Col+1
HzTimer=HzTimer+$7A12 ' Add 0.5 seconds to count
ELSE
' Do something here but must be less than 65.5 mSec
ENDIF
if Col=11 then
Col=1
endif
LCDOut $FE,$D4,#HH DIG 1,#HH DIG 0,":",#MM DIG 1,#MM DIG 0,":",#SS DIG 1,#SS Dig 0
If SetButton=0 then ; jump to programming
goto mainmenu
endif
If S1H=HH and S1M=MM and S1S=SS then
B_Fade_in = 5
lcdout $FE,$80+13,"DAWN "
B_PWM=B_PWM+1
If B_PWM=B_MAX then
lcdout $FE,$80+13,"DAY "
endif
If Counter1 =set2 then
B_PWM = B_PWM-1
lcdout $FE,$80+13,"DUSK "
endif
Endif
hpwm 1,W_PWM,200
hpwm 2,B_PWM,200
If (B_PWM * 100)/255 = 100 then
lcdout $FE,$80,"BLUES ",dec3 (B_PWM *100)/255,"%"
endif
If (B_PWM * 100)/255 =0 or (B_PWM * 100)/255 <10 then
lcdout $FE,$80,"BLUES ",dec1 (B_PWM *100)/255,"% "
endif
if (B_PWM * 100)/255 >=10 and (B_PWM * 100)/255 <100 then
lcdout $FE,$80,"BLUES ",dec2 (B_PWM *100)/255,"% "
endif
If (W_PWM*100)/255 >= 100 then
lcdout $FE,$C0,"WHITES ",dec3 (W_PWM *100)/255,"%"
endif
If (W_PWM * 100)/255 <=0 or (W_PWM * 100)/255 <10 then
lcdout $FE,$C0,"WHITES ",dec1 (W_PWM *100)/255,"% "
endif
if (W_PWM * 100)/255 >=10 AND (W_PWM * 100)/255 <100 then
lcdout $FE,$C0,"WHITES ",dec2 (W_PWM *100)/255,"% "
endif
'Get and display the temperature
OWOUT DQ, 1, [$CC, $44] ' Start temperature conversion
OWOUT DQ, 1, [$CC, $BE] ' Read the temperature
OWIN DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]
temperature = temperature */ 1600
lcdout $FE,$D4+11,"TEMP ",DEC(temperature / 100),$DF,"C"
GOTO Main
Bookmarks