[SOLVED] Smart Card interfacing with PIC18F452

Status
Not open for further replies.

ericparggah

Newbie level 5
Joined
Oct 18, 2008
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,352
Hi,
I am trying to write and read a 16-bit value to a memory based smart card using I2C. Write and Read functions are as shown below:
Code:
void WriteToSmartCard(unsigned int Value)
{	
	unsigned char HiByte, LoByte;
	LoByte = Value; // Assign the 8-LSb to the variable Addres/Value >>= 8; // Shift the 8-LSB out of the variable Value
	Value >>= 8;
	HiByte = Value; // Assign the remaining 8-MSb to the variable AddressHi
	I2Cwrite((0x00), HiByte);
	I2Cwrite((0x01), LoByte);
}

unsigned int ReadSmartCard()
{
	CardAmount = I2Cread(0x00);
	CardAmount <<= 8;
	CardAmount |= (I2Cread(0x01);
	return CardAmount;
}
The problem is that when I display the various bytes to the LCD, I get the correct Values. However, when i then concatenate the two bytes i get something different from what I expect. For example, 2557 ---> 0x09FD
So HiByte = 0x09, LoByte = 0xFD.
When displayed individually, they are correct. And when I shift the upper byte 0x09 8 places the left and display i get 2304 which is correct. But when i OR the LoByte then there is a problem.
Note: HiByte = I2Cread(0x00), LoByte = I2Cread(0x01).

Any help would be greatly appreciated.
Thanks
 

So HiByte = 0x09, LoByte = 0xFD.
When displayed individually, they are correct. And when I shift the upper byte 0x09 8 places the left and display i get 2304 which is correct. But when i OR the LoByte then there is a problem.

You do not OR the LoByte.You shift the CardAmount by 8 bits,so that it can read the LoByte,and return the value CardAmount.Make CardAmount to be 16 bits and remove an extra incomplete bracket in the second function,to get this code.
Code:
unsigned int ReadSmartCard()
{
	CardAmount = I2Cread(0x00);
	CardAmount <<= 8;
	CardAmount |= I2Cread(0x01);
	return CardAmount;
}
 

When I implemented the code code you suggested, it is only displaying the Low Byte. So for the 2557, it is displaying 253. Any further help?
 

Hi All,
Thanks for all your efforts.
My colleague got the problem resolved by inserting a small delay between the two write statements:
Code:
void WriteToSmartCard(unsigned int Value)
{	
	unsigned char HiByte, LoByte;
	LoByte = Value; // Assign the 8-LSb to the variable Addres/Value >>= 8; // Shift the 8-LSB out of the variable Value
	Value >>= 8;
	HiByte = Value; // Assign the remaining 8-MSb to the variable AddressHi
	I2Cwrite((0x00), HiByte);
	I2Cwrite((0x01), LoByte);
}

Thanks.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…