wanbong
Newbie level 5
pic16f877 lcd
hi guys, I'm just an newbie with PIC
I'm trying to control the LCD 16x2 (model SC1602-B) with a PIC 16f877
The simulation of my code on the Proteus 7 can run but when I work with real thing it couldn't print out anything
please help me out, below is my code
Added after 5 hours 48 minutes:
after I ground all the unused pins, there is array of blocks appear in the second line the LCD still can not print out anything
hi guys, I'm just an newbie with PIC
I'm trying to control the LCD 16x2 (model SC1602-B) with a PIC 16f877
The simulation of my code on the Proteus 7 can run but when I work with real thing it couldn't print out anything
please help me out, below is my code
/*
* PORTB bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
* PORTA bit 2 is connected to the LCD RS input (register select)
* PORTA bit 3 is connected to the LCD EN bit (enable)
*
* To use these routines, set up the port I/O (TRISA, TRISB) then
* call lcd_init(), then other routines as required.
*
*/
//Define compiler error message.
#ifndef __CPU_16F877__
#error "This program is tailored for PIC16F877 controller"
#endif
//Include required header files here.
#include "io16f877.h" //the hardware register definition file.
//-----------------------------------------------------------------delay---------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
void DelayUs(int count)
{
int i;
int j;
for(i=0;i<count;i++)
{
for(j=0;j<5;j++);
//This for loop has 5 NOPs & wastes 1 uS for our PIC clock frequency of 20MHz.
}
}
void DelayMs(int count)
{
int i;
for(i=0;i<count;i++)
{DelayUs(4000);}
}
//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------
#define LCD_RS RA2 // Register select
#define LCD_EN RA3 // Enable
#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)
{
PORTB = (PORTB & 0xF0) | (c >> 4);
LCD_STROBE;
PORTB = (PORTB & 0xF0) | (c & 0x0F);
LCD_STROBE;
DelayUs(40);
}
/*
* 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
PORTB = (PORTB & 0xF0) | (c >> 4);
LCD_STROBE;
PORTB = (PORTB & 0xF0) | (c & 0x0F);
LCD_STROBE;
DelayUs(40);
}
/*
* 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(void)
{
LCD_RS = 0; // write control bytes
DelayMs(15); // power on delay
PORTB = 0x3; // attention!
LCD_STROBE;
DelayMs(5);
LCD_STROBE;
DelayUs(100);
LCD_STROBE;
DelayMs(5);
PORTB = 0x2; // set 4 bit mode
LCD_STROBE;
DelayUs(40);
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
lcd_write(0x08); // display off
lcd_write(0x0F); // display on, blink curson on
lcd_write(0x06); // entry mode
}
void IniPort()
{
ADCON1=0x06;
TRISA=0x00;
TRISB=0x00;
PORTA=0x00;
PORTB=0x00;
}
char text[]="len di\n";
int count=0;
void main(void)
{
IniPort();
lcd_init();
while (text[count]!='\n')
{
lcd_putch(text[count]);
count++;
}
DelayUs(100);
while(1){}
}
Added after 5 hours 48 minutes:
after I ground all the unused pins, there is array of blocks appear in the second line the LCD still can not print out anything