PDA

View Full Version : Problems with 16F684



fratello
- 11th November 2011, 06:50
I try to build one thermo-volt-meter for using in my car. I wrote the code, based on my (working) previous experiences with DS18B20 and the great example of Mr.Gioppy (SerVolt). But...the result are ...strange : despite Ubat = 16 Volts, on display the value is 0.02, alternating with 0.22 ?!?
Maybe a sharp mind will find my mistake(s). Thanks in advance !!!

;
; http://www.picbasic.co.uk/forum/showthread.php?t=7182&highlight=gioppy
; Volt-termometru cu 16F676
;
@ DEVICE pic16F684, intrc_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON
define osc 4


TRISA= %00010000
TRISC= %00110000
CMCON0 = %00000111 'Disable analog comparators.

DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time

VRCON = %00000000 ' Disable Comparator Voltage Reference
ANSEL = %00000100 ' Set pin (AN2) to analog input, the rest to digital
ADCON0 = %10001001 ' Set up A/D converter - Right Just., VDD REF., CH 2, ON
ADCON1 = %00110000 ' Set up A/D Converter clock source to internal RC

DEFINE LCD_DREG PORTC ' LCD on port B
DEFINE LCD_DBIT 0 ' Data bits B4..B7
DEFINE LCD_RSREG PORTA ' RS on PORTA
DEFINE LCD_RSBIT 1 ' RS on A1
DEFINE LCD_EREG PORTA ' E on PORTA
DEFINE LCD_EBIT 0 ' E on A0
DEFINE LCD_BITS 4 ' LCD 4 bit mode
DEFINE LCD_LINES 2 ' 2 line LCD display
Define LCD_COMMANDUS 2000 ' Command Delay (uS)
Define LCD_DATAUS 50 ' Data Delay (uS)


Temperature1 Var Word ' Temperature storage
Temperature2 Var Word ' Temperature storage
TempC1 Var Word
TempC2 Var Word
Float1 Var Word
Float2 Var Word
Sign1 Var Byte ' +/- sign
Sign2 Var Byte ' +/- sign
DQ1 Var PORTC.5 ' One-wire data pin
DQ2 Var PORTC.4 ' One-wire data pin

Twist1 Var Bit
Dummy1 Var Byte
V1 Var Word
Twist2 Var Bit
Dummy2 Var Byte
V2 Var Word


Vfs con 20480 '20,480v
K16 con 61441 'coefficiente filtro 15/16
Vbatt var word 'Vbatt filtrato
TmpW var word
Acc_Vbatt var word 'accumulatore Vbatt filtrato * 16

DS18B20_1_12bit CON %01111111 ' 750ms, 0.0625°C (default)
DS18B20_2_12bit CON %01111111 ' 750ms, 0.0625°C (default)


Twist1 = 0
Twist2 = 0

Pause 500
LCDOUT $FE, 1, $FE, $0C ' Clear display, cursor off
Pause 500


' Init Sensor 1
OWOUT DQ1, 1, [$CC, $4E, 0, 0, DS18B20_1_12bit]
OWOut DQ1, 1, [$CC, $48] ' Start temperature conversion
OWOut DQ1, 1, [$CC, $B8]
OWOut DQ1, 1, [$CC, $BE]
Pause 50
OWIn DQ1, 2, [temperature1.byte0, temperature1.byte1]
Pause 50
' Init Sensor 2
OWOUT DQ2, 1, [$CC, $4E, 0, 0, DS18B20_2_12bit]
OWOut DQ2, 1, [$CC, $48] ' Start temperature conversion
OWOut DQ2, 1, [$CC, $B8]
OWOut DQ2, 1, [$CC, $BE]
Pause 50
OWIn DQ2, 2, [temperature2.byte0, temperature2.byte1]
Pause 50

'================================================= ================================================== ===============

Main :

Part1:
' Start temp.conv.Sensor1
OWOut DQ1, 1, [$CC, $44]

WaitLoop1:
While not DQ1
Wend

OWOut DQ1, 1, [$CC, $BE]
OWIn DQ1, 2, [temperature1.byte0, temperature1.byte1]

If Temperature1.15 then
Temperature1= ~Temperature1 +1
Twist1 = 1
Endif

Dummy1 = 625 * Temperature1
TempC1 = DIV32 10
TempC1 = (Temperature1 & $7FF) >> 4
Float1 = ((Temperature1.Lowbyte & $0F ) * 25 )>>2
Temperature1 = TempC1*100 + Float1

If Twist1 then
V1= 10000 - Temperature1
Twist1 = 0
else
V1= 10000 + Temperature1
EndIf

If V1 >= 10000 then
Temperature1=V1-10000
else
Temperature1=10000-V1
EndIf
'================================================= ================================================== ================
Part2 :
' Start temp.conv.Sensor1
OWOut DQ2, 1, [$CC, $44]

WaitLoop2:
While not DQ2
Wend

OWOut DQ2, 1, [$CC, $BE]
OWIn DQ2, 2, [temperature2.byte0, temperature2.byte1]

If Temperature2.15 then
Temperature2= ~Temperature2 +1
Twist2 = 1
Endif

Dummy2 = 625 * Temperature2
TempC2 = DIV32 10
TempC2 = (Temperature2 & $7FF) >> 4
Float2 = ((Temperature2.Lowbyte & $0F ) * 25 )>>2
Temperature2 = TempC2*100 + Float2

If Twist2 then
V2= 10000 - Temperature2
Twist2 = 0
else
V2= 10000 + Temperature2
EndIf

If V2 >= 10000 then
Temperature2=V2-10000
else
Temperature2=10000-V2
EndIf
'================================================= ================================================== =======================
GoSub SelectSign
Gosub Get_Vbatt ' make a new measure
TmpW = Vbatt * Vfs ' AD * 20480
TmpW = Div32 1024 '(AD * 20480) / 1024
if (temperature1/100) =>10 then
LcdOut $FE, $80, "I:", Sign1, DEC (Temperature1 / 100), ".", DEC Temperature1 dig 1, 223
else
LcdOut $FE, $80, "I:", Sign1, $14, DEC (Temperature1 / 100), ".", DEC Temperature1 dig 1, 223
endif
if (temperature2/100) =>10 then
LcdOut $FE, $c0, "O:", Sign2, DEC (Temperature2 / 100), ".", DEC Temperature2 dig 1, 223, " ", "V:",dec TmpW dig 4,dec TmpW dig 3,",",dec TmpW dig 2

LcdOut $FE, $C0, "O:", Sign2, $14, DEC (Temperature2 / 100), ".", DEC Temperature2 dig 1, 223, " ", "V:", dec TmpW dig 4,dec TmpW dig 3,",",dec TmpW dig 2
endif

Goto Main
'================================================= ================================================== =======================
SelectSign:
If v1 = 10000 then
Sign1=" "
else
If v1 < 10000 then
Sign1="-"
else
Sign1="+"
EndIf
EndIf

If v2 = 10000 then
Sign2=" "
else
If v2 < 10000 then
Sign2="-"
else
Sign2="+"
EndIf
EndIf

Return
'================================================= ================================================== =======================
Get_Vbatt:
ADCON0.1 = 1 'start A/D
while ADCON0.1 = 1
wend
TmpW.byte1 = ADRESH 'get A/D value
TmpW.byte0 = ADRESL
Acc_Vbatt = Acc_Vbatt ** K16 + TmpW 'filtro 15/16
Vbatt = Acc_Vbatt / 16 ' / 16 valore normalizzato
Acc_Vbatt = TmpW * 16 'filter preload Vbatt * 16
return


6115

fratello
- 11th November 2011, 08:00
I found two errors :
- TRISA= 000100
- mising "else" in display "O:" temperature.
Now display works fine, but...maximum value of Ubat is 6,44.
?!?
...
LE : I did it ! RV2 changed to 5 K. The value on display it's correct now !6117

fratello
- 18th November 2011, 09:32
I need to re-configure the I/O pin assignement, so I re-wrote the code for new hardware. In ISIS I have this problem :
6124
It's just one simulation problem or software problem ? Need advice ! Thanks !

@ DEVICE pic16F684, intrc_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_OFF
define osc 4

TRISA= %00010000
TRISC= %10110000

CMCON0 = %00000111 'Disable analog comparators.

DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time

VRCON = %00000000 ' Disable Comparator Voltage Reference
ANSEL = %10000000 ' Set pin (AN7) to analog input, the rest to digital
ADCON0 = %10011101 ' Set up A/D converter - Right Just., VDD REF., CH 7, ON
ADCON1 = %00110000 ' Set up A/D Converter clock source to internal RC

;----[ Change these to match your LCD ]---------------------------------------
LCD_DB4 VAR PORTA.2
LCD_DB5 VAR PORTC.0
LCD_DB6 VAR PORTC.1
LCD_DB7 VAR PORTC.2
LCD_RS VAR PORTA.0
LCD_E VAR PORTA.1
LCD_Lines CON 2 ' # of Lines on LCD, 1 or 2 (Note: use 2 for 4 lines)
LCD_DATAUS CON 50 ' Data delay time in us
LCD_COMMANDUS CON 2500 ' Command delay time in us
INCLUDE "LCD_AnyPin.pbp"
Note : I modify pbppic14.lib like Mr.Darrel say in "LCD_Any_pin" and I have the properly .pbp in PBP directory.

luxornet
- 18th November 2011, 13:03
Hi!
I made my own tacho-volt-therometer with about the same schematic:
http://www.vandi.ro/electronics/turometru/turometru_auto.htm
This page is in Romanian language but maybe you and others found it useful.

fratello
- 18th November 2011, 14:12
Thank You for link ; but with (almost) similar hardware as Yours, my schematic work fine. BUT ... I intend to use another configuration for LCD pins (for optimised PCB), based on "LCD_Any_pin" ! This is my problem !
I check many variants for these :


LCD_Lines CON 2 ' changed on 1, of course !!!
LCD_DATAUS CON 50 ' Data delay time in us -- changed to 200
LCD_COMMANDUS CON 2500 ' Command delay time in us -- changed to 5000
without results...

fratello
- 18th November 2011, 16:11
Maybe the .dsn file help to understand...

Darrel Taylor
- 18th November 2011, 18:31
fratello,

Can you post your code.
I don't get the error with your .DSN file using this code...

define OSC 4
TRISA= %00010000
TRISC= %10110000
'
CMCON0 = %00000111 'Disable analog comparators.
DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time
VRCON = %00000000 ' Disable Comparator Voltage Reference
ANSEL = %10000000 ' Set pin (AN7) to analog input, the rest to digital
ADCON0 = %10011101 ' Set up A/D converter - Right Just., VDD REF., CH 7, ON
ADCON1 = %00110000 ' Set up A/D Converter clock source to internal RC
'
;----[ Change these to match your LCD ]---------------------------------------
LCD_DB4 VAR PORTA.2
LCD_DB5 VAR PORTC.0
LCD_DB6 VAR PORTC.1
LCD_DB7 VAR PORTC.2
LCD_RS VAR PORTA.0
LCD_E VAR PORTA.1
LCD_Lines CON 2 ' # of Lines on LCD, 1 or 2 (Note: use 2 for 4 lines)
LCD_DATAUS CON 50 ' Data delay time in us
LCD_COMMANDUS CON 2500 ' Command delay time in us
INCLUDE "LCD_AnyPin.pbp"
'
X VAR WORD
'
Main:
X = X + 1
LCDOUT $FE,$80,DEC X," "
GOTO Main

6129

fratello
- 18th November 2011, 20:36
Mr.Darrel ! THANK YOU for watching my post ! It's such a nice surprise !
The rest of code it's in post #1 ; just the parts concerning 16F684 and "LCD_AnyPin" it's changed...

Darrel Taylor
- 18th November 2011, 21:26
I don't get any errors with the code from post#1.

Well, at least not with the LCD.
I think you still have some problems in the code working with the DS18B20 though.

fratello
- 19th November 2011, 06:15
It's strange ! Using hardware and code from post #1 (and #2) the things are OK, in Proteus and "in vivo" ; changing just the configuration of LCD pins the schematic dont work anymore...I will try to build the schematic and see what happens :(

fratello
- 19th November 2011, 10:16
Simple schematic, simple code, WITHOUT DS18B20 ...Still no result !!! I am :culpability: ...

@ DEVICE pic16F684, intrc_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON
define osc 4

TRISA= %00000000
TRISC= %00001000

CMCON0 = %00000111 'Disable analog comparators.

DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time

VRCON = %00000000 ' Disable Comparator Voltage Reference
ANSEL = %10000000 ' Set pin (AN7) to analog input, the rest to digital
ADCON0 = %10011101 ' Set up A/D converter - Right Just., VDD REF., CH 7, ON
ADCON1 = %00110000 ' Set up A/D Converter clock source to internal RC

;----[ Change these to match your LCD ]---------------------------------------
LCD_DB4 VAR PORTA.2
LCD_DB5 VAR PORTC.0
LCD_DB6 VAR PORTC.1
LCD_DB7 VAR PORTC.2
LCD_RS VAR PORTA.0
LCD_E VAR PORTA.1
LCD_Lines CON 1 ' # of Lines on LCD, 1 or 2 (Note: use 2 for 4 lines)
LCD_DATAUS CON 50 ' Data delay time in us
LCD_COMMANDUS CON 2000 ' Command delay time in us

INCLUDE "LCD_AnyPin.pbp"

Vfs con 20480 '20,480v
K16 con 61441 'coefficiente filtro 15/16
Vbatt var word 'Vbatt filtrato
TmpW var word
Acc_Vbatt var word 'accumulatore Vbatt filtrato * 16


Pause 500
LCDOUT $FE, 1, $FE, $0C ' Clear display, cursor off
Pause 500

Main :
ADCON0.1 = 1 'start A/D
while ADCON0.1 = 1
wend
TmpW.byte1 = ADRESH 'get A/D value
TmpW.byte0 = ADRESL
Acc_Vbatt = Acc_Vbatt ** K16 + TmpW 'filtro 15/16
Vbatt = Acc_Vbatt / 16 ' / 16 valore normalizzato
Acc_Vbatt = TmpW * 16 'filter preload Vbatt * 16
TmpW = Vbatt * Vfs ' AD * 20480
TmpW = Div32 1024 '(AD * 20480) / 1024
LcdOut $FE, $c0, dec TmpW dig 4,dec TmpW dig 3,",",dec TmpW dig 2
Goto Main
return
6130

fratello
- 19th November 2011, 10:44
Same weird behavior using JUST code for thermometer ...

Main :
OWOut DQ1, 1, [$CC, $44]
OWOut DQ1, 1, [$CC, $BE]
OWIn DQ1, 2, [temperature1.byte0, temperature1.byte1]

If Temperature1.15 then
Temperature1= ~Temperature1 +1
Twist1 = 1
Endif

Dummy1 = 625 * Temperature1
TempC1 = DIV32 10
TempC1 = (Temperature1 & $7FF) >> 4
Float1 = ((Temperature1.Lowbyte & $0F ) * 25 )>>2
Temperature1 = TempC1*100 + Float1

If Twist1 then
V1= 10000 - Temperature1
Twist1 = 0
else
V1= 10000 + Temperature1
EndIf

If V1 >= 10000 then
Temperature1=V1-10000
else
Temperature1=10000-V1
EndIf
'================================================= ================================================== ================
GoSub SelectSign
if (temperature1/100) =>10 then
LcdOut $FE, $80, "I:", Sign1, DEC (Temperature1 / 100), ".", DEC Temperature1 dig 1, 223
else
LcdOut $FE, $80, "I:", Sign1, $14, DEC (Temperature1 / 100), ".", DEC Temperature1 dig 1, 223
endif

Goto Main
'================================================= ================================================== =======================
SelectSign:
If v1 = 10000 then
Sign1=" "
else
If v1 < 10000 then
Sign1="-"
else
Sign1="+"
EndIf
EndIf
Return

fratello
- 19th November 2011, 12:35
Sorry for pollute this great forum ... I think it's a PROTEUS error ; with the code posted by Mr.Darrel I have the same messages :( .6131

fratello
- 24th November 2011, 10:41
Bad news, for me :( ... The hardware DON'T WORK ! Just some stranges characters appears on display, not stable-fast changing ...cannot post pictures... I try even code of Mr.Darrel (posted above). I think it's a "incompatibility" between THIS PIC and "LCD_AnyPin" ?!?
What else I cand do (outside of using "clasic" configuration of LCD-PIC pins) ?

fratello
- 25th November 2011, 18:07
Please, can someone to compile this file for me ? Maybe it's something wrong with my PBP or WinXp or...I dont know....
Thanks in advance !

jellis00
- 26th November 2011, 22:38
Please, can someone to compile this file for me ? Maybe it's something wrong with my PBP or WinXp or...I dont know....
Thanks in advance !

I would be willing to compile your code with my PBP v. 2.6 compiler, and I tried to do so. But it won't compile without the include file "LCD_AnyPin.pbp" . If you can post the code to this include file I will give it a go for you.
Regards, jellis00

fratello
- 26th November 2011, 23:08
Thank You for help ! Mr.Darrel code can be found there : http://www.picbasic.co.uk/forum/content.php?r=250-Using-LCD-on-any-pins

jellis00
- 27th November 2011, 05:37
Sorry, fratello....I thought I could help you but I found out your code is setup for use only with the PM assembler which I can't use with my 64 bit machine. I can only use the MPASM assembler. Maybe someone else can help you test compile this.

fratello
- 27th November 2011, 07:55
Never mind...Thanks anyway for help !
Regards !

luxornet
- 27th November 2011, 09:01
Hi fratello!
I tried to compile but I got :
Error UNTEST~.ASM 440: [235] opcode expected insted of 'error'

fratello
- 27th November 2011, 10:01
Strange :( ...
I have no errors on compiling...just not work !

fratello
- 30th November 2011, 16:33
With code from post #1, I build this thermo-voltmeter for using in car.
Note : for "I(nside)" I dont have anymore DS18B20 sensor...

luxornet
- 1st December 2011, 07:51
Congratulation, Fratello!
Good work.

Acetronics2
- 1st December 2011, 13:54
Hi, Fratello

I just think ISIS doesn't appreciate you do not wait for DS1820 temp conversion complete to continue running your code ...
not a real problem ( Mikroelektronika lets it as-is in its onewire drivers ... booooo )

in easy words, ....Change



Main :
OWOut DQ1, 1, [$CC, $44]
OWOut DQ1, 1, [$CC, $BE]
OWIn DQ1, 2, [temperature1.byte0, temperature1.byte1]



for


'************************************************* ****************************
' Start temperature conversion
'************************************************* ****************************
main:
OWOut DQ1, 1, [$CC, $44 ]
'************************************************* ****************************
' Check for still busy converting
'************************************************* ****************************
waitloop:

OWIn DQ1, 4, [Busy] ' Check for still busy converting

Pause 95 ' Slow Down to actual Conv. time

If NOT Busy Then waitloop

'************************************************* ****************************
' Read the temperature
'************************************************* ****************************
OWOut DQ1, 1, [$CC, $BE ]
OWIn DQ1, 2, [temperature1.byte0, temperature1.byte1]


.
.
.



you will display the very last correct measurement and not get " 85°C" or junk at power up ...

Alain

fratello
- 1st December 2011, 16:50
Thank You so much for help !
I give up to use "LCD_AnyPin" ... since don't work (see post above, #14) ! I use "classic" hardware, with good results ! And I made one good PCB too...
Best regards !

luxornet
- 2nd December 2011, 07:58
You made that PCB in Proteus?

fratello
- 2nd December 2011, 09:21
Oh, no ! In SprintLayout !