long int in 8bit uC?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everyone,

very basic question but i'm a little confused and want to be sure -

- i need to work with a value (energy accumulation in Wh) and this could increase to .. say: 50,000,000 and want to avoid any overflowing issues -

can anyone please tell me the size of int in an 8bit uC (m64)? what type should i use, long int?

thank you.
- Eric

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use C99 and its [u]int32_t which is always 32 Bits wide:

#include 

uint32_t data;

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using sizeof will give you the size of a variable, if you aren't sure.

Leon Heller G1HSM

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) It helps to tell what programming language you are using, and what toolchain.
2) Assuming C, see if your toolchain has a limits.h file.
3) ... and also stdint.h file.
4) Your toolchain documentation probably has info on the above.
5) How many bits does it take to hold a value of at least 50 million?
6) Also consider whether the values will ever be negative.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you, i had forgotten about sizeof

1- c language - not sure what 'toolchain' is but i'm assuming it is all the *.h files in 'external dependencies' (AVR studio 4)

2 - limits.h is NOT included - should i include it? i'm including these libraries

#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 					
#include 

3- stdint.h is included
4-
5- 26 bits -
6- the value should never be negative - uint32_t should work, right?

Quote:
Unsigned: From 0 to 4,294,967,295 which equals 2^32 − 1

thanks again,
Eric

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The C compiler you are using is avr-gcc. On Windows this is supplied in two different distributions, one called "WinAVR" and the other called "AVR Toolchain".

You don't have to include limits.h but you can study it to find the widths of various well known types. However the GCC copy is fairly cryptic as it's based on defines that are automatically made from within the C compiler. An easier way to see these things is using the -E and -dM options when compiling any .c file:

E:\avr>avr-gcc -E -dM test.c | grep _MAX
#define __UINT_LEAST16_MAX__ 65535U
#define __UFRACT_MAX__ 0XFFFFP-16UR
#define __UINT8_MAX__ 255
#define __WINT_MAX__ 65535U
#define INT8_MAX 0x7f
#define UINT_LEAST64_MAX UINT64_MAX
#define __SIZE_MAX__ 65535U
#define __WCHAR_MAX__ 32767
#define INT16_MAX 0x7fff
#define __LLACCUM_MAX__ 0X7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFP-64LLK
#define INT8_MIN (-INT8_MAX - 1)
#define __UINT_FAST64_MAX__ 18446744073709551615ULL
#define INT16_MIN (-INT16_MAX - 1)
#define __LFRACT_MAX__ 0X7FFFFFFFP-31LR
#define __UINT_FAST8_MAX__ 65535U
#define INT_FAST8_MAX INT8_MAX
#define __DEC64_MAX_EXP__ 385
#define __UINT_LEAST64_MAX__ 18446744073709551615ULL
#define __SHRT_MAX__ 32767
#define __LDBL_MAX__ 3.40282347e+38L
#define __FRACT_MAX__ 0X7FFFP-15R
#define __UINT_LEAST8_MAX__ 255
#define __UINT32_MAX__ 4294967295UL
#define __ULFRACT_MAX__ 0XFFFFFFFFP-32ULR
#define __LDBL_MAX_EXP__ 128
#define UINT_FAST16_MAX UINT16_MAX
#define __SCHAR_MAX__ 127
#define __WCHAR_MIN__ (-__WCHAR_MAX__ - 1)
#define UINT8_MAX (__CONCAT(INT8_MAX, U) * 2U + 1U)
#define __DEC32_MAX__ 9.999999E96DF
#define __INT32_MAX__ 2147483647L
#define __UACCUM_MAX__ 0XFFFFFFFFP-16UK
#define INT_FAST16_MAX INT16_MAX
#define PTRDIFF_MAX INT16_MAX
#define __ULLACCUM_MAX__ 0XFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFP-64ULLK
#define __DBL_MAX__ ((double)3.40282347e+38L)
#define __INT_FAST32_MAX__ 2147483647L
#define __LACCUM_MAX__ 0X7FFFFFFFFFFFFFFFP-32LK
#define UINT_LEAST8_MAX UINT8_MAX
#define __DEC128_MAX__ 9.999999999999999999999999999999999E6144DL
#define __INT_LEAST32_MAX__ 2147483647L
#define __ACCUM_MAX__ 0X7FFFFFFFP-16K
#define __DBL_MAX_EXP__ 128
#define __SFRACT_MAX__ 0X7FP-7HR
#define __PTRDIFF_MAX__ 32767
#define __LONG_LONG_MAX__ 9223372036854775807LL
#define __ULACCUM_MAX__ 0XFFFFFFFFFFFFFFFFP-32ULK
#define SIZE_MAX (__CONCAT(INT16_MAX, U))
#define INT32_MIN (-INT32_MAX - 1L)
#define UINTPTR_MAX UINT16_MAX
#define UINT_LEAST16_MAX UINT16_MAX
#define __USFRACT_MAX__ 0XFFP-8UHR
#define UINT32_MAX (__CONCAT(INT32_MAX, U) * 2UL + 1UL)
#define INT_LEAST8_MAX INT8_MAX
#define __UINT16_MAX__ 65535U
#define INT_LEAST32_MAX INT32_MAX
#define SIG_ATOMIC_MAX INT8_MAX
#define INT32_MAX 0x7fffffffL
#define UINT64_MAX (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)
#define __INT16_MAX__ 32767
#define __UINT64_MAX__ 18446744073709551615ULL
#define INTPTR_MAX INT16_MAX
#define __SACCUM_MAX__ 0X7FFFP-8HK
#define __SIG_ATOMIC_MAX__ 32767
#define INT_LEAST64_MAX INT64_MAX
#define UINT_FAST32_MAX UINT32_MAX
#define __INT_FAST16_MAX__ 32767
#define __UINT_FAST32_MAX__ 4294967295UL
#define __USACCUM_MAX__ 0XFFFFP-8UHK
#define UINTMAX_MAX UINT64_MAX
#define __FLT_MAX_10_EXP__ 38
#define __LONG_MAX__ 2147483647L
#define __DEC64_MAX__ 9.999999999999999E384DD
#define __INT_LEAST16_MAX__ 32767
#define __INT64_MAX__ 9223372036854775807LL
#define __UINT_LEAST32_MAX__ 4294967295UL
#define INT64_MAX 0x7fffffffffffffffLL
#define __DEC32_MAX_EXP__ 97
#define __INT_FAST8_MAX__ 32767
#define __INTPTR_MAX__ 32767
#define INT64_MIN (-INT64_MAX - 1LL)
#define __SIG_ATOMIC_MIN__ (-__SIG_ATOMIC_MAX__ - 1)
#define __UINTPTR_MAX__ 65535U
#define __INT_FAST64_MAX__ 9223372036854775807LL
#define INT_FAST32_MAX INT32_MAX
#define UINT_FAST8_MAX UINT8_MAX
#define __INT_MAX__ 32767
#define __FLT_MAX_EXP__ 128
#define __INT_LEAST64_MAX__ 9223372036854775807LL
#define UINT_FAST64_MAX UINT64_MAX
#define __INT_LEAST8_MAX__ 127
#define __LDBL_MAX_10_EXP__ 38
#define __LLFRACT_MAX__ 0X7FFFFFFFFFFFFFFFP-63LLR
#define INTMAX_MAX INT64_MAX
#define __INTMAX_MAX__ 9223372036854775807LL
#define __INT8_MAX__ 127
#define __FLT_MAX__ 3.40282347e+38F
#define INT_FAST64_MAX INT64_MAX
#define UINT_LEAST32_MAX UINT32_MAX
#define __DEC128_MAX_EXP__ 6145
#define __UINTMAX_MAX__ 18446744073709551615ULL
#define __DBL_MAX_10_EXP__ 38
#define UINT16_MAX (__CONCAT(INT16_MAX, U) * 2U + 1U)
#define __UINT_FAST16_MAX__ 65535U
#define INT_LEAST16_MAX INT16_MAX
#define __ULLFRACT_MAX__ 0XFFFFFFFFFFFFFFFFP-64ULLR

Of all that the key details are:

uint8_t   0 .. 255
int8_t    -128 .. +127
uin16_t   0 .. 65535
int16_t   -32768 .. +32767
uint32_t  0 .. 4294967295
int32_t   -2147483648 .. +2147483647
uint64_t  0 .. 18446744073709551615
int64_t   -9223372036854775808 .. +9223372036854775807

So, as already advised your best bet is uint32_t.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you Cliff and Lee - always learning from you guys

- Eric