CIRCUIT DIAGRAM
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMs4wG4AI7vDvuZelo2YMd7w8qhBNUqeBdF-3P_ltxL51k5TRgpxqe4id0_dulKF3hjzkcwLwCJAO46COjvANN2b5YbowmTMyBe7O8CYyUS2HNy_416h4gt0cpESyb8u9UXos86brCYXx9/s1600/LDR.jpg)
SOURCE CODE
//----------------------------------------------------------------------------------
#include <xc.h> // this file contains the SFR information of the chip
//----------------------------------------------------------------------------------
#define _XTAL_FREQ 4000000L
//Function prototypes
void port_initialise(void);
void init_variables (void);
void DelayMs(unsigned int msCount);
void ADCLdrread(void);
void adc_init(void);
//--------------------------------------------------------------------------
//These are required if you are using this hex file with a device programmer or Debugger.
//When using with bootloader this will be ignored
//These config settings will start 452 system.
//But this example code after bootup switches clock to 4Mhz internal oscillator.
#pragma config WDTE = OFF
#pragma config FOSC = HS
#pragma config PWRTE = ON
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config DEBUG = ON
#pragma config CP = OFF
//--------------------------------------------------------------------------
//#pragma udata access myaccess
near volatile struct // This is a structure to create logic flags.
{ // FLAGAbits
unsigned FLAG0:1; //
unsigned FLAG1:1; //
unsigned FLAG2:1; //
unsigned FLAG3:1; //
unsigned FLAG4:1; //
unsigned FLAG5:1; //
unsigned FLAG6:1;
unsigned FLAG7:1;
} FLAGAbits;
//-------------------------------------------------------------------------
near union
{ //This is a union to hold the ADC reading for ADC converter. This ADC result
unsigned char ADCbytes[2];// we are getting as bytes from ADRSL and ADRSH. But it is
unsigned int Volt16Bit ; // a 10 bit number. So for calculation purpose
}ADCvolts; //we need to process it as a two byte number- so union
//---------------------------------------------------------------------------
// Variable dclarations(Global)
//-------------------------------------------------------------------------
// Defines
//----------------------------------------------------------------------------
//This is the intruupt area.
void interrupt InterruptArea(void)
{
}
//#pragma code MainlineCode //*********************************************************************************
void main (void)
{ // main loop starts-------------
unsigned char count;
unsigned char n;
port_initialise(); // setting up I/O lines as input and output
init_variables (); // variables are to be initialised
adc_init(); // initialises ther ADC module
//---------------------------------------------------------------------------------------------------
while(1) // Normally, every MCU programe is a never ending loop. here starts it
{ //while 1 loop starts----------
ADCwait++; // ADC need not be reated on every loop
if(ADCwait>10)
{
ADCwait=0; // do ADC only count reaches 11
ADCLdrread();
}
} //while 1 loop ends------------
} // main loop ends--------------
///*********************************************************************************************************
//#pragma code functions
//------------------------------------------------------------------------------------------------------
void ADCLdrread(void)
{
ADCON0bits.GO=1;
while(ADCON0bits.GO); // if conversion over
ADCvolts.ADCbytes[0]=ADRESL;
ADCvolts.ADCbytes[1]=ADRESH; // load ADC result bytes
ADCcurrentValue=(ADCvolts.Volt16Bit/2); // the 16bit number stord 5
}
//-------------------------------------------------------------------------------------------------------
void adc_init(void) // three analogue channels enables ,module on. but ch2 selected
{
ADCON0 = 0b01000001 ; //selecting channel 1 & making ADC module on .
ADCON1 = 0b10000100 ;//making channels AN0 & AN1 and AN2 as analog inputs .
TRISA |= 0b00101011;// three PORTA pins as inputs
ADCON0 &= 0b11000111;
ADCON0 |= 0b00011000; //select channel 3 and set the GO/DONE bit
ADRESH = 0x00;
ADRESL = 0x00;
}
//------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
void port_initialise (void)
{
TRISD = 0X00; // LED port is made as output
PORTD = 0X00; // and cleared
ADCON1 = 0b10000100; // A/D module not ued.All pins digital
TRISA = 0b00001011; // ADC Pins as input pins all other pins as output
PORTA = 0X00; // and cleared
TRISC &= 0b11111110; // LCD enable pin output
TRISE = 0B00000111; //sw pins as input
PORTE = 0X00;
TRISB = 0b00000000;// switch pins as input- all else output
PORTB = 0X00 ; // buzzer pins made output and kept low.
LEDenable=0; // led array enable transistor off
Digit1_En=0; // digits to be kept low
Digit2_En=0;
}
//*******************************************************************************************************
void init_variables (void)
{
Digit1=0;Digit2=0;DebounceCnt=0;
FlagIncri=FlagDecri=FlagDebounce=0;
LCDiniFlag=0;
LCDFrameFlag=0;cutoffDispFlag=1;
//_asm nop _endasm // you can add any varibale initialisation here if required
}
//**************************************************************************
void DelayMs(unsigned int msCount)
{
unsigned int locCnt;
for(locCnt=0;locCnt<=msCount;locCnt++)
{
__delay_ms(1); // This XC8 function creates a delay of 1 millisecond
} // looping this msCount times will give that much milliseconds delay
}
}
}
EXPLANATIONS
This program is to under stand the working of light-dependent resistor (LDR).its aphotoresistor (or light-dependent resistor,LDR, or photocell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. The output from ldr is connected to an adcpin.since 16f877a got a 10 bit adc we get a value from 0 to 1023 this value is divided by 2 to convert it to 0-5.11V.This volt is shown in the lcd and seven segment. We can see the change in the voltage by blocking the light to the LDR
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMs4wG4AI7vDvuZelo2YMd7w8qhBNUqeBdF-3P_ltxL51k5TRgpxqe4id0_dulKF3hjzkcwLwCJAO46COjvANN2b5YbowmTMyBe7O8CYyUS2HNy_416h4gt0cpESyb8u9UXos86brCYXx9/s1600/LDR.jpg)
SOURCE CODE
//----------------------------------------------------------------------------------
#include <xc.h> // this file contains the SFR information of the chip
//----------------------------------------------------------------------------------
#define _XTAL_FREQ 4000000L
//Function prototypes
void port_initialise(void);
void init_variables (void);
void DelayMs(unsigned int msCount);
void ADCLdrread(void);
void adc_init(void);
//--------------------------------------------------------------------------
//These are required if you are using this hex file with a device programmer or Debugger.
//When using with bootloader this will be ignored
//These config settings will start 452 system.
//But this example code after bootup switches clock to 4Mhz internal oscillator.
#pragma config WDTE = OFF
#pragma config FOSC = HS
#pragma config PWRTE = ON
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config DEBUG = ON
#pragma config CP = OFF
//--------------------------------------------------------------------------
//#pragma udata access myaccess
near volatile struct // This is a structure to create logic flags.
{ // FLAGAbits
unsigned FLAG0:1; //
unsigned FLAG1:1; //
unsigned FLAG2:1; //
unsigned FLAG3:1; //
unsigned FLAG4:1; //
unsigned FLAG5:1; //
unsigned FLAG6:1;
unsigned FLAG7:1;
} FLAGAbits;
//-------------------------------------------------------------------------
near union
{ //This is a union to hold the ADC reading for ADC converter. This ADC result
unsigned char ADCbytes[2];// we are getting as bytes from ADRSL and ADRSH. But it is
unsigned int Volt16Bit ; // a 10 bit number. So for calculation purpose
}ADCvolts; //we need to process it as a two byte number- so union
//---------------------------------------------------------------------------
// Variable dclarations(Global)
//-------------------------------------------------------------------------
// Defines
//----------------------------------------------------------------------------
//This is the intruupt area.
void interrupt InterruptArea(void)
{
}
//#pragma code MainlineCode //*********************************************************************************
void main (void)
{ // main loop starts-------------
unsigned char count;
unsigned char n;
port_initialise(); // setting up I/O lines as input and output
init_variables (); // variables are to be initialised
adc_init(); // initialises ther ADC module
//---------------------------------------------------------------------------------------------------
while(1) // Normally, every MCU programe is a never ending loop. here starts it
{ //while 1 loop starts----------
ADCwait++; // ADC need not be reated on every loop
if(ADCwait>10)
{
ADCwait=0; // do ADC only count reaches 11
ADCLdrread();
}
} //while 1 loop ends------------
} // main loop ends--------------
///*********************************************************************************************************
//#pragma code functions
//------------------------------------------------------------------------------------------------------
void ADCLdrread(void)
{
ADCON0bits.GO=1;
while(ADCON0bits.GO); // if conversion over
ADCvolts.ADCbytes[0]=ADRESL;
ADCvolts.ADCbytes[1]=ADRESH; // load ADC result bytes
ADCcurrentValue=(ADCvolts.Volt16Bit/2); // the 16bit number stord 5
}
//-------------------------------------------------------------------------------------------------------
void adc_init(void) // three analogue channels enables ,module on. but ch2 selected
{
ADCON0 = 0b01000001 ; //selecting channel 1 & making ADC module on .
ADCON1 = 0b10000100 ;//making channels AN0 & AN1 and AN2 as analog inputs .
TRISA |= 0b00101011;// three PORTA pins as inputs
ADCON0 &= 0b11000111;
ADCON0 |= 0b00011000; //select channel 3 and set the GO/DONE bit
ADRESH = 0x00;
ADRESL = 0x00;
}
//------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
void port_initialise (void)
{
TRISD = 0X00; // LED port is made as output
PORTD = 0X00; // and cleared
ADCON1 = 0b10000100; // A/D module not ued.All pins digital
TRISA = 0b00001011; // ADC Pins as input pins all other pins as output
PORTA = 0X00; // and cleared
TRISC &= 0b11111110; // LCD enable pin output
TRISE = 0B00000111; //sw pins as input
PORTE = 0X00;
TRISB = 0b00000000;// switch pins as input- all else output
PORTB = 0X00 ; // buzzer pins made output and kept low.
LEDenable=0; // led array enable transistor off
Digit1_En=0; // digits to be kept low
Digit2_En=0;
}
//*******************************************************************************************************
void init_variables (void)
{
Digit1=0;Digit2=0;DebounceCnt=0;
FlagIncri=FlagDecri=FlagDebounce=0;
LCDiniFlag=0;
LCDFrameFlag=0;cutoffDispFlag=1;
//_asm nop _endasm // you can add any varibale initialisation here if required
}
//**************************************************************************
void DelayMs(unsigned int msCount)
{
unsigned int locCnt;
for(locCnt=0;locCnt<=msCount;locCnt++)
{
__delay_ms(1); // This XC8 function creates a delay of 1 millisecond
} // looping this msCount times will give that much milliseconds delay
}
}
}
EXPLANATIONS
This program is to under stand the working of light-dependent resistor (LDR).its aphotoresistor (or light-dependent resistor,LDR, or photocell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. The output from ldr is connected to an adcpin.since 16f877a got a 10 bit adc we get a value from 0 to 1023 this value is divided by 2 to convert it to 0-5.11V.This volt is shown in the lcd and seven segment. We can see the change in the voltage by blocking the light to the LDR
No comments:
Post a Comment