[SOLVED] pic18f242 problem with analog reading

Status
Not open for further replies.

tpscar22

Newbie level 3
Joined
Apr 5, 2011
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,303
we

dfhjjks snjksjkd kpokpkadsd kpokposd
sdsdkjnjijnsdoin onjaosjoisd sdpojmpoja
 
Last edited:

when you simulate what do you see..?
whatever value is shown on PORTB will remain constant since you are not using a continuous loop. so it will only show the value read on the first attempt..
 

when i put a value.say 0V all the leds light even when i put 8V or anythng beyong that
 

that it is what i am saying you should add a continuous while loop i.e while(1) in the main program...
try this....



hope it works then...
 

if you disconnect the voltage source then still the first condition remains true which is
if(result < RED)
{
PORTB = 0b10000000;
}
and will continue to lite the led....
try adding a condition
if (result < 5)
PORTB = 0;
 

we

deddk kmlmsd kllkjklsd
sdon dsonion
 
Last edited:

still there is no change in your code in terms of conditions...
first you should check that what value is ADC reading..?
try to put it on serial port and view it on virtual terminal or put it on another port and then view it only then you can know which condition is going to be true...
tell me what value is your ADC reading or if it is not reading...
 

It might be something to do with signed and unsigned arithmetic?
The following code should work.

Code:
#define RED 102U
#define BLUE 204U
#define MIN 0U
#define MAX 256U 

void main(void)
  {
  unsigned int result;

  // configure A/D convertor
  OpenADC( ADC_FOSC_RC & ADC_LEFT_JUST & ADC_8ANA_0REF,
  ADC_CH0 & ADC_INT_OFF );

  while(1)
    {
    Delay10TCYx( 5 ); // Delay

    ConvertADC(); // Start conversion

    while(BusyADC()); // Wait for completion

    result = ReadADC(); // Read result

   // CloseADC(); // Disable A/D converter

    TRISB = 0;

    if(result < RED)
      {
      PORTB = 0b10000000;
      }
    else if(result < BLUE)
      {
      PORTB = 0b01000000;
      }
    else
      {
      PORTB = 0b00100000;
      }
    }
  }
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…