Wednesday, 4 September 2013

QL200 example 1 photoresistor circuit

#include <16F877A.h>
#device adc=8
#FUSES NOWDT      //No Watch Dog Timer
#FUSES XT         //Highspeed Osc > 4mhz
#FUSES PUT        //Power Up Timer
#FUSES NOPROTECT  //Code not protected from reading
#FUSES NODEBUG    //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP      //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD      //No EE protection
#use delay(clock=4M) //crystal oscillator at 20000000 hertz
//#use rs232(baud=9600, xmit=PIN_C6, invert)  //serial port output pin & baud rate // use this code if you are using serial port

//run photoresistor signal wire to pin AN0
//connect LED/resistor to pin RB7

void main(){
   int16 photo=0; //16 bit integer, safer than using int because
                  //int is only 8 bit which might lead to overflow problems for add, multiply
                 
   setup_adc(ADC_CLOCK_INTERNAL); //configure analog to digiral converter
   setup_adc_ports(ALL_ANALOG);   //set pins AN0-AN7 to analog (can read values from 0-255 instead of just 0,1)

   while(true){ //loop forever
      set_adc_channel(0);//set the pic to read from AN0
      delay_us(20);//delay 20 microseconds to allow PIC to switch to analog channel 0
      photo=read_adc(); //read input from pin AN0: 0<=photo<=255
     
       //turn on LED when input > 127, else turn off LED
       //Put finger over photoresistor & take it off to see LED turn on/off
       //127 may not be the actual value that separates light from dark, so try different values
      if(photo > 127){
         output_high(PIN_B7);
         output_low(PIN_C7);
      }
      else{
         output_low(PIN_B7);
         output_high(PIN_C7);
      }
   }
}

No comments:

Post a Comment