CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

US2066 OLED I2C Driver 04x20 - NHD-0420CW-AB3

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
daveh



Joined: 30 Aug 2013
Posts: 19

View user's profile Send private message

US2066 OLED I2C Driver 04x20 - NHD-0420CW-AB3
PostPosted: Mon Dec 05, 2022 11:02 am     Reply with quote

The following is code to initialize and use the US2066 OLED display driver using I2C. Code tested on NewHaven Display NHD-0420CW-AB3 display.

Code:
#include <33EP512MC504.h>
#FUSES NOWDT                    //No Watch Dog Timer (must be off to control from firmware)
#FUSES NOJTAG                   //JTAG disabled
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES PROTECT
#FUSES FRC_PLL
#build (stack=1024)
#use delay(internal=40MHz)
#include <STDLIB.H>     // for rand()

// PIN - DESCRIPTIONS
// 01 - Vss - 0v
// 02 - Vdd - 3.3v
// 03 - REGVdd - 0v
// 04 - SA0 - 0v    - Changing to Vdd changes the address from 0x3C to 0x3D
// 05-06 - NC - 0v
// 07 - SCL - PIN_B4
// 08 - SDAin  - PIN_A8
// 09 - SDAout - PIN_A8
// 10-15 - NC - 0v
// 16 - /RES - PORTB.10
// 17 - BS0 - 0v    - I2C = BS:010
// 18 - BS1 - 3.3v
// 19 - BS2 - 0v
// 20 - Vss - 0v

#byte PORTB = 0xE12
#bit LCD_RES = PORTB.10      // /RES pin on LCD

// Define I2C Master Module - Hardware
#define i2c2_SCL PIN_B4
#define i2c2_SDA PIN_A8
#bit I2C2EN_BIT = 0x0216.15      // I2CEN bit to enable/disable hardware I2C module
#define i2cM_Baud 400000    // Primary baudrate for I2C
#use i2c(MASTER,STREAM=i2cM,I2C2,FAST=i2cM_Baud,FORCE_HW)  // Master I2C module 2 read sensors MCP9600-100kHzMAX

#define NHD_0420CW_ADR 0x78         // if SA0=0 ADR=0x3C (0x78 shifted by 1), if SA0=1 ADR=0x3D (0x7A shifted by 1)
void initLCD_US2066();              // Initialize US2066 Display
void LCD_Data(int8 ByteVal);
void LCD_Command(int8 ByteVal);

void main() {
    set_tris_a(0b1111101111100101);
    set_tris_b(0b0100001011110011);
    set_tris_c(0b1111111100110000);

    initLCD_US2066();
 
    printf(LCD_Data,"Hello World");
    printf(LCD_Data,"\4Print on line4");
    printf(LCD_Data,"\2     Print on line2");
    printf(LCD_Data,"\3Random Number %u",rand());
           
    while(true){
    }
}

void initLCD_US2066(){
    LCD_RES=1;
    delay_ms(1);
    // Initialization commands from NHD-0420CW-AB3 example code

    LCD_Command(0x2A);    //function set (extended command set) SET RE=1 IS=0
    // IS=0 RE=1 SD=0 - EXTENDED
     LCD_Command(0x71);    //function selection A
      LCD_Data(0x00);    // disable internal VDD regulator (2.8V I/O). data(0x5C) = enable regulator (5V I/O)

    LCD_Command(0x28);    //function set (fundamental command set) SET RE=0 IS=0
    // IS=0 RE=0 SD=0  - FUNDAMENTAL
     LCD_Command(0x08);    //display off, cursor off, blink off

    LCD_Command(0x2A);    //function set (extended command set) SET RE=1 IS=0
    LCD_Command(0x79);    //OLED command set enabled SET SD=1
    // IS=0 RE=1 SD=1 - OLED
     LCD_Command(0xD5);    //set display clock divide ratio/oscillator frequency
      LCD_Command(0x70);    //set display clock divide ratio/oscillator frequency

    LCD_Command(0x78);    //OLED command set disabled SET SD=0
    // IS=0 RE=1 SD=0 - EXTENDED
     LCD_Command(0x09);    //extended function set 3or4-lines display / 5-dot font width / disable inverting cursor /
     LCD_Command(0x06);    //COM SEG direction
     LCD_Command(0x72);    //function selection B
      LCD_Data(0x08);     //ROM CGRAM selection - Select ROM C

//    LCD_Command(0x2A);    //function set (extended command set) SET RE=1 IS=0     // not neeed, RE already set
    LCD_Command(0x79);    //OLED command set enabled SET SD=1
    // IS=0 RE=1 SD=1 - OLED
     LCD_Command(0xDA);    //set SEG pins hardware configuration
      LCD_Command(0x10);    //set SEG pins hardware configuration
     LCD_Command(0xDC);    //function selection C
      LCD_Command(0x00);    //function selection C
     LCD_Command(0x81);    //set contrast control
      LCD_Command(0x7F);    //set contrast control
     LCD_Command(0xD9);    //set phase length
      LCD_Command(0xF1);    //set phase length
     LCD_Command(0xDB);    //set VCOMH deselect level
      LCD_Command(0x40);    //set VCOMH deselect level

    LCD_Command(0x78);    //OLED command set disabled SET SD=0
    LCD_Command(0x28);    //function set (fundamental command set) SET RE=0 IS=0
    // IS=0 RE=0 SD=0  - FUNDAMENTAL
     LCD_Command(0x01);    //clear display
     LCD_Command(0x02);    //set DDRAM address to 0x00 'HOME'
     LCD_Command(0x0C);    //display ON

    delay_ms(100);
}


void LCD_Data(int8 ByteVal){
    switch (ByteVal)
    {
        case '\a'   :               // Home
            LCD_Command(0x02);        // Return Home
            break;
        case '\f'   : 
            LCD_Command(0x01);        // Clear Display
            LCD_Command(0x02);        // Return Home
            break;
        case '\n'   : 
            LCD_Command(0xA0);        // Goto Line2 (0x20 | 0b10000000)
            break;
        case '\2'   : 
            LCD_Command(0xA0);        // Goto Line2 (0x20 | 0b10000000)
            break;
        case '\3'   : 
            LCD_Command(0xC0);        // Goto Line3 (0x40 | 0b10000000)
            break;
        case '\4'   : 
            LCD_Command(0xE0);        // Goto Line4 (0x60 | 0b10000000)
            break;

        default     :
            i2c_start(i2cM);
            i2c_write(i2cM,(NHD_0420CW_ADR)|0);     // NHD-0420CW
            i2c_write(i2cM,0x40);
            i2c_write(i2cM,ByteVal);
            i2c_stop(i2cM);                    // Restart
            delay_us(20);
            break;
    }
}

void LCD_Command(int8 ByteVal){
    i2c_start(i2cM);
    i2c_write(i2cM,(NHD_0420CW_ADR)|0);     // NHD-0420CW
    i2c_write(i2cM,0x00);
    i2c_write(i2cM,ByteVal);
    i2c_stop(i2cM);                    // Restart
    delay_us(20);
}

MartinOl



Joined: 10 Aug 2023
Posts: 1
Location: Prague, Czech Republic

View user's profile Send private message Visit poster's website

It works with PIC16F also :)
PostPosted: Thu Aug 10, 2023 1:44 am     Reply with quote

Hi. I used your code with small modifications for PIC16F18877 and it works well. Thank you very much. Just do not forget pull-up (approx. 4k7) resistors on the SDA and SCL.

Code:

#include <16F18877.h>
#device ADC=10

#use delay(internal=8000000,restart_wdt)
#use FIXED_IO( D_outputs=PIN_D1 )
#define LED   PIN_D1
#define RES   PIN_A0

#pin_select SDA1=PIN_C4
#pin_select SCL1=PIN_C3
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3,restart_wdt,force_hw)

// OLED PIN - DESCRIPTIONS
// 01 - Vss - 0v
// 02 - Vdd - 3.3v
// 03 - REGVdd - 0v
// 04 - SA0 - 0v    - Changing to Vdd changes the address from 0x3C to 0x3D
// 05-06 - NC - 0v
// 07 - SCL - PIN_C3 with 4k7 pull-up resistor to 3V3
// 08 - SDAin  - PIN_C4 with 4k7 pull-up resistor to 3V3
// 09 - SDAout - PIN_C4
// 10-15 - NC - 0v
// 16 - /RES - 3v3
// 17 - BS0 - 0v    - I2C = BS:010
// 18 - BS1 - 3.3v
// 19 - BS2 - 0v
// 20 - Vss - 0v

#byte PORTB = 0xE12



#define NHD_0420CW_ADR 0x78         // if SA0=0 ADR=0x3C (0x78 shifted by 1), if SA0=1 ADR=0x3D (0x7A shifted by 1)
void initLCD_US2066();              // Initialize US2066 Display
void LCD_Data(int8 ByteVal);
void LCD_Command(int8 ByteVal);

void main() {
/*
    set_tris_a(0b1111101111100101);
    set_tris_b(0b0100001011110011);
    set_tris_c(0b1111111100110000);
*/
    initLCD_US2066();
 
    printf(LCD_Data,"Hello World");
    printf(LCD_Data,"\4Print on line4");
    printf(LCD_Data,"\2Print on line2");
    printf(LCD_Data,"\3Print on line3");
           
    while(true){
    }
}

void initLCD_US2066(){
    output_high(RES);
    delay_ms(1);
    // Initialization commands from NHD-0420CW-AB3 example code

    LCD_Command(0x2A);    //function set (extended command set) SET RE=1 IS=0
    // IS=0 RE=1 SD=0 - EXTENDED
     LCD_Command(0x71);    //function selection A
      LCD_Data(0x00);    // disable internal VDD regulator (2.8V I/O). data(0x5C) = enable regulator (5V I/O)

    LCD_Command(0x28);    //function set (fundamental command set) SET RE=0 IS=0
    // IS=0 RE=0 SD=0  - FUNDAMENTAL
     LCD_Command(0x08);    //display off, cursor off, blink off

    LCD_Command(0x2A);    //function set (extended command set) SET RE=1 IS=0
    LCD_Command(0x79);    //OLED command set enabled SET SD=1
    // IS=0 RE=1 SD=1 - OLED
     LCD_Command(0xD5);    //set display clock divide ratio/oscillator frequency
      LCD_Command(0x70);    //set display clock divide ratio/oscillator frequency

    LCD_Command(0x78);    //OLED command set disabled SET SD=0
    // IS=0 RE=1 SD=0 - EXTENDED
     LCD_Command(0x09);    //extended function set 3or4-lines display / 5-dot font width / disable inverting cursor /
     LCD_Command(0x06);    //COM SEG direction
     LCD_Command(0x72);    //function selection B
      LCD_Data(0x08);     //ROM CGRAM selection - Select ROM C

//    LCD_Command(0x2A);    //function set (extended command set) SET RE=1 IS=0     // not neeed, RE already set
    LCD_Command(0x79);    //OLED command set enabled SET SD=1
    // IS=0 RE=1 SD=1 - OLED
     LCD_Command(0xDA);    //set SEG pins hardware configuration
      LCD_Command(0x10);    //set SEG pins hardware configuration
     LCD_Command(0xDC);    //function selection C
      LCD_Command(0x00);    //function selection C
     LCD_Command(0x81);    //set contrast control
      LCD_Command(0x7F);    //set contrast control
     LCD_Command(0xD9);    //set phase length
      LCD_Command(0xF1);    //set phase length
     LCD_Command(0xDB);    //set VCOMH deselect level
      LCD_Command(0x40);    //set VCOMH deselect level

    LCD_Command(0x78);    //OLED command set disabled SET SD=0
    LCD_Command(0x28);    //function set (fundamental command set) SET RE=0 IS=0
    // IS=0 RE=0 SD=0  - FUNDAMENTAL
     LCD_Command(0x01);    //clear display
     LCD_Command(0x02);    //set DDRAM address to 0x00 'HOME'
     LCD_Command(0x0C);    //display ON

    delay_ms(100);
}


void LCD_Data(int8 ByteVal){
    switch (ByteVal)
    {
        case '\a'   :               // Home
            LCD_Command(0x02);        // Return Home
            break;
        case '\f'   :
            LCD_Command(0x01);        // Clear Display
            LCD_Command(0x02);        // Return Home
            break;
        case '\n'   :
            LCD_Command(0xA0);        // Goto Line2 (0x20 | 0b10000000)
            break;
        case '\2'   :
            LCD_Command(0xA0);        // Goto Line2 (0x20 | 0b10000000)
            break;
        case '\3'   :
            LCD_Command(0xC0);        // Goto Line3 (0x40 | 0b10000000)
            break;
        case '\4'   :
            LCD_Command(0xE0);        // Goto Line4 (0x60 | 0b10000000)
            break;

        default     :
            i2c_start();
            i2c_write(NHD_0420CW_ADR|0);     // NHD-0420CW
            i2c_write(0x40);
            i2c_write(ByteVal);
            i2c_stop();                    // Restart
            delay_us(20);
            break;
    }
}

void LCD_Command(int8 ByteVal){
    i2c_start();
    i2c_write(NHD_0420CW_ADR|0);     // NHD-0420CW
    i2c_write(0x00);
    i2c_write(ByteVal);
    i2c_stop();                    // Restart
    delay_us(20);
}

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group