Reputation: 920
I am getting division by zero error at this line:
if (tim2_st_ovf < T2_PREK_250)
These values are defines like this:
volatile uint8_t tim2_st_ovf = 0;
#define T2_PREK_250 ((250 * (F_CPU / 1000)) / ((UINT8_MAX + 1) * 1024))
#define F_CPU 16000000UL
And UINT8_MAX
equals to 255.
Why am I getting this? I calculated it several times on calculator and am getting ~15. Also, if I change 1024 to 1023 it doesnt show any error.
Upvotes: 5
Views: 1939
Reputation: 87201
((UINT8_MAX + 1) * 1024)
may become 0, because UINT8_MAX + 1
is usually 256, and 256 * 1024
is 0 modulo 216. So if sizeof(int) == 2
on your achitecture, then you get 0.
On the typical modern desktop architectures with GCC, sizeof(int) == 4
, and you wouldn't get the division by 0.
To fix it, replace 1024
with 1024UL
. That will work, because unsigned long
is guaranteed to go up to 4294967295. (Thanks to Pascal Cuoq for explaining it.)
Upvotes: 10