CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Using a LED as Light sensor

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

Using a LED as Light sensor
PostPosted: Wed Oct 28, 2009 4:23 am     Reply with quote

Hello,

I am trying to use a LED behave as a LIGHT sensor.
Like this example. So reverse the LED and measure the voltage.
http://www.micro-examples.com/public/microex-navig/doc/096-led-light-sensor.html

When I do that (no PIC attached) I wil see a light related voltage across the LED.
But when I attach the PIC(12F615) I get a stable 200mV.

Did I did something wrong with the PIC settings????
Code:

unsigned int   red_duty, light;

void main()
{
   setup_adc_ports(sAN3|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,255,1);
   setup_ccp1(CCP_PWM);
   
   while(1){

   set_adc_channel(3);
   delay_us(10);
   light = read_adc();
   light = light * 1;
   set_pwm1_duty(light);
   }
}


Thanks in advance,

Jody
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

Got it working!
PostPosted: Wed Oct 28, 2009 8:49 am     Reply with quote

The Vref setting messed things up.............

Code:

void main()
{
   setup_adc_ports(sAN3|VSS_Vref);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,255,1);
   setup_comparator(NC_NC_NC_NC);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(512);;   

   while(1){
   delay_ms(255);
   delay_ms(255);
   light_old = 0;
   light = 0;
   set_adc_channel(3);
   delay_us(10);

      for(i = 0; i <1000; ++i)
      {
      light = read_adc();
      light_old = light_old+light;
      }
      light = (light_old / 1000);
      if(light < 15) light =15;
      set_pwm1_duty(light);;     
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group