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

help! 16 bit array index not working

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



Joined: 08 Feb 2011
Posts: 32

View user's profile Send private message

help! 16 bit array index not working
PostPosted: Thu Nov 24, 2011 7:41 pm     Reply with quote

Hi guys,

I'm try to use array with 16 bit index but no success, when using 8 bit index its ok!

Code:

#include <18F4550.h>
#use delay(clock=20M)

#build( reset=0x1000, interrupt=0x1008 )
#org 0x0000,0x0FFF {}

int8 test_array[512];

void main() {
  test_array[2] = 0x30;
  test_array[500] = 0x30;

  while ( 1) {
    /* Works! */
    if ( test_array[2] == 0x30 ) { output_toggle( PIN_B2 ); }

    /* not working! pic just freeze! */
    if ( test_array[500] == 0x30 ) { output_toggle( PIN_B2 ); }

    delay_ms( 500 );
  }
}

Using compiler 4.124

The code is only for view! do not try to build.

My concept is not correct? or there is a problem with compiler ?
My real code is using usb_cdc & microchip bootloader! pic can start and works, except when use array index > 255.

Code:

CCS PCH C Compiler, Version 4.124, 5967               24-nov-11 23:35

               Filename: D:\pic\test_array.lst

               ROM used: 116 bytes (0%)
                         Largest free fragment is 28552
               RAM used: 505 (25%) at main() level
                         506 (25%) worst case
               Stack:    1 locations

*
1000:  GOTO   1032
.................... #include <18F4550.h> 
.................... //////// Standard Header file for the PIC18F4550 device ////////////////
.................... #device PIC18F4550
.................... #list
.................... 
.................... #use delay(clock=20M) 
*
1008:  MOVLW  02
100A:  MOVWF  FEA
100C:  MOVLW  05
100E:  MOVWF  FE9
1010:  MOVF   FEF,W
1012:  BZ    102E
1014:  MOVLW  06
1016:  MOVWF  01
1018:  CLRF   00
101A:  DECFSZ 00,F
101C:  BRA    101A
101E:  DECFSZ 01,F
1020:  BRA    1018
1022:  MOVLW  7B
1024:  MOVWF  00
1026:  DECFSZ 00,F
1028:  BRA    1026
102A:  DECFSZ FEF,F
102C:  BRA    1014
102E:  GOTO   1070 (RETURN)
.................... 
.................... #build( reset=0x1000, interrupt=0x1008 ) 
.................... #org 0x0000,0x0FFF {} 
.................... 
.................... int8 test_array[512]; 
.................... 
.................... void main() { 
1032:  CLRF   FF8
1034:  BCF    FD0.7
1036:  CLRF   FEA
1038:  CLRF   FE9
103A:  MOVF   FC1,W
103C:  ANDLW  C0
103E:  IORLW  0F
1040:  MOVWF  FC1
1042:  MOVLW  07
1044:  MOVWF  FB4
....................   test_array[2] = 0x30; 
1046:  MOVLW  30
1048:  MOVWF  06
....................   test_array[500] = 0x30; 
104A:  MOVLB  1
104C:  MOVWF  xF8
.................... 
....................   while ( 1) { 
....................     /* Works! */ 
....................     if ( test_array[2] == 0x30 ) { output_toggle( PIN_B2 ); } 
104E:  MOVF   06,W
1050:  SUBLW  30
1052:  BNZ   1058
1054:  BCF    F93.2
1056:  BTG    F8A.2
.................... 
....................     /* not working! pic just freeze! */ 
....................     if ( test_array[500] == 0x30 ) { output_toggle( PIN_B2 ); } 
1058:  MOVF   xF8,W
105A:  SUBLW  30
105C:  BNZ   1062
105E:  BCF    F93.2
1060:  BTG    F8A.2
.................... 
....................     delay_ms( 500 ); 
1062:  MOVLW  02
1064:  MOVLB  2
1066:  MOVWF  x04
1068:  MOVLW  FA
106A:  MOVWF  x05
106C:  MOVLB  0
106E:  BRA    1008
1070:  MOVLB  2
1072:  DECFSZ x04,F
1074:  BRA    1068
....................   } 
1076:  MOVLB  1
1078:  BRA    104E
.................... } 
107A:  SLEEP



Thanks.

edited to correct from 500 to 512 array size.


Last edited by freesat on Thu Nov 24, 2011 8:01 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 7:48 pm     Reply with quote

Quote:
int8 test_array[500];

void main() {
test_array[2] = 0x30;
test_array[500] = 0x30;

This is a mistake. See this FAQ on common errors in C.
See section 2.4 on size of arrays.
http://www.drpaulcarter.com/cs/common-c-errors.php
freesat



Joined: 08 Feb 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 7:59 pm     Reply with quote

Im Sorry PCM!

the correct is...
Code:

int8 test_array[512];

void main() {
test_array[2] = 0x30;
test_array[500] = 0x30;


i know array start at zero.
i will correct on original post.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 8:07 pm     Reply with quote

I modified your test program so it would be better for MPLAB simulator.
I can't test with 4.124 at the moment, so I tested it with vs. 4.119.
It works:
Quote:

test_array[2] value: 0x30
test_array[500] value: 0x30

I can test it with 4.124 later today or tomorrow, but I suspect that it will
still work.
Code:

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

#build( reset=0x1000, interrupt=0x1008 )
#org 0x0000,0x0FFF {}

int8 test_array[512];

void main()
{
test_array[2] = 0x30;
test_array[500] = 0x30;

if(test_array[2] == 0x30 )
   printf("test_array[2] value: 0x30 \r");

if(test_array[500] == 0x30 ) 
   printf("test_array[500] value: 0x30 \r");

while(1);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Nov 25, 2011 4:30 am     Reply with quote

Some comments:
The assembler posted works fine.
It just loads 0x30, into location 6, and also into location 0x1F8. These are the values it then tests.
Have you actually tested the code 'as posted', or is this cut out of something else?. If the latter, my guess would be that you have some other problem. Classic things would be:
1) Something walking over part of memory.
2) Using a 16bit array index, with a index value that is updated in an interrupt - remember if you do this, you _must_ disable interrupts round the point where you use the value, or provide some equivalent method of ensuring that the value doesn't update between accessing the first and second bytes of the value.

Also, how are you actually testing the output_toggle?. If (for instance), you have an LED on this line, then the double toggle if both tests are used, would never be visible - the LED would be on for about 1uSec, so with both toggles enabled, the chip would just sit doing apparently nothing....

Best Wishes
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