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

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.String & Custom Patterns - Hardware SPI - PWM Controlled Backlight.

// Configuration registers.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO67
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

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

// LCD - NHD-C0216CZ - ST7032 - Hardware SPI - PWM Controlled Backlight.
// String & Custom Patterns.

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

// MODULE.01 -> GND.
// MODULE.02 -> VDD - 3V3.
// MODULE.03 -> MCU.RC0.
// MODULE.04 -> MCU.RC1.
// MODULE.05 -> MCU.RC3.
// MODULE.06 -> MCU.RC5.
// MODULE.07 -> MCU.RC2.
// MODULE.08 -> NC.
// 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													LATCbits.LATC1
#define SPI_CS_TRIS												TRISCbits.TRISC1
#define SPI_SCK_TRIS											TRISCbits.TRISC3
#define SPI_SDI_TRIS											TRISCbits.TRISC5
// ST7032 Port.
#define ST7032_RS												LATCbits.LATC0
#define ST7032_RS_TRIS											TRISCbits.TRISC0
// Backlight.
#define BACKLIGHT												CCPR1L
#define ST7032_BACKLIGHT										LATCbits.LATC2
#define ST7032_BACKLIGHT_TRIS									TRISCbits.TRISC2
// 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)
{
	// PWM Initialization.
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// SPI Port.
	SPI_CS = 1;
	SPI_CS_TRIS  = 0;
	SPI_SCK_TRIS = 0;
	SPI_SDI_TRIS = 0;
	// ST7032 Register Select.
	ST7032_RS = 0;
	ST7032_RS_TRIS = 0;
	// Output for Backlight.
	ST7032_BACKLIGHT = 0;
	ST7032_BACKLIGHT_TRIS = 0;

	// PWM Initialization.
	// Timer 2 - 1kHz @8MHz.
	T2CON = 0b00000110;
	TMR2 = 0x00;
	PR2 = 0x80;
	// PWM Mode.
	CCP1CON = 0b00001100;

	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);

	uint8_t backlight;
	while(1){
		for(backlight=0; backlight<0x80; backlight++){
			BACKLIGHT = backlight;
			__delay_ms(50);
		}
		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)
{
	// SPI - Master, Mode 0, MSB First.
	// SPI Clock frequency = 8000000 / 16 = 500kHz.
	SSPSTAT = 0x40;
	SSPCON1 = 0x21;
}

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

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

void st7032_writeInstruction(char cData)
{
	uint8_t dummy;

	SPI_CS = 0;
	ST7032_RS = 0;
	SSPBUF = (cData);
	while(!SSPSTATbits.BF);
	dummy = SSPBUF;
	SPI_CS = 1;
}

void st7032_writeData(char cData)
{
	uint8_t dummy;

	SPI_CS = 0;
	ST7032_RS = 1;
	SSPBUF = (cData);
	while(!SSPSTATbits.BF);
	dummy = SSPBUF;
	SPI_CS = 1;
}

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.String & Custom Patterns - Software SPI - PWM Controlled Backlight.

// Configuration registers.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO67
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

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

// LCD - NHD-C0216CZ - ST7032 - Software SPI - PWM Controlled Backlight.
// String & Custom Patterns.

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

// MODULE.01 -> GND.
// MODULE.02 -> VDD - 3V3.
// MODULE.03 -> MCU.RC0.
// MODULE.04 -> MCU.RC1.
// MODULE.05 -> MCU.RC3.
// MODULE.06 -> MCU.RC5.
// MODULE.07 -> MCU.RC2.
// MODULE.08 -> NC.
// 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													LATCbits.LATC1
#define SPI_CS_TRIS												TRISCbits.TRISC1
#define SPI_SCK                                                 LATCbits.LATC3
#define SPI_SCK_TRIS											TRISCbits.TRISC3
#define SPI_SDI                                                 LATCbits.LATC5
#define SPI_SDI_TRIS											TRISCbits.TRISC5
// ST7032 Port.
#define ST7032_RS												LATCbits.LATC0
#define ST7032_RS_TRIS											TRISCbits.TRISC0
// Backlight.
#define BACKLIGHT												CCPR1L
#define ST7032_BACKLIGHT										LATCbits.LATC2
#define ST7032_BACKLIGHT_TRIS									TRISCbits.TRISC2
// 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 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 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// SPI Port.
	SPI_CS = 1;
	SPI_CS_TRIS  = 0;
	SPI_SCK = 0;
	SPI_SCK_TRIS = 0;
	SPI_SDI = 0;
	SPI_SDI_TRIS = 0;
	// ST7032 Register Select.
	ST7032_RS = 0;
	ST7032_RS_TRIS = 0;
	// Output for Backlight.
	ST7032_BACKLIGHT = 0;
	ST7032_BACKLIGHT_TRIS = 0;

	// PWM Initialization.
	// Timer 2 - 1kHz @8MHz.
	T2CON = 0b00000110;
	TMR2 = 0x00;
	PR2 = 0x80;
	// PWM Mode.
	CCP1CON = 0b00001100;

	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);

	uint8_t backlight;
	while(1){
		for(backlight=0; backlight<0x80; backlight++){
			BACKLIGHT = backlight;
			__delay_ms(50);
		}
		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 st7032_clearDisplay(void)
{
	st7032_writeInstruction(ST7032_CLEAR_DISPLAY);
	__delay_ms(2);
}

void st7032_initialize(void)
{
	__delay_ms(50);

	st7032_writeInstruction(ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_EXTENSION);
	st7032_writeInstruction(ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_EXTENSION);
	st7032_writeInstruction(ST7032_INTERNAL_OSC_BIAS_1_5_VDD3V_183H_VDD5_192H);
	st7032_writeInstruction(ST7032_POWER_ICON_OFF_BOOST_ON_CONTRAST_MSB_2);
	st7032_writeInstruction(ST7032_CONTRAST_LSB_6);
	st7032_writeInstruction(ST7032_FOLLOWER_CONTROL_ON_RAB_5);
	st7032_writeInstruction(ST7032_FUNCTION_SET_8_BIT_TWO_LINE_FONT5x8_NORMAL);
	st7032_writeInstruction(ST7032_ENTRY_MODE_DDRAM_INCREMENT_NOSHIFT);
	st7032_writeInstruction(ST7032_DISPLAY_ON_CURSOR_OFF);
	st7032_writeInstruction(ST7032_CLEAR_DISPLAY);
	__delay_ms(2);
}

void st7032_writeInstruction(char cData)
{
	uint8_t count;

	count = 7;
	SPI_CS = 0;
	ST7032_RS = 0;
	do{
		if(cData & 0x80)
			SPI_SDI = 1;
		else
			SPI_SDI = 0;
		SPI_SCK = 1;
		SPI_SCK = 0;
		cData = cData<<1;
	}while(count--);
	SPI_CS = 1;
	SPI_SDI = 0;
}

void st7032_writeData(char cData)
{
	uint8_t count;

	count = 7;
	SPI_CS = 0;
	ST7032_RS = 1;
	do{
		if(cData & 0x80)
			SPI_SDI = 1;
		else
			SPI_SDI = 0;
		SPI_SCK = 1;
		SPI_SCK = 0;
		cData = cData<<1;
	}while(count--);
	SPI_CS = 1;
	SPI_SDI = 0;
}

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++);
}

09.2018