View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19792
|
|
Posted: Mon Feb 22, 2021 11:42 am |
|
|
Change your clock line, to:
#use delay(INTERNAL=8000000)
Currently there is no guarantee that this will be the selected clock.
The default is not 8MHz, so it may well not be running at this rate. |
|
 |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Mon Feb 22, 2021 11:50 am |
|
|
Defining the RX TX pin is not mandatory, however I tried and it did not fix the issue.
Code: | #define MODBUS_SERIAL_TX_PIN PIN_C6
#define MODBUS_SERIAL_RX_PIN PIN_C7 |
|
|
 |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Mon Feb 22, 2021 2:02 pm |
|
|
No improvement, it seems that both "internal" or "clock" works equally.
I toggle a LED in the while loop and get the following frequency on the output pin:
4M: 17.8 kHz
8M: 35.6 kHz
16M: 71 kHz |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9459 Location: Greensville,Ontario
|
|
Posted: Mon Feb 22, 2021 2:43 pm |
|
|
Ok, How about trying the ex_sisr.c example program that CCS supplies ?
It does work and be a good test to confirm/deny whether it's a 'hardware' or a 'software' problem.
Any PC terminal prgram will work with it, just need a TTL<>USB adapter. |
|
 |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Tue Feb 23, 2021 5:19 am |
|
|
That example works!
I only changed the top defines to
Code: | #include <16F1788.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) |
Without changing any connection I tried again the modbus example,
Code: |
#include <16F1788.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8M)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_SERIAL_TYPE MODBUS_RTU //use MODBUS_ASCII for ASCII mode
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#define MODBUS_ADDRESS 0x02
#include <modbus.c> |
and it doesn't work.
So the problem is in the modbus library.  |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9459 Location: Greensville,Ontario
|
|
Posted: Tue Feb 23, 2021 5:32 am |
|
|
You should ( well need actually) to add 'errors' to the #USE RS232 ( ...options....). This will prevent the UART from 'locking up' after 3 characters arrive and not processed.
I can't see the MODBUS library being the problem as the exact same code worked on another PIC. |
|
 |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Tue Feb 23, 2021 5:58 pm |
|
|
We have a solution, thanks to a hint from CCS support.
The parity!!!
My PC was set to None, but the default for the modbus library is to Odd.
So, setting it to Odd was enough to make it work. So damn simple solution, as it often happens.
Code: | //the parity used by the Modbus driver can be change by making one of the defined before the driver is included:
#define MODBUS_PARITY "NONE"
#define MODBUS_PARITY "EVEN"
#define MODBUS_PARITY "ODD" |
I wish this was mentioned in the modbus example instructions.
Now I am using a TTL-USB serial converter, hence full duplex. I will test tomorrow with the MAX485. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9459 Location: Greensville,Ontario
|
|
Posted: Tue Feb 23, 2021 6:03 pm |
|
|
grrrr....well at least you found it !!!!
The odds are that '8 bit, NO parity' is probably the default for 99.44% of all programs since the hardware UART was invented.....
Have no idea why MODbus would be different than 'industry standard'. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19792
|
|
Posted: Wed Feb 24, 2021 12:36 am |
|
|
This actually depends on what mode you are using.
Modbus for most modes uses no parity, but Modbus in V1.02, specifically
says that parity must be 'even' in RTU mode.
However a lot of MODBUS stuff ignores this, and instead used a second
stop bit. |
|
 |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Thu Feb 25, 2021 3:46 pm |
|
|
It works also with the MAX485.
Thanks for the help!
One more thing I can do with PIC rather than Arduino compatible chips.
To only use one pin to control the direction:
Code: | //#define MODBUS_SERIAL_RX_ENABLE PIN_B4 // Controls RE pin for RS485. Comment out if RE-DE are connected together.
#define MODBUS_SERIAL_ENABLE_PIN PIN_B5 // Controls DE pin for RS485 |
|
|
 |
|