PDA

View Full Version : symmetric power supply pic 16f877a



josepic
- 16th July 2022, 13:45
symmetric power supply pic 16f877a
best regard
I come to you for help to improve my programming code for a symmetrical digital power supply.
well the code one part is in Spanish and the other is in English.

In the simulation with Proteus negative voltage and positive voltage are displayed, but the voltages flicker and do not complain fixed, and I want these values to remain fixed.

I also want to change pic 16f877a for pic 16f886
I have also seen on the microchip page that the pic 16f877a will no longer be manufactured, in the electronic stores I could not find it, they are out of stock.
I enclose the code, and link of the video and you will see the voltage flashing.
I hope and help me improve my code, thanks

Joseph


include "fp2032.bas" ' Include file for 14-bit core with RAM at $20 (32-bit)

DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_ SAMPLEUS 50

'************************************************* *******************
' Definición de puertos y variables
'************************************************* *******************
LED VAR PORTB.6 'Define el led en el pin 6 del puerto B
TXD VAR PORTC.6 'Define el pin de transmision serial en el pin 6 del puerto C
B1 VAR WORD
B2 VAR WORD
VALOR1 VAR BYTE
VALOR2 VAR BYTE
fpplaces var byte ' Used to define number of decimal places in fpdisplay,fpdisplayr
ahold var word ' Required by fpdisplay
bhold var word ' Required by fpdisplay

'************************************************* *******************
' Inicio del programa principal
'************************************************* *******************
INICIO:
TRISC.6 = 0 'Configura puerto RC6 como salida
TRISC.7 = 1 'Configura puerto RC7 como entrada
TRISA = 255 'Configura puerto A como entrada
ADCON1 = %11000010 'Justifica el resultado hacia la izquierda
TRISB.6 = 0 'Configura puerto RB6 como salida

LCDOUT $FE,2," FUENTE SIMETRICA "
LCDOUT $FE,$C0," JOSE LUIS COMAS LL "
LCDOUT $FE,$94,"VOLT.NEGATIVO:-"
LCDOUT $FE,$D4,"VOLT.POSITIVO:+"
COMENZAR:
ADCIN 1, B1 'Lee el voltaje negativo en el canal 1 del ADC
ADCIN 2, B2 'Lee el voltaje positivo en el canal 2 del ADC

'Envia los valores del ADC a la terminal
SEROUT2 TXD,188,["$", "$", DEC4 B1,"$", "$",DEC4 B2,10,13]

aint = B1 'Transfiere el valor del ADC del voltaje negativo
gosub itofa 'Conviertelo a punto flotante
bint = 2000 'Carga el valor 2000 (5*400)
gosub itofb 'Conviertelo en punto flotante
gosub fpmul 'ADCres * 2000
bint = 52224 'Carga el valor de la cuenta maxima del ADC * 51
gosub itofb 'Conviertelo en punto flotante
GOSUB fpdiv '(ADCres * 2000)/52224
LCDOUT $FE,$A3," " 'Limpia el espacio donde se coloca el voltaje negativo
LCDOUT $FE,$A3
fpplaces = 2 : Gosub fpdisplayr ' Call display routine

aint = B2 'Transfiere el valor del ADC del voltaje positivo
gosub itofa 'Conviertelo a punto flotante
bint = 35 'Carga el valor 35 (5*7)
gosub itofb 'Conviertelo en punto flotante
gosub fpmul 'ADCres * 35
bint = 1024 'Carga el valor de la cuenta maxima del ADC
gosub itofb 'Conviertelo en punto flotante
GOSUB fpdiv '(ADCres * 35)/1024
LCDOUT $FE,$E3," " 'Limpia el espacio donde se coloca el voltaje positivo
LCDOUT $FE,$E3
fpplaces = 2 : Gosub fpdisplayr ' Call display routine
HIGH LED
GOTO COMENZAR

' The fpdisplayr routine checks for number of places, then adds the appropriate
' value to achieve rounding. To save RAM space, the values are hard coded
' in floating point format.

fpdisplayr: If fpplaces=0 Then ' Set floating point barg to 0.5
bexp = $7E
bargb0 = $00
bargb1 = $00
bargb2 = $01
Endif
If fpplaces=1 Then ' Set floating point barg to 0.05
bexp = $7A
bargb0 = $4C
bargb1 = $CC
bargb2 = $CD
Endif
If fpplaces=2 Then ' Set floating point barg to 0.005
bexp = $77
bargb0 = $23
bargb1 = $D7
bargb2 = $0B
Endif
If fpplaces=3 Then ' Set floating point barg to 0.0005
bexp = $74
bargb0 = $03
bargb1 = $12
bargb2 = $6F
Endif
If fpplaces=4 Then ' Set floating point barg to 0.00005
bexp = $70
bargb0 = $51
bargb1 = $B7
bargb2 = $17
Endif

Gosub fpadd ' add barg to aarg



' The fpdisplay routine outputs the signed value of aarg in decimal floating point format. It
' can display a positive value of 65535, and decimals to 4 places. The number of decimal
' places should be stored in fpplaces before calling the routine. The routine reads the
' floating point value of aarg. This value should NOT be converted to an integer before
' calling fpdisplay. The integer conversion will be perfomed as part of this routine, and
' aint will be returned to the calling program just as from the itofa routine.

fpdisplay: bexp = aexp ' Store the FP value of aarg to the barg variables
bargb0 = aargb0
bargb1 = aargb1
bargb2 = aargb2

Gosub ftoia ' Convert aarg to integer
ahold = aint ' Save this value for the final display

Gosub itofa ' Convert integer back to float

Swap aexp,bexp ' Swap the FP values of aarg and barg before subtraction
Swap aargb0,bargb0
Swap aargb1,bargb1
Swap aargb2,bargb2

Gosub fpsub ' Subtract the integer portion from the full number


bint = 10 ' Make bint = 10 E fpplaces
If fpplaces=2 Then
bint = 100
Endif
If fpplaces=3 Then
bint = 1000
Endif
If fpplaces=4 Then
bint = 10000
Endif

bhold = bint ' Save the integer value of bint for zeros loop

Gosub itofb ' Convert bint to integer prior to FP multiply
Gosub fpmul ' Multiply the decimal portion x 10 E fpplaces

Gosub ftoia ' Convert result to aint integer


Lcdout dec ahold ' Display integer portion

If fpplaces > 0 Then
Lcdout "." ' Display decimal point

zeros: bhold = bhold / 10 ' Set bhold to be next place to right
If (aint < bhold) AND (bhold > 1) Then ' Check for leading zero in decimal
Lcdout "0" ' Display leading zero
Goto zeros ' loop to check for another zero
Endif

Lcdout dec aint ' Display the rest of the decimal portion

Endif

aint = ahold ' Restore the original value of aint

Return


https://drive.google.com/file/d/19zVIgpcGwliJ5BENU3GAp1xTRELDTNa4/view
link of video

Acetronics2
- 16th July 2022, 21:25
Hi, Josepic

you should avoid floating point calculations ... it consumes much much time, and your flickering problems probably only come because of that ...

remember the ADC is only 10 bits, so, no need to look for more than .1% to be displayed !

Alain

josepic
- 16th July 2022, 22:13
can you please help me by correcting the friend code?:frown:

richard
- 17th July 2022, 04:39
its just like deja vu all over again
https://support.melabs.com/forum/picbasic-pro-compiler-3-0-and-later/pbp3-picbasic-pro-3-0/7303-symmetric-source-code-pic-16f877a
4 years later still the same scant detail

josepic
- 17th July 2022, 11:40
Since nobody wanted to help me in this forum, I wanted to try again in this forum. is there a problem? if you are not going to help, do not criticize

richard
- 17th July 2022, 12:23
is there a problem?


i doubt many would risk unvetted content from google drive, youtube would be safer.
there is no config settings in your code.
what type of osc and speed.
what have you tried to port code to 16f886 and what happened.
does it work on a 16f877a.
has it ever worked or is it just a proteus simulation, proteus does not really work for some real time things.
there is no schematic.
why use fp ,can you really expect accuracy to 2 decimals.
what does symmetric power supply pic 16f877a mean ,the code is just a display
but the voltages flicker and do not complain fixed, what does this mean , are the voltages out of control or is it the the readings
two dp accuracy from single asynchronous adc results is not plausible afaics esp without a stable voltage reference for adc

josepic
- 17th July 2022, 12:55
In the google drive link I uploaded a video of the proteus simulation of the code with the negative and positive voltages flashing. the code works with pic 16f877a, microchip announced that it will stop making pic 16f877a and i want to migrate the code to work with pic 16f886. In addition, anyone can make a symmetrical source, in my case I implement a digital indicator with a pic and use an operational amplifier in inverter mode to input negative voltage to the pic, use a voltage divider for the positive input and not damage the adc of the pic. If it won't help, don't comment.

Ioannis
- 17th July 2022, 20:51
Get rid of Floating point. All will be much more smoother.

Either you like it or not, Richard has nailed the problems. If I were you, I would read carefully his suggestions.

Ioannis

josepic
- 17th July 2022, 21:27
If I eliminated the floating point, the positive and negative voltages would NOT be the same as those recorded by the voltmeters in the Proteus simulation. attached proteus simulation image I also attach the old code when I started to make the code with the first code the negative and positive voltages are different. so i expanded the code and now the negative and positive voltages are flashing, but the negative and positive voltages that register on lcd 20 x4 are the same as the voltmeters in the proteus simulation. if it won't help don't comment













DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_ SAMPLEUS 50

TRISC.6 = 0
TRISC.7 = 1
TRISA = 255
ADCON1 = %11000010
TRISB.6 = %0

LED VAR PORTB.6
B1 VAR WORD
B2 VAR WORD
VALOR1 VAR BYTE
VALOR2 VAR BYTE

STAR:
ADCIN 1, B1
ADCIN 2, B2
VALOR1=B1/9
VALOR2=B2/12

SEROUT2 PORTC.6,188,["$", "$", DEC4 B1,"$", "$",DEC4 B2,10,13]
LCDOUT $FE,2," SYMMETRIC FONT "
LCDOUT $FE,$C4," JOSE LUIS "
LCDOUT $FE,$90," VOLTAGE NEGATIVE:-",DEC2 VALOR1
LCDOUT $FE,$D0," VOLTAGE POSITIVE:+",DEC2 VALOR2
HIGH LED
GOTO STAR

richard
- 18th July 2022, 01:30
10 bit adc @5v <=> .00488 volts per count
point A2 in sch = @1.76 v ie. 360 counts
360/12=30
this is the displayed result. what were you expecting


there is no visible relationship between A2 voltage and any other shown point in the sch.
how A1 voltage is derived is unknowable
pic vcc is a guess

richard
- 18th July 2022, 02:16
just found this, no wonder i felt i had seen it all before
http://www.picbasic.co.uk/forum/showthread.php?t=24035

still finding the same lack of detail

josepic
- 18th July 2022, 02:26
attached in .zip format the simulation in proteus. to visualize the simulation of the symmetric source

richard
- 18th July 2022, 03:18
zip files from a unvetted source , no thanks

a screen shot or a simple description of how A2 is derived and an explanation of how that voltage is to be displayed would help

its a simple thing.
you have two voltages A1 and A2 to be read by adc , that reading needs to scaled to display a value representing the supply outputs
A1 and A2 obviously must have a fixed linear relationships to those outputs to get meaningful results.

a 16f886 can do it just as well as a 16f877a can
if you want more than 1 dp resolution you need to employ some oversampling
to get a stable reading some low pass filtering is required , analog or digital or both

josepic
- 30th July 2022, 15:49
I have solved my problem of flickering of the negative and positive voltages, change in the source code the pic 16f877a and use the pic 16f886.
the only thing left for me is to buy pic 16f886 and design the pcb for the symmetrical source and present the project to the university.
I really received little help in this forum

Ioannis
- 30th July 2022, 19:59
Depends on how one is looking the case.

Richard did his best nailing your problem and suggesting a solution. If it is not what you expected, well, OK.

In any case this forum helped many and does have a whealth of info and solutions.

Sorry you feel that way.

Ioannis

josepic
- 30th July 2022, 20:14
The solution provided by Richard did not solve the problem for me, I had to correct the following in the code:

'++++++++++++++
'pv LCDOUT $FE,$A3," clean negative voltage
LCDOUT $FE,$A3
'++++++++++++++


'++++++++++++++
'pv LCDOUT $FE,$E3," " clean positive voltage
LCDOUT $FE,$E3
'++++++++++++++