# 'long' data type problem

Status
Not open for further replies.

#### ibrahim03

##### Full Member level 1
avr studio datatypes

I am trying to write sectors to a mmc card. I am using the following method to write the sectors:

Code:
//****************************************************************************************

unsigned char MMC_WRITE_BLOCK( unsigned int SECTOR,unsigned char WRT)
{
unsigned char LPCNT=0;
//           than 65535 !!

/*-----------------------CMD24--------------------------------------*/
SPIPORT &= ~CS;

CHAR_SPI( 0x58);
CHAR_SPI( 0xFF);
.............
the code works fine for smaller addresses but when the address value exceeds 65535 it 'rolls over'.
I mean if I define

SECTOR = 160 then the address will be calculated as

ADDRESS = SECTOR*512 = 16384 !!

which should be wrong (correct value should be 81920) because I have defined ADDRESS as 'unsigned long' and that should be 32 bits. But why is it acting as 16 bits ?

I am using WINAVR-20050214 and AVR Studio 4.12 to compile my program.I am not using the latest version because with that AVR Studio wont simulate the code with it.

I tried to solve the problem by defining ADDRESS as uint32_t but that was no good either.
I even compiled the code with Imagecraft C compiler ICC AVR but still I had the same problem.

Can anyone please identify the problem over here.How can I (actually) define ADDRESS as a 32 bit number?

#### btbass

Shot in the dark,
Try ADDRESS = (unsigned long)SECTOR * 512UL

### ibrahim03

points: 2

#### abbas1707

##### Full Member level 4
unsigned int sector;
sector=160;

it works fine

### ibrahim03

points: 2

#### btbass

Good!
This is a dangerous inconsistency in the c language of type widening in integral promotion. It is safer never to rely on the widening type afforded by integral promotion.
Misra rule 10.6 (Required) A "U" suffix shall be applied to all constants of unsigned type.

So use '512UL'

Status
Not open for further replies.