Hi jefrem
New improved code. Pauses not required. Address set command now works. If you don't write to all 16 displays some segments of displays not written to turn on when they shouldn't, therefore it is best to write to all 16 displays and over-write these segments.
Phil
Code:
'PIC18F2550 @ 48mHz, USB bootloader, JY-LM1640 16 x 7 seg display
DEFINE OSC 48
DEFINE RESET_ORG 1000h
dat_com_inc VAR BYTE 'Display command for auto inc address
dat_com_fix VAR BYTE 'Display command for fixed address
dis_com_off VAR BYTE 'Display command OFF
dis_com_on VAR BYTE 'Display command ON
address VAR BYTE 'Display address ($00 - $0F)
brightness VAR BYTE 'Brightness value (0 - 7)
dat VAR BYTE 'Data to send to TM1640
digit VAR BYTE '7 seg display (0 - 15)
n VAR BYTE 'Counter
dat_com_inc = %01000000 'Data command ADDRESS auto + 1
dat_com_fix = %01000100 'Data command FIXED address
dis_com_off = %10000000 'Switch display OFF
dis_com_on = %10001000 'Switch display ON
brightness = %00000011 'Display brightness (0 = MIN 7 = MAX)
address = %11000000 'Display ADDRESS $00
din VAR LATB.7
sclk VAR LATB.6
TRISC = %00111000
LATC = 0
TRISB = %00111111
din = 1 'Data pin HIGH to start
sclk = 1 'Clock pin HIGH to start
PAUSE 500
'---------- Start of program ------------------------------------
FOR digit = 0 TO 4 'Scroll to right, increase brightness
GOSUB PRINT:
NEXT
FOR digit = 3 TO 0 STEP -1 'Scroll to left, decrease brightness
GOSUB PRINT:
NEXT
END
'---------- Print to display ------------------------------------
PRINT:
dat = dat_com_inc 'Data command setting
GOSUB COMMAND:
dat = address + digit 'Set address
din = 0 : sclk = 0 'Start transmission process
SHIFTOUT din,sclk,0,[dat] 'LSB first, clock idles low
FOR n = 0 TO 15 'Print 16 characters
LOOKUP n,[0,115,6,57,6,127,113,91,109,109,63,0,64,64,64,64],dat
SHIFTOUT din,sclk,0,[dat] 'Print message
NEXT
sclk = 1 : din = 1 'End transmission process
dat = dis_com_on + digit 'Set brightness
GOSUB COMMAND:
PAUSE 1000
RETURN
'----------------------------------------------------------------
COMMAND:
din = 0 : sclk = 0 'Start transmission process
SHIFTOUT din,sclk,0,[dat] 'LSB first, clock idles low
sclk = 1 : din = 1 'End transmission process
RETURN
'----------------------------------------------------------------
Bookmarks