PIC8-Bit Trainer - Module LCD - NHD-C0216CZ - ST7032 - 2x16 Characters.

0.Contents

PIC18F Trainer : PIC18F2xJ13 @8MHz Internal oscillator.

1.String & Custom Patterns - MSSP1 SPI - Backlight auto power off.


- SPI MODE0 : CKP = 0, CKE = 1.
- SPI MODE1 : CKP = 0, CKE = 0.
- SPI MODE2 : CKP = 1, CKE = 1.
- SPI MODE3 : CKP = 1, CKE = 0.

// Configuration registers.
#pragma config WDTEN=OFF, PLLDIV=1, CFGPLLEN=OFF, STVREN=ON, XINST=OFF
#pragma config CP0=OFF, OSC=INTOSCO, SOSCSEL=HIGH, CLKOEC=OFF, FCMEN=OFF
#pragma config IESO=OFF, WDTPS=32768, DSWDTOSC=INTOSCREF, RTCOSC=T1OSCREF
#pragma config DSBOREN=ON, DSWDTEN=ON, DSWDTPS=G2
#pragma config IOL1WAY=ON, ADCSEL=BIT10, PLLSEL=PLL4X, MSSP7B_EN=MSK7
#pragma config WPFP=PAGE_63, WPCFG=OFF, WPDIS=OFF, WPEND=PAGE_WPFP

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18FxxJ13 - Compile with XC8(v2.05).
// PIC18FxxJ13 - @8MHz Internal oscillator.

// LCD - NHD-C0216CZ - ST7032 - MSSP1 SPI Backlight auto power off.
// String & Custom Patterns.

// Expansion Board - NHD-C0216CZ - Rev.A.

// MODULE.01 -> GND.
// MODULE.02 -> VDD - 3V3.
// MODULE.03 -> MCU.RC2.
// MODULE.04 -> MCU.RA5.
// MODULE.05 -> MCU.RC3.
// MODULE.06 -> MCU.RC5.
// MODULE.07 -> MCU.RC0.
// MODULE.08 -> GND.
// MODULE.09 -> NC.
// MODULE.10 -> NC.
// MODULE.11 -> NC.
// MODULE.12 -> NC.
// MODULE.13 -> NC.
// MODULE.14 -> NC.
// MODULE.15 -> NC.
// MODULE.16 -> NC.

// JP1 - SCL Open.
// JP2 - SDA Open.
// JP3 - VEE Open.
// JP4 - BCKL Not Use.

// Definitions.
// SPI Port.
#define SPI_CS_TRIS												TRISCbits.TRISC1
#define SPI_SCK_TRIS											TRISCbits.TRISC3
#define SPI_SDI_TRIS											TRISCbits.TRISC4
#define SPI_SDO_TRIS											TRISCbits.TRISC5
// ST7032 Port.
#define ST7032_RS												LATCbits.LATC2
#define ST7032_RS_TRIS											TRISCbits.TRISC2
#define ST7032_BACKLIGHT										LATCbits.LATC0
#define ST7032_BACKLIGHT_TRIS									TRISCbits.TRISC0
// ST7032 Commands IS = 0 - Normal Instruction Select.
#define ST7032_CLEAR_DISPLAY									0x01
#define ST7032_RETURN_HOME										0x02
#define ST7032_ENTRY_MODE_DDRAM_DECREMENT_NOSHIFT				0x04
#define ST7032_ENTRY_MODE_DDRAM_DECREMENT_SHIFT_RIGHT			0x05
#define ST7032_ENTRY_MODE_DDRAM_INCREMENT_NOSHIFT				0x06
#define ST7032_ENTRY_MODE_DDRAM_INCREMENT_SHIFT_LEFT			0x07
#define ST7032_DISPLAY_OFF										0x08
#define ST7032_DISPLAY_ON_CURSOR_OFF							0x0C
#define ST7032_DISPLAY_ON_CURSOR_ON_NOBLINK						0x0E
#define ST7032_DISPLAY_ON_CURSOR_ON_BLINK						0x0F
#define ST7032_DISPLAY_CURSOR_SHIFT_LEFT						0x10
#define ST7032_DISPLAY_CURSOR_SHIFT_RIGHT						0x14
#define ST7032_DISPLAY_SCREEN_SHIFT_LEFT						0x18
#define ST7032_DISPLAY_SCREEN_SHIFT_RIGHT						0x1C
#define ST7032_FUNCTION_SET_4_BIT_ONE_LINE_FONT5x8_NORMAL		0x20
#define ST7032_FUNCTION_SET_4_BIT_ONE_LINE_FONT5x8_EXTENSION	0x21
#define ST7032_FUNCTION_SET_4_BIT_ONE_LINE_FONT5x16_NORMAL		0x24
#define ST7032_FUNCTION_SET_4_BIT_ONE_LINE_FONT5x16_EXTENSION	0x25
#define ST7032_FUNCTION_SET_4_BIT_TWO_LINE_FONT5x8_NORMAL		0x28
#define ST7032_FUNCTION_SET_4_BIT_TWO_LINE_FONT5x8_EXTENSION	0x29
#define ST7032_FUNCTION_SET_8_BIT_ONE_LINE_FONT5x8_NORMAL		0x30
#define ST7032_FUNCTION_SET_8_BIT_ONE_LINE_FONT5x8_EXTENSION	0x31
#define ST7032_FUNCTION_SET_8_BIT_ONE_LINE_FONT5x16_NORMAL		0x34
#define ST7032_FUNCTION_SET_8_BIT_ONE_LINE_FONT5x16_EXTENSION	0x35
#define ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_NORMAL		0x38
#define ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_EXTENSION	0x39
#define ST7032_SET_CGRAM_ADDRESS								0x40
#define ST7032_SET_DDRAM_ADDRESS_FIRST_LINE						0x80
#define ST7032_SET_DDRAM_ADDRESS_SECOND_LINE					0xC0
// ST7032 Commands IS = 1 - Extension Instruction Select.
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_122H_VDD5_120H		0x10
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_131H_VDD5_133H		0x11
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_144H_VDD5_149H		0x12
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_161H_VDD5_167H		0x13
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_183H_VDD5_192H		0x14
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_221H_VDD5_227H		0x15
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_274H_VDD5_277H		0x16
#define ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_347H_VDD5_347H		0x17
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_122H_VDD5_120H		0x18
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_131H_VDD5_133H		0x19
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_144H_VDD5_149H		0x1A
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_161H_VDD5_167H		0x1B
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_183H_VDD5_192H		0x1C
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_221H_VDD5_227H		0x1D
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_274H_VDD5_277H		0x1E
#define ST7032_INTERNAL_OSC_BIAS_1_4_VDD3V_347H_VDD5_347H		0x1F
#define ST7032_SET_ICON_ADDRESS									0X40
#define ST7032_POWER_ICON_OFF_BOOST_OFF_CONTRAST_MSB_0			0x50
#define ST7032_POWER_ICON_OFF_BOOST_OFF_CONTRAST_MSB_1			0x51
#define ST7032_POWER_ICON_OFF_BOOST_OFF_CONTRAST_MSB_2			0x52
#define ST7032_POWER_ICON_OFF_BOOST_OFF_CONTRAST_MSB_3			0x53
#define ST7032_POWER_ICON_OFF_BOOST_ON_CONTRAST_MSB_0			0x54
#define ST7032_POWER_ICON_OFF_BOOST_ON_CONTRAST_MSB_1			0x55
#define ST7032_POWER_ICON_OFF_BOOST_ON_CONTRAST_MSB_2			0x56
#define ST7032_POWER_ICON_OFF_BOOST_ON_CONTRAST_MSB_3			0x57
#define ST7032_POWER_ICON_ON_BOOST_OFF_CONTRAST_MSB_0			0x58
#define ST7032_POWER_ICON_ON_BOOST_OFF_CONTRAST_MSB_1			0x59
#define ST7032_POWER_ICON_ON_BOOST_OFF_CONTRAST_MSB_2			0x5A
#define ST7032_POWER_ICON_ON_BOOST_OFF_CONTRAST_MSB_3			0x5B
#define ST7032_POWER_ICON_ON_BOOST_ON_CONTRAST_MSB_0			0x5C
#define ST7032_POWER_ICON_ON_BOOST_ON_CONTRAST_MSB_1			0x5D
#define ST7032_POWER_ICON_ON_BOOST_ON_CONTRAST_MSB_2			0x5E
#define ST7032_POWER_ICON_ON_BOOST_ON_CONTRAST_MSB_3			0x5F
#define ST7032_FOLLOWER_CONTROL_OFF								0x60
#define ST7032_FOLLOWER_CONTROL_ON_RAB_0						0x68
#define ST7032_FOLLOWER_CONTROL_ON_RAB_1						0x69
#define ST7032_FOLLOWER_CONTROL_ON_RAB_2						0x6A
#define ST7032_FOLLOWER_CONTROL_ON_RAB_3						0x6B
#define ST7032_FOLLOWER_CONTROL_ON_RAB_4						0x6C
#define ST7032_FOLLOWER_CONTROL_ON_RAB_5						0x6D
#define ST7032_FOLLOWER_CONTROL_ON_RAB_6						0x6E
#define ST7032_FOLLOWER_CONTROL_ON_RAB_7						0x6F
#define ST7032_CONTRAST_LSB_0									0x70
#define ST7032_CONTRAST_LSB_1									0x71
#define ST7032_CONTRAST_LSB_2									0x72
#define ST7032_CONTRAST_LSB_3									0x73
#define ST7032_CONTRAST_LSB_4									0x74
#define ST7032_CONTRAST_LSB_5									0x75
#define ST7032_CONTRAST_LSB_6									0x76
#define ST7032_CONTRAST_LSB_7									0x77
#define ST7032_CONTRAST_LSB_8									0x78
#define ST7032_CONTRAST_LSB_9									0x79
#define ST7032_CONTRAST_LSB_10									0x7A
#define ST7032_CONTRAST_LSB_11									0x7B
#define ST7032_CONTRAST_LSB_12									0x7C
#define ST7032_CONTRAST_LSB_13									0x7D
#define ST7032_CONTRAST_LSB_14									0x7E
#define ST7032_CONTRAST_LSB_15									0x7F

// Function prototypes.
void spi_initializeMaster(void);
void st7032_clearDisplay(void);
void st7032_initialize(void);
void st7032_writeInstruction(char cData);
void st7032_writeData(char cData);
void st7032_writeString(const char * cData);
void st7032_writeStringSetCursor(const char * cData, uint8_t u8Data);

// Strings & Custom patterns.
const char string0[] = "Tronix I/O";
const char string1[] = "www.tronix.io";
const char custom_patterns[5][8] = {
	{0x0e, 0x1b, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1f},
	{0x0e, 0x1b, 0x11, 0x11, 0x11, 0x1f, 0x1f, 0x1f},
	{0x0e, 0x1b, 0x11, 0x11, 0x1f, 0x1f, 0x1f, 0x1f},
	{0x0e, 0x1b, 0x11, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f},
	{0x0e, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f}
};

// Main.
int main(void)
{
	// MCU Initialization.
	// Internal Oscillator Frequency 8MHz.
	OSCCONbits.IRCF = 0b111;
	// Ports Initialization.
    // Open-drain Output Settings.
    ODCON3 = 0b00000000;
    ODCON2 = 0b00000000;
    ODCON1 = 0b00000000;
	// Analog Input Settings.
	ANCON1 = 0b00011111;
	ANCON0 = 0b00011111;	// For PIC18F2xJ13.
	//ANCON0 = 0b11111111;	// For PIC18F4xJ13.
	// Port A.
	PORTA = 0b00000000;
	TRISA = 0b00000000;
	// Port B.
	PORTB = 0b00000000;
	TRISB = 0b00000000;
	// Port C.
	PORTC = 0b00000000;
	TRISC = 0b00000000;
	// Port D.
	//PORTD = 0b00000000;	// For PIC18F4xJ13.
	//TRISD = 0b00000000;	// For PIC18F4xJ13.
	// Port E.
	//PORTE = 0b00000000;	// For PIC18F4xJ13.
	//TRISE = 0b00000000;	// For PIC18F4xJ13.
	// SPI Port.
	SPI_CS_TRIS = 0;
	SPI_SCK_TRIS = 0;
	SPI_SDI_TRIS = 1;
	SPI_SDO_TRIS = 0;
	// ST7032 Register Select.
	ST7032_RS = 0;
	ST7032_RS_TRIS = 0;
	// ST7032 Backlight.
	ST7032_BACKLIGHT = 1;
	ST7032_BACKLIGHT_TRIS = 0;

	spi_initializeMaster();
	st7032_initialize();

	// Write 5x8 Dots Custom Patterns in ST7032 CGRAM.
	st7032_writeInstruction(ST7032_SET_CGRAM_ADDRESS);
	uint8_t line = 0;
	uint8_t pattern = 0;
	for(pattern=0; pattern<5; pattern++){
		for(line=0; line<8; line++){
			st7032_writeData(custom_patterns[pattern][line]);
		}
	}

	// Display String.
	st7032_writeStringSetCursor(string0, ST7032_SET_DDRAM_ADDRESS_FIRST_LINE);
	st7032_writeStringSetCursor(string1, ST7032_SET_DDRAM_ADDRESS_SECOND_LINE);

	// Backlight delay ~36s.
	uint8_t st7032_backlight = 10;
	while(1){
		st7032_backlight--;
		if(!st7032_backlight){
			ST7032_BACKLIGHT = 0;
		}

		for(pattern=0; pattern<5; pattern++){
			st7032_writeInstruction(ST7032_SET_DDRAM_ADDRESS_FIRST_LINE + 0x0f);
			st7032_writeData(pattern);
			__delay_ms(800);
		}
	}
	return(0);
}

// Functions.
void spi_initializeMaster(void)
{
	// SPI1 - Master - Mode 0.
    SSPSTAT = 0b01000000;
    // SPI1 - Fosc / 16.
	// Clock frequency = 8000000 / 16 = 500kHz.
	SSP1CON1 = 0b00000001;
    // SPI1 Start.
    SSP1CON1bits.SSPEN1 = 1;
}

void st7032_clearDisplay(void)
{
	st7032_writeInstruction(ST7032_CLEAR_DISPLAY);
	__delay_ms(2);
}
void st7032_initialize(void)
{
	uint8_t dummy;

	__delay_ms(50);
	ST7032_RS = 0;
	SSP1BUF = ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_EXTENSION;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_EXTENSION;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_183H_VDD5_192H;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_POWER_ICON_OFF_BOOST_ON_CONTRAST_MSB_2;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_CONTRAST_LSB_6;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_FOLLOWER_CONTROL_ON_RAB_5;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_NORMAL;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_ENTRY_MODE_DDRAM_INCREMENT_NOSHIFT;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_DISPLAY_ON_CURSOR_OFF;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	SSP1BUF = ST7032_CLEAR_DISPLAY;
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
	__delay_ms(2);
}

void st7032_writeInstruction(char cData)
{
	uint8_t dummy;

	ST7032_RS = 0;
	SSP1BUF = (cData);
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
}

void st7032_writeData(char cData)
{
	uint8_t dummy;

	ST7032_RS = 1;
	SSP1BUF = (cData);
	while(!SSP1STATbits.BF);
	dummy = SSP1BUF;
}

void st7032_writeString(const char * cData)
{
	while(*cData != '\0')
		st7032_writeData(*cData++);
}

void st7032_writeStringSetCursor(const char * cData, uint8_t u8Data)
{
	st7032_writeInstruction(u8Data);
	while(*cData != '\0')
		st7032_writeData(*cData++);
}

2.TODO - String & Custom Patterns - MSSP2 SPI - PPS - Backlight auto power off. - TODO

05.2019