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.

pic18f242 for lighting leds with varring voltage

Status
Not open for further replies.

spade22

Newbie level 3
Joined
Mar 8, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,306
hey guys am using a PIC18f242 for my design,am having some difficulty in analog to digital conversion amd configuring the port itself

here is what is really want to achieve

am using an FSR sensor which will varry the voltage from 0-5v with respect to the varrying force imposed on the sensor,so i will be using a single analog input to the PIC,i want to condition the varrying voltage(which is the input to the pic) produced by the sensor such that e.g from 0v-2v a green led wil be switched on then 2.1v-3v a blue led and 3.1v-5v a yellow led should be on,am new to programming pics,and would like t\your help in C language because thats what i understand better

any kind of help will be appreciated>>>thanx
 

u can light up the LED's connected in ports accoring to the values of ADC, Compare the ADC value with the refrance value and made the decision that which LED shd glow
 
If it's a 10-bit A/D and a 5 volt reference then the A/D resolution would be
5 / 1024 = 0.00488;

So 2 Volts would be an A/D output of 2 / 0.00488 = 409;
and 3 volts would be 3 / 0.00488 = 614;

So you could try

Code:
#define GREEN_ON 409
#define BLUE_ON  614

if(sensor_volts < GREEN_ON){
 green_led = ON;
 blue_led = OFF;
 yellow_led = OFF;
  }
else if(senor_volts < BLUE_ON){
  green_led = OFF;
  blue_led = ON;
  yellow_led = OFF;
  }
else{
  green_led = OFF;
  blue_led = OFF;
  yellow_led = ON;
  }
 
Btbass>>thanx for your insight mate,that part i think i understand,my big problem is how to configured the port which i will be using for the analog input,if you could include that in the code as well i thnk my problem would br solved,just select any analog input for understanding purposes

-----------------------------------------------------------------------------

sreepss>>>my problem is to implement that in coding starting from the analog port configuration
 

Code:
/*--- Initialise A/D convertor ---*/

void init_ad(void)
  {
  ADCON0 = 0x80U;   /* Conversion clock Fosc/32 */
  ADCON1 = 0x80U;   /* 10 bit Right justified result, all analog inputs, Vdd as ref */    
  }
 
/*--- Read A/D conversion ---*/

int read_ad(unsigned char channel)
  {
  int result = 0U;
  unsigned char acquisition_time = 3U;

  ADCON0 = 0x01U;             /* Turn on A/D */
  ADCON0 |= (channel << 3U);  /* Select channel */ 
        
  while(acquisition_time--){ /* Sample channel */
    ;
    }
    
  GODONE = 1U;    /* Start conversion */
  
  while(GODONE){  /* Wait for conversion end */
    ;
    }
    
  result = ADRESH;
  result <<= 8U;
  result += ADRESL;
  return result;
  }
 

Btbass>hey thanx man, it think from here i can make my way through,thanx man>>will post my final code as soon as am done with it
 

Btbass,dude i have a problem relating to this and i have pasted my code there,can u pliz have a look at it and possibly help me out

the article in titled 'pic18f242 problem with analog reading'
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top