Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[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....


#include <p18f242.h>
#include <adc.h>
#include <delays.h>

int result;

void main( void )
{
// 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;

#define RED 102
#define BLUE 204

if(result < RED)
{
PORTB = 0b10000000;
}

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

else if(result >BLUE )
{
PORTB = 0b00100000;
}
}

}

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top