Using SPI interface 20X2 to Newhaven slim OLED

RSanger

New Member
This represents about 10 hours of research & experimentation! I tried to create minimal code to get Newhaven’s slim OLED functional. The part uses a US2066 controller – specifically I’m using a NHD-216CW-AY3

Hardware
- 20X2 Pin 13 (hspi hdi) connected to ground. If it is floating, it may cause picaxe reset
- 20X2 Pin 9 (hspi sdo) connected to OLED pin 8 (SDI)
- 20X2 Pin 11 (hspi scl) clock connected to OLED pin 7 (SCLK)
- Other OLED lines connected to +5 or 0V, as per documentation
- the "spi mode" of the OLED is "1,1" (clk polarity = 1, clk phase = 1)
Software
- hspiout shifts data out MSB first. However, the OLED module wants LSB first. Using the “rev” command is needed to reconcile this.
- data sent to the OLED has to be 1 nibble at a time, wth 0000 nibbles separating them!
- Output B.7 had to be used at one point to work around bug, so I included it

Here’s my OLED initialization code – I tried to keep it to only what is needed
#no_data

Symbol TempByte1 = B1
Symbol TempByte2 = B2
Symbol Out_Byte= B3
symbol n = b4


pause 1 ' 1 ms in example
sertxd("RESET!")

Init_OLED:
hspisetup spimode11e, spislow
output b.7 ' needed to fix bug in clock

Out_Byte = $2A ' turn on RE
gosub Send_Command

' Out_Byte = 4 ' writes from upper right to lower left, chars reversed, bottom is toward header
' gosub Send_Command
'Out_Byte = $05
' gosub Send_Command ' ribbon is on left, bottom is toward header
Out_Byte = $06 ' ' top is toward header, chars normal
gosub Send_Command
' Out_Byte = $07 'top is toward header, chars reversed
' gosub Send_Command


Out_Byte = $71 ' turn on internal regulator
gosub Send_Command
Out_Byte = $5C ' enable 5V regulator
gosub Send_Data

Out_Byte = $72
gosub Send_Data ' choose ROM table
Out_Byte = $00


Out_Byte = $28 ' turn off RE & set to 2-line
gosub Send_Command


out_Byte = $0F ' turn on display with blinking cursor
gosub Send_Command

out_byte=$01 ' clear display & initialize dram and set AC
gosub Send_Command

Main:
' now write some test data
Out_Byte = $01 ' clear display, reset pointers
gosub Send_Command

for n = $30 to $3F
Out_Byte = n' display something
gosub Send_Data
next n

Out_Byte = $A0 ' go to 2nd line at $40
gosub Send_Command

pause 2000
for n = $41 to $50
Out_Byte = n
gosub Send_Data
next n

pause 2000
goto main
'======================================
Send_Command:

'$F8 is sync bits plus "write" plus "command"
Out_Byte = Out_Byte rev 8
TempByte2 = Out_Byte AND $F0 ' zero out LS Nibble

Tempbyte1 = Out_Byte << 4 ' shift left

hspiout ($f8,Tempbyte2,TempByte1)


return

'===========================================

Send_Data:
' $FA is sync bits plus "write" plus "data"
Out_Byte = Out_Byte rev 8
TempByte2 = Out_Byte AND $F0 ' zero out LS Nibble
Tempbyte1 = Out_Byte <<4
hspiout ($FA,Tempbyte2,TempByte1)
return
 
Last edited:
Top