Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

'long' data type problem

Status
Not open for further replies.

ibrahim03

Full Member level 1
Joined
Sep 29, 2005
Messages
99
Helped
5
Reputation
10
Reaction score
2
Trophy points
1,288
Activity points
2,335
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;
	unsigned long ADDRESS = 0;	
	ADDRESS=SECTOR*512;		//PROBLEM : 'ADDRESS' does not hold a value larger
					//           than 65535 !!


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

	CHAR_SPI( 0x58);
	CHAR_SPI (ADDRESS>>24); CHAR_SPI (ADDRESS>>16);
	CHAR_SPI (ADDRESS>>8); CHAR_SPI (ADDRESS>>0);
	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

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
Shot in the dark,
Try ADDRESS = (unsigned long)SECTOR * 512UL
 

    ibrahim03

    points: 2
    Helpful Answer Positive Rating

abbas1707

Full Member level 4
Joined
May 3, 2007
Messages
216
Helped
20
Reputation
40
Reaction score
8
Trophy points
1,298
Activity points
2,371
unsigned int sector;
unsigned long address;
sector=160;
address=(unsigned long)sector*512;

it works fine:D
 

    ibrahim03

    points: 2
    Helpful Answer Positive Rating

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top