Alberto
- 20th August 2016, 14:28
Care to share the code?
/Henrik.
Not at all! here the code. Please note that it needs the include "Elapsed.bas" in order to work. Please also note that the original code has been posted by timc and I did modify his code to have it working with 1,3 inch oled driven by SH1106.
Alberto
;----[16F88 Hardware Configuration]---------------------------------------------
#IF __PROCESSOR__ = "16F88"
#DEFINE MCU_FOUND 1
#CONFIG
cfg1 = _INTRC_IO ; INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin
cfg1&= _WDT_OFF ; WDT disabled
cfg1&= _PWRTE_OFF ; PWRT disabled
cfg1&= _MCLR_ON ; RA5/MCLR/VPP pin function is MCLR
cfg1&= _BODEN_OFF ; BOR disabled
cfg1&= _LVP_OFF ; RB3 is digital I/O, HV on MCLR must be used for programming
cfg1&= _CPD_OFF ; Code protection off
cfg1&= _WRT_PROTECT_OFF ; Write protection off
cfg1&= _DEBUG_OFF ; In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
cfg1&= _CCP1_RB0 ; CCP1 function on RB0
cfg1&= _CP_OFF ; Code protection off
__CONFIG _CONFIG1, cfg1
cfg2 = _FCMEN_ON ; Fail-Safe Clock Monitor enabled
cfg2&= _IESO_ON ; Internal External Switchover mode enabled
__CONFIG _CONFIG2, cfg2
#ENDCONFIG
#ENDIF
;----[Verify Configs have been specified for Selected Processor]----------------
; Note: Only include this routine once, after all #CONFIG blocks
#IFNDEF MCU_FOUND
#ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
#ENDIF
INCLUDE "Elapsed.bas"
'=============================== MCU SETUP =====================================
DEFINE OSC 8 ' define fosc
define I2C_HOLD 1 ' define I2C type
TrisB = %00000000 ' Set all port B pins as outputs
TrisA = %00001100 ' Set portA.2 & portA.3 as inputs
CMCON = 7 ' Turn Off Comparators
ADCON1 = 7 ' Disable A/D converter
ANSEL = %00000000 ' set all analog pins to digital
OSCCON = %01110000 ' Set PIC16F88 to 8 MHz = %01110000
' or set to 4 MHz = %01100000
'================================================= ==============================
LED var PortB.3
SDA var PortA.2 ' I2C Data PortA.2
SCL var PortA.3 ' I2C Clock PortA.3
Res var PortB.0
'================================================= ==============================
False con 0
True con 1
Flag_Text var bit
I2CDevice var byte
LENGTH VAR BYTE[2]
DC VAR Byte ' DATA OR COMMAND: $40=DATA or $0=COMMAND
LCD_DATA VAR BYTE
COM VAR BYTE ' COMMAND
PAGE VAR BYTE ' LCD PAGE(B0 TO B7)
I VAR BYTE
J VAR BYTE
X VAR BYTE ' LCD POSITION X(0 TO 127)
Y VAR BYTE ' LCD POSITION Y(0 TO 7)
say var byte ' counter
text_reg var byte ' index for text
letter_reg var byte ' data carrier for text chars
letter_count var byte ' count of letters in your text
Text_Buff var byte[27]
Text_String var byte
Old_Seconds var byte
Counter var word
charset var word ' Char codes index
chartmp var word ' char codes index temp
letter_temp var word ' working copy
'================================================= ==============================
CLEAR
I2CDevice = $78 ' Display = $78 (SA0 pin = 0)
DC = $00 ' value for commands
for I = 0 to 10
pause 100
led = !Led
next I
led = 0
'====================== lcd initialization for (SH1106 & SSD1306 driver) ===========================
INIT:
I2CWrite SDA,SCL,I2CDevice,[DC,$AE] ' Display Off
I2CWrite SDA,SCL,DC,I2CDevice,[$20]
I2CWrite SDA,SCL,I2CDevice,[DC,$10]
I2CWrite SDA,SCL,I2CDevice,[DC,$B0]
I2CWrite SDA,SCL,I2CDevice,[DC,$C8]
I2CWrite SDA,SCL,I2CDevice,[DC,$00]
I2CWrite SDA,SCL,I2CDevice,[DC,$10]
I2CWrite SDA,SCL,I2CDevice,[DC,$40]
i2cwrite sda,scl,I2CDevice,[DC,$81]
i2cwrite sda,scl,I2CDevice,[DC,$7F]
i2cwrite sda,scl,I2CDevice,[DC,$A1]
i2cwrite sda,scl,I2CDevice,[DC,$A6]
I2CWrite SDA,SCL,I2CDevice,[DC,$A8]
I2CWrite SDA,SCL,I2CDevice,[DC,$3F]
I2CWrite SDA,SCL,I2CDevice,[DC,$A4]
I2CWrite SDA,SCL,I2CDevice,[DC,$D3]
I2CWrite SDA,SCL,I2CDevice,[DC,$00]
I2CWrite SDA,SCL,I2CDevice,[DC,$D5]
I2CWrite SDA,SCL,I2CDevice,[DC,$F0]
I2CWrite SDA,SCL,I2CDevice,[DC,$D9]
I2CWrite SDA,SCL,I2CDevice,[DC,$22]
I2CWrite SDA,SCL,I2CDevice,[DC,$DA]
I2CWrite SDA,SCL,I2CDevice,[DC,$12]
I2CWrite SDA,SCL,I2CDevice,[DC,$DB]
I2CWrite SDA,SCL,I2CDevice,[DC,$20]
I2CWrite SDA,SCL,I2CDevice,[DC,$8D]
I2CWrite SDA,SCL,I2CDevice,[DC,$14]
I2CWrite SDA,SCL,I2CDevice,[DC,$AF] ' Display On
PAUSE 100
'================================================= ==============================
GOSUB CLEAR_LCD
pause 5
Gosub ResetTime ' Reset Time to 0d-00:00:00.00
Gosub StartTimer ' Start the Elapsed Timer
'-------------------------------------------------------------------------------
'First byte is the column number (2-129)
'Second byte is the page number (0-7)
'Third byte on is text (max 21 characters)
'First byte after text must be a NULL
'-------------------------------------------------------------------------------
arraywrite Text_Buff,[2,1,"ELAPSED TIME:",0]
gosub Print_XY
arraywrite Text_Buff,[30,6,"Test C(30);R(6)",0]
gosub Print_XY
Main:
Pause 1
IF MinutesChanged THEN MinutesChanged = 0
if Old_Seconds <> Seconds then
Old_Seconds = Seconds
arraywrite Text_Buff,[2,3, DEC Days,"d-",DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds,0]
gosub Print_XY
ENDIF
Counter = Counter + 1
if Counter > 50 then Led = False
if Counter > 1000 then
Led = True
Counter = 0
endif
GOTO MAIN
'============================== Print Lines ====================================
Print_XY
X = Text_Buff[0] ' Column in first position
Y = Text_Buff[1] ' Page (Row) in second
gosub Set_XY ' set cursor position
Text_String = 2 ' point to 1st character to print
DO while Text_Buff[Text_String] <> 0
letter_reg = Text_Buff[Text_String]
gosub Find_Char
Text_String = Text_String + 1
loop
return
'=============================== Send data =====================================
SEND_DATA:
DC = $40
I2CWrite SDA,SCL,I2CDevice,[DC,LCD_DATA]
RETURN
'============================== Send Command ===================================
SEND_COMMAND:
DC = $00
I2CWrite SDA,SCL,I2CDevice,[DC,COM]
RETURN
'============================== clear lcd ======================================
CLEAR_LCD:
X = 2
Y = 0
LCD_DATA = $00
FOR Y = 0 TO 7
gosub SET_XY
FOR I = 0 TO 131
GOSUB SEND_DATA
NEXT I
NEXT Y
X = 2
Y = 0
gosub SET_XY
RETURN
'======================= Set Page & Column position ============================
SET_XY:
COM = 176 + y :GOSUB SEND_COMMAND ' PAGE (ROW) SELECTION
COM = x//15 :GOSUB SEND_COMMAND ' LOW NIBBLE COLUMN SELECTION
COM = (x/15) + 16 :GOSUB SEND_COMMAND ' HIGH NIBBLE COLUMN SELECTION
RETURN
'================================ Find Char ====================================
Find_Char:
letter_temp = letter_reg - 32 ' Space is now at index 0 all other characters are 32 less into index
letter_temp = letter_temp * 5 ' Every character is made of 5 bytes
for charset=letter_temp to letter_temp+4
IF letter_reg < "A" then
chartmp = charset - 0 ' lookup variable must be 8 bits
lookup chartmp,[$00,$00,$00,$00,$00,$00,$00,$5F,$00,$00,$14,$7F,$1 4,$7F,$14,$00,$08,$07,$03,$00,_ 'sp,!,#,"
$24,$2A,$7F,$2A,$12,$23,$13,$08,$64,$62,$36,$49,$5 6,$20,$50,_ '$,%,&
$00,$08,$07,$03,$00,$00,$1C,$22,$41,$00,$00,$41,$2 2,$1C,$00,_ '',(,)
$2A,$1C,$7F,$1C,$2A,$08,$08,$3E,$08,$08,$00,$00,$7 0,$30,$00,_ '*,+,,
$08,$08,$08,$08,$08,$00,$00,$60,$60,$00,$20,$10,$0 8,$04,$02,_ '-,.,/
$3E,$51,$49,$45,$3E,$00,$42,$7F,$40,$00,$72,$49,$4 9,$49,$46,_ '0,1,2
$21,$41,$49,$4D,$33,$18,$14,$12,$7F,$10,$27,$45,$4 5,$45,$39,_ '3,4,5
$3C,$4A,$49,$49,$31,$41,$21,$11,$09,$07,$36,$49,$4 9,$49,$36,_ '6,7,8
$46,$49,$49,$29,$1E,$00,$00,$14,$00,$00,$00,$40,$3 4,$00,$00,_ '9,:,;
$00,$08,$14,$22,$41,$14,$14,$14,$14,$14,$00,$41,$2 2,$14,$08,_ '<,=,>
$02,$01,$59,$09,$06,$3E,$41,$5D,$59,$4E],LCD_DATA '?,@
elseif (letter_reg < "[") then
chartmp = charset - 165 ' subtract previous table length so "A" = zero
lookup chartmp,[$7C,$12,$11,$12,$7C,_ 'A
$7F,$49,$49,$49,$36,$3E,$41,$41,$41,$22,$7F,$41,$4 1,$41,$3E,_ 'B,C,D
$7F,$49,$49,$49,$41,$7F,$09,$09,$09,$01,$3E,$41,$4 1,$51,$73,_ 'E,F,G
$7F,$08,$08,$08,$7F,$00,$41,$7F,$41,$00,$20,$40,$4 1,$3F,$01,_ 'H,I,J
$7F,$08,$14,$22,$41,$7F,$40,$40,$40,$40,$7F,$02,$1 C,$02,$7F,_ 'K,L,M
$7F,$04,$08,$10,$7F,$3E,$41,$41,$41,$3E,$7F,$09,$0 9,$09,$06,_ 'N,O,P
$3E,$41,$51,$21,$5E,$7F,$09,$19,$29,$46,$26,$49,$4 9,$49,$32,_ 'Q,R,S
$03,$01,$7F,$01,$03,$3F,$40,$40,$40,$3F,$1F,$20,$4 0,$20,$1F,_ 'T,U,V
$3F,$40,$38,$40,$3F,$63,$14,$08,$14,$63,$03,$04,$7 8,$04,$03,_ 'W,X,Y
$61,$59,$49,$4D,$43],LCD_DATA 'Z
else
chartmp = charset - 295 ' subtract previous tables length so "[" = zero
lookup chartmp,[$00,$7F,$41,$41,$41,$02,$04,$08,$10,$20,_ '[,\
$00,$41,$41,$41,$7F,$04,$02,$01,$02,$04,$40,$40,$4 0,$40,$40,_ '],^,_
$00,$03,$07,$08,$00,$20,$54,$54,$38,$40,$7F,$28,$4 4,$44,$38,_ '`,a,b
$38,$44,$44,$44,$28,$38,$44,$44,$28,$7F,$38,$54,$5 4,$54,$18,_ 'c,d,e
$00,$08,$7E,$09,$02,$0C,$52,$52,$4A,$3C,$7F,$08,$0 4,$04,$78,_ 'f,g,h
$00,$44,$7D,$40,$00,$20,$40,$40,$3D,$00,$7F,$10,$2 8,$44,$00,_ 'i,j,k
$00,$41,$7F,$40,$00,$7C,$04,$78,$04,$78,$7C,$08,$0 4,$04,$78,_ 'l,m,n
$38,$44,$44,$44,$38,$7C,$18,$24,$24,$18,$18,$24,$2 4,$18,$7C,_ 'o,p,q
$7C,$08,$04,$04,$08,$48,$54,$54,$54,$24,$04,$04,$3 F,$44,$24,_ 'r,s,t
$3C,$40,$40,$20,$7C,$1C,$20,$40,$20,$1C,$3C,$40,$3 0,$40,$3C,_ 'u,v,w
$44,$28,$10,$28,$44,$4C,$50,$50,$50,$3C,$44,$64,$5 4,$4C,$44,_ 'x,y,z
$00,$08,$36,$41,$00,$00,$00,$77,$00,$00,$00,$41,$3 6,$08,$00,_ '{,|,}
$02,$01,$02,$04,$02],LCD_DATA '~
endif
gosub SEND_DATA
next charset
LCD_DATA = $00 ' space between characters always $00
gosub SEND_DATA
return
'================================================= ==============================
END
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.