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.

[PIC] Convert ADC reading to DECIMAL numbers

Status
Not open for further replies.

kasamiko

Full Member level 3
Joined
May 23, 2004
Messages
154
Helped
16
Reputation
32
Reaction score
18
Trophy points
1,298
Location
Philippines
Activity points
1,121
Hi,

I'm simulating this circuit of battery charger in Proteus and wondering how to convert the result into decimal numbers. Like 14.42 V instead of 14420 mV.


PIC Simulator IDE code:

Code:
'PROGRAM TO CHARGE LEAD ACID BATTERIES WITH SOLAR PANEL
'USES PIC 16F877 AT 4 MHZ
'AUTHOR SANTHOSH JAYARAJAN

'NOTES:



'*********************************************
'RA2=SOLAR CELL VOLTAGE
'RA1=BATTERY CHARGE CURRENT IB

'RB0-RB7:LCD DATA LINES

'RC0=PWM OUTPUT FOR CHARGER

'RD1:LCD R/S
'RD2:LCD R/W
'RD3:LCD-E
'RD4:SPARE
'RD6:SPARE

'VARIABLE DECLARATION FOR LCD MODULE
Define LCD_BITS = 8
Define LCD_DREG = PORTB
Define LCD_DBIT = 0
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 1
Define LCD_EREG = PORTD
Define LCD_EBIT = 3
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 2
Define ADC_SAMPLEUS = 100
Define LCD_LINES = 2
Define LCD_CHARS = 16

'for simulation only
Define LCD_COMMANDUS = 5000  'delay after LCDCMDOUT, default value is 5000
Define LCD_DATAUS = 100  'delay after LCDOUT, default value is 100
Define LCD_INITMS = 20

'SETTING THE TRISA,TRISB AND ADCON1 REGISTERS
'ALL A PINS ARE INPUTS AND 3 E PORTS AS INPUTS

TRISA = %00110111
ADCON1 = %00000000
TRISE = %00000111
ADCON0 = 0x80
TRISD = %11000000
TRISB = %00000000
INTCON = %10100000
OPTION_REG = %00000111

'VARIABLE DECLARTION
'FOR BATTERY VOLTAGE MEASUREMENT
Dim batt_curr As Word
Dim batt_curr_count As Byte
Symbol chg_on = PORTA.3
Dim boost_on As Bit
Dim trickle_on As Bit
Dim was_boost As Bit
Dim was_trickle As Bit
Dim trickle_time_count As Word

'FOR BATTERY and SOLAR VOLTAGE
Dim batt_volt_raw As Word
Dim batt_volt As Word
Dim solar_volt_raw As Word
Dim solar_volt As Word

'FOR BATTERY CURRENT
Dim batt_curr_raw As Word
Dim batt_cur As Word
Dim batt_curr_data(5) As Word
Dim set_curr As Word

'SOLAR CELL MINIMUM  VALUES
Dim batt_min_volt As Word
Dim solar_min_volt As Word

'POWER AND ENERGY CALCULATION
Dim power As Word
Dim energy As Word
Dim energy_aux As Word
Dim power_act As Word

'FOR INTERRUPT
Dim int_count As Byte
Dim en_count As Word

'MAIN LOGO AND DISPLAY INITIALISE
Lcdinit 0
WaitMs 10
Lcdcmdout LcdClear
Lcdcmdout LcdLine1Home
Lcdout "Solar Charge 1.0"
Lcdcmdout LcdLine2Home
Lcdout "SOLAR_CHARGE.hex"
WaitMs 2000
' FOR ENERGY CALCULATIONS
Dim en_unit As Byte
Dim en_ten As Byte
Dim en_hun As Byte
Dim en_thou As Byte
Dim en_total As Word
Dim en_unitc As Byte
Dim en_tenc As Byte
Dim en_hunc As Byte
Dim en_thouc As Byte
Dim en_totalc As Word

'START THE MAIN PROGRAM
solar_min_volt = 12600
batt_min_volt = 12100
energy = 0

'CLEAR SCREEN
Lcdcmdout LcdClear
Lcdcmdout LcdHome
Lcdcmdout LcdLine1Home

'START MAIN PROGRAM
start:
Disable
Read 0, en_thou
Read 1, en_hun
Read 2, en_ten
Read 3, en_unit
Enable
en_total = en_thou * 1000 + en_hun * 100 + en_ten * 10 + en_unit

Gosub chk_solar_volt
Gosub chk_batt_volt

	If solar_volt > solar_min_volt Then
trickle_time_count = trickle_time_count + 1
If batt_volt < batt_min_volt Then
boost_on = 1
trickle_on = 0
Endif

If batt_volt >= batt_min_volt Then
boost_on = 0
trickle_on = 1
Endif

If boost_on = 1 Then
chg_on = 1
Endif

If trickle_on = 1 Then
	If trickle_time_count <= 2 Then
	chg_on = 1
	Endif

	If trickle_time_count > 2 Then
	chg_on = 0
	Endif
	
	If trickle_time_count >= 4 Then
	trickle_time_count = 0
	Endif

Endif
WaitMs 1000
Gosub chk_batt_curr
Lcdcmdout LcdClear
Lcdcmdout LcdHome
Lcdcmdout LcdLine1Clear
Lcdcmdout LcdLine1Home
Lcdout "B.Volts:", #batt_volt, " mV"

Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home
Lcdout "C.Curr:", #batt_curr, " mA"

WaitMs 2000

Lcdcmdout LcdLine1Clear
Lcdcmdout LcdLine1Home
Lcdout "Energy(Wh)", #en_total,
Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home
Lcdout "Power: ", #power, " mW"

WaitMs 2000

Lcdcmdout LcdLine1Clear
Lcdcmdout LcdLine1Home
Lcdout "Solar V:", #solar_volt, " mV"
Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home
Lcdout "B.Volt: ", #batt_volt, " mV"

WaitMs 2000

Lcdcmdout LcdLine1Clear
Lcdcmdout LcdLine1Home
If boost_on = 1 Then
Lcdout "Boost Chg: ON"
Else
Lcdout "Boost Chg: OFF"
Endif

Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home

If trickle_on = 1 Then
Lcdout "Trickle Chg: ON"
Else
Lcdout "Trickle Chg: OFF"
Endif

WaitMs 1000

	Else
Lcdcmdout LcdLine1Clear
Lcdcmdout LcdLine1Home
Lcdout "Low Solar Volts"
Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home
Lcdout "Waiting..",

WaitMs 2000
Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home
Lcdout "B.Volt: ", #batt_volt, " mV"

WaitMs 2000
Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home
Lcdout "Solar V:", #solar_volt, " mV"
WaitMs 2000

	Endif
Goto start

End
'CHECK SOLAR CELL VOLTAGE
'return batt_volt in mV
chk_solar_volt:
If boost_on = 1 Then
was_boost = 1
Else
was_boost = 0
Endif

If trickle_on = 1 Then
was_trickle = 1
Else
was_trickle = 0
Endif
chg_on = 0

WaitMs 50
Adcin 2, solar_volt_raw
solar_volt = solar_volt_raw * 20

If was_boost = 1 Then
boost_on = 1
Else
boost_on = 0
Endif

If was_trickle = 1 Then
trickle_on = 1
Else
trickle_on = 0
Endif
was_boost = 0
was_trickle = 0

Return
'CHECK BATTERY CURRENT
'return batt_curr in ma
chk_batt_curr:
For batt_curr_count = 0 To 4
Adcin 1, batt_curr_data(batt_curr_count)
WaitMs 10
Next batt_curr_count
batt_curr_raw = (batt_curr_data(0) + batt_curr_data(1) + batt_curr_data(2) + batt_curr_data(3) + batt_curr_data(4)) / 5
batt_curr = batt_curr_raw
Return
'CHECK BATTERY VOLTAGE
'return batt_volt in mV
chk_batt_volt:
If boost_on = 1 Then
was_boost = 1

Else
was_boost = 0
Endif

If trickle_on = 1 Then
was_trickle = 1
Else
was_trickle = 0
Endif

chg_on = 0
WaitMs 50
Adcin 0, batt_volt_raw
batt_volt = batt_volt_raw * 20

If was_boost = 1 Then
boost_on = 1
Else
boost_on = 0
Endif

If was_trickle = 1 Then
trickle_on = 1
Else
trickle_on = 0
Endif
was_boost = 0
was_trickle = 0

Return

On Interrupt
Save System
en_count = en_count + 1
int_count = int_count + 1

'1 SEC INTERRUPT
If int_count = 15 Then
int_count = 0
power = (batt_volt / 1000) * (batt_curr)
energy_aux = energy_aux + power

'ENERGY IN WS
If energy_aux >= 1000 Then
energy = energy + 1
energy_aux = 0
Endif

'ENERGY IN WH
If energy >= 3600 Then
en_totalc = en_totalc + 1
energy = 0
Endif

If en_totalc >= 9999 Then
en_totalc = 0
Endif

Endif

'1/2 HR INTERRUPT
If en_count = 27000 Then
en_count = 0
en_unitc = en_totalc Mod 10
en_tenc = (en_totalc / 10) Mod 10
en_hunc = (en_totalc / 100) Mod 10
en_thouc = en_totalc / 1000
Write 0, en_thouc
Write 1, en_hunc
Write 2, en_tenc
Write 3, en_unitc
Endif

INTCON.T0IF = 0
Resume


 

One of the logic which i learnt from net are following .

14420 millivolt separate each digit by modular division .
remove last zero digit by dividing it by 10 .
then the result is 1442 milli volt

then separate each digit as following

4th digit = 1442 % 10 = 2.
3rd digit = 1442 /10 = 4
@nd Digit = (1442/100)%10 =4
1st digit = (1442/1000)%10 =1

Then display each digit

i made an project in which these logic are used ,but which is AVR https://circuits-collection.blogspot.in/2015/12/microcontroller-based-ammeter-and.html
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top