CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

modbus RTU example code explanation
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2020 6:42 am     Reply with quote

Mvedovetto wrote:

Where I can find isr timer1 routine ?

The #use timer() library function automatically creates the ISR code
if you put in the 'ISR' parameter. You can see it in the .LST file.
You don't have to write the ISR routine. All you have to do is to enable
global interrupts.

Look in the #use timer() section of the CCS manual:
Quote:
ISR -
Uses the timer's interrupt to increment the upper bits of the tick timer. This mode requires the the global interrupt be enabled in the main program.
Mvedovetto



Joined: 17 Aug 2007
Posts: 38

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 02, 2020 9:00 am     Reply with quote

PCM programmer wrote:
Mvedovetto wrote:

Where I can find isr timer1 routine ?

The #use timer() library function automatically creates the ISR code
if you put in the 'ISR' parameter. You can see it in the .LST file.
You don't have to write the ISR routine. All you have to do is to enable
global interrupts.

Look in the #use timer() section of the CCS manual:
Quote:
ISR -
Uses the timer's interrupt to increment the upper bits of the tick timer. This mode requires the the global interrupt be enabled in the main program.


Ok, Thank you. I didn't know this feauture of #USE TIMER. I never read of self create isr routine because section of CCS manual just says:
Quote:
    " This directive creates a tick timer using one of the PIC's timers. The tick timer is initialized to zero at program start. This directive also creates the define TICKS_PER_SECOND as a floating point number, which specifies that number of ticks that will occur in one second. "


Global interrupts are enabled by modbus_init() function

Code:

// Purpose:    Initialize RS485 communication. Call this before
//             using any other RS485 functions.
// Inputs:     None
// Outputs:    None
void modbus_init()
{
   output_low(MODBUS_SERIAL_ENABLE_PIN);

   RCV_ON();

   #if defined(__PCD__)
      enable_interrupts(INTR_GLOBAL);
   #else
      enable_interrupts(GLOBAL);
   #endif
}
Mvedovetto



Joined: 17 Aug 2007
Posts: 38

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 02, 2020 11:12 am     Reply with quote

Also CCS customer support answered to my questions, so now thank either to your support too, I've understood.

Quote:

#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA

To answer your questions:
1. The modbus_kbhit() function will return TRUE when it has received a new message who's calculated CRC matches the received CRC.
2. The way the Modbus RTU protocol works is that it's done sending a message when it hasn't received any data for a certain amount of time. When that time has expired the modbus_timeout_now() function gets called and if the calculated CRC matches the received CRC it sets modbus_serial_new to TRUE, indicating it has received a new message.
3. The CRC is calculated as the bytes of the messages are received, after the CRC has been received the calculated CRC should be 0 meaning that the calculated CRC matched the received CRC.
4. When the ISR is used with #use timer() the compiler generates the timer's ISR and put is were it puts most internal functions. If you want to look at the ISR you'll have to open the .lst file and find where it calls the timer's ISR from the interrupt vector at the beginning of the .lst file.


Now I have another issue because I used the old 16F88, but I need 16F15xxx family that let me debug via RS232 modbus communication (two USART are available on that family) ... source complies, but results are very strange!
I'll open another thread.
Thank you all
temtronic



Joined: 01 Jul 2010
Posts: 9632
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Mar 02, 2020 11:40 am     Reply with quote

I suppose you want to learn about MODBUS....as there are easier ways to exchange data between 2 PICS
Mvedovetto



Joined: 17 Aug 2007
Posts: 38

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 02, 2020 12:06 pm     Reply with quote

temtronic wrote:
I suppose you want to learn about MODBUS....as there are easier ways to exchange data between 2 PICS


No, Temtronic, I didn't. I must provide several units in project: one (the master) retrieves signals state and the other units (up to 16) must replicate main unit in other places at a distance up to 500 meters. That is why I thought that MODBUS RTU could fit system requirements. If you know an easier way, please tell me.
Ttelmah



Joined: 11 Mar 2010
Posts: 20056

View user's profile Send private message

PostPosted: Mon Mar 02, 2020 1:01 pm     Reply with quote

Seriously, doing that with ModBus, would involve a lot of overhead and
complexity.
Simpler by far to design your own basic protocol to do what is needed.
I'd guess a 'header' saying that the data is for 'all units', and another
specifying a unit number for it to go to.
Use 9bit serial and set the 9th bit for the header.
Have the header followed by a byte count, then the data packet, and a
checksum.
You then need only send the data that is for all units once.
Keep your baud rate reasonably low (the distance that RS485 supports
drops with data rate). It supports over 1000m at up to 90000bps.
If you want to know for sure that packets have been received OK, add
an acknowledgement from each unit with time delays to avoid conflicts.
temtronic



Joined: 01 Jul 2010
Posts: 9632
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Mar 02, 2020 6:25 pm     Reply with quote

CCS does supply examples of RS485 communications, in the 'examples' folder. Be sure to read all the comments within the code.....
Also be sure the hardware has proper pullup, bias, pulldown resistors based upon speed, distance and wiring....
As Mr. T points out, it's good for your project, I suggest starting at 9600 baud. Once working 100% THEN you can increase baudrate IF required.500m is easily doable at 9600,I'm assuming you're using some form of twisted pair cable. If not, slower speed may be required. I've gone 15 miles, using regular Belltell lines, though at a much,much slower speed.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group