Milead
Newbie level 5
yesterday i posted a similar problem with Mikroc and i managed to solve it.
today im facing the same problem in Hitech.
i only see black squares on the first row.
im using the hitech example code, but my code uses a single "port(B)".
PIC16f88-(i took care of config bits not in the code, but by going to Configure>Configuration Bits) Oscillator is set to HS.
code works fine in simulation (ISIS 7)
PORTB bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
PORTB bit 4 is connected to the LCD RS input (register select)
PORTB bit 5 is connected to the LCD EN bit (enable)
LCD.C:
#define _XTAL_FREQ 4000000
#include <htc.h>
#include "lcd.h"
#define LCD_RS RB4
#define LCD_RW RB6
#define LCD_EN RB5
#define LCD_DATA PORTB
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
int control_store;
void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F )^control_store;
LCD_STROBE();
LCD_DATA = ( c & 0x0F )^control_store;
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void
lcd_clear(void)
{
LCD_RS = 0;
control_store=PORTB & 0xF0;
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
control_store=PORTB & 0xF0;
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
control_store=PORTB & 0xF0;
lcd_write( c );
}
/*
* Go to the specified position
*/
void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
control_store=PORTB & 0xF0;
lcd_write(0x80+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
ANSEL = 0x00;
ADCON1 = 0x06; // Disable analog pins on PORTA
ADCON0 = 0x06;
init_value = 0x3;
TRISA=0;
TRISB=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
control_store=PORTB & 0xF0;
__delay_ms(20); // wait 15mSec after power applied,
LCD_DATA = init_value^control_store;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2^control_store; // 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
}
Main:
#include <htc.h>
#include "lcd.h"
#define _XTAL_FREQ 4000000
void
main(void)
{
ANSEL = 0x00;
ADCON1 = 0x06; // Disable analog pins on PORTA
ADCON0 = 0x06;
__delay_ms(100);
lcd_init();
__delay_ms(100);
lcd_goto(0); // select first line
lcd_puts("12345678");
lcd_goto(0x40); // Select second line
lcd_puts("Hello world");
for(;;
}
today im facing the same problem in Hitech.
i only see black squares on the first row.
im using the hitech example code, but my code uses a single "port(B)".
PIC16f88-(i took care of config bits not in the code, but by going to Configure>Configuration Bits) Oscillator is set to HS.
code works fine in simulation (ISIS 7)
PORTB bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
PORTB bit 4 is connected to the LCD RS input (register select)
PORTB bit 5 is connected to the LCD EN bit (enable)
LCD.C:
#define _XTAL_FREQ 4000000
#include <htc.h>
#include "lcd.h"
#define LCD_RS RB4
#define LCD_RW RB6
#define LCD_EN RB5
#define LCD_DATA PORTB
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
int control_store;
void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F )^control_store;
LCD_STROBE();
LCD_DATA = ( c & 0x0F )^control_store;
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void
lcd_clear(void)
{
LCD_RS = 0;
control_store=PORTB & 0xF0;
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
control_store=PORTB & 0xF0;
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
control_store=PORTB & 0xF0;
lcd_write( c );
}
/*
* Go to the specified position
*/
void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
control_store=PORTB & 0xF0;
lcd_write(0x80+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
ANSEL = 0x00;
ADCON1 = 0x06; // Disable analog pins on PORTA
ADCON0 = 0x06;
init_value = 0x3;
TRISA=0;
TRISB=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
control_store=PORTB & 0xF0;
__delay_ms(20); // wait 15mSec after power applied,
LCD_DATA = init_value^control_store;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2^control_store; // 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
}
Main:
#include <htc.h>
#include "lcd.h"
#define _XTAL_FREQ 4000000
void
main(void)
{
ANSEL = 0x00;
ADCON1 = 0x06; // Disable analog pins on PORTA
ADCON0 = 0x06;
__delay_ms(100);
lcd_init();
__delay_ms(100);
lcd_goto(0); // select first line
lcd_puts("12345678");
lcd_goto(0x40); // Select second line
lcd_puts("Hello world");
for(;;
}