;;Driver for JY-LKM1638
;Allows control of 7 segment display and LEDs. Will read all the switch values into
;variables
;
;DS18B20 ---> pin 3 08M2
;
;LKM1638 Input pin 3 (CLK) ---> pin 7 08M2
;LKM1638 Input pin 4 (DIO) ---> pin 6 08M2
;LKM1638 Input pin 5 (STB0) ---> pin 5 08M2
;
;The LKM1638 uses 3 pins to control 8 seven segment displays, 8 bicolour LEDs and read
;the values form 8 pushbuttons.
;This allows a 08M2 to have these all these inputs and outputs available with pins still spare
;
;The LKM1638 module has four basic command types
;1) Write to the display in fixed address format
;2) Write to the display in auto increment address format
;3) Set the display brightness
;4) Read the pushbuttons
;
;These four modes are used in the program below
;The main program performs two activities:
;a)High resolution temperature is read from a DS18B20 and displayed both on the lower and
;upper blocks of 4 digits.
;b)The individual switch positions are read and displayed on the respective LEDs (The LEDs
;display red in this instance as the switch output (1) is merely sent to the LED.
;To display green, the value 2 would have to be sent)
;
;
;
#picaxe 08m2
#no_data
dirsc = %00010111 ;c0, c1, c2, c4 as output
symbol clock = c.0 ;Clock output pin
symbol dio = c.1 ;Data input output pin
symbol strobe = c.2 ;Strobe output pin
symbol tempture = c.4 ;DS18B20 io
symbol s1 = bit16 ;b2
symbol s2 = bit17 ;b2
symbol s3 = bit18 ;b2
symbol s4 = bit19 ;b2
symbol s5 = bit20 ;b2
symbol s6 = bit21 ;b2
symbol s7 = bit22 ;b2
symbol s8 = bit23 ;b2
symbol dataio = b0 ;w0 and bit 0 to bit 7
symbol pad = b1 ;w0 and bit 8 to bit 15
symbol iobuf = w0 ;b0, b1
symbol keys = b2 ;bit16 to bit 23
symbol fixaddr = b3 ;display start address
symbol deci = b4 ;w2
symbol huns = b5 ;w2
symbol tens = b6 ;w3
symbol units = b7 ;w3
symbol hrtemp = w4 ;b8 / b9
symbol sign = b10;w5
symbol whole = b11;w5
symbol char = b12;w6
symbol bank = b13;w7
symbol tmpry = b14;w7
symbol dispbrit = b15;w8
symbol autoaddr = b16;w8
symbol readmode = b17;w9
symbol tmpry2 = b18;w9
fixaddr = $c0
dispbrit = $88
autoaddr = $40
readmode = $42
;
init:
high strobe ;Ensure strobe is initially high
call clearchar ;Clear all characters
;--------------------------------------------------------
main:
readtemp12 tempture,hrtemp ;Read raw value into hrtemp, lower 11 bits have temp, upper 5 have sign
call gethrtemp
bank = 0 ;First block of digits
call convdigit ;Display temperature on first block
call display
bank = 8 ;Second block of 4 digits
call convdigit ;Display temperature on second block
call display
;
call getkeys ;Read the keys state
;
for tmpry2 = 0 to 7 ;Cycle through each LED address
dataio = tmpry2*2 + fixaddr + 1 ;LEDs are at odd addresses 1 to 15
low strobe
call sendchar
lookup tmpry2,(s1,s2,s3,s4,s5,s6,s7,s8),dataio ;Write switch value to respective LED
call sendchar
high strobe
;
dataio = dispbrit ;Display control on, brightness level
low strobe ;Strobe low
call sendchar
high strobe ;Strobe high
next
;
goto main
end
;--------------------------------------------------------
convdigit: ;Convert the whole and decimal parts or temperature into digits for 7 seg display
huns = whole /100
tens = whole //100 /10
units = whole //100 //10 ;Need to append DP to this digit
return
;--------------------------------------------------------
display: ;Displays data on the 7 seg displays, using 2 blocks of 4 digits
;Note: not measuring above 99 deg so do not display hundreds digit
dataio = fixaddr + bank + 0 ;Set sign write address
low strobe ;Strobe low
call sendchar
char = sign ;Lookup the display code for sign
call lookupchar
call sendchar
high strobe ;End of data - Strobe high
;
dataio = fixaddr + bank + 2 ;Set tens write address
low strobe ;Strobe low
call sendchar
char = tens ;Lookup the display code for tens digit
call lookupchar
call sendchar
high strobe ; End of data - Strobe high
;
dataio = fixaddr + bank + 4 ;Set units write address
low strobe ; Strobe low
call sendchar
char = units ;Lookup the display code for units digit
call lookupchar
dataio = dataio or $80 ;Append dec pnt to this digit
call sendchar
high strobe ; End of data - Strobe high
;
dataio = fixaddr + bank + 6 ;Set decimal write address
low strobe ; Strobe low
call sendchar
char = deci ;Lookup the display code for decimal digit
call lookupchar
call sendchar
high strobe ; End of data - Strobe high
;
dataio = dispbrit ;Display brightness level
low strobe ; Strobe low
call sendchar
high strobe ; Strobe high
return
;--------------------------------------------------------
clearchar: ;Clear LEDs and 7 seg displays
dataio = autoaddr ; Data mode auto increment
low strobe ; Strobe low
call sendchar
high strobe ; Strobe high
;
low strobe ; Strobe low
dataio = fixaddr ; Set start address
call sendchar
for tmpry = 1 to $0f ;LEDs and 7 seg displays
dataio = 0 ;Zero blanks the display
call sendchar
next
high strobe ; Strobe high, keep low to end of data
return
;--------------------------------------------------------
sendchar: ;Fundamental routine to send all characters to LKM1638 module serially
pad = $ff ;Set counter
high clock ;Ensure clock is high for pulseout
do
pinc.1 = bit0 ;Make c.1 the value in bit0
iobuf = iobuf/2 ;Shift right
pulsout clock,1 '10us clock pulse
loop Until pad = 0
return
;--------------------------------------------------------
getkeys: ;Reads the input keys in and places them in bits16 to bits23
dataio = readmode ; Data mode read
low strobe
call sendchar
input c.1 ;set c.1 as input
high clock ;Ensure clock is high for pulseout
for tmpry = 1 to 16 ;Read in bits 0-15
bit0 = pinc.1 ;Make bit0 the value on c.1. Need to use c.1 as it is both in & out
iobuf = iobuf*2 ;Shift bit left
pulsout clock,1 ;10us clock pulse, read next bit
next
s6 = bit3 ;Move 1st word switch values out of buffer
s2 = bit7
s5 = bit11
s1 = bit15
for tmpry = 1 to 16 ;Read in bits 16-31
bit0 = pinc.1 ;Make bit0 the value on b.0. Need to use c.1 as it is both in & out
iobuf = iobuf*2 ;Shift bit left
pulsout clock,1 ;10us clock pulse, read next bit
next
s8 = bit3 ;Move 2nd word switch values out of buffer
s4 = bit7
s7 = bit11
s3 = bit15
output c.1 ;Return c.1 to output
high strobe
return
;--------------------------------------------------------
lookupchar: ;Looks up the code to display the digit in 'char' on the 7 seg display
;character ( 0 , 1, 2 , 3 , 4 , 5 , 6 , 7, 8 , 9 , A , b , C , d , E , F , . , - , )
lookup char,($3f,$6,$5b,$4f,$66,$6d,$7d,$7,$7f,$6f,$77,$7c,$39,$5e,$79,$71,$80,$40,$0),dataio
return
;--------------------------------------------------------
gethrtemp: ;Converts the DS18B20 temperature in + and -ve temperatures to 0.1 deg
sign= 18 ; 18 is lookup offset for 'blank', i.e. +ve temp
IF hrtemp>= $800 THEN ; bit 12 is set so below zero
sign= 17 ; 17 is lookup offset for '-'
hrtemp= NOT hrtemp ;Invert each bit (TWO's complement)
hrtemp= hrtemp AND $fff + 1 ;mask off top 4 bits,add 1 for correction (TWO's complement)
ENDIF
hrtemp= hrtemp*10 / 16 ; x10 = 1 decimal(0.1) resolution
whole= hrtemp / 10
deci = hrtemp // 10
return
;--------------------------------------------------------