Hi homayon50
First the input range for a 16F877A is 0 to 5 Volts maximum.
Therefore you will need some way of dropping or level shifting from 0 - 8 (allow for a new or fully charged battery) to 0 to 5V.
Perhaps a resister divider network.
E.g with two resistors the same value say 10K connected in series between Battery in and GND then the voltage across the junction of resistors and GND = V/2
so if the battery voltage is 7.5V, then If the ADC is connected to the resistors junction, Max ADC = 7.5/2 = 3.75
If you want a better resolution then you can use say 8/5 = 1.6 as a ratio for the potential divider resistor network, so that will enable you to calculate correct resistances.
When calculating correct value don't go too high for resistor that's connected between ADC i/p and GND otherwise ADC value will be wondering.
With the value connected between battery and ADC don't go too low otherwise damage could occur to microcontroller if resistor connected between ACD i/p and GND becomes open circuit.
Then next you can produce a table of actual battery voltage verses number of illuminated LED's
An ADC component will then be required to convert measured voltage to a value.
If ReadAsByte is used then value will be between 0 and 255. Resolution is 5/256 = 19.5mV
If ReadAsInt is used then value will be between 0 and 1023. Resolution is 5/1024 = 4.88mV
Next determine the maximum value that will be measured by ADC e.g 0 to 200 or 0 to 800 as an example.
If using 0 to 200 then finally you will need to use decision icons so the value can be converted into number of LEDs that should be lit.
e.g If ADC >= 50 & ADC <20 then Value = 7
The 7 comes into the equation form the following:
If you connect all the anodes via a limiting resistor to each 8 pin of 8bit port e.g portD (connect all the cathodes to GND)
With the port there is 8 bits so thats 0 to 7
The number to add is derived from 2 to power of bits.
Code: Select all
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
....
2^7 = 128
for 1st LED's you require the value to be 1
the 1st and 2nd LED's you require the value to be 1 + 2 =3
the 1st 2nd and 3rd LED's you require the value to be 1 + 2 + 4 =7
You will need to limit the current though each LED to about 5 to 10mA no more, since you must not exceed the maximum current rating of microcontroller or LED's.
To learn more about Flowcode you can visit the learning center here:
http://www.matrixmultimedia.com/lc_index.php?p=24
Where you will find loads of useful information including a beginners course and other online courses.
Hope this is helpful.
If you try and create a flowchart and get stuck, or you would like us to have a look at what you have done so far then just post your flowchart and I will take a look at it for you.
Martin