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

modscan32 with PIC18F2550

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
CDE_modicaon



Joined: 11 Dec 2013
Posts: 7

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

modscan32 with PIC18F2550
PostPosted: Thu Dec 12, 2013 12:04 am     Reply with quote

we want to implement a modbus communication b/w Modbus32 and Pic controller. I am using PCWHD compiler version 4.093. Plz help me how to implement and let me know which example should be used:

Ex_modbus.c
Ex_modbus_slave

I am using FTDI cable for communication b/w PC (modscan32) to PIC controller. Plz reply.
_________________
Israr
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Dec 12, 2013 7:59 am     Reply with quote

you need to decide if you are going to use modbus RTU or modbus ACII.

then you need to decide if your making a slave or a master...

Then you need to look in you examples/Driver folders, CCS provides all you need.

the entire modbus Driver/example is (at least in the later compilers) made of 7 files total.

you need to configure the example by selecting the right defines on modbus.c

read the file headers of the drivers. CCS provides some kind of isntructions on how to set it up.


Modbus RTU/ASCII run over RS485... I dont know if modscan32 will work with FTDI coms since its made for RS232... asuming you have the FTDI cable like from sparkfun.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
CDE_modicaon



Joined: 11 Dec 2013
Posts: 7

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

modscan32 with PIC18F2550
PostPosted: Thu Dec 12, 2013 9:50 am     Reply with quote

Thank you very much for your reply....

My aim is to communicate b/w pic18F2550 (as a slave) with S7-1200 Siemens PLC (as a master).

I read the EX_modbus.c and EX_modbus_Slave.c which is given in PICC compiler.


If i assume that FTDI cable is working with Pic, then what is the next step ? How to start slave programming ? How to read holding register if master polling ?
_________________
Israr
CDE_modicaon



Joined: 11 Dec 2013
Posts: 7

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

Re: modscan32 with PIC18F2550
PostPosted: Fri Dec 13, 2013 3:26 am     Reply with quote

plz check the code which i am using for PIC18F2550 (as a slave).
Code:

#include <18F2550.h>
#use delay(internal=8MHz)
#fuses HS,NOLVP,NOWDT,NOBROWNOUT,PROTECT, PUT

#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_SERIAL_RX_BUFFER_SIZE 40
#define MODBUS_SERIAL_BAUD 9600

#ifndef USE_WITH_PC
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_EXT

#define MODBUS_SERIAL_TX_PIN PIN_A5   // Data transmit pin(FTDI CABLE)
#define MODBUS_SERIAL_RX_PIN PIN_A4   // Data receive pin(FTDI CABLE)
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA

#define USE_WITH_PC 1
#include "modbus.c"

#define MODBUS_ADDRESS 0x05

/*This function may come in handy for you since MODBUS uses MSB first.*/
int8 swap_bits(int8 c)
{
 return ((c&1)?128:0)|((c&2)?64:0)|((c&4)?32:0)|((c&8)?16:0)|((c&16)?8:0)
        |((c&32)?4:0)|((c&64)?2:0)|((c&128)?1:0);
}

void main()

   int i,one_time=0;
   int8 coils1  = 0b11111111;
   int8 inputs = 0b11111111;
   int poll=0;
   setup_timer_0(RTCC_INTERNAL);
   modbus_init();
   
   while(1)
   {
      output_toggle(PIN_C2);
      if(one_time==1)
      {
         one_time=0;
         if(bit_test (coils1, 0)) output_bit(PIN_B4,1); else output_bit(PIN_B4,0);
         if(bit_test (coils1, 1)) output_bit(PIN_B3,1); else output_bit(PIN_B3,0);
         if(bit_test (coils1, 2)) output_bit(PIN_C0,1); else output_bit(PIN_C0,0);
         if(bit_test (coils1, 3)) output_bit(PIN_C1,1); else output_bit(PIN_C1,0);
      }
      poll++;               
     
      if(modbus_kbhit()!=1)
      {
       
      }
      else
      {
         poll=0;
     
         //check address against our address, 0 is broadcast
         if((modbus_rx.address == MODBUS_ADDRESS) || modbus_rx.address == 0)
         {     
            switch(modbus_rx.func)
            {
               case FUNC_WRITE_MULTIPLE_COILS:
               if(modbus_rx.data[0] || modbus_rx.data[2] ||
               modbus_rx.data[1] >= 8 || modbus_rx.data[3]+modbus_rx.data[1] > 8)
               {
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               }
               else
               {
                  int i,j;                 
                  modbus_rx.data[5] = swap_bits(modbus_rx.data[5]);                 
                 
                  for(i=modbus_rx.data[1],j=0; i < modbus_rx.data[1]+modbus_rx.data[3];++i,++j)
                  { 
                     if(bit_test(modbus_rx.data[5],j))
                     bit_set(coils1,7-i);
                     else
                     bit_clear(coils1,7-i);
           
                  }
                  one_time=1;
                  modbus_write_multiple_coils_rsp(MODBUS_ADDRESS,
                                 make16(modbus_rx.data[0],modbus_rx.data[1]),
                                 make16(modbus_rx.data[2],modbus_rx.data[3]));
               }
               break;
             
               default:    //We don't support the function, so return exception
               modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_FUNCTION);
            }   
         }
      }
   } //while
}   //main
temtronic



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

View user's profile Send private message

PostPosted: Fri Dec 13, 2013 7:51 am     Reply with quote

comment: Do not 'assume' the FTDI module is working with PIC.
Cut code for a simple 'loopback' program(there's an example in the FAQ section) and CONFIRM the module and PIC are operational. It's easy to get one fuse wrong, PIC won't run and you 'think' it's your MODBUS code...
Once you've confirmed the PIC and FTDI are good , then proceed with your code.Small steps!

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Fri Dec 13, 2013 9:28 am     Reply with quote

There are _two_ defines for the MODBUS interrupt source. One using INT_EXT, and a software UART, and one using INT_RDA and the hardware UART. Both are present in the posted code....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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