Hi_tech picc18 ile lcd yi 4 bit modunda kullanmak

Başlatan igolcubasi, 07 Şubat 2009, 20:08:00

igolcubasi

Hi_tech picc18 ile lcd yi 8 bit modunda çalıştırdım.Fakat dört bit modunda çalıştıramadım.
c ustalarından küçük bir program yazarak yardımlarını rica ediyorum.
c öğrenenlere başarılar.

mucit07


f_machine

/*
 *	LCD interface example
 *	Uses routines from delay.c
 *	This code will interface to a standard LCD controller
 *	like the Hitachi HD44780. It uses it in 4 bit mode, with
 *	the hardware connected as follows (the standard 14 pin 
 *	LCD connector is used):
 *	
 *	PORTD bits 4-7 are connected to the LCD data bits 4-7 (high nibble)
 *	PORTD bit 0 is connected to the LCD RS input (register select)
 *	PORTD bit 1 is connected to the LCD EN bit (enable)
 *	
 *	To use these routines, set up the port I/O (TRISD) then
 *	call lcd_init(), then other routines as required.
 *	
 */


 typedef struct {
     unsigned     :4;
     unsigned data:4;
  } Reg;

static volatile Reg PORTD_REG  @ (unsigned)&PORTD;  


#ifndef XTAL_FREQ
 // Unless specified elsewhere, 4MHz system frequency is assumed
#define XTAL_FREQ 40000000
#endif


#include	<htc.h>
#include	"lcd.h"

#define	LCD_RS RD0
#define LCD_EN RD1

#define LCD_DATA	PORTD_REG.data

#define	LCD_STROBE()	((LCD_EN = 1),(LCD_EN=0))

/* write a byte to the LCD in 4 bit mode */

void   lcd_write(const unsigned char c)
{
	DelayUs(40);
	
	LCD_DATA = ( (c>>4) & 0x0F );
	LCD_STROBE();   
	LCD_DATA = ( c & 0x0F );
	LCD_STROBE();
}

/*
 * 	Clear and home the LCD
 */

void
lcd_clear(void)
{
	LCD_RS = 0;
	lcd_write(0x1);
	DelayMs(2);
}

/* write a string of chars to the LCD */

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

/* write one character to the LCD */

void
lcd_putch(char c)
{
	LCD_RS = 1;	// write characters
	lcd_write( c );
}


/*
 * 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()
{
	char init_value;


	init_value = 0x3;
	TRISD=0;
	LCD_RS = 0;
	LCD_EN = 0;
	
	DelayMs(15);	// wait 15mSec after power applied,
	LCD_DATA	 = init_value;
	LCD_STROBE();
	DelayMs(5);
	LCD_STROBE();
	DelayUs(200);
	LCD_STROBE();
	DelayUs(200);
	LCD_DATA = 2;	// Four bit mode
	LCD_STROBE();

	lcd_write(0x28); // Set interface length
	lcd_write(0xC); // Display On, Cursor On, Cursor Blink
	lcd_clear();	// Clear screen
	lcd_write(0x6); // Set entry Mode
}


burda pinlerin nereye bağlanacağı yazıyor,portd için hi-tech in değiştirdiğim kütüphanesi.istersen portb filanda yapabilirsin..