go_away

Author Topic: the code doesnt work for photoresistor pic16f877a  (Read 1316 times)

0 Members and 1 Guest are viewing this topic.

Offline mahmoshaTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
the code doesnt work for photoresistor pic16f877a
« on: September 23, 2011, 02:45:25 PM »
hi all i made a code for photo resistor that takes the cds reading and saves it to integer and makes a led lighten up
and then if the cds reading changed it makes the led flash
but unfortunately it doesnt work
the led is alwayes lighten up so the photoresistor doesnt have any new reading
and btw
i am connecting the cds without a resis it from 5+ to the pic directly
please help?
here is the code
Code: [Select]
void main (){
int firstvalue;
int seceondvalue;
int x;
    trisa=1;
    trisd=0;
    portd=0;
    adcon1=0x00;
    firstvalue=adc_read(2) ;
    while (1)
    if (firstvalue==adc_read (2))
    {portd.f1=1;
    }else{

       for  (x=0;x<3;x++){
        portd.f1=1;
        delay_ms(200);portd.f1=0;} }
}
« Last Edit: September 23, 2011, 02:52:35 PM by mahmosha »

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: the code doesnt work for photoresistor pic16f877a
« Reply #1 on: September 23, 2011, 08:57:59 PM »
There seems to be code missing.
What are the config bits?
Which C compiler are you using?
What does the adc_read(2) function do?

What are the values for adcon0 register?
What is the acquisition delay time?

Did you run this code in the MPLAB Simulator?

How you written code that just blinks the LED?

????
« Last Edit: September 24, 2011, 08:51:33 AM by waltr »

Offline mahmoshaTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: the code doesnt work for photoresistor pic16f877a
« Reply #2 on: September 24, 2011, 02:51:10 AM »
well i am using mikroc and it also doesnt work on the simulator but the code seems okay

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: the code doesnt work for photoresistor pic16f877a
« Reply #3 on: September 24, 2011, 08:23:35 AM »
If the code doesn't run in the simulator then it is not ok.

Without seeing all of the code I can not help.

Offline mahmoshaTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: the code doesnt work for photoresistor pic16f877a
« Reply #4 on: September 24, 2011, 10:06:31 AM »
this is the hokle code
adc_read(2) should make the pic read analog input 2 AN2
and i think adcon1=0x00 make all the analog inputs
what else do u need to know please i need help :/

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: the code doesnt work for photoresistor pic16f877a
« Reply #5 on: September 24, 2011, 10:27:41 AM »
I still have the same questions I asked above:

There seems to be code missing.
What are the config bits?
What does the adc_read(2) function do?
What are the values for adcon0 register?
What is the acquisition delay time?

Have you written code that just blinks the LED?
This ensures that the PIC is wired correctly and actually runs. If this doesn't work then there could be hardware issues that code will not fix.

Quote
adc_read(2) should make the pic read analog input 2 AN2
"Should" but does it? What does the code look like.

One point in your code:
Code: [Select]
if (firstvalue==adc_read (2))May never become true since ADC values can jump around a bit due to noise.
It would be better to test on a 'greater than' or a 'less than' this way it is ensured that the test does became true.

Offline mahmoshaTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: the code doesnt work for photoresistor pic16f877a
« Reply #6 on: September 24, 2011, 01:42:25 PM »
okay mr waltr can u please write a code example so i can see and learn from? :/

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: the code doesnt work for photoresistor pic16f877a
« Reply #7 on: September 24, 2011, 02:15:41 PM »
Check the code in these tutorials (has both assembler and C lessons):
http://www.gooligum.com.au/tutorials.html

Lesson 8 of the Midrange PIC C covers the use and programming of the ADC.
Lesson 1 covers blinking an LED. Even through the lessons use a PIC16F629 the code is almost exactly the same for a PIC16F877.

I currently don't have any 'simple' code to demo the ADC on a PIC16F877 but there are many examples available. The Data Sheet even has a pseudo-code example.

Offline mahmoshaTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: the code doesnt work for photoresistor pic16f877a
« Reply #8 on: September 25, 2011, 04:03:47 AM »
okay however i own u an explaination for mikroc
at the start of programing there is a combobox where u chose ur pic and then u dont need to deifine anything like normal c programming every future is there every pin is loaded automaticlly
so actually portb.f0=1;
means RB0 = output 5 volts
i hope i helped u like u helped me
:)

 


Get Your Ad Here