2009/05/17 22:05:48
rockier
I have some question about the Explorer PIC18 demo board.

1. I am looking for C18 source for the LCD display and the 18F8722.

2. What LCD display is on the board?

3. Can the LCD work with the plug in board with the 18F46J50?


Anyone that can point me in the right direction would be great.
2009/06/05 00:33:27
rockier
Well I have not got any response to this so I have worked thru the demo for the LCD and came up with a C18 version.

This is for anyone looking to try the C18 compiler with the PICDEM HPC Explorer 18 Demo Board

This is my frist version but I think it is working preety good.



//*****************************************************************
// PICDEM HPC Explorer 18 LCD function
// Filename : pic18lcd.c
//
// By Rockie Roper
// Converted from the ASM that come with the demo board
//
// Created on 2009-06-05
//
//*****************************************************************
#ifndef __PIC18LCD_C
#define __PIC18LCD_C

#include <p18F8722.h>

#define LCD_CS (LATAbits.LATA2) //LCD chip select
#define LCD_CS_TRIS (TRISAbits.TRISA2) //LCD chip select
#define LCD_RST (LATFbits.LATF6) //LCD chip select
#define LCD_RST_TRIS (TRISFbits.TRISF6) //LCD chip select

#define LCD_TXSTA_TRMT (TXSTAbits.TRMT)
#define LCD_SPI_IF (PIR1bits.SSPIF)
#define LCD_SCK_TRIS (TRISCbits.TRISC3)
#define LCD_SDO_TRIS (TRISCbits.TRISC5)
#define LCD_SSPBUF (SSPBUF)
#define LCD_SPICON1 (SSP1CON1)
#define LCD_SPICON1bits (SSP1CON1bits)
#define LCD_SPICON2 (SSP1CON2)
#define LCD_SPISTAT (SSP1STAT)
#define LCD_SPISTATbits (SSP1STATbits)

extern void Delay(void);
extern void SDelay(void);

#pragma code

//*****************************************************************
// LCD busy delay
//*****************************************************************
void LCDBusy(void)
{
SDelay();
SDelay();
}
//*****************************************************************
// Write to MCP923S17 Port A
//*****************************************************************
void WritePortA(char b)
{
LCD_CS = 0;

LCD_SSPBUF = 0x40;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = 0x12;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = b;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_CS = 1;
}
//*****************************************************************
// Write to MCP923S17 Port B
//*****************************************************************
void WritePortB(char b)
{
LCD_CS = 0;

LCD_SSPBUF = 0x40;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = 0x13;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = b;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_CS = 1;
}
//*****************************************************************
// Write the data to the display
//*****************************************************************
void d_write(char b)
{
WritePortA(0x80);
LCDBusy();
WritePortB(b);
Nop();
Nop();
Nop();
Nop();
WritePortA(0xC0);
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
WritePortA(0x00);
TXREG = b; //carriage return
while(!LCD_TXSTA_TRMT); //wait for data TX
LCD_TXSTA_TRMT = 0;
}
//*****************************************************************
// Send a instruction to the display
//*****************************************************************
void i_write(char b)
{
WritePortA(0x00);
LCDBusy();
WritePortB(b);
Nop();
Nop();
Nop();
Nop();
WritePortA(0x40);
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
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);
Nop();
Nop();
Nop();
WritePortA(0x40);
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
WritePortA(0);
}
//*****************************************************************
// Initialize MCP923S17 Port A
//*****************************************************************
void InitPortA_SPI(char b)
{
LCD_CS = 0;
LCD_SSPBUF = 0x40;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = 0x00;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = b;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_CS = 1;
}
//*****************************************************************
// Initialize MCP923S17 Port B
//*****************************************************************
void InitPortB_SPI(char b)
{
LCD_CS = 0;
LCD_SSPBUF = 0x40;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = 0x01;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_SSPBUF = b;
while(!LCD_SPI_IF);
LCD_SPI_IF = 0;

LCD_CS = 1;
}
//*****************************************************************
// Initialize MCP923S17 SPI
//*****************************************************************
void InitSPI(void)
{
LCD_SCK_TRIS = 0;
LCD_SDO_TRIS = 0;

LCD_SPICON1 = 0x22;
LCD_SPISTATbits.CKE = 1;
//LCD_SPISTATbits.SMP = 0;
LCD_SPI_IF = 0;
}
//******************************************************************
// LCD Initialization function
//******************************************************************
void LCDInit(void)
{
LCD_CS_TRIS = 0;
LCD_CS = 1;
Delay();
Delay();
Delay();

LCD_RST_TRIS = 0;
LCD_RST = 0;
Delay();
LCD_RST = 1;

InitSPI();
InitPortA_SPI(0);
InitPortB_SPI(0);

WritePortA(0);

Delay();
InitWrite(0x3C); //0011NFxx

Delay();
InitWrite(0x0C); //Display Off

Delay();
InitWrite(0x01); //Display Clear

Delay();
InitWrite(0x06); //Entry mode
}

Attachment(s)

HPC18.zip (3.77 KB)
2010/07/19 09:12:56
dushyantha
Hey,i am having little problem regarding SPI.my PIC 18 Explorer boards LCD connect to MCU through MCP23S17 SPI IO Expander.the PIN configuration is given below..

LCD D0-D7 connected to MCP23S17's GPB0-GPB7 
LCD E ->GPB6, RS->GPB7
MCP23S17's SCK connect to PIC's RC3
MCP23S17's Slconnect to PIC's RC5
MCP23S17's RST connect to PIC's RC0
MCP23S17's CS connect to PIC's RC1

but pic Config define in MikroC most pin configs are same but LCD E ->GPB4, RS->GPB2

so when i write some programme using mikroC SPI Lcd8 (8-bit interface) Library LCD doesnt not display anything...

how can i solve this problem?can i change this library ?
2010/07/26 11:38:15
dushyantha
hey ,this coding nor working .pls send me woking code withng this week .i have to present my prototype.
2010/07/26 11:38:58
dushyantha
can u upload working code....
2010/09/26 15:26:53
Martyn
Thanks for this i was just converting the ASM to C and got up to the Check function when stumbling upon this! great stuff thanks you have saved me some time! 
2010/09/30 12:58:54
Martyn
what is not working? it worked fine for me! just save the code rockier generously posted to a file like "LCD.C" and save it in your root dir then include "lcd.c" at the top of the main code file  then Call LCDInit(); 
[font="monospace; border-collapse: collapse; color: rgb(102, 0, 102); white-space: pre; -webkit-border-horizontal-spacing: 1px; -webkit-border-vertical-spacing: 1px; "]Call LCDLine_1() or LCDLine_2() depending which line you want to use (top = 1, bottom = 2)


then i use d_write('A'); which will display A on the LCD


this will works for me on the explorer demo board with the pic18f87j11
[font="monospace; border-collapse: collapse; white-space: pre; -webkit-border-horizontal-spacing: 1px; -webkit-border-vertical-spacing: 1px; "]


if you want i can upload a project that's fully working for me, have to be tomorrow though.


[font="monospace; border-collapse: collapse; white-space: pre; -webkit-border-horizontal-spacing: 1px; -webkit-border-vertical-spacing: 1px; "]

//write a NULL terminated char array to the LCD *note lcd is 16 chars width

//not sure if this is correct c18 syntax but it works for me
// top = lcd line to write to 1= top, 2= bottom
//i probably exaggerated the NOPs a bit and may not need that many
LCDWriteBytes(far char *bytes, char top)
{
if (top == 1)
    LCDLine_1();

else if(top == 2)
    LCDLine_2();



while (*bytes)
{
d_write(*bytes++);
Nop;Nop;
Nop;Nop;Nop;Nop;
Nop;Nop;
}
}




2010/11/14 01:27:04
skysoft
Thanks !, you saved me lots of time today !
2011/02/24 02:07:22
ravikumar001

hi,

PIC C18 tutorial LCD here

i hope it useful.






2011/05/13 08:37:29
indigo79
This is awesome Rockier! This is what I was looking for! However, I have extracted the project and included the "pic18lcd.c" in the main c file and I get this "multiple definitions" error for every function... how do I sort this? I've played with it a while, I can't see where it gets the multple definitions from... I haven't modified anything else. Cheers!
12
© 2017 APG vNext Commercial Version 4.5

Use My Existing Forum Account