Home Code PIC16F877 and LDR warning example

PIC16F877 and LDR warning example

by shedboy71

In this example we will connect an LDR again but this time when the ADC value goes below a certain number we will switch an LED on, in real life this could be an alarm or a night light.

The LCD is more for debug purposes and could be removed

Schematic

 

PIC16F877 LDR and LED

PIC16F877 LDR and LED

Code

The code was witten in mikroC pro for PIC, its fairly simple when the value falls below 100 we switch on the LED connected to PORT C 0

 

 

[codesyntax lang=”c”]

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;
unsigned int adcvalue,value,temp_res;
unsigned char car,x,y;
char *voltage = "00.00";
long temp;

// Routine to show the value of the ADC_read
void ShowADC(int x, int y, unsigned int adcvalue)
{
car = adcvalue / 1000;
LCD_Chr(x,y,48+car);
adcvalue=adcvalue-1000*car;
car = (adcvalue / 100);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-100*car;
car = (adcvalue / 10);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-10*car;
car = adcvalue;
LCD_Chr_CP(48+car);
delay_ms(30);
}

void main()
{
TRISA = 0xFF; // PORTA is input
TRISB = 0;
TRISC = 0;
PORTB = 0;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 1, " ADC :");

do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
adcvalue = temp_res;
ShowADC (1,7,adcvalue);

if(adcvalue < 100)
{
PORTC = 0x01;
}
else
{
PORTC = 0x00;
}
Delay_ms(200);
} while(1);

} // end main

[/codesyntax]

 

 

Links

50 pcs Light Dependent Resistors

250pcs 3MM LED Assortment Kit

QL200 PIC16F877A-I/P PIC16F877 PIC 8-bit Development Board

Share

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More