View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
how to increment value in array variable |
Posted: Tue Nov 26, 2013 10:52 pm |
|
|
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: 19516
|
|
Posted: Wed Nov 27, 2013 1:37 am |
|
|
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
|
|
Posted: Wed Nov 27, 2013 1:54 am |
|
|
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: 19516
|
|
Posted: Wed Nov 27, 2013 2:01 am |
|
|
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.... |
|
|
|