One Wire and PIC18F4580

General discussion on mikroProg™ programmer and debugger and mikroProg Suite software as well.
Post Reply
Author
Message
amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

One Wire and PIC18F4580

#1 Post by amfortas » 29 May 2017 23:53

Hi All,

I am using this code in a PIC18F452 processor and it works

Function Temperature:word;

begin
// if (Ow_Reset(PortE,2)=0) then Led3:=1; // Led3 illuminates
Ow_Reset(PortE,2);
Ow_Write(PortE,2,0xCC);
Ow_Write(PortE,2,0x44);
Delay_us(120);

Ow_Reset(PortE,2);
Ow_Write(PortE,2,0xCC);
Ow_Write(PortE,2,0xBE);

Temp:=Ow_Read(PortE,2);
Temp:=(Ow_Read(PortE,2) shl 8) + Temp;
//Temp:=0xAA; // Test display
Result:=Temp;

end;

I have recompiled for a PIC18F4580 to take advantage of the CAN Module but the function no longer works. The function Ow_Reset(PortE,2); returns zero
whether the DS18S20 is connected or not.

Does anyone know whether portE,2 requires prior configuration to secure One Wire operation or can suggest why the code does not work in a PIC18F4580 device ?

Many thanks,

Amfortas

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#2 Post by amfortas » 30 May 2017 16:59

Hi,

On further investigation the centre pin of the DS18S20 reveals this signal which would suggest a transaction is taking place. But the temp word remains 0x00.

I will look at ONE WIRE specifications to attempt to understand what is going on.

Amfortas
Attachments
This signal is seen on the centre pin of the<br />DS18S20 device
This signal is seen on the centre pin of the
DS18S20 device
Centre PIN.jpg (47.15 KiB) Viewed 7445 times

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: One Wire and PIC18F4580

#3 Post by dusan.poluga » 31 May 2017 17:48

Hi,

This behavior is not rational.
Can you please zip and send me your project files so i can try it on my hardware?

Best Regards,
Dusan Poluga.

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#4 Post by amfortas » 01 Jun 2017 17:58

Thanks DUSAN

The ONE Wire routine is part of a substantial animal feed weighing system which I upgraded from PIC18F452 to PIC18F4580 with the temperature routine coming at line 232.

I am reading a DS18S20 which works fine on the circuit before the upgrade. There may be a PCB error which I have failed to detect but all appears fine after extensive checking. The device I/O pin is certainly active and even your comment that is does not make sense is very helpful. I will noe read the I/O line on the previous board for comparison.

The wiring is as per the ONE WIRE library file in MikroP i.e the device is directly powered from a 5v supply.

The routine is triggered from a CANBUS signal and is clearly triggered O.K. Only the TEMP word remains :=o.

I am currently breaking down the OW commands individually to clarify the device response. Oddly the RESET command returns a device present signal whether the device is plugged in or not. (Makes me think I may be actually detecting a device NOT present when reset returns 0 so it may be the DS18S20 is not present but it is wired correctly).

I have added the MPAS files which I do not think will help but if you see anything that would be great.

I understand what the OW 0xCC 0x44 0xBE do but I have not worked out the transaction waveform sequenc.

Very many thanks for you interest - meanwhile I think I have to understand the pulse sequence. The pull up resistor is 4k7 as per the library and specification sheet requirement and the logic switches from +5 to 0v

Thanks,

Amfortas
Attachments
Project ZIP.zip
(13.67 KiB) Downloaded 185 times

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#5 Post by amfortas » 02 Jun 2017 23:28

The investigation into the temp word stuck at 0 revealed proper functioning of the OWRESET command:

This shows that the PIC18F4580 PrtE2 is functional as a TX output and the DS18S20 is responding so the device is connected and sending the presence response. But when the {} are removed Temp still remains 0. The earlier PIC18F452 circuit works and Temp contains temperature word. This result also proves that PORTE2 is open drain to allow the DS18S20 to pull the signal line low

The possibility is that PORTE2 Rx is not working which calls for further investigation. Presumably the PORTE2 Direction is controlled by the library OneWire tool? (on the fly).

Function Temperature:word;
begin

// if (Ow_Reset(PortE,2)=0) then Led3:=1;
LED3:=1;
Ow_Reset(PORTE,2);
{ Ow_Write(PORTE,2,0xCC);
Ow_Write(PORTE,2,0x44);
Delay_us(120);

Ow_Reset(PORTE,2);
Ow_Write(PORTE,2,0xCC);
Ow_Write(PORTE,2,0xBE);

Temp:=Ow_Read(PORTE,2);
Temp:=(Ow_Read(PORTE,2) shl 8) + Temp; }

//Temp:=0xAA;
Result:=Temp;
end;

This post is only provided to show the test effort but if anyone has experienced us of the PIC18F4580 I would be interested to know of any success or failure !

Many thanks,

Amfortas

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#6 Post by amfortas » 06 Jun 2017 13:21

4850 OneWire Problem

Hi,
I am being defeated by a failure of OneWire Library to work with a PIC18F4580 using code which works with a PIC452,
Function Temperature:word;
var U:byte;
Temp:word;
begin
U:=0;
U:= Ow_Reset(PORTE,2);
// if U=0 then LED3:=1; Delay_ms(500); Led3:=0; // U=0 so presence pulse accepted
Ow_Write(PORTE,2,0xCC); // 0xCC seen on DQ
Ow_Write(PORTE,2,0x44); // 0x44 seen onDQ
Delay_us(120);
// The DQ line transmits a 500uS Low and the PRESENCE Rx is the response. Testing U confirms //that the master acknowledges the DS18S20 is present. It is powered by an external 5v, The 0xCC //and 0x44 commands are seen on the DD line
// OW-RESET REPEATED JUST TO TEST
U:=0;
U:= Ow_Reset(PORTE,2);
// if U=0 then LED3:=1; Delay_ms(500); Led3:=0; // U=0 so presence pulse accepted
Ow_Write(PORTE,2,0xCC); // 0xCC seen on DQ
Ow_Write(PORTE,2,0x44); // 0x44 seen onDQ
Delay_us(120);


Ow_Reset(PORTE,2);
Ow_Write(PORTE,2,0xCC);
Ow_Write(PORTE,2,0xBE);
// 0xCC and 0xBE seen on DQ master TX ,no response

Temp:=Ow_Read(PORTE,2);
Temp:=(Ow_Read(PORTE,2) shl 8) + Temp;
// if Temp=0 then Delay_ms(500);LED3:=1; Delay_ms(500); Led3:=0;
//Temp=0 so no Temp Read = 0x00
Result:=Temp;
// Temp:=0 using PIC 18F4580
// Temp = ambient using PIC18F452
end;

Final bits 9 bit of both processors
PIC18F452

PIC18F4580

I find is difficult to make sense of these final transactions. It appears that my scope reads the final 9 bits as all Logic 1 high – suggesting the temperature processing has ended {?) but the 4580 does not return the LSB and MSB bytes. The only difference in the DQ line signal is the longer Low at the * shown for the 4580. That is the only difference seen in the complete transaction.
Can anyone suggest what might be wrong ? (I had to load the waveforms as files so the asterisk does not appear. Scrutiny of the two files may detect the longer low transition in the 4580 processor)

Many thanks,
Amfortas
Attachments
4580_ALL.jpg
4580_ALL.jpg (95.84 KiB) Viewed 7387 times
452_ALL.jpg
452_ALL.jpg (99.96 KiB) Viewed 7387 times

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#7 Post by amfortas » 07 Jun 2017 06:50

Hi,

U:= Ow_Reset(PORTE,2);
// if U=0 then LED3:=1; Delay_ms(500); Led3:=0; // U=0 so presence pulse accepted
Ow_Write(PORTE,2,0xCC); // 0xCC seen on DQ
Ow_Write(PORTE,2,0x44); // 0x44 seen onDQ

I have checked the Temperature function with the DS18S20 unplugged and OW_RESET(PORTE,2) remains U=0 so my diagnosis that PORTE,2 is receiving
data (Rx) is wrong.

The DQ line master Tx definitely works and OW commands are seen.

Is it now a reasonable diagnosis that the PIC18F4580 PORTE,2 is not being configured by the library OneWire code as an input pin and is only transmitting ?

The code works fine with a PIC18F452 device.

Any experienced conclusions would be immensely helpful !!

Many thanks,
AMFORTAS

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: One Wire and PIC18F4580

#8 Post by Dany » 07 Jun 2017 15:03

Hi,

Just a remark, may or may not be related to your problem:

The temperature conversion time of the DS1820 is minimal 750 ms. In the code there is only a waiting time of 120us.
See viewtopic.php?f=97&t=65171&hilit=+delay and viewtopic.php?f=12&t=57752#p228577 for more information.
Also please see https://libstock.mikroe.com/projects/vi ... -lib-usage, remark "mE compiler help errors in the one_wire library example".
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: One Wire and PIC18F4580

#9 Post by darko.ilijevski » 07 Jun 2017 16:44

Hello,

I see that you have used ADCON1 = 0x07, which makes the PORTE.B2 pin as analog. Also after the POR, port E is set to analog. Try setting it as 0x08, I am not sure how many analog ports you even need, but that value will set the pin RE2 as digital, keeping the rest as analog. The settings about the analog pins can be found in the datasheet, page 248, from HERE

Best regards
BR,
Darko

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#10 Post by amfortas » 07 Jun 2017 22:48

Thanks Danny,

I chose the 120uS from the MikroP OneWire Library and it works in my PIC18F452 circuit. 120uS also appears in a DS18S20 spec sheet however I am not fully understanding OneWire in depth.

I have tried increasing to 500uS but will try greater delay.

The fact that the OWRESET =0 regardless of device being connected makes me think the presence response is not being read because the PORTE,2 pin is maybe not switching to an input for read slots.

I greatly appreciate your thoughts since I am out of ideas on this problem.

Very Many thanks,

Amfortas

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#11 Post by amfortas » 07 Jun 2017 23:10

Thanks Darko,

"I see that you have used ADCON1 = 0x07, which makes the PORTE.B2 pin as analog. Also after the POR, port E is set to analog. Try setting it as 0x08, I am not sure how many analog ports you even need, but that value will set the pin RE2 as digital, keeping the rest as analog. The settings about the analog pins can be found in the datasheet, page 248, from HERE"

Yes, my suspicion is that the configuration of PORTE,2 is not correct and I introduced ADCON and set bit 7 =0 to turn off GIE.

My 18F452 application simply used the MikroP library OneWire example since in my system interrupts are not active. Therefore I did not use ADCON.
It worked fine with no specific defination of analog inputs since my system does not use any analog inputs. The 452 circuit worked fine.
I then changed to PIC18F4580 to benefit from the built in CAN module. This has worked fine simply by swapping CANSPI to CAN.

The only failure was the Temperature Function where all Tx commands can be seen but temp remains 0.

It seems that something is different in the 4580 in the PORTE,2 use as a bi-directional port.

I simply cannot track down what it is and why the library OneWire functions do not set up PORTE,2 to receive.

Very many thanks for your helpful comments at I will take a look at page 248.

Regards,

AMFORTAS

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#12 Post by amfortas » 07 Jun 2017 23:33

Hi Darko and Danny,

I have used ADCON1:=0x0F and the Temperature Function now works fine.

So very many thanks to you both and for the very useful links. The surprising thing was that my 452 circuit worked !

Best regards and thanks,

AMFORTAS

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: One Wire and PIC18F4580

#13 Post by darko.ilijevski » 08 Jun 2017 09:32

Hi,

You are welcome.

If you think about it and know how PIC usually sets things up - it's not such a big surprise it works on one chip and not on the other. Microchip uses one type of peripherals on one device, and other type (generation) on some other device. It means that they have to move the registers around, rename to avoid confusion, etc. In this case, analog pin selection is set differently than on the PIC18F452. ADCON1 PCFG bits have a different meaning there: 0x07 would set all the pins as digital. For the same thing, you need to use 0x0F on the PIC18F4580. In both cases, the library doesn't set the pin's type of signal. You should have on mind that our libraries usually do not set the signal's type on a pin, exactly because of those reasons - so it's up to user to set up pin's signal type properly. Therefore, you will see many examples starting with something like ANSELA = 0, ADCON1 = 0x0F and so on. First thing you do in the main function, is to set up the pins as digital.

A good practice for doing bit operations on an isolated bit (in my opinion), is to first search if the bit is defined in the compiler: for example GIE_bit = 1 would be "global interrupt bit enable = 1", saves you the time and avoids accidental clearing of other bits. All the "important" bits are predefined usually. Also - when working with the PIC micro controller - it's almost a rule of thumb to always have the datasheet in front of you.

Best regards
BR,
Darko

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: One Wire and PIC18F4580

#14 Post by amfortas » 08 Jun 2017 22:02

Good advice Darko!

I have only used the 18F452 for years and simply ignored ANSEL as a 16F era Register and ICON simply out of lazyness. Being the wrong side of 80 I have been guilty of -if it works- move on.

Regards and thanks

AMFORTAS

Post Reply

Return to “mikroProg™ programmer and debugger”