Driving JY-LKM1638 from 08M2

mjy58

Member
PICAXE driver for JY-LKM1638 multi display module

Originally inspired by this thread http://www.picaxeforum.co.uk/showthread.php?18788-Worth-a-punt I thought the following may be useful for others.

Although not finished regarding the hardware, I thought the code may be useful for others who want to use the JY-LKM1638 module for their own projects http://club.dealextreme.com/forums/Forums.dx/threadid.936370

The JY-LKM1638 module uses serial comms to control all functions and is based on the TM1638 driver chip. The translated datasheet is available here https://docs.google.com/file/d/0B84N2SrJaybwZTgxYjM4ZmEtY2EyZi00YjVjLWIzOTctYTlhMjJkM2MxMTBl/edit

I could not find any code to drive this from a PICAXE so wrote my own serial comms. In this project I have used the module and an 08M2 to build a thermostat:

- Temperature measured and displayed to 0.1 deg C
- Maximum temperature recorded
- Minimum temperature recorded
- Set temperature from 0 - 99 deg C in 0.5 deg increments
- Adjustable brightness
- PICAXE reset from pushbutton

However the thermostat code is all in the Main loop. The subroutines are generic for controlling the JY-LKM1638 and could be incorporated easily for other purposes.

This is my first major PICAXE project and thanks to all the forum contributors on here. A lot of the code is based on forum posts from others. It is based on an 08M2, has used less than 50% program space and left some ordinary variables and an input pin free:)

Notes:
- Because I am using a DS18B20 to record 12bit temperatures and all the communications with the JY-LKM1638 is serial, the response from the pushbuttons appears sluggish at first, but you quickly get used to the delay (sub 0.5 second)
- The thermostat will be driving a mains heater so the output is via a relay, so no PID control here.
- I have only used one JY-LKM1638 module but the code should be easily modified to include STB1 - STB5 signals to drive an additional 5 more displays
- the lookup table only includes digits and a few characters. This could easily be extended to include pseudo characters to display text on the 7 segment displays

View attachment LKM1638 Temperature 1.pdf

View attachment TM1638 thermostat basic code.pdf
 
Last edited:

mjy58

Member
Last edited:

mezzrow

Member
Dear mjy58, your work seems to me as a gift from heaven, because is a big part of a project i'm trying to develope. The temperature control with a
ds18b20, in my plan, is a must, and i need more IN and OUT from the chip.
So i' m trying to interface the lkm1638 to a picaxe 20m2.
I have try it after few modification to your code( like cutting away the code about the pullup on C.3, and use a normal high and low).
Seems to work as it is resetting on button 1, but no led or 7 seg is on.
Everything is, and stay, dark.

Hope you can come up with a suggestion.
Thank you anyway.
Andrea
Excuse my english, really poor.
_____________________________________________________
SORRY, everything is working fine now.

You are great !

Andrea
 
Last edited:

mjy58

Member
I'm glad someone else found the code useful:)

I am in the process of mounting mine in a box and have removed the IDC sockets from the front of the board and replaced them with header connectors on the back. Unfortunately, this changes all the pin outs on the ribbon cable and I have succeeded in frying both my display units:(. More on order.

Are you mounting yours in a box? I am struggling to find a knob or equivalent to press the tactile switches once it is mounted behind a panel.
 

mezzrow

Member
Excuse me Mjy58, i check daily the forum, but just the active part.
I'm going to remove the sockets to... i will be very carefull !
I'm far away from mounting the "THING" in a box, but i will use a very thin, transparent and flexible plastic in front to seal it from the aggressive atmosfere.
In this way i still can clearly see the leds and the 7 seg.
With some printing( on the back of the plastic, or on paper glued to the back ) i will have the indication of what the light leds mean and the button do.
From my experience is possible to swich on the buttons through the plastic, you just have to glue on top of each button a little thing to reach the same high
of the leds and the 7 seg.
Hope to have returned something to you, as you have give a lot to me.
Andrea
 

mjy58

Member
Tactile switch caps

Thanks for the reply Andrea.

I have considered mounting it like you, but I may try and drill a panel and see how I get on.

It is the 'little thing' to glue on top of the button I am struggling to find. I want something cheap and readily available to prevent me having to make 8 of them each time I use the 1638. I have found a Chinese supplier of caps for tactile switches, but his minimum quantity is 1000, which is rather high if they are not suitable.

If I come up with a solution, I will post back.

Malcolm
 

JimPerry

Senior Member
Just trying mine out - applied power to do a "smoke" test and nothing lights up - is this normal? Will connect up Picaxe later today and keep fingers crossed ... :rolleyes:
 

mjy58

Member
Hi Jim,

Yes unfortunately, applying power will yield nothing from the display. That was the most frustrating thing developing the code for this unit. you have to get the protocol and timing right before you actually see anything from the display:(

However, very satisfying when it lights up for the first time:D

Malcolm
 

mjy58

Member
Knobs for the pushbuttons

I ended up using these 'knobs' for my push buttons (SW050)


P1010688.JPG
On the bottom of the knob I do a few turns with a 4mm drill to 'round' off the square so it sits on top of the push button slightly better. To stop the knob coming out the top of the panel I have used a 3.1mm O ring around the base.

P1010686.JPG
This is a picture showing the knobs in situ sitting on the top of the switch and the O ring sitting against the bottom the front panel. To assemble, you put the knobs in the front panel holes first and then mount the LKM1638 behind. For my front panel (a double socket blanking plate!) the knobs protrude about 0.5mm, just enough to provide a positive tactile feedback.
 

russbow

Senior Member
@Malcolm. Any chance you could put the code in code tags please. I cant get it from PDF to ProgEdit without a syntax error on every line.

Got the thing working with you other code, but can't work out the button handling routines.

Thanks,

Russ
 

eclectic

Moderator
@Russ
Unless Malcolm or anyone else can come up with a better idea,

download the pdf
copy and paste to the PE

Then spend a while doing the boring bit:
down arrow and delete keys

Here's a start

Code:
#rem
 ;Driver for JY-LKM1638
 ;Allows control of 7 segment display and LEDs. Will read all the switch values into
 ;variables
 ;
 ;DS18B20 ---> pin 3 08M2
 ;Output to relay ---> pin 2 08M2 this is an input pin, but uses 'pullup' to act as output
 ;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 from 8 pushbuttons.
 ;This program also reads temperature (to 0.1deg) from a DS18B20 and outputs to a relay to control a heater
 ;All from an 08M2!
 ;
 ;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
 ;
 ;This code should be used in conjunction with the TM1638 driver chip datasheet for the LKM1638, here
 ;https://docs.google.com/file/d/0B84N2SrJaybwZTgxYjM4ZmEtY2EyZi00YjVjLWIzOTctYTlhMjJkM2MxMTBl/edit
24 ;
25 ;The Main program loop is used as a thermostat
26 ;Permanent temperatur readout on the LH 4 digits
27 ;The RH 4 digits displaying either maximum temp, minimum temp or thermostat set value
28 ;The function of the RH digits is controlled by the pushbuttons as described below
29 ;The display brightness is also controlled by a pushbutton and PICAXE reset by another
30 ;Maximum, minimum, set and brightness levels are retained in eeprom
31 :
32 ;To use the JY-LKM1638 module for other applications, use the subroutines provided and write your code
33 ;to call them as required
34 ;
35 ;Subroutines
36 ;
37 ;Convsettemp:
38 ;Passed settemp
39 ;Returns huns, tens, units deci, sign
40 ;Purpose Converts settemp variable into digits for 7seg display
41 ;Calls None
42 ;
43 ;Display:
44 ;Passed bank, sign, tens, units, deci
45 ;Returns None
46 ;Purpose Display 3 digits and a sign on the 7seg displays as two banks of 4 digits
47 ; LH 4 digits are bank 0, RH 4 digits are bank 8
48 ;Calls writemode, lookupchar, writedisplay, displaybrit
49 ;
50 ;Clearleds:
51 ;Passed None
52 ;Returns None
53 ;Purpose Clears all the LEDs
54 ;Calls writemode, writedisplay, displaybrit
55 ;
56 ;Clearchar:
57 ;Passed None
58 ;Returns None
59 ;Purpose Clears all the 7seg displays and the LEDs
60 ;Calls writemode, sendchar, displaybrit
61 ;
62 ;Sendchar:
63 ;Passed dataio
64 ;Returns None
65 ;Purpose Sends the bits of dataio serially to the LKM1638
66 ;Calls None
67 ;
68 ;Getkeys:
69 ;Passed None
70 ;Returns s1, s2 s3, s4, s5, s6, s7, s8 (also contained in keys)
71 ;Purpose Gets the individual switch values
72 ;Calls sendchar
73 ;
74 ;Lookupchar:
75 ;Passed char
76 ;Returns dispvalue
77 ;Purpose Takes the char variable and converts it into the correct code to display on 7seg display
78 ; e.g. char value 8 displays '8', char value 17 displays '-'
79 ; The number of characters in the lookup can be increased if requred for symbols etc.
80 ;Calls None
81 ;
82 ;Gethrtemp
83 ;Passed hrtemp
84 ;Returns deci, units, tens, huns, sign
85 ;Purpose Takes hrtemp and convers into individual digits for display. Correctely handling negative
86 ; values. (Note: hrtemp value is different upon return)
87 ;Calls None
88 ;
89 ;Writedisplay:
90 ;Passed dispaddr, dispvalue
91 ;Returns None
92 ;Purpose Writes value to individual 7seg display or LED. For 7 seg, dispaddr are 8 even addresses from 0
93 ; to 14 and dispvalue is obtained from Lookupchar subroutine.
94 ; For LEDs dispaddr are 8 odd addresses from 1 to 15 and dispvalue is 0=0ff, 1=red, 2=green.
95 ;Calls sendchar, displaybrit
96 ;
97 ;Displaybrit:
98 ;Passed dispbrit
99 ;Returns None
100 ;Purpose The last byte in protocol to set display brightness. From $88 min to $8f max. Min normally ok
101 ;Calls sendchar
102 ;
103 'Writemode
104 ;Passed autoaddr
105 ;Returns None
106 ;Purpose Puts display into autocrement mode, where all the 7 seg and LEDs can be written to just by sending
107 ; data bytes. Used in Clearchar subroutine to blank all displays
108 ;Calls sendchar
#endrem
 #picaxe 08m2
 #no_data ;Minimum(0,1), Maximum(2,3), set(4), dispbrit(5) stored in eeprom so do not overwrite
;C:\Users\Malcolm\Documents\PIC\PICAxe\TM1638 temperature set values.bas
 dirsc = %00010111 ;c4, c2, c1, c0 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 tempio = c.4 ;DS18B20 io
 symbol s1 = bit16 ;b2 - switch 1 - Reset program
 symbol s2 = bit17 ;b2 - switch 2
 symbol s3 = bit18 ;b2 - switch 3 - Cycle the display brightness
 
 ; now carry om from here
e
 

russbow

Senior Member
Did exactly that EC. What a task
Tried to upload it and found 1000 character upload limit. :eek:
Don't want to tread on Malcolms toes, but will upload in sections tomorrow.
 

mjy58

Member
@Russ
I am not sure what I should have done here. I thought a pdf was the 'best' way of uploading the code, however having tried to get it back into PE, perhaps it is not. (how do you select the whole of a pdf document:confused:)

Anyway, as a slight improvement I have printed it again with the Picaxe PDF printer, but removed the option to display lines in PE first. So at least you can import it into PE without having syntax errors on the line numbers. The attached code is a slightly later version than initially published, very minor changes.

View attachment TM1638 temperature control V1.pdf

I did try to upload it in CODE tags, but it truncates the code. Is this the 1000 character upload limit?

Any guidance on the best way to upload 'long' code?
 

nick12ab

Senior Member
RI thought a pdf was the 'best' way of uploading the code,
No it isn't. For code too long to post using code tags, upload a .bas file.


I did try to upload it in CODE tags, but it truncates the code. Is this the 1000 character upload limit?
The limit is 10,000 characters and it will tell you that you have exceeded it rather than truncate it.
 

russbow

Senior Member
Thank you for that Malcolm. Much easier now.
Cut n Paste from a PDF ok but the paste included the line numbers as part of the program, hence syntax errors.

Just over 2700 keystrokes later, all cleaned and, like you, didn't realise the upload limit.

Now get to play with the "Thing". Very impressive bit of coding. Interesting that you use call for gosub. Had to check that out in the manual. Also spent some time looking for bank 0. :eek:.

Thanks for your efforts.

R.
 

IronJungle

Senior Member
Interesting that you use call for gosub. Had to check that out in the manual.
I am using some of Malcolm's early code and had the same experience to the call command. I finally had to search and replace it with gosub so I could sleep at night. :)

Big thanks again to Malcolm for unlocking the mysteries of driving this display. My project with it is progressing thanks to his efforts.
 

mjy58

Member
Glad the code is of use:)

I am not sure why I prefer the 'Call' command, maybe a throwback to my assembler days?

I actually found mounting the 'Thing' nearly as much of a challenge as writing the code. I will be interested to see how you guys do it.

Malcolm
 
Top