Code:
'****************************************************************
'* Name : Nixie_DST_InstantInt.BAS *
'* Author : [Dwight Merkley, N7KBC] *
'* Notice : Copyright (c) 2016 *
'* : All Rights Reserved *
'* Date : 3/10/2016 *
'* Version : 1.0 *
'* Notes : 16F1828 *
'* : *
'****************************************************************
#CONFIG
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_25 & _LVP_OFF
#endconfig
;-- Copied from DT_INTS-14 as per instruction in DT_INTS-14 -------------
;-- The compiler will tell you which lines to un-comment --
;-- Do Not un-comment these lines --
;---------------------------------------------------------------------------
;wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
wsave VAR BYTE $70 SYSTEM ' alternate save location for W
' if using $70, comment wsave1-3
' --- IF any of these three lines cause an error ?? ------------------------
' Comment them out to fix the problem ----
' -- Which variables are needed, depends on the Chip you are using --
;wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
;wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
;wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3
' --------------------------------------------------------------------------
define OSC 8
OSCCON = %01110000 'set internal osc to 8 mhz
include "ALLDIGITAL.Pbp"
include "DT_INTS-14.bas" 'Needs to be in same PBP folder as PBPW.EXE
include "ReEnterPBP.bas" 'Needs to be in same PBP folder as PBPW.EXE
include "ElapTimer_5ms_Interrupt.pbp" 'Needs to be in same PBP folder as PBPW.EXE
'==================================================
H10 VAR PortC.0 'hours tens digit enable
H1 var PortC.1 'hours ones digit enable
M10 var PortC.2 'minutes tens digit enable
M1 var PortC.3 'minutes ones digit enable
symbol PORT_PIN = PORTC 'used to sequentially enable portC.0-3 nixie tubes
res var PortC.4 '0-9 digit reset
clk var PortC.6 '0-9 digit counter input
ena var PortC.7 'ENA on nixie decade counter
led var LATC.5 'pwm led backlight
sw1 var portA.2 'input upper switch
sw2 var portA.1 'input lower corner switch
swflg1 var bit 'bit used to debounce switch pushes
swflg2 var bit 'bit used to debounce switch pushes
ts var portB.4 'OneWire Temperature sensor
rxE var portB.5 'input rx data from esp8266
txE var portB.7 'tx data to esp8266
fet var portB.6 'High Voltage shutdown
tube var byte 'refrence tube 0-3 [hh h mm m]
'hh var byte 'variable holding hours
h12 var byte
'mm var byte 'variable holding minutes
'ss var byte 'variable holding seconds
d1 var byte
d2 var byte
d3 var byte
d4 var byte
s1 var byte
s0 var byte
dow var byte 'sun = 0, sat = 6
mth var byte 'Jan = 01 ... Dec = 12
date var byte 'numerical day of month
dst var bit
mode var byte '12 to 24 hour conversion flag'
num var byte 'current digit's pulse count
x var byte 'general purpose variable
y var byte 'counter to track time between esp serial request output
dgt var byte
temp var byte 'temporary lcd display data
IntCnt var word
IntFlag var bit
dispflag var bit 'flag to indicate if display should be off
temptime var byte 'var to determine if time to check temperature
'the following var's are used in the anti-cathode poisoning routine
posx var byte
numx var byte
cath var byte
index var byte
CatCnt var word
CC var bit
'-------------[variables and constants for temperature routine]--------------
''DS18B20_9bit CON %00011111 ' set resolution on DS18B20 93.75ms, 0.5 C
''DQ VAR PortB.4 ' One-wire Data-Pin "DQ" on PortB.4
''Busy VAR BIT ' Busy Status-Bit
''Raw VAR WORD ' RAW Temperature readings
''TempF VAR WORD ' Temp in deg F
''Cold_Bit VAR Raw.BIT11 ' Sign-Bit for +/- Temp. 1 = Below 0 deg C
''Sign VAR BYTE[1] ' +/- sign for temp display
''TempH var word ' var for highest temp
''TempL var word ' var for lowest temp
''SignH var byte[1]
''SignL var byte[1]
''C VAR WORD ' Celsius
''F VAR WORD ' Fahrenheit
''INCLUDE "Temp_Convert.pbp"
'===================================================
TRISA = %00000110
TRISB = %00100000
TRISC = %00000000
WPUA = %00000110
'====================================================
espbaud con 84
Hours = 99
Minutes = 00
Seconds = 00
mode = 2 'modes
'0=24 hour only display
'1=12 hour only display
'2=24 hour with alternating 12 hour
' for dummies who don't know 24 hour time
swflg1=0 'flag used to debounce sw1 press
swflg2=0 'flag used to debounce sw1 press
CC = 0
numx=0
IntCnt = 1
temp = 0
'================================================
' Define and Create the Instant Interrupt for Timer1
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ClockCount, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
ENDASM
Gosub ResetTime ' Reset Time to 0d-00:00:00.000
Gosub StartTimer ' Start the Elapsed Timer
Hours = 99
txe=1 'high the serial output pin, needs to start high
pause 1000
']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Main:
if swflg1 = 1 and sw1=1 then swflg1 = 0 '(sw debounce) clear swflg when switch released
if swflg1 = 0 and sw1=0 then 'sw1 button detected
swflg1 = 1
mode=mode+1
if mode = 3 then mode = 0
endif
if sw2=0 then gosub gettime 'sw2 button detected
'an interrupt is generated every .005 sec
'so clear the IntFlag and cycle to the next nixie tube
if MSecsChanged = 1 then
MSecsChanged = 0
catcnt = catcnt + 1
gosub Tubes
endif
iF SecondsChanged = 1 then
SecondsChanged = 0
gosub NextSec
endif
'this next routine fires every 15 minutes between 4 and 7 seconds
'it causes all digits in each nixie to be cycled to prevent
'cathode poisioning
if (Hours !=99 and Minutes//15=0) and (Seconds >4 and Seconds<11) then
cc=1
else
cc=0
endif
if catcnt>19 and cc=1 then gosub CatClean
if cc=0 then catcnt=0
'this next routine blanks the display between 2200 and 0600
if (Hours>21) or (Hours<6) then 'turn display off between 2200 and 0600
dispflag = 0
led=1 'turn off the LED backlight
else
dispflag = 1
endif
'this routine only runs on startup and waits for 30 sec
'then it goes and reads the time from the ESP module
if Hours = 99 then
d1=Seconds//10
d2=Seconds/10
d3=0
d4=0
dispflag = 1
if Seconds=40 then gosub GetTime
endif
'this routine sets the clock once a day at 0500
if Hours=5 and Minutes=0 and Seconds=0 then gosub GetTime
if dispflag =1 then toggle led 'for backlight brigtness at 50%
' can use O-Scope to determine Main loop timing
goto Main
'=============================================================
' increment seconds and trickle down time
'=============================================================
NextSec:
if cc=1 then SkipSec
gosub dstcalc
if Hours > 12 then
h12=Hours - 12
else
h12 = Hours
endif
d1=Minutes//10
d2=Minutes/10
d3=Hours//10
d4=Hours/10
s1=Seconds/10
s0=Seconds//10
if mode=1 then '12 hr only mode
d3=h12//10
d4=h12/10
endif
if mode = 2 and (s0=0 or s0=5) then '24 hr mode with
d3=h12//10 '12 hr overlay at 0 & 5 sec
d4=h12/10
endif
SkipSec:
Return
'=================================================
'--- Routine to cycle all digits on all tubes ---
'--- to prevent cathode poisoning (look it up) ---
'=================================================
CatClean:
catcnt=0
if Hours=99 then
d1=Minutes//10
d2=Minutes/10
goto SkipClean
endif
numx=numx + 1
if numx >9 then numx=0
for posx=0 to 3
index = numx + posx
lookup index,[1,6,2,7,5,0,4,9,8,3,1,6,2,7,5,0,4,9,8,3],cath
if posx = 0 then d1=cath
if posx = 1 then d2=cath
if posx = 2 then d3=cath
if posx = 3 then d4=cath
next posx
SkipClean:
return
'=============================================================
' display next NIXIE digit
'=============================================================
Tubes:
PORT_PIN.0[tube] = 0 'Turn OFF the current tube
if dispflag = 0 then Skipdisp
tube=tube + 1
if tube > 3 then tube = 0
Digit:
high res 'reset the decade counter
@ nop
low res 'release the reset
lookup2 tube,[d4,d3,d2,d1],num
for x = 1 to num 'pulse the clock for "num" counts
high clk
@ nop
low clk
next x
PORT_PIN.0[tube] = 1 'Turn ON the next tube
SkipDisp:
return
'=============================================================
' Get TIME from ESP8266-01 NTP Time Server
'=============================================================
SerTmout:
led=0
pause 200
led=1
pause 200
led=0
pause 200
led=1
pause 200
GetTime:
@ INT_DISABLE TMR1_INT ; Disable Timer 1 Interrupts
PORT_PIN.0[tube] = 0 'Turn OFF the current tube
' Wait for >,CR,LF from ESP module
serin2 rxe,espbaud,2000,SerTmout,[wait(">"),wait(13),wait(10)]
' ESP module expects "Time?" before it will give hhmmss
' to change this modify the ESP code
serout2 txe,espbaud,["Time?"]
serin2 rxe,espbaud,3000,SerTmout,_
[dec1 dow,wait("/"),dec2 mth,wait("/"),dec2 date,wait("/"),_
dec2 Hours,dec2 Minutes,dec2 Seconds]
gosub dstcalc 'check to see if DST is in effect
if dst=1 then Hours = Hours + 1
if Hours = 24 then Hours = 00
' set NIXIE tube positions for their
' respective h h m m, d4,d3,d2,d1
d4=Hours/10
d3=Hours//10
d2=Minutes/10
d1=Minutes//10
s1=Seconds/10 ' s0 & s1 only gets used when displaying seconds
s0=Seconds//10 ' untill time retreval from ESP module at 30 sec
@ INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
Return
'=============================================================
' Read Temp Sensor and convert to deg F
'=============================================================
''GetTemp:
'' PORT_PIN.0[tube] = 0 'Turn OFF the current tube
'' OWOUT DQ,%001,[$CC, $4E, 0, 0, DS18B20_9bit] 'set resolution of sensor
''Start_Convert:
'' OWOUT DQ,%001,[$CC, $44] ' Skip ROM search & do temp conversion
''Wait_Convert:
'' OWIN DQ,%100,[Busy] ' Read busy-bit
'' IF Busy = 0 THEN Wait_Convert ' Still busy..?, Wait_Convert..!
'' OWOUT DQ,%001,[$CC, $BE] ' Skip ROM search & read scratchpad memory
'' OWIN DQ,%010,[Raw.byte0, Raw.byte1]' Read two bytes / end comms
'' '-------------- Convert_Temp: -----------------------
'' Sign="+"
'' IF Cold_Bit = 1 THEN 'it's below zero Celsius
'' C=(ABS Raw>>4)*-10 'so shift the ABS value right and mult X -10
'' ELSE
'' C=(Raw>>4)*10 'else shift value right and mult X 10
'' ENDIF
''@ CtoF _C, _F ; Convert Celsius to Fahrenheit
'' 'converted value will be X 10, ie. 756=75.6 deg F
'' 'so devide by 10 to get whole degrees
'' IF f.15=1 THEN Sign="-" 'if converted value is below zero F then sign is "-"
'' TempF = (ABS f)/10 'take tha ABS value and /10
'' IF f//10 >4 THEN TempF=TempF+1 'check remainder, if >4 then round up
'' PIR1.0 = 0 ' clear timer1 overflow flag
''return 'with TempF containing current temperature
'================================================
'--- DST Calc (dec1 dow,dec2 mth,dec2 date) ---
'================================================
dstcalc:
' if dayschanged = 1 then
' dayschanged = 0
' dow = dow+1
' if dow >7 then dow=0
' endif
dst = 1 'covers Apr, May, Jun, Jul, Aug, Sep, Oct
If mth=01 then dst = 0 'Jan
If mth=02 then dst = 0 'Feb
If mth=12 then dst = 0 'Dec
March:
if mth=03 then 'March
if date <8 then
dst=0
elseif (date>7 and date<15) and dow=1 and hours<2 then
dst=0
elseif (date=8 and dow>1) then
dst=0
elseif (date=9 and dow>2) then
dst=0
elseif (date=10 and dow>3) then
dst=0
elseif (date=11 and dow>4) then
dst=0
elseif (date=12 and dow>5) then
dst=0
elseif (date=13 and dow>6) then
dst=0
endif
endif
November:
if mth=11 then 'November
if date >7 then
dst=0
elseif (date <8 and dow=1) and hours>1 then
dst=0
elseif (date=2 and dow<3) then
dst=0
elseif (date=3 and dow<4) then
dst=0
elseif (date=4 and dow<5) then
dst=0
elseif (date=5 and dow<6) then
dst=0
elseif (date=6 and dow<7) then
dst=0
endif
endif
return
'===========================================
end
'===========================================================
'--- This is the code running on the ESP8266-01 module ---
'===========================================================
'''memclear
'''cls
'''timesetup(-7,0)
'''baudrate 9600
'''serialtimeout 2000
'''delay 1000
'''button "Exit" [Exit]
'''timer 100 [PicSer]
'''wait
''''
'''[PicSer]
'''serialflush
'''input picin
'''picin = mid(picin,1,5)
'''if picin == "Time?" then gosub [gettime]
'''wait
''''
'''[gettime]
'''bla = time()
'''dy = mid(bla,1,3) 'dow
'''mh = mid(bla,5,3) 'month
'''dd = mid(bla,9,2) 'date
'''hh = mid(bla,12,2) 'hour
'''mm = mid(bla,15,2) 'min
'''ss = mid(bla,18,2) 'sec
'''yr = mid(bla,21,4) 'year
''''
'''if dy == "Sun" then dow = "1/"
'''if dy == "Mon" then dow = "2/"
'''if dy == "Tue" then dow = "3/"
'''if dy == "Wed" then dow = "4/"
'''if dy == "Thu" then dow = "5/"
'''if dy == "Fri" then dow = "6/"
'''if dy == "Sat" then dow = "7/"
''''
'''if mh == "Jan" then mth = "01/"
'''if mh == "Feb" then mth = "02/"
'''if mh == "Mar" then mth = "03/"
'''if mh == "Apr" then mth = "04/"
'''if mh == "May" then mth = "05/"
'''if mh == "Jun" then mth = "06/"
'''if mh == "Jul" then mth = "07/"
'''if mh == "Aug" then mth = "08/"
'''if mh == "Sep" then mth = "09/"
'''if mh == "Oct" then mth = "10/"
'''if mh == "Nov" then mth = "11/"
'''if mh == "Dec" then mth = "12/"
''''
'''picout = dow & mth
'''picout = picout & dd
'''picout = picout & "/"
'''picout = picout & hh
'''picout = picout & mm
'''picout = picout & ss
''''
'''serialprintln picout
'''return
''''
'''[Exit]
'''end
Bookmarks