Hello
I'm trying to control an LCD in 4-bit with a PIC microcontroller using the example code provided in the datasheet of the LCD
but I'm having difficulty understanding what to put instead P1 in their example code.
P1 in my code is in both the header and main files
I'm using
1- PIC16F1847 microcontroller
2- NHD-0216BZ-FL-YBW LCD (datasheet attached)
Here is my code for main.c
and here is my code for the header file
I'm trying to control an LCD in 4-bit with a PIC microcontroller using the example code provided in the datasheet of the LCD
but I'm having difficulty understanding what to put instead P1 in their example code.
P1 in my code is in both the header and main files
I'm using
1- PIC16F1847 microcontroller
2- NHD-0216BZ-FL-YBW LCD (datasheet attached)
Here is my code for main.c
C:
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "functions.h"
#define _XTAL_FREQ 8000000
#define RS RB2
void main(void) {
// initialize all ports to outputs (RB0 through RB7)
TRISB = 0b00000000;
P1 = 0;
__delay_ms(100); //Wait >40 msec after power is applied
P1 = (0x30); //put 0x30 on the output port
__delay_ms(5); //must wait 5ms, busy flag not available
Nybble(); //command 0x30 = Wake up
__delay_us(160); //must wait 160us, busy flag not available
Nybble(); //command 0x30 = Wake up #2
__delay_us(160); //must wait 160us, busy flag not available
Nybble(); //command 0x30 = Wake up #3
__delay_us(160); //can check busy flag now instead of delay
P1 = (0x20); //put 0x20 on the output port
Nybble(); //Function set: 4-bit interface
command(0x28); //Function set: 4-bit/2-line
command(0x10); //Set cursor
command(0x0E); //Display ON; Blinking cursor
command(0x06); //Entry Mode set
//write (0x41);
return;
}
and here is my code for the header file
C:
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h> // include processor files - each processor file is guarded.
#define _XTAL_FREQ 8000000 //For __delay_ms()
#define RS RB2
#define E RB3
//function to read a byte, 4bits at a time
void Nybble()
{
E = 1;
__delay_ms(0.0003); //enable pulse width >= 300ns
E = 0; //Clock enable: falling edge
}
//Function to send commands
void command(char i)
{
P1 = i; //put data on output Port
RS = 0; //D/I=LOW : send instruction
//R/W=LOW : Write ----> not needed since R/W pin is connected to ground
Nybble(); //Send lower 4 bits
i = i<<4; //Shift over by 4 bits
LATB = i; //put data on output Port
Nybble(); //Send upper 4 bits
}
//Function to send data
void write(char i)
{
P1 = i; //i is 0 for command or 1 for data (RB1 is connected to RS)
RS = 1; //D/I=HIGH : send data
//R/W=LOW : Write ----> not needed since R/W pin is connected to ground
Nybble(); //Clock lower 4 bits
i = i<<4; //Shift over by 4 bits
LATB = i; //put data on output Port
Nybble(); //Clock upper 4 bits
}