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.

Voltage monitoring with PIC18F458

Status
Not open for further replies.
A

ankushl

Guest
Hi...I am trying to make a circuit using PIC18F458...If the voltage falls below 4V, the LED should glow red, and if the voltage is above 4V the led should glow green...the problem is that the led always glows red. Two leds, one red and one green, are used. please help

vmon.jpg

#include<p18f458.h>
#include<delays.h>

#pragma config OSC=HS
#pragma config OSCS=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF
#pragma config LVP=OFF

//Program to monitor battery voltage

//Port and pin definitions
#define voltage PORTAbits.AN2
#define red PORTBbits.RB6
#define green PORTBbits.RB7

void mydelay(unsigned char);

void main(void)
{
unsigned int h_byte, l_byte, byte_temp, adc;
float volt;
TRISAbits.TRISA2 = 1;
ADCON0 = 0x91;
ADCON1 = 0xC0; //all adc channels input
TRISB = 0x00;
red = 0;
green = 0;
while(1)
{
mydelay(1);
ADCON0bits.GO = 1;
while (ADCON0bits.DONE==1);
l_byte=ADRESL;
h_byte=ADRESH;
h_byte=h_byte<<8;
byte_temp=l_byte|h_byte;
//mydelay(1);
adc=byte_temp;

volt = (float)adc/204.6;
if (volt<4)
red = 1;
else
green = 1;

TRISAbits.TRISA2=0;
TRISBbits.TRISB7=0;
TRISBbits.TRISB5=0;
}

}

void mydelay(unsigned char val)
{
for(;val>=1;val--)
{
Delay1KTCYx(50);
}
}
 

With the circuit given, the output voltage is 12V * 1.5/6.2 = 2.9V

2.9V < 4V

Thus, the red LED stays on.

You also need to use resistors between the LEDs and the PIC pins to limit current to the LEDs.
 
  • Like
Reactions: Technar

    Technar

    Points: 2
    Helpful Answer Positive Rating
Hi Tahmid...i got this circuit from the internet...how do i solve the problem of voltage monitoring using this circuit...my understanding is that the circuit i made makes use of the resistors in a voltage divider circuit...how did you arrive at the values 1.5 and 6.2?
 

you can adjust the value of resisters R2 and R3, use a pot to adjust the voltage to the pin AN2 use the equation vout ( AN2) =Vin * R3/(R2+R3)

hope this helps...
 

Hi Tahmid...i got this circuit from the internet...how do i solve the problem of voltage monitoring using this circuit...my understanding is that the circuit i made makes use of the resistors in a voltage divider circuit...how did you arrive at the values 1.5 and 6.2?

If you have a voltage divider as such:

8428248900_1373304328.png


the formula for the output is:

5055652500_1373304409.png


In your circuit the resistances are 1.5k and 4.7k; 1.5k is the resistance across which you are taking the output voltage and 6.2k is the total resistance (=1.5k + 4.7k).

To observe the variable effect, use a pot instead of the voltage divider set up so that you can obtain a variable output voltage. You may replace both resistors with one pot or just the 1.5k resistor with a, for example, 10k pot.

Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top