-->
Showing posts with label PIC. Show all posts
Showing posts with label PIC. Show all posts

ADC reading with Internal adc of PIC16F77 and displaying on LCD

By
In PIC16F877 has 8 channels are available for adc input ie AN0 to AN7.Any one can be use ,we can select the the channel by selecting channel selection bit.

The Whole project Can be download at the Bottom of the post .


            In ADC the Vref voltage is important in accuracy of the ADC conversion.Low Vref gets more accuracy . But This PIC has a 10 bit result ion so it is enough As follows some example

If    Vref             =    4.096 volt
     Bit resolution  = 8 ie 2^8 =256
then  4.096/256 = 16Millvolt Per Bit
        3.88/256 = 15mv/bit
        2.56/256 = 10mv/bit
giving vcc as vref (5Volt) in 10 bit Resolution
Vref =5 v
Bit resolution  = 10 bit ,ie 2^10 = 1024
so
            5/1024 = 4.8828 millivolt /Bit
Here  taking 5 volt as vref

For getting ADC value the following steps are needed

 * Initialization of ADC
 * Reading ADC value


 Initialization of ADC

 Initialization of ADC mean that
Configure the ADC PORT  for that PIC has 4 registers they are
* ADCON 0
* ADCON 1
* ADRESH
*ADRESL

ADCON0 :- it is an 8 bit register it decide how the ADC worrks in accordance with the following bit have to set .
Lasr(msb bit) two bit decide the clock of the ADC ie the conversion speed.if we select the the main PIC crystal frequency as 20MHz
then I will select the First tw register value is 10
ie 10 = Fosc/32
after calculating the clock speed there must be a time 1.6 micro second for one bit conversion these time is called TAD . So to convert  10 bit  need 10x1.6 microsecond .,Look Data sheet page No .133 Section 11-2.

 I am selecting the ADC clock selection bit is 10

ADCS1  = 1
ADCS0  = 0
ie fosc/32    Look Datasheet Page No. 130 section 11.2

Calculation
            20Mhz/32 = 625000Hz ie 1.6Microsecond
           For one TAD is 1.6 is correct .(Look Data sheet page No .133 Section 11-2)

Then ,
The the 3rd,4the and 5th bit is the analog channel selection Bit from the Port AN0 to AN7.




The second bit(GO/DONE) is used to Start the conversion  of analog value to digital when its value is one .And automatically reset (become zero) whenever the completion take place  

 GO/DONE: A/D Conversion Status bit
When ADON = 1:
1 = A/D conversion in progress (setting this bit starts the A/D conversion which is automatically
cleared by hardware when the A/D conversion is complete)
0 = A/D conversion not in progress  

The Second Bit is not used Leave it as Zero

The First Bit is the Bit to Turn On the ADC .(powering on the ADC).

So the ADCON0 value is 10000001 in Hex a 0x81;

ADCON1 :-
         
         
       
The Bit From Bit 0 to Bit 3  are used decide  to How act the Analog PIN . How the Vref and GND voltage are given. I am selecting the first one
0000 becuase the 8 bit of AN is selected as Analog (denoted above as "A")
And the Vref is taken as VDD ie 5volt (VCC)
* channel selected
So the ADCON1 value is 10000000 in hex format = 0x80;

The Last Bit of ADCON1 is ADFM is used to alignment the result of adc (ie the value )
If its value is One The 10 bit adc value is right alimented
If its value is zero The 10 bit is left aligned

ADRESH :- is the Register (8bit) the result of ADC saved here . Our result is 10 bit if we use 10 bit resolution. But the ADRESH register is 8 bit So the 8 bit of the 10 bit adc result is saved in ADRESH the balance 2 bit is saved in ADRESL .



Circuit diagram 






Main Programm
/* 
* File: adc_main.c
* Author: gElectron/Gireesh
*
* Created on September 27, 2015, 6:24 AM
*/


// PIC16F877 Configuration Bit Settings

// 'C' source line config statements

#include
#include"lcd.h"
#include"adc.h"
#include
#define _XTAL_FREQ 20000000 // 20Mh
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF // FLASH Program Memory Write Enable (Unprotected program memory may not be written to by EECON control)


void main()
{
TRISB = 0X00; // output for the 8 bit
TRISA =0XFF;
int adc_value=0x00,ch_sec = 0;
unsigned char digits[5];
lcd_init(); // lcd initialization
adc_init(); // adc initialization
adc_value = read_adc(ch_sec); // reading adc value
string("gElectron ");
LINE2;
digits[0] = adc_value / 1000 + 48;
digits[1] = (adc_value / 100) % 10 + 48;
digits[2] = (adc_value / 10) % 10 + 48;
digits[3] = adc_value % 10 + 48;
digits[4] = '\0';
string(digits);

while(1); // endless loop
}

ADC file
/* 
* File: adc.c
* Author: Krishna
*
* Created on September 27, 2015, 7:14 AM
*/
#include
#include"adc.h"
//#define _XTAL_FREQ 20000000
void adc_init()
{

ADCON0 = 0x81;// 10000001 TAD is fosci/32 and last bit is adc poweron
ADCON1 = 0x80; //all port selected as output ,vref= vcc,
TRISA = 0x2f; //ENANLING AS ANALOG INPUT
TRISE = 0X07; // ENANLING AS ANALOG INPUT
}

unsigned int read_adc(unsigned char ch_selection)
{
if(ch_selection>7) // checking is channel greater than 7
return 0; // yes return zero
__delay_us(50); //adc acquisition time as 19.72 ?s as datasheet
GO_nDONE = 1;
while(GO_nDONE); // after conversion GO_nDONE value become 0 autmatically.
return((ADRESH<<8)+ADRESL);

Downlaod the Whole Project Including Proteus Simulation click heredownload

ADC reading with Internal adc of PIC16F77 and displaying on LCD

By
In PIC16F877 has 8 channels are available for adc input ie AN0 to AN7.Any one can be use ,we can select the the channel by selecting channel selection bit.

The Whole project Can be download at the Bottom of the post .


            In ADC the Vref voltage is important in accuracy of the ADC conversion.Low Vref gets more accuracy . But This PIC has a 10 bit result ion so it is enough As follows some example

If    Vref             =    4.096 volt
     Bit resolution  = 8 ie 2^8 =256
then  4.096/256 = 16Millvolt Per Bit
        3.88/256 = 15mv/bit
        2.56/256 = 10mv/bit
giving vcc as vref (5Volt) in 10 bit Resolution
Vref =5 v
Bit resolution  = 10 bit ,ie 2^10 = 1024
so
            5/1024 = 4.8828 millivolt /Bit
Here  taking 5 volt as vref

For getting ADC value the following steps are needed

 * Initialization of ADC
 * Reading ADC value


 Initialization of ADC

 Initialization of ADC mean that
Configure the ADC PORT  for that PIC has 4 registers they are
* ADCON 0
* ADCON 1
* ADRESH
*ADRESL

ADCON0 :- it is an 8 bit register it decide how the ADC worrks in accordance with the following bit have to set .
Lasr(msb bit) two bit decide the clock of the ADC ie the conversion speed.if we select the the main PIC crystal frequency as 20MHz
then I will select the First tw register value is 10
ie 10 = Fosc/32
after calculating the clock speed there must be a time 1.6 micro second for one bit conversion these time is called TAD . So to convert  10 bit  need 10x1.6 microsecond .,Look Data sheet page No .133 Section 11-2.

 I am selecting the ADC clock selection bit is 10

ADCS1  = 1
ADCS0  = 0
ie fosc/32    Look Datasheet Page No. 130 section 11.2

Calculation
            20Mhz/32 = 625000Hz ie 1.6Microsecond
           For one TAD is 1.6 is correct .(Look Data sheet page No .133 Section 11-2)

Then ,
The the 3rd,4the and 5th bit is the analog channel selection Bit from the Port AN0 to AN7.




The second bit(GO/DONE) is used to Start the conversion  of analog value to digital when its value is one .And automatically reset (become zero) whenever the completion take place  

 GO/DONE: A/D Conversion Status bit
When ADON = 1:
1 = A/D conversion in progress (setting this bit starts the A/D conversion which is automatically
cleared by hardware when the A/D conversion is complete)
0 = A/D conversion not in progress  

The Second Bit is not used Leave it as Zero

The First Bit is the Bit to Turn On the ADC .(powering on the ADC).

So the ADCON0 value is 10000001 in Hex a 0x81;

ADCON1 :-
         
         
       
The Bit From Bit 0 to Bit 3  are used decide  to How act the Analog PIN . How the Vref and GND voltage are given. I am selecting the first one
0000 becuase the 8 bit of AN is selected as Analog (denoted above as "A")
And the Vref is taken as VDD ie 5volt (VCC)
* channel selected
So the ADCON1 value is 10000000 in hex format = 0x80;

The Last Bit of ADCON1 is ADFM is used to alignment the result of adc (ie the value )
If its value is One The 10 bit adc value is right alimented
If its value is zero The 10 bit is left aligned

ADRESH :- is the Register (8bit) the result of ADC saved here . Our result is 10 bit if we use 10 bit resolution. But the ADRESH register is 8 bit So the 8 bit of the 10 bit adc result is saved in ADRESH the balance 2 bit is saved in ADRESL .



Circuit diagram 






Main Programm
/* 
 * File:   adc_main.c
 * Author: gElectron/Gireesh
 *
 * Created on September 27, 2015, 6:24 AM
 */


// PIC16F877 Configuration Bit Settings

// 'C' source line config statements

#include 
#include"lcd.h"
#include"adc.h"
#include 
#define _XTAL_FREQ 20000000 // 20Mh
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF        // FLASH Program Memory Write Enable (Unprotected program memory may not be written to by EECON control)


void main()
{
    TRISB = 0X00;  // output for the 8 bit
    TRISA =0XFF;
    int adc_value=0x00,ch_sec = 0;
    unsigned char digits[5];
    lcd_init();   // lcd initialization
    adc_init();   // adc initialization
    adc_value = read_adc(ch_sec);  // reading adc value
    string("gElectron ");
    LINE2;
        digits[0] = adc_value / 1000 + 48;
        digits[1] = (adc_value / 100) % 10 + 48;
        digits[2] = (adc_value / 10) % 10 + 48;
        digits[3] = adc_value % 10 + 48;
        digits[4] = '\0';
        string(digits);
   
    while(1);             // endless loop
}

ADC file
/* 
 * File:   adc.c
 * Author: Krishna
 *
 * Created on September 27, 2015, 7:14 AM
 */
#include
#include"adc.h"
//#define _XTAL_FREQ 20000000
void adc_init()
{

   ADCON0 = 0x81;// 10000001 TAD is fosci/32 and last bit is adc poweron
   ADCON1 = 0x80; //all port selected as output ,vref= vcc,
   TRISA = 0x2f; //ENANLING AS  ANALOG INPUT
   TRISE = 0X07; // ENANLING AS  ANALOG INPUT
}

unsigned int read_adc(unsigned char ch_selection)
{
 if(ch_selection>7) // checking is  channel greater than 7
      return 0;      // yes return zero
  __delay_us(50); //adc  acquisition time as 19.72 ?s as datasheet
  GO_nDONE = 1;
 while(GO_nDONE); // after conversion GO_nDONE value become 0 autmatically.
 return((ADRESH<<8)+ADRESL);
 
} 
Downlaod the Whole Project Including Proteus Simulation click heredownload

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

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