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

Array element increment?

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



Joined: 22 Mar 2007
Posts: 3

View user's profile Send private message

Array element increment?
PostPosted: Thu Mar 22, 2007 6:05 am     Reply with quote

int8 mydata[4];
when I use command mydata[2]++; I got all mydata elemnts incremented not only mydata[2];
What is problem in My code?

My version is 3.236.
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 22, 2007 4:26 pm     Reply with quote

Here is a test program that I compiled with PCM vs. 3.236.
You didn't say what PIC you're using, so I compiled it for a 16F877.
Code:

#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

//==========================
void main()
{
int8 mydata[4];

mydata[2]++;

while(1);
}


Here's the .LST file:
Code:

.................... void main() 
.................... { 
0004:  CLRF   04
0005:  MOVLW  1F
0006:  ANDWF  03,F
0007:  BSF    03.5
0008:  BSF    1F.0
0009:  BSF    1F.1
000A:  BSF    1F.2
000B:  BCF    1F.3
.................... int8 mydata[4]; 
.................... 
.................... mydata[2]++; 
000C:  BCF    03.5
000D:  INCF   23,F
.................... 
.................... while(1);
000E:  GOTO   00E
.................... }
.................... 


Here is the mydata array in the .SYM file:
Code:
021-024 main.mydata

Data is stored LSB first by the compiler. So the .LST file shows
that it's incrementing the byte at index = 2, which is correct.
It's not incrementing all bytes in the array.
ahmedzamus



Joined: 22 Mar 2007
Posts: 3

View user's profile Send private message

still problem to increment one element
PostPosted: Thu Mar 22, 2007 5:20 pm     Reply with quote

My processor is 18F458
Here is My code :
Code:

if(button(PIN_c7, 0, 50, 10, c7, 1))
      {
      x++;
      output_toggle(PIN_b6);
   output_toggle(PIN_d0);
   output_toggle(PIN_d1);
mydata[0]++;
}
OUTPUT_B(mydata[1]);

each time I increment mydata[0] the mydata[1] is incremented also!
please elet me know what is problem?
here is list :


    ................... x++;
    03F4: INCF 1E,F
    .................... output_toggle(PIN_b6);
    03F6: BCF F93.6
    03F8: BTG F8A.6
    .................... output_toggle(PIN_d0);
    03FA: BCF F95.0
    03FC: BTG F8C.0
    .................... output_toggle(PIN_d1);
    03FE: BCF F95.1
    0400: BTG F8C.1
    .................... mydata[0]++;
    0402: INCF 20,F
    .................... //*(mydata+0)=*(mydata+0)+1;
    .................... //mydata[1]=0;
    .................... OUTPUT_B(mydata[1]);
    0404: CLRF F93
    0406: MOVFF 21,F8A
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 22, 2007 5:34 pm     Reply with quote

I don't see it in the .LST file. The ASM code only increments
the first byte in the array. Maybe some other code in your
program is incrementing the 2nd byte in the array.
ahmedzamus



Joined: 22 Mar 2007
Posts: 3

View user's profile Send private message

still problem to increment one element
PostPosted: Thu Mar 22, 2007 5:43 pm     Reply with quote

all items is incremented,not only [1].
Here is My full program code :
Code:

void main()
{
int8 mydata[6] ;
   setup_adc_ports(AN0_VREF_VREF);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_ON);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DIV_BY_16,8,8);
   setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);

   lcd_init();

   disable_interrupts(INT_TIMER0);
   disable_interrupts(INT_TIMER1);
   disable_interrupts(INT_TIMER2);
   disable_interrupts(INT_TIMER3);
   disable_interrupts(GLOBAL);
   setup_oscillator(False);



  output_low(PIN_b6);
  output_low(PIN_c0);
  output_low(PIN_c1);
  output_low(PIN_c2);
  output_HIGH(PIN_c3);

 set_tris_c(0xf0);

 set_tris_d(0xfd);
 

   while (1)
   {


   if(button(PIN_c4, 0, 50, 10, c4, 1))
      y++;
     
   if(button(PIN_c7, 0, 50, 10, c7, 1))
      {
      x++;
      output_toggle(PIN_b6);
   output_toggle(PIN_d0);
   output_toggle(PIN_d1);
mydata[0]++;
OUTPUT_B(mydata[1]);

//write_expanded_outputs(mydata);

   delay_ms(10);
   }
   }

}

My list :

    .................... void main()
    .................... {
    *
    0202: CLRF FF8
    0204: BCF FD0.7
    0206: BSF 0D.7
    0208: CLRF FEA
    020A: CLRF FE9
    020C: BSF FC1.0
    020E: BSF FC1.1
    0210: BSF FC1.2
    0212: BCF FC1.3
    0214: MOVLW 07
    0216: MOVWF FB4
    0218: CLRF 19
    021A: CLRF 1A
    021C: CLRF 1B
    021E: CLRF 1C
    0220: CLRF 1D
    0222: CLRF 1E
    0224: CLRF 1F
    .................... int8 mydata[6] ;
    .................... setup_adc_ports(AN0_VREF_VREF);
    0226: BSF FC1.0
    0228: BSF FC1.1
    022A: BSF FC1.2
    022C: BSF FC1.3
    .................... setup_adc(ADC_CLOCK_INTERNAL);
    022E: BCF FC1.6
    0230: BSF FC2.6
    0232: BSF FC2.7
    0234: BSF FC1.7
    0236: BSF FC2.0
    .................... setup_psp(PSP_DISABLED);
    0238: BCF F96.4
    .................... setup_spi(FALSE);
    023A: BCF FC6.5
    023C: BCF F94.5
    023E: BSF F94.4
    0240: BCF F94.3
    0242: MOVLW 00
    0244: MOVWF FC6
    0246: MOVWF FC7
    .................... setup_wdt(WDT_ON);
    0248: BSF FD1.0
    .................... setup_timer_0(RTCC_INTERNAL);
    024A: MOVLW 80
    024C: MOVWF FD5
    .................... setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
    024E: MOVLW 85
    0250: MOVWF FCD
    .................... setup_timer_2(T2_DIV_BY_16,8,8);
    0252: MOVLW 38
    0254: IORLW 06
    0256: MOVWF FCA
    0258: MOVLW 08
    025A: MOVWF FCB
    .................... setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
    025C: MOVF FB1,W
    025E: ANDLW 48
    0260: IORLW B5
    0262: MOVWF FB1
    ....................
    .................... lcd_init();
    0264: BRA 01AE
    ....................
    .................... disable_interrupts(INT_TIMER0);
    0266: BCF FF2.5
    .................... disable_interrupts(INT_TIMER1);
    0268: BCF F9D.0
    .................... disable_interrupts(INT_TIMER2);
    026A: BCF F9D.1
    .................... disable_interrupts(INT_TIMER3);
    026C: BCF FA0.1
    .................... disable_interrupts(GLOBAL);
    026E: BCF FF2.6
    0270: BCF FF2.7
    0272: BTFSC FF2.7
    0274: BRA 0270
    .................... setup_oscillator(False);
    0276: CLRF FD3
    ....................
    ....................
    ....................
    .................... output_low(PIN_b6);
    0278: BCF F93.6
    027A: BCF F8A.6
    .................... output_low(PIN_c0);
    027C: BCF F94.0
    027E: BCF F8B.0
    .................... output_low(PIN_c1);
    0280: BCF F94.1
    0282: BCF F8B.1
    .................... output_low(PIN_c2);
    0284: BCF F94.2
    0286: BCF F8B.2
    .................... output_HIGH(PIN_c3);
    0288: BCF F94.3
    028A: BSF F8B.3
    ....................
    .................... set_tris_c(0xf0);
    028C: MOVLW F0
    028E: MOVWF F94
    ....................
    .................... set_tris_d(0xfd);
    0290: MOVLW FD
    0292: MOVWF F95
    ....................
    ....................
    .................... while (1)
    .................... {
    ....................
    ....................
    .................... if(button(PIN_c4, 0, 50, 10, c4, 1))
    0294: MOVLW 7C
    0296: MOVWF 27
    0298: MOVLW 14
    029A: MOVWF 26
    029C: CLRF 28
    029E: MOVLW 32
    02A0: MOVWF 29
    02A2: MOVLW 0A
    02A4: MOVWF 2A
    02A6: MOVLW 01
    02A8: MOVWF 2B
    *
    033E: MOVF 01,F
    0340: BZ 0344
    .................... y++;
    0342: INCF 1F,F
    ....................
    .................... if(button(PIN_c7, 0, 50, 10, c7, 1))
    0344: MOVLW 7C
    0346: MOVWF 27
    0348: MOVLW 17
    034A: MOVWF 26
    034C: CLRF 28
    034E: MOVLW 32
    0350: MOVWF 29
    0352: MOVLW 0A
    0354: MOVWF 2A
    0356: MOVLW 01
    0358: MOVWF 2B
    *
    03EE: MOVF 01,F
    03F0: BZ 040E
    .................... {
    .................... x++;
    03F2: INCF 1E,F
    .................... output_toggle(PIN_b6);
    03F4: BCF F93.6
    03F6: BTG F8A.6
    .................... output_toggle(PIN_d0);
    03F8: BCF F95.0
    03FA: BTG F8C.0
    .................... output_toggle(PIN_d1);
    03FC: BCF F95.1
    03FE: BTG F8C.1
    .................... mydata[0]++;
    0400: INCF 20,F
    .................... OUTPUT_B(mydata[1]);
    0402: CLRF F93
    0404: MOVFF 21,F8A
    ....................
    .................... //write_expanded_outputs(mydata);
    ....................
    .................... delay_ms(10);
    0408: MOVLW 0A
    040A: MOVWF 27
    040C: RCALL 00FA
    .................... }
    .................... }
    040E: BRA 0294
    ....................
    .................... }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 22, 2007 6:15 pm     Reply with quote

I think you should put in some printf statements to display the
value of the array before and after you increment byte 0 in it.
Example:
Code:

printf("Before:  %x %x %x %x \n\r",
         mydata[0], mydata[1], mydata[2], mydata[3]);

mydata[0]++;

printf("After:  %x %x %x %x \n\r",
         mydata[0], mydata[1], mydata[2], mydata[3]);

This will tell you if the increment statement is affecting
all the other bytes in the array.
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