Problem conecting Pic(pic 16f887) to lcd (2x16) 4 bit mode? can you help me

Status
Not open for further replies.

luanktd

Newbie level 5
Joined
Apr 13, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,328
This is my code. I try many times but the Program not run.
Can you help me fix it?
Thanks.

Code:
#include <htc.h>
#include "delay.h"
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & BOREN_OFF
         & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF); //1st config. Word
__CONFIG(BOR4V_BOR21V); //2st config.
#define _XTAL_FREQ 8000000
#define LCD_EN RB5      //EN line
#define  LCD_RS RB4     //R/S line
#define LCD_D7 RB3
#define LCD_D6 RB2
#define LCD_D5 RB1
#define LCD_D4 RB0
//data pins for 4bit mode
#define    LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))
void lcd_write(unsigned char c)
{
    __delay_us(40);
    //MS nibble
    LCD_D7 = (c & 0x80)?1:0;
    c<<=1;
    LCD_D6 = (c & 0x80)?1:0;
    c<<=1;
    LCD_D5 = (c & 0x80)?1:0;
    c<<=1;
    LCD_D4 = (c & 0x80)?1:0;
    c<<=1;

    LCD_STROBE();

    //LS nibble
    LCD_D7 = (c & 0x80)?1:0;
    c<<=1;
    LCD_D6 = (c & 0x80)?1:0;
    c<<=1;
    LCD_D5 = (c & 0x80)?1:0;
    c<<=1;
    LCD_D4 = (c & 0x80)?1:0;

    LCD_STROBE();
}
/*
 *     Clear and home the LCD
 */

void lcd_clear(void)
{
    LCD_RS = 0;
    lcd_write(0x1);
    __delay_ms(2);
}
/*
 * Go to the specified position
 */

void lcd_goto(unsigned char pos)
{
    LCD_RS = 0;
    lcd_write(0x80+pos);
}

/* initialise the LCD - put into 4 bit mode */
void lcd_init()
{

    LCD_RS = 0;
    LCD_EN = 0;

    __delay_ms(15);    // wait 15mSec after power applied,

    LCD_D4 = 1;//0x3 & 0x01;                //bit0 000X
    LCD_D5 = 1;//(0x3>>1) & 0x01;        //bit1 00XY -> 000X
    LCD_D6 = 0;//(0x3>>2) & 0x01;        //bit2 0XYZ -> 000X
    LCD_D7 = 0;//(0x3>>3) & 0x01;        //bit3 XYZW -> 000X

    LCD_STROBE();
    __delay_ms(5);
    LCD_STROBE();
    __delay_us(200);
    LCD_STROBE();
    __delay_us(200);

    // Four bit mode
    LCD_D4 = 0;//2 & 0x01;                //bit0 000X
    LCD_D5 = 1;//(2>>1) & 0x01;        //bit1 00XY -> 000X
    LCD_D6 = 0;//(2>>2) & 0x01;        //bit2 0XYZ -> 000X
    LCD_D7 = 0;//(2>>3) & 0x01;        //bit3 XYZW -> 000X

    LCD_STROBE();

    lcd_write(0x28); // Set interface length: nibblemode, 2line, 5x7dot
    lcd_write(0b00001100); // Display On, Cursor Off, Cursor Blink off
    lcd_clear();    // Clear screen
    lcd_write(0x6); // Set entry Mode : increment, displayShiftOff
}

/* write one character to the LCD */

void lcd_putch(char c)
{
    LCD_RS = 1;    // write characters
    lcd_write( c );
}
/* write a string of chars to the LCD */

void lcd_puts(const char * s)
{
    LCD_RS = 1;    // write characters
    while(*s)
        lcd_write(*s++);
}

void lcd_unum(unsigned int num){//sign-0 +ve sign-1 -ve


    lcd_puts("    ");
    lcd_putch(num%10+'0');
    num/=10;
    for(unsigned char i=0;i<4;i++){
        //lcd_goto(i);
        LCD_RS = 0;
        lcd_write(0x10);
        lcd_write(0x10);//shift cursor one step back without writing nothing
        if(num>0)
            lcd_putch(num%10+'0');
        else
            break;
        num/=10;

    }
}


void main()
    {
   TRISB=0;//output port for lcd.
    lcd_init();
    lcd_clear();
    lcd_puts("4 bit operation!");
    __delay_ms(20);    //WAIT FOR TWO SECONDS TO SYNCRONIZE THE PICS
    while(1);
    }
 

TRY THIS CODE WITH NEEDED CHANGES FOR YOUR MCU

Code:
#define MX_PIC

//Defines for microcontroller
#define P18F4520
#define MX_EE
#define MX_EE_TYPE3
#define MX_EE_SIZE 256

//Functions
#define MX_CLK_SPEED 19660800
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic18.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x300000, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300001, 0x8
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300002, 0x1f
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300003, 0x1e
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300004, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300005, 0x81
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300006, 0x81
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300007, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300008, 0xf
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300009, 0xc0
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000a, 0xf
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000b, 0xe0
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000c, 0xf
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000d, 0x40
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif

//Internal functions
#include "C:\Program Files (x86)\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Macro function declarations


//Variable declarations
#define FCSZ_S 20
char FCV_I;
char FCV_J;
short FCV_N;
char FCV_A;
char FCV_S[FCSZ_S];




//LCDDisplay(0): //Defines:

/**** Macro Substitutions ****
portb = D1 Port
trisb = D1 Data Direction
portb = D2 Port
trisb = D2 Data Direction
portb = D3 Port
trisb = D3 Data Direction
portb = D4 Port
trisb = D4 Data Direction
portb = RS Port
trisb = RS Data Direction
portb = E Port
trisb = E Data Direction
0 = Data 1_Pin
1 = Data 2 Pin
2 = Data 3 Pin
3 = Data 4 Pin
4 = RS Pin
5 = Enable Pin
LCD_263968 = Unique Component Reference Number
2 = Row Count
16 = Column Count
******************************/

	//component connections
	#define LCD_263968_PORT0    portb
	#define LCD_263968_TRIS0    trisb
	#define LCD_263968_PORT1    portb
	#define LCD_263968_TRIS1    trisb
	#define LCD_263968_PORT2    portb
	#define LCD_263968_TRIS2    trisb
	#define LCD_263968_PORT3    portb
	#define LCD_263968_TRIS3    trisb
	#define LCD_263968_PORT4    portb
	#define LCD_263968_TRIS4    trisb
	#define LCD_263968_PORT5    portb
	#define LCD_263968_TRIS5    trisb
	#define LCD_263968_BIT0    	0
	#define LCD_263968_BIT1    	1
	#define LCD_263968_BIT2    	2
	#define LCD_263968_BIT3    	3
	#define LCD_263968_RS      	4
	#define LCD_263968_E       	5
	#define LCD_263968_ROWCNT	2
	#define LCD_263968_COLCNT	16

	#ifdef _BOOSTC
	  #define LCD_263968_DELAY   delay_10us(10)
	#endif
	#ifdef _C2C_
	  #define LCD_263968_DELAY   delay_us(100)
	#endif
	#ifdef HI_TECH_C
	  #define LCD_263968_DELAY   __delay_us(120)
	#endif
	#ifndef LCD_263968_DELAY
	  #define LCD_263968_DELAY   delay_us(100)
	#endif

	#define ModIDLCD_263968LCD_263968 (((trisb << 5) + portb) << 9)




//LCDDisplay(0): //Macro function declarations

void FCD_LCDDisplay0_RawSend(char in, char mask);
void FCD_LCDDisplay0_Start();
void FCD_LCDDisplay0_Clear();
void FCD_LCDDisplay0_PrintASCII(char Character);
void FCD_LCDDisplay0_Command(char in);
void FCD_LCDDisplay0_Cursor(char x, char y);
void FCD_LCDDisplay0_PrintNumber(short Number);
void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String);
void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions);
void FCD_LCDDisplay0_ClearLine(char Line);
void FCD_LCDDisplay0_RAM_Write(char nIdx, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7);



//LCDDisplay(0): //Macro implementations


void FCD_LCDDisplay0_RawSend(char in, char mask)
{
	
	#ifndef MX_MIAC_SYSTEM
		unsigned char pt;

		clear_bit(LCD_263968_PORT0, LCD_263968_BIT0);
		clear_bit(LCD_263968_PORT1, LCD_263968_BIT1);
		clear_bit(LCD_263968_PORT2, LCD_263968_BIT2);
		clear_bit(LCD_263968_PORT3, LCD_263968_BIT3);
		clear_bit(LCD_263968_PORT4, LCD_263968_RS);
		clear_bit(LCD_263968_PORT5, LCD_263968_E);
		pt = ((in >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(LCD_263968_PORT0, LCD_263968_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_263968_PORT1, LCD_263968_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_263968_PORT2, LCD_263968_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_263968_PORT3, LCD_263968_BIT3);
		if (mask)
		    set_bit(LCD_263968_PORT4, LCD_263968_RS);
		LCD_263968_DELAY;
		set_bit (LCD_263968_PORT5, LCD_263968_E);
		LCD_263968_DELAY;
		clear_bit (LCD_263968_PORT5, LCD_263968_E);
		pt = (in & 0x0f);
		LCD_263968_DELAY;
		clear_bit(LCD_263968_PORT0, LCD_263968_BIT0);
		clear_bit(LCD_263968_PORT1, LCD_263968_BIT1);
		clear_bit(LCD_263968_PORT2, LCD_263968_BIT2);
		clear_bit(LCD_263968_PORT3, LCD_263968_BIT3);
		clear_bit(LCD_263968_PORT4, LCD_263968_RS);
		clear_bit(LCD_263968_PORT5, LCD_263968_E);
		if (pt & 0x01)
		    set_bit(LCD_263968_PORT0, LCD_263968_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_263968_PORT1, LCD_263968_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_263968_PORT2, LCD_263968_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_263968_PORT3, LCD_263968_BIT3);
		if (mask)
		    set_bit(LCD_263968_PORT4, LCD_263968_RS);
		LCD_263968_DELAY;
		set_bit (LCD_263968_PORT5, LCD_263968_E);
		LCD_263968_DELAY;
		clear_bit (LCD_263968_PORT5, LCD_263968_E);
		LCD_263968_DELAY;

	#endif

}

void FCD_LCDDisplay0_Start()
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = 0;
		MIAC_CAN_TX[1] = 1;
		MIAC_CAN_TX[2] = 2;
		MIAC_CAN_TX[3] = 3;
		MIAC_CAN_TX[4] = 4;
		MIAC_CAN_TX[5] = 5;

		MIAC_Module_WR(ModIDLCD_263968, 608, 6,1,1);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 608, 20);

		MIAC_CAN_TX[0] = 2;
		MIAC_CAN_TX[1] = 16;
		MIAC_Module_WR(ModIDLCD_263968, 608, 2,0,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 608, 255);

	  #endif
	#else

		clear_bit(LCD_263968_TRIS0, LCD_263968_BIT0);
		clear_bit(LCD_263968_TRIS1, LCD_263968_BIT1);
		clear_bit(LCD_263968_TRIS2, LCD_263968_BIT2);
		clear_bit(LCD_263968_TRIS3, LCD_263968_BIT3);
		clear_bit(LCD_263968_TRIS4, LCD_263968_RS);
		clear_bit(LCD_263968_TRIS5, LCD_263968_E);

		Wdt_Delay_Ms(12);

		FCD_LCDDisplay0_RawSend(0x33, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x33, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x32, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x2c, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x06, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x0c, 0);
		Wdt_Delay_Ms(2);

		//clear the display
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);
	#endif

}

void FCD_LCDDisplay0_Clear()
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_Module_WR(ModIDLCD_263968, 609, 0,1,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 609, 200);
	  #endif
	#else
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);
	#endif

}

void FCD_LCDDisplay0_PrintASCII(char Character)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)



		MIAC_CAN_TX[0] = Character;

		MIAC_Module_WR(ModIDLCD_263968, 610, 1,1,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 610, 20);
	  #endif
	#else
		FCD_LCDDisplay0_RawSend(Character, 0x10);
	#endif

}

void FCD_LCDDisplay0_Command(char in)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = in;

		MIAC_Module_WR(ModIDLCD_263968, 611, 1,1,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 611, 200);
	  #endif
	#else
		FCD_LCDDisplay0_RawSend(in, 0);
		Wdt_Delay_Ms(2);
	#endif

}

void FCD_LCDDisplay0_Cursor(char x, char y)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = x;
		MIAC_CAN_TX[1] = y;

		MIAC_Module_WR(ModIDLCD_263968, 612, 2,1,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 612, 20);
	  #endif

	#else

	  #if (LCD_263968_ROWCNT == 1)
	    y=0x80;
	  #endif

	  #if (LCD_263968_ROWCNT == 2)
		if (y==0)
			y=0x80;
		else
			y=0xc0;
	  #endif

	  #if (LCD_263968_ROWCNT == 4)
		if (y==0)
			y=0x80;
		else if (y==1)
			y=0xc0;

		#if (LCD_263968_COLCNT == 16)
			else if (y==2)
				y=0x90;
			else
				y=0xd0;
		#endif

		#if (LCD_263968_COLCNT == 20)
			else if (y==2)
				y=0x94;
			else
				y=0xd4;
		#endif
	  #endif

		FCD_LCDDisplay0_RawSend(y+x, 0);
		Wdt_Delay_Ms(2);
	#endif

}

void FCD_LCDDisplay0_PrintNumber(short Number)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[1] = Number;
		MIAC_CAN_TX[0] = Number >> 8;

		MIAC_Module_WR(ModIDLCD_263968, 613, 4,1,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 613, 200);
	  #endif

	#else

		short tmp_int;
		char tmp_byte;

		if (Number < 0)
		{
			FCD_LCDDisplay0_RawSend('-', 0x10);
			Number = 0 - Number;
		}

		tmp_int = Number;
		if (Number >= 10000)
		{
			tmp_byte = tmp_int / 10000;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10000;
				tmp_byte--;
			}
		}
		if (Number >= 1000)
		{
			tmp_byte = tmp_int / 1000;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 1000;
				tmp_byte--;
			}
		}
		if (Number >= 100)
		{
			tmp_byte = tmp_int / 100;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 100;
				tmp_byte--;
			}
		}
		if (Number >= 10)
		{
			tmp_byte = tmp_int / 10;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10;
				tmp_byte--;
			}
		}
		FCD_LCDDisplay0_RawSend('0' + tmp_int, 0x10);
	#endif

}

void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String)
{
	
		char idx = 0;

	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		char msg_count, ch;
		char cmd_start = 1;
		char more_data = 1;

		// Split string into 6-byte packets
		do
		{
			msg_count = 0;
			do
			{
				ch = String[idx];
				if (ch)
				{
					MIAC_CAN_TX[msg_count] = ch;
					idx = idx + 1;
					msg_count = msg_count + 1;
				}
			}
			while (ch && (msg_count < 6) && (idx < MSZ_String));			// check for 2 x End Of String conditions and packet full

			if (idx == MSZ_String)											// end of string
			{
				more_data = 0;
			}else{
				if (String[idx] == 0) more_data = 0;						// next ch = null
			}

			MIAC_Module_WR(ModIDLCD_263968, 614, msg_count,cmd_start,more_data);		// TX data
			MIAC_Module_GetAck(ModIDLCD_263968 + 32, 614, 200);

			cmd_start = 0;
		}
		while (more_data);
	  #endif
	#else
		for (idx=0; idx<MSZ_String; idx++)
		{
			if (String[idx] == 0)
			{
				break;
			}
			FCD_LCDDisplay0_RawSend(String[idx], 0x10);
		}
	#endif

}

void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = Direction;
		MIAC_CAN_TX[1] = Num_Positions;

		MIAC_Module_WR(ModIDLCD_263968, 615, 2,1,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 615, 20);
	  #endif
	#else

		char cmd = 0;
		char count;

		//Choose the direction
		switch (Direction)
		{
			case 0:
			case 'l':
			case 'L':

				cmd = 0x18;
				break;

			case 1:
			case 'r':
			case 'R':

				cmd = 0x1C;
				break;

			default:
				break;
		}

		//If direction accepted then scroll the specified amount
		if (cmd)
		{
			for (count = 0; count < Num_Positions; count++)
				FCD_LCDDisplay0_Command(cmd);
		}
	#endif

}

void FCD_LCDDisplay0_ClearLine(char Line)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = Line;

		MIAC_Module_WR(ModIDLCD_263968, 616, 1,1,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 616, 200);
	  #endif
	#else
		char count;
		char rowcount;

		//Define number of columns per line
		#if (LCD_263968_ROWCNT == 1)
			rowcount=80;
		#endif

		#if (LCD_263968_ROWCNT == 2)
			rowcount=40;
		#endif

		#if (LCD_263968_ROWCNT == 4)
			#if (LCD_263968_COLCNT == 16)
				rowcount=16;
			#endif
			#if (LCD_263968_COLCNT == 20)
				rowcount=20;
			#endif
		#endif

		//Start at beginning of the line
		FCD_LCDDisplay0_Cursor (0, Line);

		//Send out spaces to clear line
		for (count = 0; count < rowcount; count++)
			FCD_LCDDisplay0_RawSend(' ', 0x10);

		//Move back to the beginning of the line.
		FCD_LCDDisplay0_Cursor (0, Line);
	#endif

}

void FCD_LCDDisplay0_RAM_Write(char nIdx, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7)
{
	   //set CGRAM address

	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = nIdx;
		MIAC_CAN_TX[1] = d0;
		MIAC_CAN_TX[2] = d1;
		MIAC_CAN_TX[3] = d2;
		MIAC_CAN_TX[4] = d3;
		MIAC_CAN_TX[5] = d4;

		MIAC_Module_WR(ModIDLCD_263968, 617, 6,1,1);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 617, 20);

		MIAC_CAN_TX[0] = d5;
		MIAC_CAN_TX[1] = d6;
		MIAC_CAN_TX[2] = d7;

		MIAC_Module_WR(ModIDLCD_263968, 617, 3,0,0);
		MIAC_Module_GetAck(ModIDLCD_263968 + 32, 617, 200);

	  #endif
	#else

	   FCD_LCDDisplay0_RawSend(64 + (nIdx << 3), 0);
	   delay_ms(2);

	   //write CGRAM data
	   FCD_LCDDisplay0_RawSend(d0, 0x10);
	   FCD_LCDDisplay0_RawSend(d1, 0x10);
	   FCD_LCDDisplay0_RawSend(d2, 0x10);
	   FCD_LCDDisplay0_RawSend(d3, 0x10);
	   FCD_LCDDisplay0_RawSend(d4, 0x10);
	   FCD_LCDDisplay0_RawSend(d5, 0x10);
	   FCD_LCDDisplay0_RawSend(d6, 0x10);
	   FCD_LCDDisplay0_RawSend(d7, 0x10);

	   //Clear the display
	   FCD_LCDDisplay0_RawSend(0x01, 0);
	   delay_ms(2);
	   FCD_LCDDisplay0_RawSend(0x02, 0);
	   delay_ms(2);
	#endif

}

//Macro implementations

void main()
{
	
	//Initialisation
	adcon1 = 0x0F;


	//Interrupt initialisation code
	


	//Call Component Macro
	//Call Component Macro: LCDDisplay(0)::Start
	FCD_LCDDisplay0_Start();


	//Loop
	//Loop: While 1
	while (1)
	{
		//Call Component Macro
		//Call Component Macro: LCDDisplay(0)::Cursor(0, 0)
		FCD_LCDDisplay0_Cursor(0, 0);


		//Call Component Macro
		//Call Component Macro: LCDDisplay(0)::PrintString("HELLO WORLD")
		FCD_LCDDisplay0_PrintString("HELLO WORLD",11);


	}


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}
[/CODE]
 

thanks for help ,this is schematic.
View attachment lcd 2x16.bmp
I will rewrite this program, but itn't run.
Code:
 /*
 * File:   newmainpicc.c
 * Author: DINH LUAN
 *
 * Created on 01/09, 2013, 
 */


#include "htc.h"
#define_XTAL_FREQ 800000
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & BOREN_OFF
         & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_ON); //1st config. Word
__CONFIG(BOR4V_BOR21V); //2st config.

#define RS RB4
#define EN RB5
#define LCD_OUT PORTB
// delay function
void _delay_us (unsigned char us){
	while(us--){
		asm("nop");
		asm("nop");
	};
}

//==========================================================================================
void _delay_ms(unsigned char  ms){
	unsigned char	i, j;
	while(ms--){
		for (i = 0; i < 20; i++)
			for (j = 0; j < 100; j++)
				asm("nop");
		};
}
// void LCD_STROBE
void LCD_STROBE()
{
    EN=1;
    ;
    EN=0;
}
void LCD_DATA(unsigned char c)
{   unsigned char temp;
    RS=1;
    _delay_us(50);           // delay 50us
    temp=c;                 // send 4 high bit
    temp=temp>>4;           //
    LCD_OUT = temp|0x30;    //
    LCD_STROBE();
    LCD_OUT=c|0x30;         // send 4 low bit
    LCD_STROBE();
}
void LCD_CMD(unsigned char c)
{
    RS =0;
    unsigned char temp;
    _delay_us(50);
    temp=c;
    temp=temp>>4;
    LCD_OUT = temp&0x0F;
    LCD_STROBE();
    LCD_OUT = temp&0x0F;
    LCD_STROBE();
}
void clear(void) // Clear dislay
{
    LCD_CMD(0x01);
    _delay_ms(2);
}
void LCD_INIT()
{
    TRISB =0;
    EN=0;
    RS=0;
    _delay_ms(100);// delay 100ms waiting for operting LCD
    LCD_CMD(0x28); // Function set (4 bit mode , 2 line, 5x7 Pixels)
    LCD_CMD(0x0C); // Display on cursor off
    LCD_CMD(0x06); // Set entry mode (auto increment of cursor)
}
void string(const char *q)
{
    while(*q)
    {
        LCD_DATA(*q++);
    }
}
void main()
{
    LCD_INIT(); //
    while(1)
    {
        LCD_CMD(0x80);
        string("HELLO");
        LCD_CMD(0xc0);
        string("LCD BIT MODE ");
    }
}
 

The third pin of LCD is VEE for adjusting the brightness connect a three terminal 10k variable resistor and adjust then check it out..
 

connect variable resistor 10K to LCD pin 3. Also change freq in program: #define_XTAL_FREQ 8000000
 

Hi everybody,
It's beautiful day,
I fixed my project,
This is my code,
Code:
/*
 * File:   newmainpicc.c
 * Author: DINH LUAN
 * =============================================================================
 * LCD 4bit MODE
 * RS conect
 * EN conect RB5
 * R/W conect Ground
 * DB0 conect ground
 * DB1 conect ground
 * DB2 conect ground
 * DB3 Conect ground
 * DB4 conect RB0
 * DB5 conect RB1
 * DB6 conect RB2
 * DB7 conect RB3
 * Created on 01/09, 2013,
 */


#include "htc.h"
#define_XTAL_FREQ 8000000
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & BOREN_OFF
         & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_ON); //1st config. Word
__CONFIG(BOR4V_BOR21V); //2st config.

#define RS RB4
#define EN RB5
#define LCD_OUT PORTB
// delay function
void _delay_us (unsigned char us){
	while(us--){
		asm("nop");
		asm("nop");
	};
}

//==========================================================================================
void _delay_ms(unsigned char  ms){
	unsigned char	i, j;
	while(ms--){
		for (i = 0; i < 20; i++)
			for (j = 0; j < 100; j++)
				asm("nop");
		};
}
// void LCD_STROBE
void LCD_STROBE()
{
    EN=1;
    ;
    EN=0;
}
void LCD_DATA(unsigned char c)
{   unsigned char temp, h_nibble, l_nibble;
    RS=1;
    _delay_us(50);           // delay 50us
    temp=LCD_OUT&0xF0;        
    h_nibble=(c>>4)&0x0F;
    l_nibble=c&0x0F;
    LCD_OUT = temp|h_nibble;    /// send 4 high bit
    LCD_STROBE();
    LCD_OUT=temp|l_nibble;         // send 4 low bit
    LCD_STROBE();
    _delay_us(50);
}
void LCD_CMD(unsigned char c)
{
    RS =0;
    unsigned char temp, h_nibble, l_nibble;
    _delay_us(50);
    temp=LCD_OUT&0xF0;                
    h_nibble=(c>>4)&0x0F;
    l_nibble=c&0x0F;
    LCD_OUT = temp|h_nibble;    //// send 4 high bit
    LCD_STROBE();
    LCD_OUT=temp|l_nibble;         // send 4 low bit
    LCD_STROBE();
    _delay_us(50);
}
void clear(void) // Clear dislay
{
    LCD_CMD(0x01);
    _delay_ms(2);
}
void LCD_INIT()
{
    TRISB =0;
    ANSELH=0x00;
    IOCB=0x00;
    _delay_ms(100);// delay 100ms waiting for operting LCD
    LCD_CMD(0x28); // Function set (4 bit mode , 2 line, 5x7 Pixels)
    LCD_CMD(0x0C); // Display on cursor off
    LCD_CMD(0x06); // Set entry mode (auto increment of cursor)
}
void string(const char *q)
{
    while(*q)
    {
        LCD_DATA(*q++);
    }
}
void main()
{
    LCD_INIT(); //
    while(1)
    {
        LCD_CMD(0x80);
        string("HELLO");
        LCD_CMD(0xc0);
        string("LCD BIT MODE ");
    }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…