// Rough example (full code) to understand the operation of LCD interfaced with MCP23S17 using SPI (ported from PICDEM example, Check the schematics attached)
// MOSI: pin 11
// MISO: pin 12
// SCK: pin 13
// CS: pin 10
//For 2 line and 16 character LCD. The address from 0x80 to 0x8F are visible on first line and 0xC0 to 0xCF is visible on second line
#include <SPI.h>
int i=0;
const int slaveSelectPin = 10;
void setup()
{
// set the slaveSelectPin as an output:
pinMode(slaveSelectPin, OUTPUT);
// initialize SPI:
SPI.begin();
Serial.begin(9600);
delay(1000);
LCDInit();
}
void loop() {
if (i==0)
{
i=i+1;// Execute only once
//LCDLine_1(); // Write the cammand to start on line 1
LCDline_pos(1,1); // Select LCD position to line 1, 1st position
d_write('h'); // Write the data one char at a time.
delay(500);
LCDClear(); //Celear LCD
s_write("Hello World");
delay(500);
LCDClear();
delay(500);
s_write("string print");//Write the data by string.
delay(1000);
}
if (Serial.available())
{
LCDline_pos(2,2); // Select LCD position to line 2, 2nd position
d_write(Serial.read()); // Write the data input from serial port
}
}
//*****************************************************************
// LCD busy delay
//*****************************************************************
void LCDBusy(void)
{
delay(10);
}
//*****************************************************************
// Write to MCP923S17 Port A
//*****************************************************************
void WritePortA(char b)
{
digitalWrite(slaveSelectPin, LOW);
SPI.transfer (0x40);
SPI.transfer (0x12);
SPI.transfer (b);
digitalWrite(slaveSelectPin, HIGH);
}
//*****************************************************************
// Write to MCP923S17 Port B
//*****************************************************************
void WritePortB(char b)
{
digitalWrite(slaveSelectPin, LOW);
SPI.transfer (0x40);
SPI.transfer (0x13);
SPI.transfer(b);
digitalWrite(slaveSelectPin, HIGH);
}
//*****************************************************************
// Write the data to the display
//*****************************************************************
void d_write(char b)
{
WritePortA(0x80);
LCDBusy();
WritePortB(b);
delay(0);
WritePortA(0xC0);
delay(0);
WritePortA(0x00);
}
//*****************************************************************
// Send a instruction to the display
//*****************************************************************
void i_write(char b)
{
WritePortA(0x00);
LCDBusy();
WritePortB(b);
delay(100);
WritePortA(0x40);
delay(100);
WritePortA(0x00);
}
//*****************************************************************
// Write to line 1 of the display
//*****************************************************************
void LCDLine_1(void)
{
i_write(0x80);
}
//*****************************************************************
// Write to line 1 of the display
//*****************************************************************
void LCDLine_2(void)
{
i_write(0xC0);
}
//*****************************************************************
// To clear the display
//*****************************************************************
void LCDClear(void)
{
i_write(0x01);
}
//******************************************************************
// Function to write to the PORT
//******************************************************************
void InitWrite(char b)
{
WritePortA(0);
WritePortB(b);
delay(100);
WritePortA(0x40);
delay(100);
WritePortA(0);
}
//*****************************************************************
// Initialize MCP923S17 Port A
//*****************************************************************
void InitPortA_SPI(char b)
{
digitalWrite(slaveSelectPin, LOW);
SPI.transfer (0x40);
SPI.transfer (0x00);
delay(100);
SPI.transfer (b);
delay(100);
digitalWrite(slaveSelectPin, HIGH);
}
//*****************************************************************
// Initialize MCP923S17 Port B
//*****************************************************************
void InitPortB_SPI(char b)
{
digitalWrite(slaveSelectPin, LOW);
SPI.transfer (0x40);
SPI.transfer (0x01);
SPI.transfer(b);
digitalWrite(slaveSelectPin, HIGH);
}
//******************************************************************
// LCD Initialization function
//******************************************************************
void LCDInit(void)
{
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin, HIGH);
InitPortA_SPI(0);
InitPortB_SPI(0);
WritePortA(0);
delay(500);
InitWrite(0x3C); //0011NFxx
delay(500);
InitWrite(0x0C); //Display Off
delay(500);
InitWrite(0x01); //Display Clear
delay(500);
InitWrite(0x06); //Entry mode
}
//*****************************************************************
// Point to the display writing position
//*****************************************************************
void LCDline_pos(int a,int b)
{
if (a==1)
{
if (b>=0 && b<16)
{
int a;
a= 0x80+b,HEX;
Serial.print(a,HEX);
i_write(a);
}
}
if (a==2)
{
if (b>=0 && b<16)
{
int a;
a= 0xC0+b,HEX;
Serial.print(a,HEX);
i_write(a);
}
}
}
//*****************************************************************
// Write the string to the display
//*****************************************************************
void s_write(char data[])
{
int i;
int j;
for(i = 0; data != '\0';i++);
//Serial.print("i=:",i);
for (j=0;j<i;j++)
{
Serial.print("i am in data\n");
//Serial.print("j=:",data[j]);
WritePortA(0x80);
LCDBusy();
WritePortB(data[j]);
delay(0);
WritePortA(0xC0);
delay(0);
WritePortA(0x00);
}
}
16x2LCD ported from PICDEM updated.txt (5.99 KB)