haronraziq
Newbie level 6
- Joined
- Apr 3, 2011
- Messages
- 14
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,283
- Activity points
- 1,449
okay so guys, I tried the following code under the assumption that using the lcd should be relatively the same between the microcontrollers. I just changed the first P1 to PORTC which is the identifier for the pic microcontroller. I also used the port assumptions of connecting PORT C bits 0-3 to data bits 4-7 and the enable and rs bits as according to hussain. However, I still got the same result of all black boxes, no words. Is this not correct for the pic? I will also post another code that I tried, with the same result
// 4 bit example
#include<htc.h>
#define LCD PORTC //port 1
#define LCD_EN 0x80
#define LCD_RS 0x20
//LCD Commands
#define LCD_SETMODE 0x04
#define LCD_SETVISIBLE 0x08
#define LCD_SETFUNCTION 0x28
#define LCD_SETDDADDR 0x80
void delayms(unsigned char);
void delayus(unsigned char);
void lcd_init();
void lcd_reset();
void lcd_cmd (char);
void lcd_data(unsigned char);
void lcd_str (unsigned char*);
void main (void)
{
lcd_init();
lcd_cmd(0x80); // ist line address
lcd_str(" i love ");
lcd_cmd(0xC0); // 2nd line address
lcd_str("romel");
while(1);
}
void delayus(unsigned char delay){
while(delay--);
}
void delayms(unsigned char delay){
while(delay--)
delayus(149);
}
void lcd_reset() //reset LCD
{
LCD = 0xFF;
delayms(40);
LCD = 0x03+LCD_EN;
LCD = 0x03;
delayms(40);
LCD = 0x03+LCD_EN;
LCD = 0x03;
delayms(5);
LCD = 0x03+LCD_EN;
LCD = 0x03;
delayms(5);
LCD = 0x02+LCD_EN;
LCD = 0x02;
delayms(5);
}
void lcd_init () //initialize LCD
{
lcd_reset();
lcd_cmd(LCD_SETFUNCTION); // 4-bit mode - 1 line - 5x7 font.
lcd_cmd(LCD_SETVISIBLE+0x04); // Display no cursor - no blink.
lcd_cmd(LCD_SETMODE+0x02); // Automatic Increment - No Display shift.
lcd_cmd(LCD_SETDDADDR); // Address DDRAM with 0 offset 80h.
}
void lcd_cmd (char cmd)
{
LCD = ((cmd >> 4) & 0x0F)|LCD_EN;
LCD = ((cmd >> 4) & 0x0F);
LCD = (cmd & 0x0F)|LCD_EN;
LCD = (cmd & 0x0F);
delayus(250);
delayus(250);
}
void lcd_data (unsigned char dat)
{
LCD = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
LCD = (((dat >> 4) & 0x0F)|LCD_RS);
LCD = ((dat & 0x0F)|LCD_EN|LCD_RS);
LCD = ((dat & 0x0F)|LCD_RS);
delayus(250);
delayus(250);
}
void lcd_str (unsigned char *str) //display string to LCD
{
while(*str){
lcd_data(*str++);
}
}
---------- Post added at 22:26 ---------- Previous post was at 22:22 ----------
This is the other code that I tried. First listed is my lcd.c and lcd.h files. Then listed is the main file. I tried this code and received the same result, all black boxes no changes. However, I was able to read 5 volts at each of the outputs. This also happened with the other code. Let me know if there's anything or if it is possible just an lcd problem or something.
lcd.c file (sample from hi-tech c) I just commented out the analog pin disable since I am using pic16f887 and this code is not necessary. I also tried both initializing with 0x3 and 0x2
/*
* 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 0-3 are connected to the LCD data bits 4-7 (high nibble)
* PORTA bit 3 is connected to the LCD RS input (register select)
* PORTA bit 1 is connected to the LCD EN bit (enable)
*
* To use these routines, set up the port I/O (TRISA, TRISD) then
* call lcd_init(), then other routines as required.
*
*/
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 4000000
#endif
#include <htc.h>
#include "lcd.h"
#define LCD_RS RA3
#define LCD_RW RA2
#define LCD_EN RA1
#define LCD_DATA PORTD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
__delay_us(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);
__delay_ms(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;
//ADCON1 = 0x06; // Disable analog pins on PORTA
init_value = 0x3;
TRISA=0;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}
lcd.h
/*
* LCD interface header file
* See lcd.c for more info
*/
/* write a byte to the LCD in 4 bit mode */
extern void lcd_write(unsigned char);
/* Clear and home the LCD */
extern void lcd_clear(void);
/* write a string of characters to the LCD */
extern void lcd_puts(const char * s);
/* Go to the specified position */
extern void lcd_goto(unsigned char pos);
/* intialize the LCD - call before anything else */
extern void lcd_init(void);
extern void lcd_putch(char);
/* Set the cursor position */
#define lcd_cursor(x) lcd_write(((x)&0x7F)|0x80)
main
#include <htc.h>
#include "lcd.h"
void
main(void)
{
lcd_init();
lcd_goto(0); // select first line
lcd_puts("12345678");
lcd_goto(0x40); // Select second line
lcd_puts("Hello world");
for(;;
}
// 4 bit example
#include<htc.h>
#define LCD PORTC //port 1
#define LCD_EN 0x80
#define LCD_RS 0x20
//LCD Commands
#define LCD_SETMODE 0x04
#define LCD_SETVISIBLE 0x08
#define LCD_SETFUNCTION 0x28
#define LCD_SETDDADDR 0x80
void delayms(unsigned char);
void delayus(unsigned char);
void lcd_init();
void lcd_reset();
void lcd_cmd (char);
void lcd_data(unsigned char);
void lcd_str (unsigned char*);
void main (void)
{
lcd_init();
lcd_cmd(0x80); // ist line address
lcd_str(" i love ");
lcd_cmd(0xC0); // 2nd line address
lcd_str("romel");
while(1);
}
void delayus(unsigned char delay){
while(delay--);
}
void delayms(unsigned char delay){
while(delay--)
delayus(149);
}
void lcd_reset() //reset LCD
{
LCD = 0xFF;
delayms(40);
LCD = 0x03+LCD_EN;
LCD = 0x03;
delayms(40);
LCD = 0x03+LCD_EN;
LCD = 0x03;
delayms(5);
LCD = 0x03+LCD_EN;
LCD = 0x03;
delayms(5);
LCD = 0x02+LCD_EN;
LCD = 0x02;
delayms(5);
}
void lcd_init () //initialize LCD
{
lcd_reset();
lcd_cmd(LCD_SETFUNCTION); // 4-bit mode - 1 line - 5x7 font.
lcd_cmd(LCD_SETVISIBLE+0x04); // Display no cursor - no blink.
lcd_cmd(LCD_SETMODE+0x02); // Automatic Increment - No Display shift.
lcd_cmd(LCD_SETDDADDR); // Address DDRAM with 0 offset 80h.
}
void lcd_cmd (char cmd)
{
LCD = ((cmd >> 4) & 0x0F)|LCD_EN;
LCD = ((cmd >> 4) & 0x0F);
LCD = (cmd & 0x0F)|LCD_EN;
LCD = (cmd & 0x0F);
delayus(250);
delayus(250);
}
void lcd_data (unsigned char dat)
{
LCD = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
LCD = (((dat >> 4) & 0x0F)|LCD_RS);
LCD = ((dat & 0x0F)|LCD_EN|LCD_RS);
LCD = ((dat & 0x0F)|LCD_RS);
delayus(250);
delayus(250);
}
void lcd_str (unsigned char *str) //display string to LCD
{
while(*str){
lcd_data(*str++);
}
}
---------- Post added at 22:26 ---------- Previous post was at 22:22 ----------
This is the other code that I tried. First listed is my lcd.c and lcd.h files. Then listed is the main file. I tried this code and received the same result, all black boxes no changes. However, I was able to read 5 volts at each of the outputs. This also happened with the other code. Let me know if there's anything or if it is possible just an lcd problem or something.
lcd.c file (sample from hi-tech c) I just commented out the analog pin disable since I am using pic16f887 and this code is not necessary. I also tried both initializing with 0x3 and 0x2
/*
* 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 0-3 are connected to the LCD data bits 4-7 (high nibble)
* PORTA bit 3 is connected to the LCD RS input (register select)
* PORTA bit 1 is connected to the LCD EN bit (enable)
*
* To use these routines, set up the port I/O (TRISA, TRISD) then
* call lcd_init(), then other routines as required.
*
*/
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 4000000
#endif
#include <htc.h>
#include "lcd.h"
#define LCD_RS RA3
#define LCD_RW RA2
#define LCD_EN RA1
#define LCD_DATA PORTD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
__delay_us(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);
__delay_ms(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;
//ADCON1 = 0x06; // Disable analog pins on PORTA
init_value = 0x3;
TRISA=0;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}
lcd.h
/*
* LCD interface header file
* See lcd.c for more info
*/
/* write a byte to the LCD in 4 bit mode */
extern void lcd_write(unsigned char);
/* Clear and home the LCD */
extern void lcd_clear(void);
/* write a string of characters to the LCD */
extern void lcd_puts(const char * s);
/* Go to the specified position */
extern void lcd_goto(unsigned char pos);
/* intialize the LCD - call before anything else */
extern void lcd_init(void);
extern void lcd_putch(char);
/* Set the cursor position */
#define lcd_cursor(x) lcd_write(((x)&0x7F)|0x80)
main
#include <htc.h>
#include "lcd.h"
void
main(void)
{
lcd_init();
lcd_goto(0); // select first line
lcd_puts("12345678");
lcd_goto(0x40); // Select second line
lcd_puts("Hello world");
for(;;
}