Captain Haddock
Senior Member
I've been playing with using infra red remote to change the settings in a RTC and got this code working quite nicely but could do with some pointers to reduce it as it's quite big, appologies for lack of comments and poor layout, I'm sure it could be done much smaller but not by me
It currently running on a 40x2 as it happens to be on my breadboard, IR is on D.1, LCD is on A.0, power button on remote opens settings menu, arrows choose day and number buttons set date & time.
Any pointers greatly appreciated.
This is modified from code I was using for a keypad so variable choices will probably make no sense at all.
It currently running on a 40x2 as it happens to be on my breadboard, IR is on D.1, LCD is on A.0, power button on remote opens settings menu, arrows choose day and number buttons set date & time.
Any pointers greatly appreciated.
This is modified from code I was using for a keypad so variable choices will probably make no sense at all.
Code:
#Picaxe 40X2
#No_Data
Symbol Baud = N2400_16
Symbol Lcd = A.0
Symbol keyPress = b24 'raw keypress code
Symbol lastKeyPress = b25 'resolved key number
hi2csetup i2cmaster, %11010000,i2cslow,i2cbyte
; ===================================================
SetFreq M16
serout Lcd,Baud,(254,1)
main:
do
hi2cin 0, (b0,b1,b2,b3,b4,b5,b6)
pause 100
bcdtoascii b0,b19,b20 'Secs Convert to ASCII
bcdtoascii b1,b8,b9 'Mins
bcdtoascii b2,b10,b11 'Hours
bcdtoascii b3,b21,b22 'DayOfWeek;
bcdtoascii b4,b12,b13 'Date
bcdtoascii b5,b14,b15 'Month
bcdtoascii b6,b16,b17 'Year
serout Lcd,Baud,(254,132)
serout Lcd,Baud,(b10,b11, ":",b8,b9,":",b19,b20)
serout Lcd,Baud,(254,200)
serout Lcd,Baud,(b12,b13,"/",b14,b15,"/",b16,b17)
If b22="1" then serout Lcd,Baud,(254,192,"Sun ")
elseif b22="2" then serout Lcd,Baud,(254,192,"Mon ")
elseif b22="3" then serout Lcd,Baud,(254,192,"Tue ")
elseif b22="4" then serout Lcd,Baud,(254,192,"Wed ")
elseif b22="5" then serout Lcd,Baud,(254,192,"Thur")
elseif b22="6" then serout Lcd,Baud,(254,192,"Fri ")
elseif b22="7" then serout Lcd,Baud,(254,192,"Sat ")
endIf
gosub readkeypress
if lastkeypress = 21 then gosub menu
pause 500
loop
goto main
menu: let lastkeypress = 128
pause 1000
serout Lcd,Baud,(254,1,254,128,"Time Date Day",254,192," < > ^")
do
Gosub ReadKeyPress
if lastkeypress = 17 then gosub settime
if lastkeypress = 16 then gosub setdate
if lastkeypress = 13 then gosub day
if lastkeypress = 21 then serout Lcd,Baud,(254,1):let lastkeypress = 128:goto main:endif
pause 870
loop while keyPress = 128
settime:let lastkeypress = 0
pause 1000
hour: serout Lcd,Baud,(254,1,254,128,"Set time (hh:mm)",254,200,"00:00",254,200,254,13)
do
Gosub ReadKeyPress : let b4 = lastkeypress
loop while keyPress = 128
pause 1000
if b4 = 21 then serout Lcd,Baud,(254,1):let lastkeypress = 128:goto main:endif
serout Lcd,Baud,(254,200,#b4,"0:00",254,201)
if b4 > 2 then goto hour
do
Gosub ReadKeyPress : let b5 = lastkeypress
loop while keyPress = 128
pause 1000
serout Lcd,Baud,(254,200,#b4,#b5,":00",254,203)
let b6 = b4*10 + b5
if b6 > 23 then goto hour
minute:do
Gosub ReadKeyPress : let b7 = lastkeypress
loop while keyPress = 128
pause 1000
serout Lcd,Baud,(254,200,#b4,#b5,":",#b7,"0",254,204)
if b7 > 5 then goto minute
do
Gosub ReadKeyPress : let b8 = lastkeypress
loop while keyPress = 128
pause 1000
let b9 = b7*10+b8
if b9 > 59 then goto minute
let b9 = b7*16+b8
let b6 = b4*16+b5
hi2cout 0, ($00, b9, b6)
pause 1000
serout Lcd,Baud,(254,1,254,12):goto main
setdate:let lastkeypress = 0
pause 1000
date: serout Lcd,Baud,(254,1,254,128,"Set date (d/m/y)",254,200,"00/00/00",254,13,254,200)
do
Gosub ReadKeyPress : let b4 = lastkeypress
loop while keyPress = 128
pause 1000
if b4 = 21 then serout Lcd,Baud,(254,1):let lastkeypress = 128:goto main:endif
serout Lcd,Baud,(254,200,#b4,"0","/","00","/","00",254,201)
if b4 > 3 then goto date
do
Gosub ReadKeyPress : let b5 = lastkeypress
loop while keyPress = 128
pause 1000
serout Lcd,Baud,(254,200,#b4,#b5,"/","00","/","00",254,203)
let b6 = b4*10 + b5
if b6 > 31 then goto date
month:do
Gosub ReadKeyPress : let b7 = lastkeypress
loop while keyPress = 128
pause 1000
serout Lcd,Baud,(254,200,#b4,#b5,"/",#b7,"0","/","00",254,204)
if b7 > 1 then goto month
do
Gosub ReadKeyPress : let b8 = lastkeypress
loop while keyPress = 128
pause 1000
serout Lcd,Baud,(254,200,#b4,#b5,"/",#b7,#b8,"/","00",254,206)
let b9 = b7*10+b8
if b9 > 12 then goto month
year: do
Gosub ReadKeyPress : let b10 = lastkeypress
loop while keyPress = 128
pause 1000
serout Lcd,Baud,(254,200,#b4,#b5,"/",#b7,#b8,"/",#b10,"0",254,207)
if b10 > 2 then goto year
do
Gosub ReadKeyPress : let b11 = lastkeypress
loop while keyPress = 128
pause 1000
let b12 = b10*16+b11
let b9 = b7*16+b8
let b6 = b4*16+b5
hi2cout 04, (b6, b9,b12)
serout Lcd,Baud,(254,1,254,12):goto main
day:
let lastkeypress = 0
serout Lcd,Baud,(254,1,254,128,"Set Day",254,192,"<dec Sun inc>")
let b7 = 1
do
Gosub ReadKeyPress
if lastkeypress = 17 then dec b7:let lastkeypress = 0:endif
if lastkeypress = 16 then inc b7:let lastkeypress = 0:endif
if b7 > 7 then let b7 = 1:endif
if b7 < 1 then let b7 = 7:endif
If b7= 1 then serout Lcd,Baud,(254,198,"Sun "):let b8 = b7:endif
if b7= 2 then serout Lcd,Baud,(254,198,"Mon "):let b8 = b7:endif
if b7= 3 then serout Lcd,Baud,(254,198,"Tue "):let b8 = b7:endif
if b7= 4 then serout Lcd,Baud,(254,198,"Wed "):let b8 = b7:endif
if b7= 5 then serout Lcd,Baud,(254,198,"Thur"):let b8 = b7:endif
if b7= 6 then serout Lcd,Baud,(254,198,"Fri "):let b8 = b7:endif
if b7= 7 then serout Lcd,Baud,(254,198,"Sat "):let b8 = b7:endif
if lastkeypress = 73 then exit
pause 200
loop
hi2cout 03, (b8)
serout Lcd,Baud,(254,1):goto main
ReadKeyPress:
let keypress = 128
irin [500],D.1,keypress
if keypress = 0 then let lastkeypress = 1 : endif;1
if keypress = 1 then let lastkeypress = 2 : endif;2
if keypress = 2 then let lastkeypress = 3 : endif;3
if keypress = 3 then let lastkeypress = 4 : endif;4
if keypress = 4 then let lastkeypress = 5 : endif;5
if keypress = 5 then let lastkeypress = 6 : endif;6
if keypress = 6 then let lastkeypress = 7 : endif;7
if keypress = 7 then let lastkeypress = 8 : endif;8
if keypress = 8 then let lastkeypress = 9 : endif;9
if keypress = 9 then let lastkeypress = 0 : endif;0
if keypress = 13 then let lastkeypress = 13 : endif;A
if keypress = 16 then let lastkeypress = 16 : endif;B
if keypress = 72 then let lastkeypress = 72 : endif;C
if keypress = 17 then let lastkeypress = 17 : endif;D
if keypress = 73 then let lastkeypress = 73 : endif;*
if keypress = 21 then let lastkeypress = 21 : endif;#
Return