PDA

View Full Version : Referring voltage with PIC 16F876



CLAUDY
- 19th December 2011, 20:12
Good evening everyone,

I make a battery tester for more accuracy I use a reference voltage of +5 V and +4.5 V on AN3 AN2.
AN0 is used to measure the voltage
AN1 is used to measure the current (in the test program it is useless)

Small simple test setup to test my program.
A PIC16F876,
AN0 = 4.8V measured by multimeter
AN1 = 2mV matter
AN2 = 4.5V
AN3 = 4.9V
AN2 and AN3 are connected to voltage references.
The goal is to have an accuracy of 10 bits between two voltage references.
Why I put ADCON1 = 141.
After launching the test program, I have the LCD "0" AN1, normal and "0" AN0??
Same test with ADCON1 = 132, so no voltage references and 10 bits between 0V and VDD, I have 992 on AN0
Arguably the peak operates without voltage references but if I do a "zoom" between 4V5 and 5V does not work.
For this test routine "coupe_Imax5821" is mandatory for my card but does not affect my problem.

Here the test program (I use PBP)

In advance thank you for your answers

Here is the code

' DEBUT POUR INITIALISATION DU LCD
'******************************************

DEFINE LCD_DREG PORTC 'LCD utilise les data du port C
DEFINE LCD_DBIT 4 'LCD data starting bit 0 or 4
'les data commencent de RC4 ,RC5,RC6,RC7
DEFINE LCD_RSREG PORTC 'LCD register select port
'le RS de LCD est sur le port C
DEFINE LCD_RSBIT 2 'LCD register select bit
'le bit du port utilisé pour RS de LCD est 2
DEFINE LCD_EREG PORTC 'LCD enable port
'le E de LCD est sur le port C
DEFINE LCD_EBIT 3 'LCD enable bit
'le bit du port utilisé pour LCD est 3
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
'le LCD fonctionne en 4 bit D4,D5,D6,D7
DEFINE LCD_LINES 2 'Number lines on LCD
'le nombre de ligne de LCD est 2
DEFINE LCD_COMMANDUS 2000 'Command delay time in us
DEFINE LCD_DATAUS 50 'Data delay time in us

'*** FIN DE L'INITIALISATION DU LCD ***

' DEFINITION DES VARIABLES POUR I2C
'*******************************************
DEFINE I2C_HOLD 1
DEFINE I2C_SCLOUT 1
define I2C_SDA PORTb.1
DEFINE I2C_SCL PORTb.0
Define I2C_SLOW 1 'At this clock speed this needed to be added to make it work.

sda var PORTb.1 'sda est sur le portb.1
scl var PORTb.0 'scl est sur le portb.0

'*** FIN DES DEFINITIONS DES VARIABLES POUR I2C ***


' DEBUT DES DEFINITION DES VARIABLES
'********************************************
u0 var word
temperature var word
i var word
j var word
k var word
i0 var word
ic var word
v0 var word
iu var word
bon_courant var word
diff_tension var word
aaff var word

' DEFINITION DES ENTREES POUR LES BP
'*********************************************

bp1 var portb.3 'le bp1 est reaccordé sur le port b3 bp +
bp2 var portb.5 'le bp2 est reaccordé sur le port b5 bp OK ( V)
bp3 var portb.2 'le bp3 est reaccordé sur le port b2 bp -

'*** FIN DES DEFINITIONS DES ENTREES DES BP ******




' DEFINITION DES SORTIES
'******************************************

buzzer var portb.4 'le buzzer est raccordé sur le portB4
lcdbl var porta.5 'le backlight du lcd est sur le portA3

'*** FIN DES DEFINITIONS DES SORTIES ***

' INITIALISATION DES ENTREE ANALOGIQUE DU PIC
'************************************************* ****

DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 1'3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds

'*** FIN INITIALISATION DES ENTREE ANALOGIQUE DU PIC ***


' DEFINITION DES ENTREES SORTIES DU PIC
'************************************************* **
ADCON1 = 141 ' PORTA is analog
trisa=223 'pour que RA0,RA2 et RA4 soient des entrées
trisb=47 'pour que RB3 , RB2 et RB5 soit des entrées
trisc=0 'pour que tout le port C soit des entrées

'*** FIN DES DEFINITION DES ENTREES SORTIES DU PIC ***



' INITIALISATION AVANT DEBUT DU PROGRAMME
'***********************************************
buzzer=0
lcdbl=1

'*** FIN INITIALISATION AVANT DEBUT DU PROGRAMME ***



'************************************************* **************
'* DEBUT DU PROGRAMME *
'************************************************* **************


debut:
bp1=1
bp2=1
bp3=1
goto test

bip: ' permet d'activer le buzzer une fois
trisb.4=0
buzzer=1
pause 250
buzzer=0

return


coupe_Imax5821: '***max5821 pour couper le courant de charge
i=$0F000'61440 pour max5821
i2cwrite sda,scl,$070,i 'mode etendu du max5821
j=$0C000'+49152+3
i2cwrite sda,scl,$070,j 'mode normal
return '***fin max5821


test:
gosub coupe_Imax5821
adcon1=141'132'200'136'132'136'141
trisa=223
ADCIN 1, i0 ' lecture de l'entrée analogique 1 du PIC et stocker la
' valeur du courant dans i0
ADCIN 0, u0 ' lecture de l'entrée analogique 0 du PIC et stocker la valeur
' de la tension avant test dans la variable u0



gosub bip
LCDOUT $fe, 1,"i0=",#i0," u0=",#u0
gosub coupe_Imax5821
stop
end

Darrel Taylor
- 19th December 2011, 20:44
Your Vref- violates the chips limits.

Look in the datasheet, table 15-12.
Param# A22 says that Vref- maximum = Vref+ - 2.0V.

So if Vref+ is 5.00V, Vref- can be no more than 3.0V.

CLAUDY
- 20th December 2011, 06:09
Hello,

Thank you for responding so quickly, I did not notice this detail.
I'll edit this and keep you informed of the result.
Again thank you and good day

Claudy