-->

LCD Interfacing with PIC16F877 by using MIKRO C Compiler

By
              LCD Interfacing with PIC16F877 by using MIKRO C Compiler 

            To interface a LCD 16x2 with 16F877 microcontroller PIC 16F887 microcontroller  ,First we need to learn LCD 16x2  second PIC 16F887 microcontroller . 

     At last we need to learn a compiler to program and debug. I am here taking the compiler called MIKRO C . which is very simple 

              Mikro C i s very Simple compiler .It can be use without  deep knowledge in programming . the great advantage of Mikro C it has a lot of helpful libraries which can be easily understandable .Anyway the hex file generated by mikroc  is large comparability  with other compiler.

 Then  draw the circuit diagram as we want .In that circuit diagram decide how and where the Pin of LCD connect with PIC16f877 . 
Here I used 4-bit lcd interfacing (to know more click here. )
 circuit drawn  .
So we want to write program in accordance with the above circuit.
In Mikro c there is library for interfacing the  LCD it is very simple .
First the Pin number of the PIC change to a understandable one for that we do as follows .
 sbit LCD_RS at RB4_bit;
 The above line of code means that the compiler changes RB4 pin of the PIC to LCD_RS . after the initialization of the above code the RB4 is called as LCD_RS through the program. So the PINs are configuring as follows .

  •  sbit LCD_RS at RB4_bit; // PIN name Changed 
  •  sbit LCD_EN at RB5_bit;
  •  sbit LCD_D7 at RB3_bit;
  •  sbit LCD_D6 at RB2_bit; 
  •  sbit LCD_D5 at RB1_bit;
  •  sbit LCD_D4 at RB0_bit;                                                                                                           Then the Port PB is makes as output the following code is needed . The compiler recognize it as output 


  •  sbit LCD_RS_Direction at TRISB4_bit; // making output 
  •  sbit LCD_EN_Direction at TRISB5_bit; 
  •  sbit LCD_D4_Direction at TRISB0_bit; 
  •  sbit LCD_D5_Direction at TRISB1_bit; 
  •  sbit LCD_D6_Direction at TRISB2_bit; 
  •  sbit LCD_D7_Direction at TRISB3_bit;

Next step is to initialize the LCD . .

void Lcd_Init();

It means that the compiler tells how the PIC connect to the LCD 
that is the PIN renaming , what type of LCD Modes (8bit or 4bit ) etc .

Next Step is giving the LCD Command by using LCD_Command function (ie Lcd_Cmd() .

which decide how the lcd shows look the command below .



for example to clear the LCD we put the following function 

Lcd_Cmd(_LCD_ClEAR);


To write Messages on LCD dispaly we use the following simple function 
void Lcd_Out(char row, char column, char *text);

For example Write text "Hello!" on Lcd starting from row 1, column 3:

Lcd_Out(1, 3, "Hello!"); 

It means the first Number "1"  the starting row .ie first line 
The secind Number specify the starting position of column .
the Last one is the message .

Another helpful  function is 
 void Lcd_Out_Cp(char *text);

 Example : Lcd_Out_Cp("Here!");

It uses to display the text message at the current position .


 void Lcd_Chr(char row, char column, char out_char);

The above function is used to display the value which is placed in variable 

Example : Lcd_Chr(2, 3, 'i');

It displays the 'I" value in the second line  at the third position .

Full Programm
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


void main()

{
Lcd_Init();
Lcd_Cmd(_LCD_Clear);
Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_out(1,1,"Hello");

while(1)
{}

}
Full Project Can be download here Including Proteus File click

LCD Interfacing with PIC16F877 by using MIKRO C Compiler

By
              LCD Interfacing with PIC16F877 by using MIKRO C Compiler 

            To interface a LCD 16x2 with 16F877 microcontroller PIC 16F887 microcontroller  ,First we need to learn LCD 16x2  second PIC 16F887 microcontroller . 

     At last we need to learn a compiler to program and debug. I am here taking the compiler called MIKRO C . which is very simple 

              Mikro C i s very Simple compiler .It can be use without  deep knowledge in programming . the great advantage of Mikro C it has a lot of helpful libraries which can be easily understandable .Anyway the hex file generated by mikroc  is large comparability  with other compiler.

 Then  draw the circuit diagram as we want .In that circuit diagram decide how and where the Pin of LCD connect with PIC16f877 . 
Here I used 4-bit lcd interfacing (to know more click here. )
 circuit drawn  .
So we want to write program in accordance with the above circuit.
In Mikro c there is library for interfacing the  LCD it is very simple .
First the Pin number of the PIC change to a understandable one for that we do as follows .
 sbit LCD_RS at RB4_bit;
 The above line of code means that the compiler changes RB4 pin of the PIC to LCD_RS . after the initialization of the above code the RB4 is called as LCD_RS through the program. So the PINs are configuring as follows .

  •  sbit LCD_RS at RB4_bit; // PIN name Changed 
  •  sbit LCD_EN at RB5_bit;
  •  sbit LCD_D7 at RB3_bit;
  •  sbit LCD_D6 at RB2_bit; 
  •  sbit LCD_D5 at RB1_bit;
  •  sbit LCD_D4 at RB0_bit;                                                                                                           Then the Port PB is makes as output the following code is needed . The compiler recognize it as output 


  •  sbit LCD_RS_Direction at TRISB4_bit; // making output 
  •  sbit LCD_EN_Direction at TRISB5_bit; 
  •  sbit LCD_D4_Direction at TRISB0_bit; 
  •  sbit LCD_D5_Direction at TRISB1_bit; 
  •  sbit LCD_D6_Direction at TRISB2_bit; 
  •  sbit LCD_D7_Direction at TRISB3_bit;

Next step is to initialize the LCD . .

void Lcd_Init();

It means that the compiler tells how the PIC connect to the LCD 
that is the PIN renaming , what type of LCD Modes (8bit or 4bit ) etc .

Next Step is giving the LCD Command by using LCD_Command function (ie Lcd_Cmd() .

which decide how the lcd shows look the command below .



for example to clear the LCD we put the following function 

Lcd_Cmd(_LCD_ClEAR);


To write Messages on LCD dispaly we use the following simple function 
void Lcd_Out(char row, char column, char *text);

For example Write text "Hello!" on Lcd starting from row 1, column 3:

Lcd_Out(1, 3, "Hello!"); 

It means the first Number "1"  the starting row .ie first line 
The secind Number specify the starting position of column .
the Last one is the message .

Another helpful  function is 
 void Lcd_Out_Cp(char *text);

 Example : Lcd_Out_Cp("Here!");

It uses to display the text message at the current position .


 void Lcd_Chr(char row, char column, char out_char);

The above function is used to display the value which is placed in variable 

Example : Lcd_Chr(2, 3, 'i');

It displays the 'I" value in the second line  at the third position .

Full Programm
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


 void main()

   {
      Lcd_Init();
      Lcd_Cmd(_LCD_Clear);
      Lcd_Cmd(_LCD_CURSOR_OFF);
      
      Lcd_out(1,1,"Hello");
      
      while(1)
      {}

   }

Full Project Can be download here Including Proteus File click

ADC Interfacing with 8051

By
ADC Interfacing with 8051


Read the Introduction here for the ADC  tutorials .   For LCD tutorials Click here

Both the above two tutorials combined  and  tried to read the adc value and Displaying the value in 16x2 LCD only for the learning purpose . If you have any doubt ask me By emailing thnnara123@gmail.com ,or comment below .


 Circuit 



Program In written Keil  with C compiler 
**********************************************************************************
Main file // ADC File
***********************************************************************************
#include <REGX51.H>  
#include "lcd.h" 
#include"adc.h"
#define test_port P2
#include<stdio.h>

void main(){
unsigned char buffer[8];

int adc_value;
LCDPORT = 0x00;
adc_port = 0xFF;
test_port =0x00;
lcd_init();
string("adc ");
get_adc();
P2 = adc_port;
adc_value = 0x60;//adc_port;
LINE2
sprintf(buffer,"%d",adc_value);   // used to convert Binary value from ADC to ASCI value for Lcd

string(buffer);

while(1);

}

***********************************************************************************
    adc.h // ADC Header File
************************************************************************************
#define adc_port P3
sbit RD_adc = P1^0;
sbit WR_adc = P1^1;
sbit INTR = P1^2; 

 get_adc()
 { 
  
 WR_adc = 0;
 delay(1);
 WR_adc =1;
 while(INTR==1);
 RD_adc = 1;
 delay(1);
 RD_adc = 0;

  }
**********************************************************************************
LCD.h // LCD Header File
***********************************************************************************
 #include"delay.h"
#define LCDPORT P0 // named the Port2 as LCDPORT
sbit RS=LCDPORT^2; // P2.2 named RS
sbit E=LCDPORT^3; // P2.3 named as E
#define LINE2 lcd_cmd(0xc0); // used to display the second line oxc0 is


void latch(void) // used to a high to low pulse the pin E
{
E = 1;
delay(1);
E = 0;
}

void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
RS = 0; // send a '0' value to select to send command
delay(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
delay(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}

void lcd_data(unsigned char c)
{
RS =1; // send 1 to send data
delay(1);
LCDPORT = c & 0xf0 | 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
delay(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}

void lcd_init()
{
delay(20);
lcd_cmd(0x30); //as per data sheet
delay(20);
lcd_cmd(0x30); //as per data sheet
delay(4);
lcd_cmd(0x32); //as per data sheet
delay(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}

void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}

***********************************************************************************
To download the Whole Project Click here

ADC Interfacing with 8051

By
ADC Interfacing with 8051


Read the Introduction here for the ADC  tutorials .   For LCD tutorials Click here

Both the above two tutorials combined  and  tried to read the adc value and Displaying the value in 16x2 LCD only for the learning purpose . If you have any doubt ask me By emailing thnnara123@gmail.com ,or comment below .


 Circuit 



Program In written Keil  with C compiler 
**********************************************************************************
Main file // ADC File
***********************************************************************************
#include <REGX51.H>  
#include "lcd.h" 
#include"adc.h"
#define test_port P2
#include<stdio.h>

void main(){
unsigned char buffer[8];

int adc_value;
LCDPORT = 0x00;
adc_port = 0xFF;
test_port =0x00;
lcd_init();
string("adc ");
get_adc();
P2 = adc_port;
adc_value = 0x60;//adc_port;
LINE2
sprintf(buffer,"%d",adc_value);   // used to convert Binary value from ADC to ASCI value for Lcd

string(buffer);

while(1);

}

***********************************************************************************
    adc.h // ADC Header File
************************************************************************************
#define adc_port P3
sbit RD_adc = P1^0;
sbit WR_adc = P1^1;
sbit INTR = P1^2; 

 get_adc()
 { 
  
 WR_adc = 0;
 delay(1);
 WR_adc =1;
 while(INTR==1);
 RD_adc = 1;
 delay(1);
 RD_adc = 0;

  }
**********************************************************************************
LCD.h // LCD Header File
***********************************************************************************
 #include"delay.h"
 #define LCDPORT P0  // named the Port2 as LCDPORT
 sbit RS=LCDPORT^2;  // P2.2 named RS
 sbit E=LCDPORT^3;  // P2.3 named as E
 #define LINE2 lcd_cmd(0xc0);   // used to display the second line oxc0 is 
 
 
 void latch(void)  // used to a high to low pulse the pin E 
{
    E = 1;
    delay(1);
    E = 0;
}

void lcd_cmd(unsigned char c)   // used to send the command / Instruction to the lcd port 
{          
RS = 0;    // send a '0' value to select to send command
delay(1);     
LCDPORT = c & 0xf0;  //  send  the command c only 4 bit by masking the lower bit 
latch();
delay(1); 
LCDPORT =  (c << 4); // giving the lowerbit  by shifting the 4 bit to left 
latch(); 
}

 void lcd_data(unsigned char c)
 {
 RS =1;    // send 1 to send data 
 delay(1);
LCDPORT = c & 0xf0 | 0x4; //send  the data  only  4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
delay(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit  by shifting the 4 bit to left 
latch(); 
}
 
void lcd_init()
{
  delay(20);
    lcd_cmd(0x30);  //as per data sheet
  delay(20);
    lcd_cmd(0x30);    //as per data sheet
  delay(4);
    lcd_cmd(0x32);    //as per data sheet
 delay(4);
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x0c);            // Make cursorinvisible
    lcd_cmd(0x6);            // Set entry Mode(auto increment of cursor)
}

 void string(const char *q)    // used to send single charcter to display the lcd 
{
    while (*q) {
        lcd_data(*q++);
    }
}
***********************************************************************************
To download the Whole Project Click here

LCD 4 Bit Interfacing with AVR

By
LCD 4 Bit Interfacing with AVR  -Atmega16

After interfacing with 8051 and PIC16f877  am providing AVR interfacing to LCD ,with a light changing in the previous code (ClikHere) .

I am taken Atmeg16 Controller . 




************* Please Comment If you have Doubt **********


/*
 * LCD4bit.c
 *
 * Created: 16-Sep-15 10:22:26 PM
 * Author: Krishna
 */

#define F_CPU 12000000UL
#include <avr/io.h>
#include<util/delay.h>
#define LCDPORT PORTD // Renaming the PORTD to LCDPORT
#define RS PD2 // Renaming the RS pin to 2
#define E PD3 // Renaming the E to number 3
#define LINE2 lcd_cmd(0xc0);
void latch(void) // used to a high to low pulse the pin E
{
PORTD |= ~(1<<E); // here we give a high to PORTD.3
_delay_ms(1);
PORTD |= (1<<E); // Here we give a LOW to PORTD.#
}




void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
PORTD |= ~(1<<RS); // send a '0' value to select to send command
_delay_ms(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
_delay_ms(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}

 void gotoxy(unsigned char  x,unsigned char y)  
{
if(x<40)
{
if(y) x|=0b01000000;
x|=0b10000000;
lcd_cmd(x);
}

void lcd_data(unsigned char c)
{
PORTD |= (1<<RS); // send 1 to send data
_delay_ms(1);
LCDPORT = c & 0xf0| 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
_delay_ms(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}

void lcd_init()
{
_delay_ms(20);
lcd_cmd(0x30); //as per data sheet
_delay_ms(20);
lcd_cmd(0x30); //as per data sheet
_delay_ms(4);
lcd_cmd(0x32); //as per data sheet
_delay_ms(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}

void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}


void main(){

DDRD = 0xFF;
lcd_init();
string("Lcd Testing ");
LINE2
string("its displaying ");
while(1);
}



Circuit Diagram 
Downloa here the Full project including Proteus Click here

LCD 4 Bit Interfacing with AVR

By
LCD 4 Bit Interfacing with AVR  -Atmega16

After interfacing with 8051 and PIC16f877  am providing AVR interfacing to LCD ,with a light changing in the previous code (ClikHere) .

I am taken Atmeg16 Controller . 




************* Please Comment If you have Doubt **********


/*
 * LCD4bit.c
 *
 * Created: 16-Sep-15 10:22:26 PM
 *  Author: Krishna
 */ 

#define F_CPU 12000000UL
#include <avr/io.h>
#include<util/delay.h>
#define LCDPORT PORTD   // Renaming the PORTD to LCDPORT
#define RS PD2          // Renaming the RS pin to 2 
#define E PD3            // Renaming the E to number 3
#define LINE2 lcd_cmd(0xc0);  
 void latch(void)  // used to a high to low pulse the pin E
 {
  PORTD |= ~(1<<E);  // here we give a high to PORTD.3
 _delay_ms(1);
  PORTD |= (1<<E);   // Here we give a LOW to PORTD.#
 }
 
 
 
 
 void lcd_cmd(unsigned char c)   // used to send the command / Instruction to the lcd port
 {
  PORTD |= ~(1<<RS);    // send a '0' value to select to send command
  _delay_ms(1);
  LCDPORT = c & 0xf0;  //  send  the command c only 4 bit by masking the lower bit
  latch();
  _delay_ms(1);
  LCDPORT =  (c << 4); // giving the lowerbit  by shifting the 4 bit to left
  latch();
 }

 void gotoxy(unsigned char  x,unsigned char y)  
{
 if(x<40)
 {
  if(y) x|=0b01000000;
  x|=0b10000000;
  lcd_cmd(x);
  }
 
 void lcd_data(unsigned char c)
 {
  PORTD |= (1<<RS);    // send 1 to send data
  _delay_ms(1);
  LCDPORT = c & 0xf0| 0x4; //send  the data  only  4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
  _delay_ms(1);
  latch();
  
  LCDPORT = (c << 4)| 0x4; ; // giving the lower bit  by shifting the 4 bit to left
  latch();
 }
 
  void lcd_init()
  {
   _delay_ms(20);
   lcd_cmd(0x30);  //as per data sheet
   _delay_ms(20);
   lcd_cmd(0x30);    //as per data sheet
   _delay_ms(4);
   lcd_cmd(0x32);    //as per data sheet
   _delay_ms(4);
   lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
   lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
   lcd_cmd(0x0c);            // Make cursorinvisible
   lcd_cmd(0x6);            // Set entry Mode(auto increment of cursor)
  }
 
  void string(const char *q)    // used to send single charcter to display the lcd
  {
   while (*q) {
    lcd_data(*q++);
   }
  }
 
 
void main(){
 
  DDRD = 0xFF;
 lcd_init();
  string("Lcd Testing ");
 LINE2
 string("its displaying ");
 while(1);
}


Circuit Diagram 
Downloa here the Full project including Proteus Click here

4-Bit LCD Interfacing with PIC 16f877 -MPLABX

By
4-Bit LCD Interfacing with PIC 16f877 -MPLABX :-

The Previous Post click Here talked about How to interface LCD in 4 bit mode with 8051 . with a little changes of the previous  code, we can use it in PIC16F877a with MPLABx ..




#include 
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)


#define _XTAL_FREQ 8000000
#define LCDPORT PORTB
#define RS RB2
#define E RB3
//#define LIINE2 lcd_cmd(0xc0)

void latch(void)
{
E = 1;
__delay_ms(1);
E = 0;

}
void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
RS = 0; // send a '0' value to select to send command
__delay_ms(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
__delay_ms(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}
void lcd_data(unsigned char c)
{
RS =1; // send 1 to send data
__delay_ms(1);
LCDPORT = c & 0xf0 | 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
__delay_ms(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}

void lcd_init()
{
__delay_ms(20);
lcd_cmd(0x30); //as per data sheet
__delay_ms(20);
lcd_cmd(0x30); //as per data sheet
__delay_ms(4);
lcd_cmd(0x32); //as per data sheet
__delay_ms(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}

void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}


void main()
{
TRISB = 0x00;

lcd_init();
string("Lcd working ");
lcd_cmd(0xc0);
string("its displaying ");
while(1);
}


Download the full project including Proteus File Clik here

4-Bit LCD Interfacing with PIC 16f877 -MPLABX

By
4-Bit LCD Interfacing with PIC 16f877 -MPLABX :-

The Previous Post click Here talked about How to interface LCD in 4 bit mode with 8051 . with a little changes of the previous  code, we can use it in PIC16F877a with MPLABx ..




#include 
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)


#define _XTAL_FREQ 8000000
#define LCDPORT PORTB
#define RS RB2
#define E RB3
//#define LIINE2 lcd_cmd(0xc0)

void latch(void)
{
    E = 1;
    __delay_ms(1);
    E = 0;

}
void lcd_cmd(unsigned char c)   // used to send the command / Instruction to the lcd port
{
RS = 0;    // send a '0' value to select to send command
__delay_ms(1);
LCDPORT = c & 0xf0;  //  send  the command c only 4 bit by masking the lower bit
latch();
__delay_ms(1);
LCDPORT =  (c << 4); // giving the lowerbit  by shifting the 4 bit to left
latch();
}
 void lcd_data(unsigned char c)
 {
 RS =1;    // send 1 to send data
 __delay_ms(1);
LCDPORT = c & 0xf0 | 0x4; //send  the data  only  4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
__delay_ms(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit  by shifting the 4 bit to left
latch();
}

 void lcd_init()
{
 __delay_ms(20);
    lcd_cmd(0x30);  //as per data sheet
  __delay_ms(20);
    lcd_cmd(0x30);    //as per data sheet
  __delay_ms(4);
    lcd_cmd(0x32);    //as per data sheet
 __delay_ms(4);
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x0c);            // Make cursorinvisible
    lcd_cmd(0x6);            // Set entry Mode(auto increment of cursor)
}

 void string(const char *q)    // used to send single charcter to display the lcd
{
    while (*q) {
        lcd_data(*q++);
    }
}


void main()
{
    TRISB = 0x00;

lcd_init();
string("Lcd working ");
lcd_cmd(0xc0);
string("its displaying ");
while(1);
}


Download the full project including Proteus File Clik here

MPLAB XC8 Compiler – LED Blinking - Basic Start-up Tutorials

By
A sample program to blink an LED  by using MPLBX - C Compilers  in  PIC16F877A as our previous post  PIC Microcontroller Tutorial gives the basic Details of PIC16f877A.

           In MPLABX  at first we need to configure the bit first.  That means ,


  •  How or what type Oscillator will use  ?
  •  Is there needed Watch Dog timer ?
  •  Program Memory Code Protection bit ie, no one can be read the HEX code from  the Controller.

      For more learn from Here.

#include<xc.h> 
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)


#define _XTAL_FREQ 8000000 // taking as crystal frequency as 8Mhz
#define LED_Port PORTD // defining PORTD as LED_Port
int main()
{
TRISD = 0x00; // Making PORTD as output
while(1)
{
LED_Port =0Xff; // giving High values to Port to lightup LED
__delay_ms(500); // giving a half second delay
LED_Port = 0x00; // Giving a low to port to turn off the LEDs
__delay_ms(500);
}

}

MPLAB XC8 Compiler – LED Blinking - Basic Start-up Tutorials

By
A sample program to blink an LED  by using MPLBX - C Compilers  in  PIC16F877A as our previous post  PIC Microcontroller Tutorial gives the basic Details of PIC16f877A.

           In MPLABX  at first we need to configure the bit first.  That means ,


  •  How or what type Oscillator will use  ?
  •  Is there needed Watch Dog timer ?
  •  Program Memory Code Protection bit ie, no one can be read the HEX code from  the Controller.

      For more learn from Here.

#include<xc.h> 
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)


#define _XTAL_FREQ 8000000 // taking as crystal frequency as 8Mhz
#define LED_Port PORTD     // defining PORTD as LED_Port
int main()
{
    TRISD = 0x00;          // Making PORTD as output
    while(1)
    {
        LED_Port =0Xff;    // giving High values to Port to lightup LED
        __delay_ms(500);   // giving a half second delay
        LED_Port = 0x00;   // Giving a low to port to turn off the LEDs
        __delay_ms(500);
    }

}

Configuration Bits setting in MPLABX

By
Set Configuration Bits -PIC16f877.


How Configuration Bits are set

Each PIC MCU has it's own set of configuration bits. The Special Features section of the individual datasheets contains the definition for each of the bits.
The setting for each configuration bit is determined by directives written in the application software. The syntax for the configuration bit settings is available from the compiler manual. This tutorial will show how to generate proper configuration code without consulting the compiler manual for the proper syntax.

How to display the Configuration Bits window

                                                 From the main menu select Window ▶ PIC Memory Views ▶ Configuration Bits

ConfigBitsMenu.pngConfigBitsWindow.png

The Configuration Bits window will open in a tab in the Output area under the editor.

How to set configuration bits and generate initialization code



ConfigBits.png
In the configuration bits window, click on any value in the Option column and it will turn into a combo box that will allow you to select the value you desire.

The example on the left shows the WatchDog timer being changed from "enabled" to "disabled"

GenerateConfigBits.png




Click on the Generate Source Code to Outputbutton:

ConfigBitsSaveAs.png

The IDE will automatically generate the code necessary to initialize all the configuration bits to the settings you specified in the window. This code may now be copied and pasted into one of your source files, or you may save it to its own file and add it to your project. To save the file, right click anywhere in the output window and select Save Asfrom the popup menu as shown in the screenshot at left.

Configuration Bits setting in MPLABX

By
Set Configuration Bits -PIC16f877.


How Configuration Bits are set

Each PIC MCU has it's own set of configuration bits. The Special Features section of the individual datasheets contains the definition for each of the bits.
The setting for each configuration bit is determined by directives written in the application software. The syntax for the configuration bit settings is available from the compiler manual. This tutorial will show how to generate proper configuration code without consulting the compiler manual for the proper syntax.

How to display the Configuration Bits window

                                                 From the main menu select Window ▶ PIC Memory Views ▶ Configuration Bits

ConfigBitsMenu.pngConfigBitsWindow.png

The Configuration Bits window will open in a tab in the Output area under the editor.

How to set configuration bits and generate initialization code



ConfigBits.png
In the configuration bits window, click on any value in the Option column and it will turn into a combo box that will allow you to select the value you desire.

The example on the left shows the WatchDog timer being changed from "enabled" to "disabled"

GenerateConfigBits.png




Click on the Generate Source Code to Outputbutton:

ConfigBitsSaveAs.png

The IDE will automatically generate the code necessary to initialize all the configuration bits to the settings you specified in the window. This code may now be copied and pasted into one of your source files, or you may save it to its own file and add it to your project. To save the file, right click anywhere in the output window and select Save Asfrom the popup menu as shown in the screenshot at left.

More Posts

gElectron. Powered by Blogger.

Contributors

16x2 LCD Interfacing with STM32,STM32F103C6

 16x2 LCD  Interfacing with STM32,STM32F103C6 lcd_init(); LCD_LINE1; lcd_String(" GeElectron"); LCD_LINE2; lc...

Contact Form

Name

Email *

Message *

Contact us

Name

Email *

Message *

Follow Us

https://www.facebook.com/gElectron-393939667321867/ FBbox/https://www.facebook.com/IVYthemes

Comments

[blogger]

MKRdezign

Test

Latest

[recent][newsticker]

Technology

Top Ads

RECENT COMMENTS

Subscribe Via Email

Subscribe to our newsletter to get the latest updates to your inbox. ;-)


Your email address is safe with us!

RECENT COMMENTS