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

Simple Code question for a Beginner...

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



Joined: 02 Apr 2012
Posts: 1

View user's profile Send private message

Simple Code question for a Beginner...
PostPosted: Mon Apr 02, 2012 3:42 pm     Reply with quote

I've been given some legacy code I need to port over to a new system. The code is from a CCS C Compiler PCM Version 4.069.

I was wondering if someone could explain what these couple of lines do as I'm not the greatest at C and haven't been able to find anything after spending half the day searching online.

Basically I have array that I'm trying to transmit over the serial port to another device.

The array is declared with the following line:
Code:

char txBuff[20];

for this example the data would be as follows:
txBuff[0] = 0
txBuff[1] = 0
txBuff[2] = 1
txBuff[3] = 2
txBuff[4] = 3
...
txBuff[19] = 18

The first 2 characters at a marker for the beginning of the data packet.

the module I'm stuck in and am confused is the following:
Code:

//------------------------------------------------------------------------------
//   FUNCTION NAME:
//      void uart_tx_isr( void)
//
//   PARAMETERS:
//      none
//
//   DESCRIPTION:
//      UART transmit interrupt service routine.
//
//   RETURNS:
//      none
//------------------------------------------------------------------------------
#INT_TBE
void uart_tx_isr( void)
{
   static unsigned int8 _txPtr;

   if( bUartTxStart)
   {
      bUartTxStart = FALSE;
      _txPtr = 1;
   }

   putc(txBuff[_txPtr++]);
   if( --txBuff[0] == 0)
   {
      disable_interrupts( INT_TBE);
   }
   RxTimeout = RX_TIMEOUT;
}
//------------------------------------------------------------------------------

From what I've been able to figure out the putc(txBuff[_txPtr++]); loads the data one at a time into some external module (I don't have access to) and I'm not bothered by it. The part I'm confused about is the line right below that says if( --txBuff[0] == 0)

I don't understand what --txBuff[0] == 0 means. I'm assuming this is some sort of pre-decrement. but with this being an array I'm a big confused by it. I could understand if it was a pointer and we were decrementing the value (basically moving a byte lower in the array) but this is saying the array itself is smaller? I would think this would be referencing a point smaller than the array itself?

I was hoping someone here would have a bit of insight and could point me in the right direction.

If I need to provide addition info I can.

Thanks,

-J
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 02, 2012 4:16 pm     Reply with quote

Quote:
I don't understand what --txBuff[0] == 0 means.

They are checking to see if the first element in the array (at index 0)
is equal to 1.

Run a test in MPLAB simulator and see what happens. Fill a small array
with 1, 2, 3. Then run that line of code and print the contents of the array.
You get this:
Quote:

00, 02, 03

Code:

#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main(void)
{
int8 txBuff[3] = {1, 2, 3};

--txBuff[0];

printf("%x, %x %x \n\r",txBuff[0], txBuff[1], txBuff[2]);

while(1);
}


How to setup MPLAB simulator to send printf(), putc(), etc. text to the
MPLAB Output window:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 3:23 am     Reply with quote

PCM programmer wrote:
Quote:
I don't understand what --txBuff[0] == 0 means.

They are checking to see if the first element in the array (at index 0)
is equal to 1.


The first "character" is a count of the number of characters to be transmitted. The index _txPtr is initialised to 1, so the first charater sent in txBuff[1].

This is linear buffering, not the more common circular buffering. Each message is sent as lump, the buffer containing just one message. The mainline code filling the buffer will probably wait until the count, txBuf[0] is zero before putting a new message in the buffer and enabling INT_TBE interrupt to start the transmission process.

RF Developer.
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