'************************************************* ***************
'* Name : RTC+2TM.BAS *
'* Author : Denis Kuznetsov *
'* Notice : Copyright (c) 2003 CopyLeft :) *
'* : All Rights Reserved *
'* Date : 23.11.2003 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'Include "MODEDEFS.BAS"
include "fp20.bas"
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 0
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
DS_SCL VAR PORTC.6 ' I2C clock pin
DS_SDA VAR PORTC.7 ' I2C data pin
RTC CON %11010000 ' RTC device address (byte addressing)
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2
pr CON $14
blink CON $0F
pos var byte
pos_end var byte
button1 VAR PORTC.0
button2 VAR PORTC.1
button3 VAR PORTC.2
beep var PORTC.3
SecReg CON $00 ' seconds address (00 - 59)
' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)
ContReg CON $07 ' control register
RTCflag CON 0 ' RTC flag (location 0 of internal EEPROM)
RTCset VAR BIT ' bit to check if RTC has been set
cntrl CON %00010000 ' sets the SQW/OUT to 1Hz pulse, logic level low
' The variable below holds the values entered:
' entered by the user
_sec VAR BYTE ' $00 set seconds
_min VAR BYTE ' $56 set minutes
_hr VAR BYTE ' $23 set hour
_day VAR BYTE ' $06 set day
_date VAR BYTE ' $28 set date
_mon VAR BYTE ' $02 set month
_yr VAR BYTE ' $03 set year
Set var byte(8)
sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year
hr_A var byte
min_A var byte
on_A var byte
off_A var byte
test_A var byte
ps var byte
tm var word
B1 VAR BYTE 'Working buffer 1 for button command
B2 VAR BYTE 'Working buffer 2 for button command
B3 VAR BYTE 'Working buffer 3 for button command
n1 var byte
n2 var byte
n3 var byte
m1 var byte
m2 var byte
m3 var byte
d1 var byte
d2 var byte
d3 var byte
c var byte ' Debounce loop variable
mode var byte '0 - часы, 1 - календарь, 2 - температура
test var byte
cnt var byte
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 500 ' Set sampling time in uS
adval0 var word ' Create adval0 to store result
adval1 var word ' Create adval1 to store result
rez00 VAR word
rez10 VAR word
rez01 VAR word
rez11 VAR word
rez0 VAR word
rez1 VAR word
rez000 VAR word
rez111 VAR word
fpplaces var byte ' Used to define number of decimal places in fpdisplay,fpdisplayr
ahold var word ' Required by fpdisplay
bhold var word ' Required by fpdisplay
resulthold var word ' Used to store results of previous calculations
TRISC = %10000001
TRISA = %11111011
ADCON1 = %10000101
Pause 1000
LCDOut I,Clr
mode = 0
ps = 0
tm = 0
test_A = 0
off_A = 0
read 0,min_A
read 1,hr_A
read 2,on_A
sound beep,[100,10]
Loop:
pause 30
I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
if hr_A = hr and min_A = MINs then
test_A = 1
else
test_A = 0
off_A = 0
endif
select case mon
case 1
m1 = $B1 : m2 = $BD : m3 = $B3
case 2
m1 = $AA : m2 = $65 : m3 = $B3
case 3
m1 = $4D : m2 = $61 : m3 = $70
case 4
m1 = $41 : m2 = $BE : m3 = $70
case 5
m1 = $4D : m2 = $61 : m3 = $B9
case 6
m1 = $A5 : m2 = $C6 : m3 = $BD
case 7
m1 = $A5 : m2 = $C6 : m3 = $BB
case 8
m1 = $41 : m2 = $B3 : m3 = $B4
case 9
m1 = $43 : m2 = $65 : m3 = $BD
case 16
m1 = $4F : m2 = $BA : m3 = $BF
case 17
m1 = $48 : m2 = $6F : m3 = $C7
case 18
m1 = $E0 : m2 = $65 : m3 = $BA
end select
select case day
case 1
d1 = $A8 : d2 = $BD : d3 = $E3
case 2
d1 = $42 : d2 = $BF : d3 = $BD
case 3
d1 = $43 : d2 = $70 : d3 = $E3
case 4
d1 = $AB : d2 = $BF : d3 = $B3
case 5
d1 = $A8 : d2 = $BF : d3 = $BD
case 6
d1 = $43 : d2 = $B2 : d3 = $BF
case 7
d1 = $42 : d2 = $63 : d3 = $BA
end select
if on_A = 1 and off_A = 0 and test_A = 1 then
sound beep,[120,20,110,20,100,20,90,20]
Button button1,0,100,0,B1,0,Loop
off_A = 1
goto Loop
endif
next_c: SELECT CASE mode
CASE 0
gosub mode0
CASE 1
goto mode1
CASE 2
gosub mode2
CASE 3
gosub mode3
END SELECT
kb:
Button button1,0,100,0,B1,0,chk_mode
mode = mode + 1
tm = 0
if mode › 3 then
mode = 0
endif
goto Loop
chk_mode:
Button button2,0,100,5,B2,0,chk_no
if B2 = 5 then
tm = 0
test = 0
goto _set
endif
chk_no:
ps = ps + 1
tm = tm + 1
if tm › 500 then
tm = 0
mode = 0
goto Loop
endif
if ps › 50 then
ps = 0
goto Loop
endif
Pause 20
GoTo kb
_set:
SELECT CASE mode
CASE 0
Set[1] = hr / 16
Set[2] = hr // 16
Set[3] = MINs / 16
Set[4] = MINs // 16
Set[5] = 0
pos_end = 4
pos = 1
gosub modeT
goto setT
CASE 2
Set[1] = date / 16
Set[2] = date // 16
Set[3] = mon / 16
Set[4] = mon // 16
Set[5] = yr / 16
Set[6] = yr // 16
Set[7] = day
pos_end = 7
pos = 1
gosub modeD
goto setT
CASE 3
read 0,min_A
read 1,hr_A
read 2,on_A
Set[1] = hr_A / 16
Set[2] = hr_A // 16
Set[3] = min_A / 16
Set[4] = min_A // 16
Set[5] = 0
pos_end = 5
pos = 1
gosub modeA
goto setT
case else
goto Loop
END SELECT
'################################################# ###
setT:
tmp1t:
tm = tm + 1
if tm › 1500 then
tm = 0
mode = 0
goto Loop
endif
pause 25
if pos = 1 then
LCDOut I,Line1,I,blink
else
LCDOut I,Line1,I,2
for cnt = 1 to pos - 1
LCDOut I,pr,I,blink
next cnt
endif
Button button2,0,100,5,B2,0,chk_b3t
test = 1
tm = 0
pos = pos + 1
if pos › pos_end then
pos = 1
goto tmp1t
endif
chk_b3t:
Button button3,0,100,5,B3,0,chk_b1t
test = 1
tm = 0
Set[pos] = Set[pos] + 1
if Set[1] › 9 then
Set[1] = 0
endif
if Set[2] › 9 then
Set[2] = 0
endif
if Set[3] › 9 then
Set[3] = 0
endif
if Set[4] › 9 then
Set[4] = 0
endif
if Set[5] › 9 then
Set[5] = 0
endif
if Set[6] › 9 then
Set[6] = 0
endif
if Set[7] › 9 then
Set[7] = 0
endif
LCDOut Set[pos]+48
chk_b1t:
if test = 0 then
goto tmp1t
endif
tmp2t:
Button button1,0,100,5,B1,0,tmp1t
tm = 0
I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
if mode = 0 then
_yr = yr
_mon = mon
_date = date
_hr = Set[1] * 16 + Set[2]
_sec = $00
_min = Set[3] * 16 + Set[4]
_day = day
I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[_sec,_min,_hr,_day,_date,_mon,_yr,cntrl]
goto exit
endif
if mode = 2 then
_yr = Set[5] * 16 + Set[6]
_mon = Set[3] * 16 + Set[4]
_date = Set[1] * 16 + Set[2]
_hr = hr
_sec = sec
_min = MINs
_day = Set[7]
I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[_sec,_min,_hr,_day,_date,_mon,_yr,cntrl]
goto exit
endif
if mode = 3 then
hr_A = Set[1] * 16 + Set[2]
min_A = Set[3] * 16 + Set[4]
on_A = Set[5]
write 0,min_A
write 1,hr_A
write 2,on_A
goto exit
endif
exit: Pause 5
goto Loop
'################################################# ####
'################################################# ###
mode0:
LCDOut I,Clr' Clear Lcd
LCDOut I,Line1, HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
LCDOut I,Line2, " ", m1, m2, m3,", ", HEX2 date
return
mode1:
rez000 = 0
rez111 = 0
for c = 1 to 20
ADCIN 0, adval0 ' Read channel 0 to adval0
ADCIN 1, adval1 ' Read channel 1 to adval1
rez000 = rez000 + adval0
rez111 = rez111 + adval1
pause 10
next c
rez0 = rez000 / 20
rez1 = rez111 / 20
aint = 1875
gosub itofa
bint = 1023
gosub itofb
gosub fpdiv
bint = rez0
gosub itofb
gosub fpmul
bint = 500
gosub itofb
gosub fpsub
bint = 10
gosub itofb
gosub fpdiv
Lcdout $fe, 1
Gosub fpdisplayr
lcdout $FE,$C0," "
aint = 1875
gosub itofa
bint = 1023
gosub itofb
gosub fpdiv
bint = rez1
gosub itofb
gosub fpmul
bint = 500
gosub itofb
gosub fpsub
bint = 10
gosub itofb
gosub fpdiv
Gosub fpdisplayr
goto kb
mode2:
LCDOut I,Clr' Clear Lcd
LCDOut I,Line1, " ", d1,d2,d3," ", HEX2 date, " "
LCDOut I,Line2, m1, m2, m3," 20", HEX2 yr
return
mode3:
LCDOut I,Clr' Clear Lcd
read 0,min_A
read 1,hr_A
read 2,on_A
LCDOut I,Line1, " ", HEX2 hr_A, ":", HEX2 min_A
if on_A = 1 then
LCDOut I,Line2,$A0,$79,$E3,$D0,$42,$BA,$BB
else
LCDOut I,Line2,$A0,$79,$E3,$D0,$42,$C3,$BA,$BB
endif
return
modeT:
LCDOut I,Clr' Clear Lcd
LCDOut I,Line1,HEX2 hr,HEX2 MINs
LCDOut I,Line2, $A9,$63,$BF,$D0,$42,$70,$65,$BC
return
modeD:
LCDOut I,Clr' Clear Lcd
LCDOut I,Line1, HEX2 date, hex2 mon, hex2 yr, Hex day
LCDOut I,Line2, $A9,$63,$BF,$D0,$E0,$61,$BF,$61
return
modeA:
LCDOut I,Clr' Clear Lcd
LCDOut I,Line1, HEX2 hr_A, hex2 min_A, hex1 on_A
LCDOut I,Line2, $A9,$63,$BF,$D0,$A0,$79,$E3,$BA
return
fpdisplayr: bexp = $7A
bargb0 = $4C
bargb1 = $CD
If aargb0.7 Then ' Check the floating point sign bit
Gosub fpsub ' If negative, subtract barg from aarg
Else
Gosub fpadd ' If positive, add barg to aarg
Endif
fpdisplay: bexp = aexp ' Store the FP value of aarg to the barg variables
bargb0 = aargb0
bargb1 = aargb1
Gosub ftoia ' Convert aarg to integer
ahold = aint ' Save this value for the final display
Gosub itofa ' Convert integer back to float
Swap aexp,bexp ' Swap the FP values of aarg and barg before subtraction
Swap aargb0,bargb0
Swap aargb1,bargb1
Gosub fpsub ' Subtract the integer portion from the full number
bint = 10 ' Make bint = 10 E fpplaces
bhold = bint ' Save the integer value of bint for zeros loop
Gosub itofb ' Convert bint to integer prior to FP multiply
Gosub fpmul ' Multiply the decimal portion x 10 E fpplaces
Gosub ftoia ' Convert result to aint integer
If (ahold.15) OR (aint.15) Then ' Check for negative integer or decimal
Lcdout "-" ' Display minus
else
Lcdout "+"
Endif
Lcdout dec abs ahold ' Display integer portion
Lcdout "."
zeros:
bhold = bhold / 10 ' Set bhold to be next place to right
If (abs aint ‹ bhold) AND (bhold › 1) Then ' Check for leading zero in decimal
Lcdout "0" ' Display leading zero
Goto zeros ' loop to check for another zero
Endif
Lcdout dec abs aint,$EF,67 ' Display the rest of the decimal portion
aint = ahold ' Restore the original value of aint
Return
End |