Code:
====== DESCRIPTION ===============================================================================' Use mini OLED 72x40 pixel display
' ====== FUSES =====================================================================================
' PIC 16F690 Fuses
' Internal oscillator (activate OSCCON register)
@ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
@ ERRORLEVEL -306
@ ERRORLEVEL -202
' ====== REGISTERS =================================================================================
' 76543210
OPTION_REG = 000000 'PORT A&B Pull-Ups disabled (see WPUA & WPUB)
OSCCON = 100000 'Internal RC set to 4MHZ
'ANSEL = 000000 'Select analog inputs Channels 0 to 7 (AN2 + AN4 selected)
'ANSELH = 000000 'Select analog inputs Channels 8 to 11
'ADCON0 = 000000 'A/D Module (Bit5:2 select channels 0:11)
'ADCON1 = 000000 'A/D control register
'CM1CON0 = 000000 'Comparator1 Module is OFF
'CM2CON0 = 000000 'Comparator2 Module is OFF
'INTCON = 000000 'INTerrupts CONtrol (TMR0 OFF)
'TRISA = 000000 'Set Input/Output (0 to 5)
'PORTA = 000000 'Ports High/Low (0 to 5)
'TRISB = 000000 'Set Input/Output (4 to 7)
'PORTB = 000000 'Ports High/Low (4 to 7)
'TRISC = 000000 'Set Input/Output (0 to 7)
'PORTC = 000000 'Ports High/Low (0 to 7)
' ====== DEFINES ===================================================================================
DEFINE OSC 4
'DEFINE NO_CLRWDT 1 'Don't waste cycles clearing WDT
'DEFINE HSER_CLOERR 1 'Automatic clear overrun error
' ====== VARIABLES =================================================================================
I2CDevice var byte
SDA VAR PORTB.4 ' I2C Data
SCL VAR PORTB.6 ' I2C Clock
TEXT VAR BYTE[20]
LENGTH VAR BYTE[2]
DC VAR Byte ' "DATA OR COMMAND", $40=DATA; $0=COMMAND --> changed from bit to byte
;seems a pointless waste if a variable
LCD_DATA VAR BYTE
COM VAR BYTE ' COMMAND
I VAR BYTE
J VAR BYTE
X VAR BYTE 'LCD POSITION X(0 TO 127)
Y VAR BYTE 'LCD POSITION Y(0 TO 7)
' ====== INITIALIZE VARIABLES ======================================================================
I2CDevice = $78
'======= SSD1306 I2C OLED initialization ===========================================================
COM = $AE : GOSUB SEND_COMMAND ' turn off oled panel
COM = $D5 : GOSUB SEND_COMMAND ' set display clock divide ratio/oscillator frequency
COM = $80 : GOSUB SEND_COMMAND ' set divide ratio
COM = $A8 : GOSUB SEND_COMMAND ' set multiplex ratio
COM = $27 : GOSUB SEND_COMMAND ' 1/40 duty
COM = $D3 : GOSUB SEND_COMMAND ' set display offset
COM = $00 : GOSUB SEND_COMMAND ' not offset
COM = $AD : GOSUB SEND_COMMAND ' Internal IREF Setting
COM = $30 : GOSUB SEND_COMMAND ' --
COM = $8D : GOSUB SEND_COMMAND ' set Charge Pump enable/disable
COM = $14 : GOSUB SEND_COMMAND ' set(0x10) disable
COM = $40 : GOSUB SEND_COMMAND ' set start line address
COM = $A6 : GOSUB SEND_COMMAND ' set normal display
; you assume display is mapped from page 0 , this may not be correct ans could be 0,1,2,3
COM = $A4 : GOSUB SEND_COMMAND ' Disable Entire Display On
COM = $A1 : GOSUB SEND_COMMAND ' set segment re-map 128 to 0
COM = $C8 : GOSUB SEND_COMMAND ' Set COM Output Scan Direction 64 to 0
COM = $DA : GOSUB SEND_COMMAND ' set com pins hardware configuration
COM = $12 : GOSUB SEND_COMMAND '
COM = $81 : GOSUB SEND_COMMAND ' set contrast control register
COM = $AF : GOSUB SEND_COMMAND '
COM = $D9 : GOSUB SEND_COMMAND ' set pre-charge period
COM = $22 : GOSUB SEND_COMMAND '
COM = $DB : GOSUB SEND_COMMAND ' set vcomh
COM = $20 : GOSUB SEND_COMMAND '
COM = $AF : GOSUB SEND_COMMAND ' turn on oled panel
' ====== PROGRAM ===================================================================================
PAUSE 20
GOSUB CLEAR_LCD
GOSUB TITLE
MAIN:
PAUSE 1000
GOTO MAIN
END
' ====== CLEAR LCD =================================================================================
CLEAR_LCD:
FOR J = 0 TO 7
FOR I = 0 TO 71 '127
LCD_DATA = $00 : GOSUB SEND_DATA
NEXT I
NEXT J
RETURN
; cant see this working without setting a "window" to write into properly
' ====== SEND COMMAND ==============================================================================
SEND_COMMAND:
dc = $0
I2CWrite SDA,SCL,I2CDevice,DC,[COM]
dc = $40
RETURN
; why not just
SEND_COMMAND:
I2CWrite SDA,SCL,I2CDevice,[0,COM]
RETURN
' ====== CLEAR LCD =================================================================================
SEND_DATA:
DC = $40
I2CWrite SDA,SCL,I2CDevice,DC,[LCD_DATA]
RETURN
; why not just
SEND_COMMAND:
I2CWrite SDA,SCL,I2CDevice,[$40,LCD_DATA]
RETURN
' ====== SEND DATA =================================================================================
FILL:
FOR J = 0 TO 7
FOR I = 0 TO 71 '127
LCD_DATA = $FF : GOSUB SEND_DATA
NEXT I
NEXT J
RETURN
; cant see this working without setting a "window" to write into properly
' ====== TITLE =====================================================================================
;here you set 6 pages "Y" of data from page 0 to 7 with page 2 missing when a 40 pixel height can have 5 pages maximum
TITLE:
X = 0 : Y = 0 : GOSUB SET_XY
FOR I = 0 TO 71 '127
LOOKUP I,[$00,$40,$60,$50,$7E,$50,$60,$40,$02,$06,$0E,$1E,$3E,$00,$00,$00,_
$00,$00,$00,$00,$00,$1F,$3F,$60,$60,$60,$60,$60,$60,$3C,$1C,$00,_
$00,$7F,$7F,$61,$61,$61,$61,$61,$61,$3F,$1E,$00,$00,$1E,$3F,$61,_
$61,$61,$61,$61,$61,$38,$18,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$1F,$3F,$60,$60,$60,$60,$60,$60,$3C,_
$1C,$00,$00,$1E,$3F,$61,$61,$61,$61,$61,$61,$38,$18,$00,$00,$3F,_
$7F,$60,$18,$06,$06,$18,$60,$7F,$3F,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$1C,$63,$41,$5D,$41,$5D,$41,$5D,$41,$5D,$41,$7F,$00],LCD_DATA
lcd_data = lcd_data rev 8
GOSUB SEND_DATA
NEXT I
X = 0 : Y = 1 : GOSUB SET_XY
for i = 0 to 71 '127
lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$F8,$FC,$06,$06,$06,$C6,$C6,$C6,$FC,$F8,$00,_
$00,$FE,$FE,$80,$80,$80,$80,$80,$80,$00,$00,$00,$00,$18,$1C,$86,_
$86,$86,$86,$86,$86,$FC,$78,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$F8,$FC,$06,$06,$06,$C6,$C6,$C6,$FC,_
$F8,$00,$00,$18,$1C,$86,$86,$86,$86,$86,$86,$FC,$78,$00,$00,$FE,_
$FE,$00,$00,$00,$00,$00,$00,$FE,$FE,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
lcd_data=lcd_data rev 8
GOSUB SEND_DATA
NEXT I
X = 0 : Y = 3 : GOSUB SET_XY
for i = 0 to 71 '127
lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$3C,$7E,$C3,$C3,$C3,$C3,_
$C3,$C3,$71,$30,$00,$00,$7F,$FF,$C3,$C3,$C3,$C3,$C3,$C3,$C3,$C3,_
$00,$00,$7F,$FF,$C1,$C1,$C1,$C1,$C1,$C1,$FF,$7F,$00,$00,$7F,$FF,_
$C3,$C3,$C3,$C3,$C3,$C3,$7E,$3C,$00,$00,$3F,$7F,$C0,$C0,$C0,$C0,_
$C0,$C0,$70,$30,$00,$00,$FF,$FF,$03,$03,$03,$03,$03,$03,$FF,$FF,_
$00,$00,$C0,$C0,$C0,$C0,$FF,$FF,$C0,$C0,$C0,$C0,$00,$00,$FF,$FF,_
$0C,$0C,$03,$03,$00,$00,$FF,$FF,$00,$00,$3F,$7F,$C0,$C0,$C0,$C1,_
$C1,$C1,$79,$39,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
lcd_data=lcd_data rev 8
GOSUB SEND_DATA
NEXT I
X = 0 : Y = 4 : GOSUB SET_XY
for i = 0 to 71 '127
lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$30,$38,$0C,$0C,$0C,$0C,_
$0C,$0C,$F8,$F0,$00,$00,$F8,$FC,$0C,$0C,$0C,$0C,$0C,$0C,$0C,$0C,_
$00,$00,$FC,$FC,$80,$80,$80,$80,$80,$80,$FC,$FC,$00,$00,$FC,$FC,_
$80,$C0,$60,$30,$18,$0C,$04,$00,$00,$00,$F0,$F8,$0C,$0C,$0C,$0C,_
$0C,$0C,$38,$30,$00,$00,$FC,$FC,$00,$00,$00,$00,$00,$00,$FC,$FC,_
$00,$00,$0C,$0C,$0C,$0C,$FC,$FC,$0C,$0C,$0C,$0C,$00,$00,$FC,$FC,_
$00,$00,$00,$00,$C0,$C0,$FC,$FC,$00,$00,$F0,$F8,$0C,$0C,$0C,$8C,_
$8C,$8C,$F8,$F0,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
lcd_data=lcd_data rev 8
GOSUB SEND_DATA
NEXT I
X = 0 : Y = 5 : GOSUB SET_XY
for i = 0 to 71 '127
lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$1F,$3F,$30,$30,$30,$30,_
$30,$30,$30,$30,$00,$00,$0F,$1F,$30,$30,$30,$30,$30,$30,$1F,$0F,_
$00,$00,$1F,$3F,$30,$30,$30,$30,$30,$30,$1F,$0F,$00,$00,$00,$00,_
$00,$00,$00,$0F,$1F,$30,$30,$30,$30,$30,$30,$1C,$0C,$00,$00,$30,_
$30,$30,$30,$3F,$3F,$30,$30,$30,$30,$00,$00,$0F,$1F,$30,$30,$30,_
$30,$30,$30,$1E,$0E,$00,$00,$3F,$3F,$03,$03,$00,$00,$00,$00,$3F,_
$3F,$00,$00,$1F,$3F,$30,$30,$30,$30,$30,$30,$3F,$1F,$00,$00,$3F,_
$3F,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
lcd_data=lcd_data rev 8
GOSUB SEND_DATA
NEXT I
X = 0 : Y = 6 : GOSUB SET_XY
for i = 0 to 71 '127
lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$FF,$FF,$C0,$C0,$C0,$C0,_
$C0,$C0,$00,$00,$00,$00,$FC,$FE,$03,$03,$03,$03,$03,$03,$FE,$FC,_
$00,$00,$FF,$FF,$E0,$F0,$D8,$CC,$C6,$C3,$81,$00,$00,$00,$00,$00,_
$00,$00,$00,$0C,$8E,$C3,$C3,$C3,$C3,$C3,$C3,$7E,$3C,$00,$00,$03,_
$03,$03,$03,$FF,$FF,$03,$03,$03,$03,$00,$00,$FC,$FE,$03,$03,$03,_
$63,$63,$63,$7E,$7C,$00,$00,$FF,$FF,$00,$00,$C0,$C0,$30,$30,$FF,_
$FF,$00,$00,$FF,$FF,$60,$60,$60,$60,$60,$60,$FF,$FF,$00,$00,$FF,_
$FF,$03,$03,$03,$03,$03,$03,$03,$03,$00,$00,$00,$00,$00,$00,$00],lcd_data
lcd_data=lcd_data rev 8
GOSUB SEND_DATA
NEXT I
RETURN
'======= SET_XY ====================================================================================
SET_XY:
COM = $21 : GOSUB SEND_COMMAND
COM = X : GOSUB SEND_COMMAND
COM = 71 : GOSUB SEND_COMMAND
COM = $22 : GOSUB SEND_COMMAND
COM = Y : GOSUB SEND_COMMAND
COM = 41 : GOSUB SEND_COMMAND
;attempt to set page to 41 when it cannot be > than 5 pages
RETURN
Bookmarks