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 support@ccsinfo.com

PIC16F726 Cap sense and Modbus [SOLVED]

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



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

PIC16F726 Cap sense and Modbus [SOLVED]
PostPosted: Sun Nov 13, 2016 9:54 am     Reply with quote

Hi All,

I'm trying to see if Capacitive touch will work along with Modbus and interestingly the capsense stops working as soon as I include Modbus.c.
Any ideas would be appreciated.

I have been not active in this forum for a long time now. Thanks to Pokemon Go. I wasted almost 4 months now.

Code:

#include <main.h>

#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_EXT          //Source of interrupts    (MODBUS_INT_EXT,MODBUS_INT_RDA,MODBUS_INT_RDA2)
#define MODBUS_SERIAL_RX_PIN PIN_c6                    //Valid pin for serial receive               
#define MODBUS_SERIAL_TX_PIN PIN_c7                   //Valid pin for serial transmit     
#define MODBUS_SERIAL_ENABLE_PIN PIN_c5              //Valid pin for serial enable, rs485 only
#define MODBUS_SERIAL_RX_ENABLE PIN_c5              //Valid pin for serial rcv enable, rs485 only   
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA  //Tipo de interrupção(assincrono) pro rs
                                 
//#include "modbus.c"                   
//#define MODBUS_ADDRESS 0x03
char msg;


void initialize()
{
output_low(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
output_low(PIN_A6);
output_low(PIN_A7);

}
/*
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 beep(){
      output_high(PIN_B6);
      delay_ms(20);
      output_low(PIN_B6);
}

/*
int8 readvalue() {
int8 valu = 0;
if(input_state(PIN_A0)) valu = valu + 1;
if(input_state(PIN_A1)) valu = valu + 2;
if(input_state(PIN_A2)) valu = valu + 4;
if(input_state(PIN_A3)) valu = valu + 8;
if(input_state(PIN_A6)) valu = valu + 16;
if(input_state(PIN_A7)) valu = valu + 32;
return valu;
}

*/

void main() {
   int8 coils = 0b00000101;
   int8 inputs = 0b00001001;
   int16 hold_regs[] = {0x8800,0x7700,0x6600,0x5500,0x4400,0x3300,0x2200,0x1100};
   int16 input_regs[] = {0x1100,0x2200,0x3300,0x4400,0x5500,0x6600,0x7700,0x8800};
   int16 event_count = 0;
   setup_adc_ports(NO_ANALOGS);
   enable_interrupts(GLOBAL);
   initialize();
  // modbus_init();
   int8 i;
   for(i=0; i<8; i++) { hold_regs[i] = 0;} // initializing all zeros.
   
   while(TRUE){
      //TODO: User Code
     // hold_regs[0] = readvalue();


     if(touchpad_hit())
      {
      msg=touchpad_getc();
     
      switch(msg)
      {
      case 'A': beep(); output_toggle(PIN_A0); break;
      case 'B': beep(); output_toggle(PIN_A1); break;
      case 'C': beep(); output_toggle(PIN_A2); break;
      case 'D': beep(); output_toggle(PIN_A3); break;
      case 'E': beep(); output_toggle(PIN_A6); break;
      case 'F': beep(); output_toggle(PIN_A7); break;
      default:  break;
      }
      }

_________________
Regards,
Devil


Last edited by devilfrmheaven on Mon Nov 14, 2016 12:01 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 13, 2016 10:22 am     Reply with quote

Where's your #use touchpad() line ? I don't see it.
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Sorry it was in the header file.
PostPosted: Sun Nov 13, 2016 11:12 am     Reply with quote

Sorry PCM programmer, It was in the header file.

Code:

#include <16F726.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOBROWNOUT               //No brownout reset

#use delay(int=8000000)

#USE TOUCHPAD(RANGE=H, THRESHOLD=6, SCANTIME=32, PIN_B0='A', PIN_B1='B', PIN_B2='C', PIN_B3='D', PIN_B4='E', PIN_B5='F', PIN_A4='G', PIN_A5='H')

_________________
Regards,
Devil
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 13, 2016 1:00 pm     Reply with quote

Your setup code has a few problems, as shown in bold below:
Quote:

#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_EXT //Source of interrupts (MODBUS_INT_EXT,MODBUS_INT_RDA,MODBUS_INT_RDA2)
#define MODBUS_SERIAL_RX_PIN PIN_c6 //Valid pin for serial receive
#define MODBUS_SERIAL_TX_PIN PIN_c7 //Valid pin for serial transmit
#define MODBUS_SERIAL_ENABLE_PIN PIN_c5 //Valid pin for serial enable, rs485 only
#define MODBUS_SERIAL_RX_ENABLE PIN_c5 //Valid pin for serial rcv enable, rs485 only
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA //Tipo de interrupção(assincrono) pro rs

1. You have two conflicting interrupt sources. The first one says to use
the External interrupt (on Pin B0), but the 2nd one says to use the RDA
interrupt for the hardware UART. Choose the one you want and delete
the other one.

2. You have PIN_C6 defined for the serial Rx and PIN_C7 for Tx.
But they are the opposite of what they should be. The true actual
hardware TX pin is PIN_C6. The true hardware Rx pin is PIN_C7. See
page 3 in the Adobe Acrobat reader for the pinout of the 16F726:
http://ww1.microchip.com/downloads/en/DeviceDoc/40001341F.pdf
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

fixed the code already but not touch.
PostPosted: Sun Nov 13, 2016 1:58 pm     Reply with quote

Thanks PCM.

I had corrected the codes you mentioned.

Code:

#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_EXT          //Source of interrupts    (MODBUS_INT_EXT,MODBUS_INT_RDA,MODBUS_INT_RDA2)
#define MODBUS_SERIAL_RX_PIN PIN_c7                    //Valid pin for serial receive               
#define MODBUS_SERIAL_TX_PIN PIN_c6                   //Valid pin for serial transmit     
#define MODBUS_SERIAL_ENABLE_PIN PIN_c5              //Valid pin for serial enable, rs485 only
#define MODBUS_SERIAL_RX_ENABLE PIN_c5              //Valid pin for serial rcv enable, rs485 only   
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA  //Tipo de interrupção(assincrono) pro rs


Unfortunately the touch stops working as soon as I add
Code:
#include "modbus.c"

_________________
Regards,
Devil
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Sun Nov 13, 2016 2:31 pm     Reply with quote

You still show the two interrupt sources....
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Thanks MR T
PostPosted: Sun Nov 13, 2016 11:20 pm     Reply with quote

Thanks MR T

The code was corrected already, I think modbus and capsense are sharing same timer. That must be the reason why capsense stops working as soon as modbus.c is included in the code.

Once Modbus was up and running I used this code
Code:
      if(touchpad_hit())
      {
      msg=touchpad_getc();
      hold_regs[1] = msg;
      }


I saw that touchpad_getc was being called each time modbus master pings the slave. The register was being updated with Values between 85 and 90

Code:

#include <main.h>

#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_RX_PIN PIN_c7                    //Valid pin for serial receive               
#define MODBUS_SERIAL_TX_PIN PIN_c6                   //Valid pin for serial transmit     
#define MODBUS_SERIAL_ENABLE_PIN PIN_c5              //Valid pin for serial enable, rs485 only
#define MODBUS_SERIAL_RX_ENABLE PIN_c5              //Valid pin for serial rcv enable, rs485 only   
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA  //Tipo de interrupção(assincrono) pro rs

#include "modbus.c"            // <- this line         
#define MODBUS_ADDRESS 0x03
char msg="";


void initialize()
{
output_low(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
output_low(PIN_A6);
output_low(PIN_A7);

}

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 beep(){
      output_high(PIN_B6);
      delay_ms(20);
      output_low(PIN_B6);
}

_________________
Regards,
Devil
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Nov 14, 2016 3:45 am     Reply with quote

It is best not to think of the CCS code as drivers. It is really example code. They work on their own, and with properly written user code, but very often these "drivers" cannot work with each other as they use the same resources such as SPI & I2C interfaces, and in this case yes, timers.

The "drivers" provide a starting point, a leg up for you, but they don't do everything and won't work, without modification, for everyone in every situation. You, the coder, have to sort out what stuff is clashing and modify one, or only if you really have to, both of the "drivers" to get them playing nicely together.

And something else, you have clashing enables. That will not work. Only define both if you have two enables to your RS485 interface IC. Some do, but most have only one, which seems likely in your case. If so, drive it with the transmit enable and do not define the other (receive) enable.
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Mon Nov 14, 2016 8:37 am     Reply with quote

Looking at it though they allow you to change the timer used. There is a define available MODBUS_TIMER_USED, allowing you to change which timer is used, for exactly this reason> Smile

#define MODBUS_TIMER_USED MODBUS_TIMER_T2
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Thanks Mr T
PostPosted: Mon Nov 14, 2016 12:01 pm     Reply with quote

That solved the problem Mr T.

Thanks for the response people..
_________________
Regards,
Devil
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