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

about int1

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



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

about int1
PostPosted: Sun Mar 17, 2019 4:43 am     Reply with quote

Hi, I have seen a variable int1 in Modbus library in CCS C, and I wonder if uint8_t two_characters=0; increment two times like that:

two_characters++; is 1
two_characters++; is 0
two_characters++; is 1 am I right? These are the code.

Code:

void incomming_modbus_serial() {
   char c;
   static int1 two_characters=0;
   static unsigned int8 datah,datal,data;

   c = fgetc(MODBUS_SERIAL) & 0x7F;

   if (!modbus_serial_new)
   {
      if(modbus_serial_state == MODBUS_START)
      {
         if(c==':')
            modbus_serial_state++;
      }
      else if(modbus_serial_state == MODBUS_GETADDY)
      {
         if(!two_characters)
         {
            if(c>=0x41)
               datah=((c-0x37)<<4);
            else
               datah=((c-0x30)<<4);
            modbus_serial_lrc=0;
         }
         else
         {
            if(c>=0x41)
               datal=c-0x37;
            else
               datal=c-0x30;
            data=(datah | datal);
            modbus_rx.address=data;
            modbus_calc_crc(data);
            modbus_serial_state++;
         }
         two_characters++;
      }
      else if(modbus_serial_state == MODBUS_GETFUNC)
      {
         if(!two_characters)
         {
            if(c>=0x41)
               datah=((c-0x37)<<4);
            else
               datah=((c-0x30)<<4);
         }
         else
         {
            if(c>=0x41)
               datal=c-0x37;
            else
               datal=c-0x30;
            data=(datah | datal);
            modbus_rx.func=data;
            modbus_calc_crc(data);
            modbus_serial_state++;
            modbus_rx.len=0;
            modbus_rx.error=0;
         }
         two_characters++;
      }
      else if(modbus_serial_state == MODBUS_GETDATA)
      {
         if(c=='\r')
         {
            modbus_serial_state++;
            modbus_rx.len--;
            modbus_serial_lrc-=data;
         }
         else if(!two_characters)
         {
            if(c>=0x41)
               datah=((c-0x37)<<4);
            else
               datah=((c-0x30)<<4);
            two_characters++;
         }
         else
         {
            if(c>=0x41)
               datal=c-0x37;
            else
               datal=c-0x30;
            data=(datah | datal);
            if (modbus_rx.len>=MODBUS_SERIAL_RX_BUFFER_SIZE)
               modbus_rx.len=MODBUS_SERIAL_RX_BUFFER_SIZE-1;
            modbus_rx.data[modbus_rx.len]=data;
            modbus_rx.len++;
            modbus_calc_crc(data);
            two_characters++;
         }
      }
      else if(modbus_serial_state==MODBUS_STOP)
      {
         if(c=='\n')
         {
            modbus_serial_lrc=((0xFF-modbus_serial_lrc)+1);
            if(modbus_serial_lrc==data)
               modbus_serial_new=TRUE;
         }
         modbus_serial_state=MODBUS_START;
         two_characters=0;
      }
   }
   #if (MODBUS_TYPE == MODBUS_TYPE_MASTER)
      modbus_serial_wait=MODBUS_SERIAL_TIMEOUT;
   #endif
}
temtronic



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

View user's profile Send private message

PostPosted: Sun Mar 17, 2019 5:35 am     Reply with quote

Yes, by looking at the code (quickly), they use it to determine which incoming data byte is the MSB and the LSB of a 'pair' of data. I'm assuming the MODBUS data is in pairs.

Rather clever really, as a simple quick test determines if high byte or low byte. Actually any '1 of 2' choices.
You could also use it as an 'odd or even' test

Jay
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Sun Mar 17, 2019 6:01 am     Reply with quote

Okay temtronic,
changing I wonder here was this
two_characters++; is 1
two_characters++; is 0
and you checked it is right. Thank you
temtronic



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

View user's profile Send private message

PostPosted: Sun Mar 17, 2019 6:50 am     Reply with quote

Of course the easy check to this ,is to have the program display the 'two_characters' value, say to a PC or LCD module....
If you have a one spare I/O pin, asttache a TTL<>USB module and use this to send 'debug data' to a PC running a 'terminal program'. This is also a great way to look at ANY variable and any point of the program.
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Sun Mar 17, 2019 6:58 am     Reply with quote

The only thing 'puzzling', is the phrasing of your question...

You say "uint8_t two_characters=0;". Er. unt8_8... No the variable is int1,
and importantly _static_. So the value is initialised to '0' on the first pass
(this is what it is explicitly initialised 'to'), and then increments 0,1,0,1 etc..
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Mon Mar 18, 2019 5:11 am     Reply with quote

Yes I sould check it using LCD but I didnt have it and I needed learn it very fast, and thank you all help.
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