PDA

View Full Version : 18F4420 and 1-Wire



Amoque
- 21st March 2015, 16:41
A lack of program space has forced me to upgrade from the 16F677A to the 18F4420. For the most part this has been fairly easy, but I have had an issue that makes me question the entirety of a very tenuous pyramid of knowledge… code that previously worked well with a DS18B20 1-Wire device now locks up –hanging, I believe at the “Waitloop”. The code is microEngineerings 1-Wire example code; other relevant bits also posted.
Anyone able/ willing to point out what I’ve missed?

This cut/ pasted from the relevant *.LST:

__CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _CCP2MX_PORTC_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L

Here my registers:


'-----REGISTERS SET------------------------------------------------------------
CMCON = %00000111
TRISA = %00011111
TRISB = %00111111
TRISC = %00010000
TRISD = %00000000
TRISE = %00000000
ADCON0.0 = 1
ADCON1 = %00001011
ADCON2 = %10110101

Here Variables:
'-----THERMOMETER

DQ VAR PORTC.6
TEMP VAR WORD
OLD_TEMP VAR WORD
COUNT_REMAIN VAR BYTE
COUNT_PER_C VAR BYTE
MIN_TEMP VAR WORD
MAX_TEMP VAR WORD

And last, the actual 1-Wire routine:

'-----THERMOMETER READ
GET_TEMP:
OWOUT DQ, 1, [$CC, $44]
WAITLOOP:
OWIN DQ, 4, [COUNT_REMAIN]
IF COUNT_REMAIN = 0 THEN WAITLOOP
OWOUT DQ, 1, [$CC, $BE]
OWIN DQ,0, [TEMP.LOWBYTE, TEMP.HIGHBYTE, SKIP 4, COUNT_REMAIN, COUNT_PER_C]
TEMP = (((TEMP>>1) * 100) - 25) + (((COUNT_PER_C - COUNT_REMAIN) * 100) / COUNT_PER_C)
TEMP = (TEMP */ 461) + 3200
RETURN

I RTFM, and looked for some working code and tutorials, but ran into some discussion regarding issues with clock speeds over 4mHz... apparently at some point this issue was resolved by mE. I suspect there must be something in my CONFIG as the code seems pretty straightforward and works well on an '877A

I'd appreciate any thoughts...

Amoque
- 21st March 2015, 16:52
Why is it that enlightenment always comes 15 seconds AFTER I post rather than 15 seconds before? Got it, thanks for listening.

Archangel
- 21st March 2015, 18:08
That's a great statement ! Thanks for sharing it and the post above. For the sake of others in the future would you please post the problem you found?

Demon
- 22nd March 2015, 03:27
Oscillator speed too high?

Forgot to disable an analog feature?

Robert

Acetronics2
- 22nd March 2015, 10:00
Hi, Amoque

something rings my bell,here ...

your code is written for DS1820 and you are stating you use a DS18B20 ... ( see the "count_remain" & "count_per_C" use ... )

Both datasheets have little differences ...

Alain

Amoque
- 22nd March 2015, 12:45
Yes, of course.

I forgot the pull-up resistor (4K7) when designing the board. I had intended to add it at the "plug" end of the cable, then forgot that as well; without this pull-up the "Waitloop" never exits. Once the hardware was correct, the temperature readings were suspiciously low (I suspect for the reasons Alain suggests). "Room temperature" reading was 62F, but my analog thermometer read closer to 70F; placing the sensor under my arm should have demonstrated somewhere near 98F, but the measured value was only 85F. Bruce's (Reynold's Electronics) conversion routine gives more accurate results:

Dummy = Raw_Temperature * 1125
Raw_Temperature = DIV32 100
Raw_Temperature = Raw_Temperature +3200
Temp_in_F = Raw_Temperature

While I cannot verify that this formula yields precise results, room temperature reading of 69.14 was much closer to thermometer reading 0f 70; and under arm reading rose to 97+.

Dave
- 23rd March 2015, 11:13
4K7 seems to be a bit on the high side, I have always used 2k2 for my pullups. Maybe a reason?

Amoque
- 23rd March 2015, 12:59
The datasheet calls for a "strong" pull-up and recommends 4K7; I searched the internet and saw others having success with this value. I have no knowledge or experience that suggests a better value, so I tried it and it seems to work well.

Acetronics2
- 23rd March 2015, 18:13
4K7 seems to be a bit on the high side, I have always used 2k2 for my pullups. Maybe a reason?

a very good reason is the cable length between the probe and the PCB .... :D

Alain

Demon
- 23rd March 2015, 19:41
I haven't tested with long wires yet:
http://www.picbasic.co.uk/forum/showthread.php?t=17282
but 4K7 worked at 115,200 at 64MHz in preliminary tests.

(Diagram in post #1 is not updated, only 2 resistors used)

Robert

Dave
- 24th March 2015, 12:50
115,200.... What?

Charlie
- 24th March 2015, 13:09
Actually the main reason for lower value resistors is when there are several devices on the bus. If you are just using one device, 4.7 K is fine and wastes less current.
Robert, you must have posted on the wrong thread? No RS-232 in this discussion...

Demon
- 24th March 2015, 13:28
115,200.... What?

Oops. Overly medicated apparently. LOL