Code:
'****************************************************************
'* Name : OLED_016002.PBP *
'* Author : Roberts *
'* Notice : *
'* Date : 10/10/2021 *
'* Version : 1.0 *
'* Notes : PBP code snippet that sends characters to LCD *
'* : via I2C interface (LCD = Vishay 2x16 OLED *
'* : character display with Winstar WS0012 Controller *
'****************************************************************
' Name : OLED_016002.pbp
' Compiler : PICBASIC PRO Compiler PBPX 3.1.1.4
' Assembler : MicroCode Studio V_5.0.0.5
' Target PIC : PIC16F18346 on Microchip Curiosity Board
' Hardware : Vishay 016O002B OLED display
' Oscillator : 16MHz internal
' Description : PICBASIC PRO program to demonstrate SPI interface to display
'
' This program demonstrates the OLED initialization sequence and writing text & numbers to a Vishay 2x16 OLED display
' having a Winstar WS-0012 Controller via its SPI interface option. Command and data writes to the WS-0012 controller
' require 10-bit arguments. Commands are expressing as 16-bit constants that are truncated to 10-bits in the SHIFTOUT
' commands, while data characters are 8-bit quantities OR'd with 16-bit write commands and then truncated to 10-bits.
' Because the WS-0012 controller requires 10-bit command and data, direct ASCII writes without the ARRAYWRITE command
' (as is possible with the US2066 controller) is not possible. All displayed characters must be put into an array and
' then written in 10-bit lengths in order to include the RS and R/W bits.
'
' The arrays hold displayed characters and must be cleared using the subroutine CLEAR_ARRAYS so that vestiges of prior
' displayed lines are erased (the clear display command 0x0001 doesn't accomplish this).
'
' The WS-0012 controller seems to be sometimes plagued with an inability to clear all of its internal registers upon
' power up and/or initialization. Pin 16 is an active low reset (having an onboard pullup resistor) that can be used
' following power up and can be connected to a microcontroller pin or to a power supervisor IC to ensure resetting.
'
' The display has 0 ohm shorting resistors on its circuit board that are used to set the interface type that can be
' 8-bit or 4-bit parallel (either 6800 mode or 8080 mode), or SPI mode. For SPI mode, the following jumper settings
' apply:
' JCS - shorted
' L_CS_H - open
' H_SP_L - high
' H_86_L - low
'
' Wiring connections for SPI mode are the following:
' Pin 1 - Power Ground
' Pin 2 - +5 VDC
' Pins 3-11 - Ground
' Pin 12 - SPI_CK (clock)
' Pin 13 - SPI_DA (out/MISO)
' Pin 14 - SPI_DA (in/MOSI)
' Pin 15 - CS (chip select - active low)
' Pin 16 - Reset (active low)
'************************************* Configure and initialize the target device *************************************
INCLUDE "PIC16F18346_Initialize_Curiosity_Board.PBP" 'Setup for PIC16F18346 on Curiosity Board (Oscillator = 16MHz)
'
'******************* Alter the following I/O port pin configurations per target application requirements **************
'
ANSELA = 000000 ' PortA all digital / available pins are A0 through A5 (6 I/O's)
TRISA = 000000 ' PortA all used pins are outputs
SPI_DA VAR LATA.5 'SPI clock pin to 2x16 OLED display (use latch addressing to avoid read/modify/write error)
SPI_CK VAR LATA.4 'SPI data pin to 2x16 OLED display
CS VAR LATA.2
OLED_ARRAY1 VAR BYTE[16] 'This array holds the 16 characters to display on OLED line 1
OLED_ARRAY2 VAR BYTE[16] 'This array holds the 16 characters to display on OLED line 2
temp VAR BYTE 'temp is the variable used to SHIFTOUT the array contents to the OLED
j VAR BYTE 'loop counter
CS = 1 'initialize CS as logic high
D CON 12345 'D is a 5-digit random value constant
H CON $1FA7 'H is a 4-hexit random value constant
'***************************** 'Initialization sequence for the WS-0012 Controller (2x16 OLED) *************************
PAUSE 500 'Controller needs 500mS to stabilize power
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0028\10] : CS = 1 'Function Set - D/L=0, N=2, Default Font
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0013\10] : CS = 1 'Character Mode / Power OFF
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0003\10] : CS = 1 'Enter Command Set #2
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0029\10] : CS = 1 'Power ON
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0068\10] : CS = 1 'BVR - Set brightness (0x0060 is max / 0x006F is min)
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0000\10] : CS = 1 'Exit Command Set #2
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0001\10] : CS = 1 'Clear Display
PAUSE 10
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0016\10] : CS = 1 'Display Direction
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0006\10] : CS = 1 'Entry Mode Set
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$000C\10] : CS = 1 'Display ON
PAUSE 1
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0002\10] : CS = 1 'Home - Start at Line 1 / Position 1
'
'********************************************* Main Program ************************************************************
'
main:
' Text displays are filled with 16 characters so that intermediate clear display commands aren't needed
ARRAYWRITE OLED_ARRAY1,["VISHAY 2x16 OLED"] '2x16 OLED Program Announcement
GOSUB OLEDOUT1
ARRAYWRITE OLED_ARRAY2,[" DISPLAY DEMO "]
GOSUB OLEDOUT2
PAUSE 2000 'Hold display for 2 seconds
ARRAYWRITE OLED_ARRAY1,["Winstar WS-0012 "] 'Controller Announcement
GOSUB OLEDOUT1
ARRAYWRITE OLED_ARRAY2,["OLED Controller "]
GOSUB OLEDOUT2
PAUSE 2000 'Hold display for 2 seconds
' Now display numerical values:
gosub CLEAR_ARRAYS 'Clear array data of prior displayed text characters
ARRAYWRITE OLED_ARRAY1, [DEC D] 'display the decimal constant 12345 (converts value to ASCII text)
GOSUB OLEDOUT1
ARRAYWRITE OLED_ARRAY2, [HEX H] 'display the hexadecimal constant 0x1FA7
GOSUB OLEDOUT2
PAUSE 2000
goto main 'Loop forever
'************************************* 2x16 OLED Display Subroutine ****************************************************
'This subroutine in universal output for Line 1
OLEDOUT1:
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0080\10] : CS = 1 'Start at Line 1 / Position 1
PAUSE 1
For j = 0 to 15
temp = OLED_ARRAY1[j] 'SHIFTOUT doesn't support array arguments
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0200 | temp\10] : CS = 1 'this creates 10-bit write data commands
Next j
Return
'This subroutine in universal output for Line 2
OLEDOUT2:
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$00C0\10] : CS = 1 'Start at Line 2 / Position 1
PAUSE 1
For j = 0 to 15
temp = OLED_ARRAY2[j] 'SHIFTOUT doesn't support array arguments
CS = 0 : SHIFTOUT SPI_DA, SPI_CK, 5, [$0200 | temp\10] : CS = 1 'this creates 10-bit write data commands
Next j
Return
'Clear both arrays of previously displayed characters
CLEAR_ARRAYS:
For j = 0 to 15 'clear the array using space characters
OLED_ARRAY1[j] = " " 'does not require SHIFTOUT to display
OLED_ARRAY2[j] = " "
next j
RETURN
'**************************************** Program End ******************************************************************
Bookmarks