Hi All,
I am having serious issue with using MPLAB XC8 software. I have used MikroC before and programming is easy with all the built-in libraries. Just few weeks ago I thought I should learn MPLAB XB8 in order to write more flexible and optimised code as it is provided by Microchip it self. I couldn't get XLCD.H to work and I have also tried few other libraries and sample code available on the internet without any success.
I am just about to give up on MPLAB XC8 and move back to MikroC where everything is nice and easy for non PRO programmers like us. Here is my problem and I hope someone can guide me in the right direction.
I have my LCD connected to PORTB. uC - PIC16F887
RS - B7
E - B6
D4 - B3
D5 - B2
D6 - B1
D7 - B0
I have followed the directions given by Ian Rogers to a fellow forum member and code as follows.
Thanks
I am having serious issue with using MPLAB XC8 software. I have used MikroC before and programming is easy with all the built-in libraries. Just few weeks ago I thought I should learn MPLAB XB8 in order to write more flexible and optimised code as it is provided by Microchip it self. I couldn't get XLCD.H to work and I have also tried few other libraries and sample code available on the internet without any success.
I am just about to give up on MPLAB XC8 and move back to MikroC where everything is nice and easy for non PRO programmers like us. Here is my problem and I hope someone can guide me in the right direction.
I have my LCD connected to PORTB. uC - PIC16F887
RS - B7
E - B6
D4 - B3
D5 - B2
D6 - B1
D7 - B0
I have followed the directions given by Ian Rogers to a fellow forum member and code as follows.
Code:
#define _XTAL_FREQ 8000000
#define LCD_PORT PORTB // constants
#define LCD_TRIS TRISB //
#define LCD_RS PORTBbits.RB7
#define LCD_E PORTBbits.RB6
#define LCD_RW PORTBbits.RB5
#include <stdio.h>
#include <stdlib.h>
#include <pic.h>
#include <xc.h>
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void Delay5MS(){
_delay(5100);
}
void Delay1MS(){
_delay(1025);
}
void Delay15MS(){
_delay(5100);
_delay(5100);
_delay(5100);
}
void Delay150MS()
{
for(int i=0;i<10;i++)
{
Delay15MS();
}
}
void Delay20MS(){
_delay(5100);
_delay(5100);
_delay(5100);
_delay(5100);
}
void Delay200US(){
_delay(200);
}
void Delay50US(){
_delay(45);
}
void Delay1US(){
_delay(1);
}
void LCD_init(void), LCD_cmd(unsigned char ch);
void LCD_goto(char line, char column), LCD_clr(void);
void LCD_cur(unsigned char ch), pulse_E(void), LCD_hex(int value);
void LCD_char(unsigned char ch), LCD_charD(unsigned char ch);
int main() {
ANSEL=0x04;
ANSELH=0x00;
LCD_RW=0;
LCD_TRIS = 0x00;
Delay150MS();
LCD_init();
while(1)
{
LCD_goto(1,0);
LCD_char('A');
Delay20MS();
}
}
void LCD_init()
{
LCD_cmd(0x20); // 4 bit
LCD_cmd(0x28); // display shift
LCD_cmd(0x6); // character mode
LCD_cmd(0xc); // display on / off and cursor
LCD_clr(); // clear display
}
/*void LCD_hex(int value)
{
char data; // dispay hex values.
data = value >> 4 & 0xf; // send upper nibble
data = HEX_Table[data]; // hex lookup
LCD_char(data);
data = value & 0xf; // send lower nibble
data = HEX_Table[data]; // hex lookup
LCD_char(data);
}*/
void LCD_goto(char line, char column) // combines line and lineW
{
unsigned char data = 0x80; // default to 1
if(line == 2)data = 0xc0; // change to 2
data += column; // add in column
LCD_cmd(data);
}
void LCD_clr()
{
LCD_cmd(1); // Clr screen
}
void LCD_cur(char on)
{
unsigned char cur = 0xc; // cursor off
if(on) cur = 0xd; // cursor on
LCD_cmd(cur);
}
void LCD_cmd(unsigned char ch)
{
LCD_PORT = ch & 0xf0; // write high nibble
LCD_RS = 0;
pulse_E();
LCD_PORT = ch <<4 & 0xf0; // write low nibble
LCD_RS = 0;
pulse_E();
Delay5MS();
}
void LCD_charD(unsigned char ch)
{
ch+=0x30;
LCD_char(ch); // convert to ascii
}
void LCD_char(unsigned char ch)
{
LCD_PORT = ch & 0xf0; // write high nibble
LCD_RS = 0;
pulse_E();
LCD_PORT = ch <<4 & 0xf0;
LCD_RS = 1;
pulse_E();
Delay5MS();
}
void pulse_E()
{
LCD_E = 1;
Delay1US();
LCD_E = 0;
}
Thanks