Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

[SOLVED] LCD Connection with PIC32MX250F128D

Status
Not open for further replies.

rajib.das

Member level 3
Joined
Oct 23, 2013
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Hi guys

I am trying to connect a 16x2 3.3V LCD with PIC32MX250F128D. Spent lot of time with no positive result. Please help me.

The following code will explain my connections.


Code:
// LCD module connections
sbit LCD_RS at LATA10_bit;
sbit LCD_EN at LATA7_bit;
//
sbit LCD_D4 at LATC6_bit;
sbit LCD_D5 at LATC7_bit;
sbit LCD_D6 at LATC8_bit;
sbit LCD_D7 at LATC9_bit;
//
sbit LCD_BackPW at LATA8_bit;     


 
//
sbit LCD_RS_Direction at TRISA10_bit;
sbit LCD_EN_Direction at TRISA7_bit;
//
sbit LCD_D4_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC7_bit;
sbit LCD_D6_Direction at TRISC8_bit;
sbit LCD_D7_Direction at TRISC9_bit;

sbit LCD_BackPW_Direction at TRISA8_bit;
// End LCD module connections



char txt1[] = "mikroElektronika";
char txt2[] = "Its a Test";


  LCD_BackPW = 0; //Clear PORTD by setting to 0
  LCD_BackPW_Direction = 0;   // designate PORTB2 pins as output
  
  Lcd_Init();                        // Initialize LCD
  LCD_BackPW = 1;
  
void main(){

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,1,txt2);                 // Write text in second row
  Delay_ms(2000);

  while(1) { 

  
  }

}
FYI I am using Pin21 as PGED1 and Pin22 as PGEC1 for Programming and its working. Oscillator Frequency [MHz] is 10.000000.

Please advise if you can. Thanks in advance.
 
Last edited:

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
here is the code for driving the LCD of a Microchip Explorer 16 with PIC32MX360F512L using MPLABX with the XC32 compiler
Code:
// lcd.c -  Explorer 16 with PIC32MX360F512L 
// functions for the PC1602-J lcd display 8-bit mode
//
// see http://www.repairfaq.org/filipg/LINK/F_LCD_progr.html

#include "hardware.h"
#include "timer.h"
#include "lcd.h"

// LCD display ports etc
#define LCDdata LATE					// data port
#define LCDdataEnable	TRISE
#define RS LATBbits.LATB15				// RS bit in LCDstatus
#define RW LATDbits.LATD5				// read/write bit in LCDstatus
#define E  LATDbits.LATD4   			// enable bit in LCDstatus
#define Eenable TRISDbits.TRISD4
#define ERSenable TRISDbits.TRISD5
#define RWenable TRISBbits.TRISB15

void lcd_delay() { mSecDelay(1); }  // if LCD does not work make this longer

// Write a byte to the LCD
// may have to adjust delays to suit the processor and clock
void lcdByte(int n)
{
	lcd_delay();
    LCDdata=n;
	lcd_delay();
    E=1;					// take clock E high 
	lcd_delay();
    E=0;
	lcd_delay();
 }

// Write a Control Command to the LCD
// This is written as two nibbles
void lcdCmd(int c)
{
 	RS=0;			        // Take RS pin low for command
	lcdByte(c);			        // Makeup Lower Nibble
}

// write a data byte to LCD
int lcdPutchar(int d)
{
//    printf("%c", d);
	RS=1; 				// Take RS pin high for data
	lcdByte(d);		            // Makeup Lower Nibble
    return 1;
}

// Initialise the LCD in 4bit Mode
void lcdInit()
{
	E=0;				// take E low
 	RS=0;				// Take RS pin low for command
 	RW=0;				// Take RS pin low for command
    // set RS, RW and E bits as output
	Eenable =0;
	ERSenable =0;
	RWenable =0;
    LCDdataEnable &= 0x0;          // set bits 0-3 output for data
	lcdByte(0x3);		// This put the LCD into Soft Reset 
	lcdByte(0x3);
	lcdByte(0x3);
	lcdByte(0x2);
	lcd_delay();
//	lcdCmd(0x28);			// 2 line, 4 bit mode 
	lcdCmd(0x38);			// 2 line, 8 bit mode 
	lcd_delay();
    lcdCmd(0x6);			// increment cursor after each write
	lcd_delay();
    lcdCmd(0x1);			// clear display
	lcd_delay();
    lcdCmd(0x2);			// home
	lcd_delay();
    lcdCmd(0xF);			// turn disply on
	lcd_delay();
}

// move to line (1 or 2) at position >= 1
void lcdCursor(unsigned char line,unsigned char position)
{
   switch (line)
	{
	  case 1:lcdCmd (0x80 + position -1 );lcdCmd (0x80 + position -1 ); break;
	  case 2:lcdCmd (0xc0 + position -1 ); lcdCmd (0xc0 + position -1 ); break;
	  default : break;
  	}
  lcd_delay();
}

void lcdString(unsigned char line, unsigned char position, unsigned char *string)
{

	lcdCursor(line,position);
	while (*string)
	  lcdPutchar(*string++);

}

void lcdStringAtCursor(unsigned char *string)
{

	while (*string)
	  lcdPutchar(*string++);
}

void lcdClear(void)
{
	lcdCmd(0x01);
}

void lcdHome(void)
{
	lcdCmd(0x02);
}


// clear line 1 or line 2
void lcdClearLine(unsigned char line)
{
	unsigned char i;
	lcdCursor(line,1);
	for (i=0; i<16; i++) lcdPutchar(' ');
	lcdCursor(line,1);
}
 

rajib.das

Member level 3
Joined
Oct 23, 2013
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Thanks Horace1.
My problem is I have to Use MikroC pro PIC32 for this project. And I am missing lcd.h, hardware.h files.
All the example programs Mikroc pro for PIC32 gave us are all for PIC32MX460F512L. and I don't think their library files are quite suiting PIC32MX250F128D. That's not a expected result from a 250$ product.

Is there anyone can help me on this to make this work. I am sure I am missing something to configure these pins properly.
MY pin configuration would be

sbit LCD_RS at LATA10_bit;
sbit LCD_EN at LATA7_bit;
//
sbit LCD_D4 at LATC6_bit;
sbit LCD_D5 at LATC7_bit;
sbit LCD_D6 at LATC8_bit;
sbit LCD_D7 at LATC9_bit;

FYI : I am using PGEC1/PGED1 for Programming.
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
the lcd.h and hardware.h header files only contain function prototypes and a few consctant for the timer and uart function

assuming your LCD hardware interface is similar to the Explorer 16 the code in post #2 should give you sufficent information to convert it to Microc
e.g. these statements define the pins used (change the declarations to suit your hardware and microC)
Code:
// LCD display ports etc
#define LCDdata LATE					// data port
#define LCDdataEnable	TRISE
#define RS LATBbits.LATB15				// RS bit in LCDstatus
#define RW LATDbits.LATD5				// read/write bit in LCDstatus
#define E  LATDbits.LATD4   			// enable bit in LCDstatus
#define Eenable TRISDbits.TRISD4
#define ERSenable TRISDbits.TRISD5
#define RWenable TRISBbits.TRISB15
and these statements set them up followed by code to initialise the LCD (this sets up 8 bits mode but there is a commented out line to set up 4 bit mode)
Code:
// Initialise the LCD in 4bit Mode
void lcdInit()
{
	E=0;				// take E low
 	RS=0;				// Take RS pin low for command
 	RW=0;				// Take RS pin low for command
    // set RS, RW and E bits as output
	Eenable =0;
	ERSenable =0;
	RWenable =0;
    LCDdataEnable &= 0x0;          // set bits 0-3 output for data
	lcdByte(0x3);		// This put the LCD into Soft Reset 
	lcdByte(0x3);
	lcdByte(0x3);
	lcdByte(0x2);
	lcd_delay();
//	lcdCmd(0x28);			// 2 line, 4 bit mode 
	lcdCmd(0x38);			// 2 line, 8 bit mode 
	lcd_delay();
    lcdCmd(0x6);			// increment cursor after each write
	lcd_delay();
    lcdCmd(0x1);			// clear display
	lcd_delay();
    lcdCmd(0x2);			// home
	lcd_delay();
    lcdCmd(0xF);			// turn disply on
	lcd_delay();
}
you should then be able to write to the LCD with statements such as
Code:
  lcdCursor(1,1);
   lcdStringAtCursor("Explorer 16");
if you are using 4 bit mode you will have to modify void lcdByte(int n) to write two 4 bit values, e.g.
Code:
// Write a nibble to the LCD
// may have to adjust delays to suit the processor and clock
void lcdNibble(int n)
{
    int lcd=LCDdata;
	lcd_delay();
  	LCDdata = (lcd&0xfff0) | ((n & 0x0f));			// send out lower Nibble
	lcd_delay();
    LCDstatus.E=1;					// take clock E high 
	lcd_delay();
    LCDstatus.E=0;
	lcd_delay();
 }

// Write a byte to the LCD
// This is written as two nibbles
void lcdByteint c)
{
 	LCDstatus.RS=0;			        // Take RS pin low for command
	lcdNibble(c >>4);		        // Makeup Upper Nibble
	lcdNibble(c);			        // Makeup Lower Nibble
}
 
Last edited:

rajib.das

Member level 3
Joined
Oct 23, 2013
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Thanks Horace1. Its really working!!!!! Both of your and Mikroc's LCD library working 100%.
By mistake I did setup the internal clock too high. All I had to do to set up the external clock to 8 MHz.


Thanks Everyone.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top