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

how to increment value in array variable

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

how to increment value in array variable
PostPosted: Tue Nov 26, 2013 10:52 pm     Reply with quote

Code:
unsigned int16 number;
unsigned char digits[4];
displaying value on Seven segment display.

void main()
{
   number = 9999;
   digits[0] = number / 1000;
   digits[1] = (number / 100) % 10;
   digits[2] = (number / 10) % 10;
   digits[3] = number % 10;
   
   while(1)
   {
      digits[1] = digits[1]++;
      delay_ms(500);
   }   
}

Above program doesn't increment the value stored in digits[1]. how to do this?
how to increment digits[0], digits[1], digits[2], digits[3] individually?
Please help.
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed Nov 27, 2013 1:37 am     Reply with quote

There are a lot of things missing:

The code can't run without a processor definition, fuses, and clock definition.
Then you declare the digits as 'char'. You do understand the difference between the number 1, and the ASCII character '1'?. If you want to do something with the digits, this needs to be handled. Which does your display code use?.
Then if you increment the individual digits, you have to handle the wrap. What happens when 'digits[1]', gets larger than 9?.
The increment instruction as written won't work. You are setting digits[1], to the value digits[1] was _before_ you increment it. This is just wrong code.

Either:
Code:

digits[1]++;
//or
digits[1]=digits[1]+1;


Then re-think your maths, for conversion. It is terrifyingly inefficient. You perform a total of three 16bit divisions, and three 16bit modulus operations to get four digits. Look at the ldiv operator, which gives quotient _and_ remainder in one operation. Three ldiv instructions, halves the work involved.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed Nov 27, 2013 1:54 am     Reply with quote

yes i have defined the processor, fuses and clock.
i do understand the differences between the number 1 and ascii number.

when digits is greater than 9, reset the value to 0.
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed Nov 27, 2013 2:01 am     Reply with quote

hemnath wrote:
yes i have defined the processor, fuses and clock.
i do understand the differences between the number 1 and ascii number.

when digits is greater than 9, reset the value to 0.


You are missing the point about the increment.

The instruction val++, says increment 'val', and return the value _before_ the increment.

So your line says increment val, then set val _back_ to the value before the increment. Duh....
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