> Hi Carl,
>
> If you use pointer arithmetics like this "*(preadarray + i - k) = *(
> preadarray + i - k - 1);" I think it would be easier to do that like "
> readarray[ i - k ] = readarray[ i - k - 1 ];".
>
> Also when you use the array as a string you would need to make sure that the
> end of the string is closed by a '\0' character -- I can't see where is that
> happen,
>
> Tamas
>
>
>
> On Tue, Jun 2, 2009 at 3:01 AM, Carl Denk <
.....cdenkKILLspam
.....windstream.net> wrote:
>
>
>> A week ago, I had a banking problem with the 18F1320 firmware. That's
>> working OK, there are a few memory locations left. Running the program
>> under MPLAB SIM is OK, but after burning and verifying the chip, which
>> communicates RS-232 with a PC Com port running 9600 baud with RealTerm,
>> the line of 24 characters, randomly, but mostly gobbles a character in
>> the middle, inserts a line feed and sometimes gobbles the end.
>>
>> Here's what the string should look like:
>>
>> @02RD031901140167W33F*
>>
>> And here's what happens frequently, the line feeds aren't always the
>> same location:
>>
>> @02RD031
>> 01140167W30C*
>>
>> and here's ASCII + HEX , the new line at "W" was added to keep line length.
>>
>> @40 030 232 R52 D44 030 333 131 0A 030 131 131 434 030 131 636 737
>> W57 333 030 C43 *2A 0A 0D 00
>>
>>
>> I have tried 2 PC's, 2 PIC boards, and 3 PICS with no change.
>>
>> Here's pieces of the code:
>>
>>
>>> #pragma udata access accessram
>>> near char readarray[25];
>>> #pragma udata
>>>
>> char c;
>>
>> start: TRISA = 0b00000111; // AN0 (RA0), AN1 (RA1), and AN2 (RA2) to
>> input (1).
>> TRISB = 0b00010000; //RX (RB4), port set to input (1), TX (RB1) and
>> others stay output(0).
>> ADCON1 = 0b01111000; // AN0, AN1 and AN2 as analog (0), set AN5 (RB1)
>> and AN6 (RB4) as digital (1).
>> RCSTAbits.SPEN = 1; // set serial port enable bit
>> TXSTAbits.TXEN = 1; // set transmit enable bit
>>
>> /* Open the USART configured as 8N1, 9600 baud, in polled mode */
>> OpenUSART (USART_TX_INT_OFF &
>> USART_RX_INT_ON &
>> USART_ASYNCH_MODE &
>> USART_EIGHT_BIT &
>> USART_CONT_RX &
>> USART_BRGH_HIGH, 51); // 9600 = 51, 2400 = 207, 19.2K= 25
>>
>>
>>
>>> // Build the output string
>>> *preadarray = '@'; // Bit 0 - String start
>>>
>> identifier
>>
>>> *(preadarray + 1) = '0'; // Bit 1 -
>>>
>> Unit identifer high digit
>>
>>> *(preadarray + 2) = '2'; // Bit 2 -
>>>
>> Unit identifier low digit
>>
>>> *(preadarray + 3) = 'R'; // Bit 3 -
>>>
>> Command character
>>
>>> *(preadarray + 4) = 'D'; // Bit 4 -
>>>
>> Command Character
>>
>>> *(preadarray + 17) = '0'; // Bit 17 -
>>>
>> ERROR IDENTIFIER
>>
>>> *(preadarray + 18) = (watchdog_count + 48); //
>>>
>> Bit 18 - ERROR QUANTITY
>>
>>> *(preadarray + 19) = '0'; // Bit 19 -
>>>
>> Dummy FCS character
>>
>>> *(preadarray + 20) = '0'; // Bit 20 -
>>>
>> Dummy FCS character
>>
>>> *(preadarray + 21) = '*'; // Bit 21 - String
>>>
>> end identifier
>>
>>> *(preadarray + 22) = '\n'; // Bit 22 - New
>>>
>> Line
>>
>>> *(preadarray + 23) = '\r'; // Bit 23 -
>>>
>> Carriage return
>>
>>> *(preadarray + 24) = '\0'; // Bit 24 - End of
>>>
>> String
>>
>>> if (watchdog_count >0) //Notify the home computer
>>>
>> there has been errors
>>
>>> {
>>> *(preadarray + 17) = 'W';
>>> }
>>> itoa(well_pressure, (preadarray + 5) );
>>> itoa (regulated_pressure, (preadarray + 9));
>>> itoa (temperature, (preadarray + 13));
>>>
>>> //Right justify the number fields
>>> for (i = 8; i <= 16; (i = i + 4)) // 3 Fields
>>> {
>>> for (j = 3; j >= 1; j--) //
>>>
>> 4 Characters
>>
>>> {
>>> for (k = 0; k < 3; k++)
>>> {
>>> if (*(preadarray + i - k)
>>>
>> == 0)
>>
>>> {
>>> *(preadarray + i -
>>>
>> k) = *(preadarray + i - k - 1);
>>
>>> *(preadarray + i - k - 1)
>>>
>> = 0;
>>
>>> }
>>> }
>>> }
>>> }
>>> for ( i = 4; i <= 18; i++)
>>> {
>>> // Change any NULLS to '0'
>>> if ( *(preadarray + i) == 0 ) *(preadarray
>>>
>> + i) = '0';
>>
>>> }
>>>
>>> //calculate the FCS (Frame Check Sequence) for the outgoing string
>>> *(preadarray + 17) = 87;
>>> *(preadarray + 18) = 51;
>>>
>>> result = *preadarray; //First byte of string to check
>>>
>>> for ( j = 1; (*(preadarray + j) == 0 ) || ( j <= 18 ) ; j++)
>>> {
>>> result ^= (*(preadarray + j)); //XOR operation
>>> }
>>> fcs_high = 48 +(result >> 4);
>>> /*If it's more than "9" then make it Alpha*/
>>> if (fcs_high > 57) fcs_high = fcs_high + 7;
>>> fcs_low = 48 + (result & 0b00001111);
>>> //If it's more than "9" then make it Alpha
>>> if (fcs_low > 57) fcs_low = fcs_low + 7;
>>> *(preadarray + 19) = fcs_high; // Bit 19 - FCS high digit
>>> *(preadarray + 20) = fcs_low; // Bit 20 - FCS low digit
>>>
>>> //Send out the string
>>> putsUSART (readarray);
>>>
>> I have also tried as the argument to putsUSART : preadarray, with no
>> change, and *(preadarray) , and "near" which did not compile.
>>
>> The program has had major reorganization , with little actual new code
>> recently. Prior to the major change there has not been this trouble for
>> several years and numerous small changes.
>>
>> The outputting area previously was as below, with no problems before the
>> major change:
>>
>> for ( i = 0; i <= 23 ; i++)
>> {
>> while ( TXSTAbits.TRMT == 0)// The transmit is busy when 0
>> TXREG = (near) *(preadarray + i);
>> }
>>
>> Also I have tried this variation which was the same issue
>>
>>
>> c = (far) *(preadarray + i);
>> TXREG = c;
>>
>>
>> As I said, everything works as expected in MPLAB SIM, but the actual
>> chip output is bad. Comments appreciated. :)
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>