Pages

Friday 3 April 2015

Programming with PIC Microcontroller: Chapter-3

GETTING STARTED WITH EMBED C PROGRAMMING:


                             Lab 1 . LED Blinking using PIC controller (16F877A) with MPLAB



I/O Connections :

PORT B4->LED1
PORTB5->LED2
PORTB6->Switch1
PORTB7->Switch2


#include <htc.h>
#define _XTAL_FREQ 20000000       //crystal frequency of 20MHZ
#define Input1 RB7                               //set port RB7 as input port
#define Input2 RB1                             //set port RB1 as input port
#define Output1 RB4                         //set port RB4 as output port
#define Output2 RB5                        //set port RB5 as output port
void main()
{
TRISB=0X82;                                  //use portB register as input as well as output port
while(1)                                             //infinite loop
{
if(Input1==0)                                   //if switch1 is pressed ie connect port RB7 to sw1
{
Output1=1;                                     //blink both the LED’S
Output2=1;
}
else if(Input2==0)                          //If switch2 is pressed ie connect port RB1 to sw2
{
Output1=0;                                     //both the LED’S are turned off
Output2=0;
}
}
}


                                  Lab2.To display a message on LCD using pic controller



I/O connection:
 
PORT B0 tO B7->DO to D7 of LCD
ENABLE->D7
R/W->GROUND
R/S->D6


#include <htc.h>
#include<string.h>
#define _XTAL_FREQ 20000000                           //crystal frequency of 20MHZ
#define EN RD7                                                        //connect enable pin of LCD to port D7
#define RS RD6                                                       //connect Register select pin of LCD
to port D6
void LCD_Delay()                                                    //delay routine
{
__delay_ms(1);
}
void LCD_Cmd(unsigned char cmd)                 //this function is to write command to
                                                                             the LCD

{
PORTB=cmd;
RS=0;                                                                 //Set RS pin to low in order to send a
                                                                              command to the LCD

EN=1;                                                                 //set EN pin to high in order to send
                                                                              high pulse

LCD_Delay();                                                  //give a small delay
EN=0;                                                               //set EN pin to low in order to make
                                                                           pulse low

LCD_Delay();                                                 //give a small delay

}
void LCD_Init()                                               //Initializing LCD
{
unsigned char cmd[5]={0X38,0X06,0X0F,0X01,0X80},Count;
             //0x38 represents 5x7 matrix ,0x06 represent entry mode,0x0f represent display on     cursor blinking,0x01 represents clearing the LCD,0x80 represents 1st row
for(Count=0;Count<5;Count++)
LCD_Cmd(cmd[Count]);
}
void LCD_SendDataByte(unsigned char data)     //this function is to write a byte on LCD
{
PORTB=data;
RS=1;                                                                    //make RS pin high inorder to send a
                                                                                data

EN=1;                                                                    //set enable pin to high in order to
                                                                                send high to low pulse

LCD_Delay();                                                      //provide a small delay
EN=0;
LCD_Delay();
}
void LCD_Display( char *addr)                         //this function is to display a string on
                                                                            LCD

{
while(*addr)
{
LCD_SendDataByte(*addr);
addr++;
}

}
void main()
{
TRISB=0x00;                                                     //make the registerB as ouput
TRISD=0x00;                                                     //make the registerD as ouput
LCD_Init();                                                          //Initialize the LCD
__delay_ms(1000);
while(1)                                                                //infinite loop
{
LCD_Cmd(0X01);                                               //clearing the LCD
LCD_Cmd(0X84);                                              //1st row 4th position
LCD_Display("123");                                         //display 123 on LCD
LCD_Cmd(0xc0);                                              //2nd row
LCD_Display(" RDL");                                     //display RDL on LCD
__delay_ms(1000);                                         //delay by 1s
LCD_Cmd(0x01);                                           //clear the LCD
LCD_Cmd(0x80);                                           //1st row
LCD_Display(" LCD");                                   //display LCD
LCD_Cmd(0xc0);                                           //2nd row
LCD_Display(" Display");                              //display on LCD
__delay_ms(1000);                                        //delay by 1s

}

 Lab3.Interfacing ADC to display analog to digital conversion values on LCD.

I/O connection:
 
PORT B0 to B7->DO to D7 of LCD
ENABLE->D7
R/W->GROUND
R/S->D6
A0->PORTC0



#include <htc.h>
#include<string.h>
#define _XTAL_FREQ 20000000                            //crystal frequency of 20MHZ
#define EN RD7                                                         //connect enable pin of LCD to port D7
#define RS RD6                                                       //connect Register select pin of LCD                                                                                           to port  D6
/*LCD code */
void LCD_Delay()                                                    //delay routine
{
__delay_ms(1);
}
void LCD_Cmd(unsigned char cmd)               //this function is to write command to the LCD
{
PORTB=cmd;
RS=0;                                          //Set RS pin to low in order to send a command to the LCD
EN=1;                                        //set EN pin to high in order to send high pulse
LCD_Delay();                        //give a small delay
EN=0;                                    //set EN pin to low in order to make pulse low

LCD_Delay();                      //give a small delay
}
void LCD_Init()                   //Initializing LCD
{
               unsigned char cmd[5]={0X38,0X06,0X0F,0X01,0X80},Count;
                                //0x38 represents 5x7 matrix ,0x06 represent entry mode,0x0f represent display on cursor blinking,0x01 represents clearing the LCD,0x80 represents 1st row
for(Count=0;Count<5;Count++)
LCD_Cmd(cmd[Count]);
}
void LCD_SendDataByte(unsigned char data)   //this function is to write a byte on LCD
{
PORTB=data;
RS=1;                                                     //make RS pin high inorder to send a data
EN=1;                                                //set enable pin to high in order to send high to low pulse
LCD_Delay();                                    //provide a small delay
EN=0;
LCD_Delay();
}
void LCD_Display( char *addr)    //this function is to display a string on LCD
{
while(*addr)
{
LCD_SendDataByte(*addr);
addr++;
}
}
void ADC_Init()
{
ADCON0 = 0x41;               //set A/D control register0 to 0x41
ADCON1 = 0xC0;              //set A/D control register1 0xc0 

}
unsigned int ADC_Read(unsigned char channel)
{
if(channel > 7)
return 0;
ADCON0 &= 0xC5;
ADCON0 |= channel<<3;
__delay_ms(2);
GO_nDONE = 1;
while(GO_nDONE);
return ((ADRESH<<8)+ADRESL);                   //left shift the higherorder bits and add the                                                                                  lower order bits
}
void display(unsigned int number)            //this function is for (0-1024)A/D conversion
{
unsigned char digit1,digit2,digit3,digit4,digit[4];
unsigned char x;
unsigned char temp;
digit1 = number / 1000u ;                                       // extract thousands digit
digit2 = (number / 100u) % 10u;                           // extract hundreds digit
digit3 = (number / 10u) % 10u;                            // extract tens digit
digit4 = nu
mber % 10u;                                                       // extract ones digit
digit[3]=digit4;
digit[2]=digit3;
digit[1]=digit2;
digit[0]=digit1;
for(x=0;x<4;x++)                                          //loop for upto 4 digits
{
temp=digit[x]|0x30;                                   //convert to ACII
LCD_SendDataByte(temp);                     //display the value on LCD
}
}
void main()
{
unsigned int value;
unsigned int a;
TRISB = 0x00;                                       //Set registerB as output
TRISC = 0x00;                                      //Set registerC as output
TRISD=0x00;                                       //set registerD as output
LCD_Init();                                          //initialize the LCD
__delay_ms(1000);                            //provide delay for 1s
ADC_Init();                                        //ADC initialisation
do
{
a = ADC_Read(0);                            //read port (A0)
__delay_ms(2000);                          //provide delay for 2s
LCD_Cmd(0x80);                            //1st row
LCD_ display(a);                             //display the value on LCD
__delay_ms(1000);                         //provide delay
} while(1);
}