Hi jefrem
I wrote the following code for the JY-MCU JY-LKM1638 using the Microcode loader and
PicBasic assembler.
It displays the numbers 0 to 7 on the 7seg displays, the buttons pressed as binary
representation on a LCD display and cycles the LEDs off -> green -> red. Hope this helps.
Phil
[code
@ DEVICE PIC16F873A, XT_OSC 'Xtal OSC
@ DEVICE PIC16F873A, WDT_OFF 'WDT off
@ DEVICE PIC16F873A, PWRT_ON 'Power-up timer on
@ DEVICE PIC16F873A, BOD_OFF 'Brown-out detect off
DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
TRISC = %00000000
TRISB = %00000001 'LCD display / I/R input
TRISA = %11000000 'A.6 & A.7 Bootloader
ADCON1 = %00000110 'PortA all digital
strobe var PORTA.4
sda VAR PORTA.3
scl VAR PORTA.2
fix_addr con $C0 'Start address of 7 segs & LEDs
'($C0 = %11000000 = address $00)
auto_addr con $40 'Address of auto-increment mode
read_mode con $42 'Address to read switches
char var byte 'LOOKUP offset for 7 seg code
disp_addr var byte 'Offset from display start address
'(ODD = LED, EVEN = 7seg)
data_io var byte 'Data IN / OUT
disp_brit var byte 'Display brightness
seg_val var byte 'Code to send to 7 seg displays
'LEDs 0 = OFF, 1 = RED, 2 = GREEN
bank var byte '0 = Left 7 segment, 2 = next segment
'...... 14 = Right 7 segment
sw1 var byte 'Byte 1 of switch read
sw2 var byte 'Byte 2 of switch read
sw3 var byte 'Byte 3 of switch read
sw4 var byte 'Byte 4 of switch read
switch var byte 'Switch pressed as binary number
counter var byte 'General counter
counter1 var byte
strobe = 1 'Set strobe pin high
SDA = 1 'Set data pin high
SCL = 1 'Set clock pin high
disp_brit = $88 'Set minimum brightness (Max = $8F)
bank = 0
DISP_ADDR = 0
'----------------------------------------------------------------
lcdout $FE,1 'Clear LCD
pause 100
lcdout $FE,$80,"Switch "
gosub clear_char: 'Clear 7 seg displays & LEDs
gosub display: 'Write Chars to 7 seg display
MAIN:
for DISP_ADDR = 1 to 15 step 2
gosub write_display 'Write to LEDs display
seg_val = seg_val + 1
if seg_val > 2 then seg_val = 0
next
for counter1 = 0 to 50 'Pause for 1 sec
pause 20
gosub GET_SWITCH: 'Check switches
next
goto MAIN:
end
'----------------------------------------------------------------
DISPLAY:
gosub WRITE_MODE:
DISP_ADDR = bank + 0
char = 0 'Print "0"
gosub lookup_char:
gosub write_display:
DISP_ADDR = bank + 2
char = 1 'Print "1"
gosub lookup_char:
gosub write_display:
DISP_ADDR = bank + 4
char = 2 'Print "2"
gosub lookup_char:
gosub write_display:
DISP_ADDR = bank + 6
char = 3 'Print "3"
gosub lookup_char:
gosub write_display:
DISP_ADDR = bank + 8
char = 4 'Print "4"
gosub lookup_char:
gosub write_display:
DISP_ADDR = bank + 10
char = 5 'Print "5"
gosub lookup_char:
gosub write_display:
DISP_ADDR = bank + 12
char = 6 'Print "6"
gosub lookup_char:
gosub write_display:
DISP_ADDR = bank + 14
char = 7 'Print "7"
gosub lookup_char:
gosub write_display:
gosub display_brit: 'Set Display brightness
return
'----------------------------------------------------------------
CLEAR_CHAR: 'Clear LEDs & 7 seg displays
gosub write_mode:
STROBE = 0
data_io = FIX_ADDR 'Set start address
gosub send_char:
for counter = 1 to 16
data_io = 0 'Blank display
gosub send_char:
next
Strobe = 1
return
'----------------------------------------------------------------
CLEAR_LEDS: 'Clear LEDs only
gosub write_mode:
for DISP_ADDR = 1 to 15 step 2
SEG_VAL = 0 'Turns LEDs OFF
gosub write_display:
next
gosub display_brit 'Set brightness
return
'----------------------------------------------------------------
SEND_CHAR:
shiftout SDA,SCL,4,[data_io]'LSB first, CLOCK idle HIGH
return
'----------------------------------------------------------------
LOOKUP_CHAR:
' lookup char,[1,2,4,8,16,32,64,128],seg_val
' Turns on a different segment on each display
lookup char,[$3F,$06,$5B,$4F,$66,$6D,$7D,$07,_
$7F,$6F,$77,$7C,$58,$5E,$79,$71,$0],SEG_VAL
'Value for 0,1,2,3,4,5,6,7,8,9,A,b,c,d,E,F,(Blank)
return
'----------------------------------------------------------------
GET_SWITCH:
data_io = read_mode 'Set to read 4 bytes
strobe = 0
gosub SEND_CHAR:
shiftin sda,scl,6,[sw1,sw2,sw3,sw4] '4 byte switch read
'MSB first, Read data after clock
strobe = 1
switch.7 = sw1.7 'Convert 4 switch read bytes
switch.6 = sw2.7 'into a single byte
switch.5 = sw3.7 'and display on LCD
switch.4 = sw4.7 'as a binary number
switch.3 = sw1.3 'Left hand 4 switches are in
switch.2 = sw2.3 'bit 7 of the 4 bytes
switch.1 = sw3.3 'Right hand 4 switches are in
switch.0 = sw4.3 'bit 3 of the 4 bytes
' switch = sw1 + (sw2 / 2) + (sw3 / 4) + (sw4 / 8)
' Alternative way to convert but uses more bytes
lcdout $FE,$88,bin8 switch
return
'----------------------------------------------------------------
DISPLAY_BRIT:
data_io = disp_brit
STROBE = 0
gosub send_char:
STROBe = 1
return
'----------------------------------------------------------------
WRITE_DISPLAY:
data_io = FIX_ADDR + DISP_ADDR
strobe = 0
gosub send_char:
data_io = SEG_VAL
gosub send_char:
STROBE = 1
gosub display_brit:
return
'----------------------------------------------------------------
WRITE_MODE:
DATA_IO = AUTO_ADDR
strobe = 0
gosub send_char:
strobe = 1
return
'================================================= ===============
/code]
Bookmarks