bigdogguru
Administrator
- Joined
- Mar 12, 2010
- Messages
- 9,821
- Helped
- 2,350
- Reputation
- 4,694
- Reaction score
- 2,272
- Trophy points
- 1,413
- Location
- Southwest, USA
- Activity points
- 62,383
That's very kind of you!
Hopefully if the C18 code is simple enough for me to understand I can send the commands the mirkroC way!
call Delay
movlw b'00110000'
movwf temp_wr
call InitWrite
call Delay
movlw b'00110000'
movwf temp_wr
call InitWrite
call Delay
movlw b'00110000'
movwf temp_wr
call InitWrite
;Function Set
call Delay
movlw b'00111100' ;0011NFxx
movwf temp_wr
call InitWrite
call Delay
movlw b'00001100' ;Display off
movwf temp_wr
call InitWrite
call Delay
movlw b'00000001' ;Display Clear
movwf temp_wr
call InitWrite
call Delay
movlw b'00000110' ;Entry mode
movwf temp_wr
call InitWrite
Thank you for finding all this! Unfortunately I don't really understand the ASM routine, I cannot tell which port of the expander is being written too. I'm pretty out of my depth here...
// Port Expander module connections
sbit SPExpanderRST at RF6_bit;
sbit SPExpanderCS at RA2_bit;
sbit SPExpanderRST_Direction at TRISF6_bit;
sbit SPExpanderCS_Direction at TRISA2_bit;
// End Port Expander module connections
void main(){
CMCON = 0x07; // turn off comparators
ADCON1 = 0x3f;
SPI1_Init();
//Note that Port Expander pins A2 A1 A0 are connected to GND so Port Expander Hardware Address is 0.
Expander_Init(0);
Expander_Set_DirectionPortA(0,0x00); // Set Expander's PORTA to be output
Expander_Set_DirectionPortB(0,0x00); // Set Expander's PORTB to be output
Expander_Write_PortB(0, 0x55); // Write to expander's PORTB
while(1);
}
//*****************************************************************
// PICDEM Explorer 18 Demo for LCD display
// Filename : main.c
//
// By Rockie Roper
// Converted from the ASM that come with the demo board
//
// Created on 2009-06-05
//
// Compiler is the C18 from MPLAB
//
//*****************************************************************
#include <p18F8722.h>
#pragma config OSC = HSPLL
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma udata UDATA_ACS
unsigned char LCDText[16*2+1];
#pragma code
void main(void)
{
int tmpcnt = 0;
OSCTUNEbits.PLLEN = 1;
// Initialize the LCD display
LCDInit();
ADCON1 = 0b00001110;
TXSTA = 0b10100100;
SPBRG = 0xff;
RCSTA = 0b10010000;
TRISBbits.TRISB0 = 1;
TRISAbits.TRISA5 = 1;
while(1)
{
// Write the cammand to start on line 1
LCDLine_1();
// Write the data one char at a time.
d_write('H');
d_write('e');
d_write('l');
d_write('l');
d_write('0');
// Write the cammand to start on line 2
LCDLine_2();
// Write the data to line 2 one char at a time
// You can put this in a loop and read from a table
d_write('P');
d_write('I');
d_write('C');
d_write('1');
d_write('8');
delay_1s();
tmpcnt++;
switch(tmpcnt)
{
case 10:
d_write(' ');
d_write('1');
d_write('0');
break;
case 20:
d_write(' ');
d_write('2');
d_write('0');
break;
case 30:
d_write(' ');
d_write('3');
d_write('0');
break;
case 40:
d_write(' ');
d_write('4');
d_write('0');
break;
}
}
}
//*****************************************************************
// 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
}
Expander_Set_DirectionPortB(0,0x00); // Set Expander's PORTB to be output
Expander_Write_PortB(0, 0x55); // Write to expander's PORTB
//******************************************************************
// LCD Initialization function
//******************************************************************
void LCDInit(void)
{
LCD_CS_TRIS = 0;
LCD_CS = 1;
Delay_ms(10);
Delay_ms(10);
Delay_ms(10);
LCD_RST_TRIS = 0;
LCD_RST = 0;
Delay_ms(10);
LCD_RST = 1;
SPI1_Init();
Expander_Init();
Expander_Set_DirectionPortA(0,0x00); // Set Expander's PORTA to be output
Expander_Set_DirectionPortB(0,0x00); // Set Expander's PORTB to be output
WritePortA(0);
Delay_ms(10);
InitWrite(0x3C); //0011NFxx
Delay_ms(10);
InitWrite(0x0C); //Display Off
Delay_ms(10);
InitWrite(0x01); //Display Clear
Delay_ms(10);
InitWrite(0x06); //Entry mode
}
TXREG = b; //carriage return
while(!LCD_TXSTA_TRMT); //wait for data TX
LCD_TXSTA_TRMT = 0;
// Port Expander module connections
sbit SPExpanderRST at RF6_bit;
sbit SPExpanderCS at RA2_bit;
sbit SPExpanderRST_Direction at TRISF6_bit;
sbit SPExpanderCS_Direction at TRISA2_bit;
// End Port Expander module connections
sbit LCD_RST at RF6_bit;
sbit LCD_CS at RA2_bit;
sbit LCD_RST_TRIS at TRISF6_bit;
sbit LCD_CS_TRIS at TRISA2_bit;
sbit LCD_SCK_TRIS at TRISC3_bit;
sbit LCD_SDO_TRIS at TRISC5_bit;
#define LCD_TXSTA_TRMT (TXSTAbits.TRMT)
#define LCD_SPI_IF (PIR1bits.SSPIF)
#define LCD_SSPBUF (SSPBUF)
#define LCD_SPICON1 (SSP1CON1)
#define LCD_SPICON1bits (SSP1CON1bits)
#define LCD_SPICON2 (SSP1CON2)
#define LCD_SPISTAT (SSP1STAT)
//extern void Delay(void);
//extern void SDelay(void);
//*****************************************************************
// Write to MCP923S17 Port A
//*****************************************************************
void WritePortA(char b)
{
Expander_Write_PortA(0, b);
}
//*****************************************************************
// Write to MCP923S17 Port B
//*****************************************************************
void WritePortB(char b)
{
Expander_Write_PortB(0, b);
}
//*****************************************************************
// 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);
Delay_ms(10);
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);
}
//******************************************************************
// LCD Initialization function
//******************************************************************
void LCDInit(void)
{
LCD_CS_TRIS = 0;
LCD_CS = 1;
Delay_ms(10);
Delay_ms(10);
Delay_ms(10);
LCD_RST_TRIS = 0;
LCD_RST = 0;
Delay_ms(10);
LCD_RST = 1;
SPI1_Init();
Expander_Init();
Expander_Set_DirectionPortA(0,0x00); // Set Expander's PORTA to be output
Expander_Set_DirectionPortB(0,0x00); // Set Expander's PORTB to be output
WritePortA(0);
Delay_ms(10);
InitWrite(0x3C); //0011NFxx
Delay_ms(10);
InitWrite(0x0C); //Display Off
Delay_ms(10);
InitWrite(0x01); //Display Clear
Delay_ms(10);
InitWrite(0x06); //Entry mode
}
// Port Expander module connections
sbit SPExpanderRST at RF6_bit;
sbit SPExpanderCS at RA2_bit;
sbit SPExpanderRST_Direction at TRISF6_bit;
sbit SPExpanderCS_Direction at TRISA2_bit;
// End Port Expander module connections
sbit LCD_RST at RF6_bit;
sbit LCD_CS at RA2_bit;
sbit LCD_RST_TRIS at TRISF6_bit;
sbit LCD_CS_TRIS at TRISA2_bit;
sbit LCD_SCK_TRIS at TRISC3_bit;
sbit LCD_SDO_TRIS at TRISC5_bit;
//extern void Delay(void);
//extern void SDelay(void);
//*****************************************************************
// Write to MCP923S17 Port A
//*****************************************************************
void WritePortA(char b)
{
Expander_Write_PortA(0, b);
}
//*****************************************************************
// Write to MCP923S17 Port B
//*****************************************************************
void WritePortB(char b)
{
Expander_Write_PortB(0, b);
}
//*****************************************************************
// Write the data to the display
//*****************************************************************
void d_write(char b)
{
WritePortA(0x80);
Delay_ms(10);
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);
Delay_ms(10);
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);
}
//******************************************************************
// LCD Initialization function
//******************************************************************
void LCDInit(void)
{
LCD_CS_TRIS = 0;
LCD_CS = 1;
Delay_ms(10);
Delay_ms(10);
Delay_ms(10);
LCD_RST_TRIS = 0;
LCD_RST = 0;
Delay_ms(10);
LCD_RST = 1;
SPI1_Init();
Expander_Init(0);
Expander_Set_DirectionPortA(0,0x00); // Set Expander's PORTA to be output
Expander_Set_DirectionPortB(0,0x00); // Set Expander's PORTB to be output
WritePortA(0);
Delay_ms(10);
InitWrite(0x3C); //0011NFxx
Delay_ms(10);
InitWrite(0x0C); //Display Off
Delay_ms(10);
InitWrite(0x01); //Display Clear
Delay_ms(10);
InitWrite(0x06); //Entry mode
}
void main(void)
{
// Initialize the LCD display.
LCDInit();
// Switch ti LCD line 1 ( sends a command to the LCD to tell it ).
LCDLine_1();
// Write the data one char at a time.
d_write('T');
d_write('H');
d_write('A');
d_write('N');
d_write('K');
d_write('S');
LCDLine_2();
d_write('B');
d_write('I');
d_write('G');
d_write(' ');
d_write('D');
d_write('O');
d_write('G');
while(1);
}
Another point would be to report the apparent bug to mikroC support.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?