In SX Microcontrollers, SX/B Compiler and SX-Key Tool, geekything wrote: Hi all, I've been playing with an OLED (Organic Light Emitting Diode) display and SX/B. Think of OLEDs as looking similar to micro-sized plasmas in terms of colour richness and black levels -- the organic compounds fluoresce directly without backlight. The display I used was from Sparkfun. The code to initialize and drive the display to produce some pretty circle patters is below. Ping me with comments, suggestions, or questions! :) -marc [code] ' ========================================================================= ' ' File...... OLED.SXB ' Purpose... OLED Test Program ' Author.... Marc Nicholas ' E-mail.... geekything (at) gmail (dot) com ' Started... 20-Aug-2006 ' Updated... 31-Dec-2006 ' Language.. SX/B 1.5 ' ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ' Simple program to init an SSD1399-base dOLED and display random patterns ' See here for the actual device: ' http://www.sparkfun.com/commerce/product_info.php?products_id=712 ' ' This example drives the display in pseudo-SPI mode rather than bus mode. ' ' Note: you could probably port this to a BASICStamp fairly easily. ' ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- DEVICE SX28, OSC4MHZ ' Should work on any SX chip FREQ 4_000_000 ' 4Mhz internal oscillator, baby! ID "OLEDTest" ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- ' ' I'm using 5-bits of Port B. ' Don't try this with Port A pins unless you're familiar with open-drain ' outputs! SCLK VAR RB.0 ' shift clock -- connect to D0 SOUT VAR RB.1 ' shift data -- connect to D1 CS VAR RB.2 ' chip select -- connect to CS CMD VAR RB.3 ' [D]ata/[C]ommand select -- connect to D/C RS VAR RB.4 ' Reset line -- connect to RES ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- cycle VAR BYTE params VAR BYTE tmpB1 VAR BYTE tmpB2 VAR BYTE tmpB3 VAR BYTE tmpB4 VAR BYTE i VAR WORD rand1 VAR BYTE rand2 VAR BYTE rand3 VAR BYTE rand4 VAR BYTE rand5 VAR BYTE rand6 VAR BYTE ' ========================================================================= PROGRAM Start ' ========================================================================= ' ------------------------------------------------------------------------- ' Subroutine Declarations ' ------------------------------------------------------------------------- Write_OLED SUB 1,4 Write_C SUB 1 Write_D SUB 1 ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: ' Set Port B to be output TRIS_B = %00000000 ' Wait 100ms for things to settle down! Probably not really necessary PAUSE 100 ' Toggle chip select so that first frame is interpreted and reset the SSD1339 CS = 0 RS = 0 PAUSE 25 CS = 1 RS = 1 ' Now comes the OLED setup! Write_OLED 0xA0, 0x34 ' Set Re-map / Color Depth 0xb4; 262K 8bit R->G->B Write_OLED 0xE3 ' NOP (No OPeration) commands for padding Write_OLED 0xA1, 0x01 ' Set display start line Write_OLED 0xE3 Write_OLED 0xA2, 0x01 ' Set display offset Write_OLED 0xE3 Write_OLED 0xA6 ' Normal display Write_OLED 0xE3 Write_OLED 0xAD, 0x8E ' Set Master Configuration Write_OLED 0xE3 Write_OLED 0xB0, 0x05 ' Power saving mode Write_OLED 0xE3 Write_OLED 0xB1, 0x11 ' Set pre & discharge Write_OLED 0xE3 Write_OLED 0xB3, 0xf0 ' clock & frequency Write_OLED 0xE3 Write_OLED 0xBB, 0x1C, 0x1C, 0x1C ' Set pre-charge voltage of color A B C Write_OLED 0xE3 Write_OLED 0xbe, 0x1f ' Set VcomH Write_OLED 0xE3 Write_OLED 0xc1, 0xaa, 0xb4, 0xc8 ' Set contrast current for A B C Write_OLED 0xE3 Write_OLED 0xc7, 0x0f ' Set master contrast Write_OLED 0xE3 Write_OLED 0xCA, 0x7f ' Duty = 127 ' Write_OLED 0xCA, 132 ' Duty = 132 Write_OLED 0xE3 Write_OLED 0xaf ' Display on Write_OLED 0xE3 PAUSE 120 Setup: Write_C 0x8E ' Clear display. We're using commands with more than four byte now Write_D 0 ' and we have to change the way we talk to the OLED! Write_D 0 Write_D 130 Write_D 130 Write_OLED 0xE3 ' NOP PAUSE 100 Write_C 0x92 Write_D 0x01 Write_OLED 0xE3 PAUSE 10 rand1 = 123 rand2 = 0 rand3 = 255 rand4 = 50 rand5 = 150 rand6 = 250 PAUSE 120 Main: ' Main code loop here. We're just going to clear the display and draw random filled circles Write_OLED 0x92, 0x01 ' Reset Write_OLED 0xE3 Write_C 0x8E ' Clear Write_D 0 Write_D 0 Write_D 132 Write_D 132 PAUSE 100 FOR i = 0 to 2500 RANDOM rand1 RANDOM rand2 RANDOM rand3 RANDOM rand4 RANDOM rand5 RANDOM rand6 rand1 = rand1 // 130 rand2 = rand2 // 130 rand3 = rand3 // 64 Write_C 0x86 Write_D rand1 Write_D rand2 Write_D rand3 Write_D rand4 Write_D rand5 Write_D rand6 Write_D rand4 PAUSE 10 NEXT i GOTO Main ' ------------------------------------------------------------------------- ' Subroutine Code ' ------------------------------------------------------------------------- Write_OLED: cycle = 1 params = __PARAMCNT tmpB1 = __PARAM1 ' save LSB of value tmpB2 = __PARAM2 tmpB3 = __PARAM3 tmpB4 = __PARAM4 INC params DO WHILE cycle < params IF cycle = 1 THEN CMD = 0 CS = 0 SHIFTOUT SOUT, SCLK, MSBFIRST, tmpB1,10 ' send the bits CS = 1 ElSEIF cycle = 2 THEN CMD = 1 CS = 0 SHIFTOUT SOUT, SCLK, MSBFIRST, tmpB2,10 ' send the bits CS = 1 ElSEIF cycle = 3 THEN CMD = 1 CS = 0 SHIFTOUT SOUT, SCLK, MSBFIRST, tmpB3,10 ' send the bits CS = 1 ElSEIF cycle = 4 THEN CMD = 1 CS = 0 SHIFTOUT SOUT, SCLK, MSBFIRST, tmpB4,10 ' send the bits CS = 1 ENDIF INC cycle LOOP RETURN Write_C: tmpB1 = __PARAM1 CMD = 0 CS = 0 SHIFTOUT SOUT, SCLK, MSBFIRST, tmpB1,10 ' send the bits CS = 1 RETURN Write_D: tmpB1 = __PARAM1 CMD = 1 CS = 0 SHIFTOUT SOUT, SCLK, MSBFIRST, tmpB1,10 ' send the bits CS = 1 RETURN [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=162785 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)