Shop OBEX P1 Docs P2 Docs Learn Events
Checking for a smart card insert and correct insertion of the card. — Parallax Forums

Checking for a smart card insert and correct insertion of the card.

ftj405ftj405 Posts: 8
edited 2011-01-21 21:29 in BASIC Stamp
I modified the sample code from the smart card reader from parallax.
I dont have an LCD display yet so use the debug for the card responses as well as red and green leds.
First it waits for a card to be inserted, then checks to see if the card is inserted correctly by trying to read a byte from the card.




' {$STAMP BS2}
' {$PBASIC 2.5}


'
[ I/O Definitions ]

IO PIN 3 ' Serial Data IO
CD PIN 2 ' Card Detect
CLK PIN 1 ' Serial Data Clock

'
[ Constants ]

Ack CON 0 ' Acknowledge - Successful data transfer
NAck CON 1 ' Acknowledge - Unsuccessful data transfer

'
[ Variables ]

index VAR Word ' Variable space for FOR..NEXT loop
command VAR Byte ' Read/Write designation
wData VAR Byte ' Write data / also used for max count when reading
rData VAR Byte ' Read data

I2CAck VAR Bit ' Acknowledge bit
temp VAR Byte ' Variable space
address VAR Word ' Current address loaction
page VAR Nib
pIndex VAR Nib ' Current

'
[ Serial Vars ]



'DIRS = $00FF
CD = 0



Main:
GOSUB POLLFORCARD
GOSUB CheckDevice


END

'
[ Initialization ]
' Declares PSC and waits for a smart card to be inserted
POLLFORCARD:
DEBUG CLS
HIGH 15 'led indicator
DO ' Check for smart card
DEBUG HOME, "No card inserted!", CR, LF
LOOP UNTIL(CD = 1)
'DEBUG HOME, "Card Inserted!", CLREOL, LF
LOW 15 'led indicator
RETURN

CheckDevice:
' I2C start bit sequence
GOSUB DeviceStart ' Call start condition
temp = %10100000 ' Device Address %1010AAA(W)
command = address.HIGHBYTE << 1
command = command & %00001110 ' Device Address %1010AAA(R) and ROM Address
temp = temp | command ' Add the ROM address into the Command
GOSUB SendOut
IF (I2CAck=NAck) THEN
GOTO InvalidInsert
ELSE
DEBUG LF, CR, "System Ready", CR, LF
RETURN
ENDIF

ExitProgram:
END





'
[ Subroutine - Device_Addressing ]
' Begins data transmission & sends address of requested Slave devices
Device_Addressing:

GOSUB DeviceStart ' Call start condition
temp = %10100000 ' Device Address %1010AAA(W)
command = address.HIGHBYTE << 1
command = command & %00001110 ' Device Address %1010AAA(R) and ROM 'Address
temp = temp | command ' Add the ROM address into the Command
GOSUB SendOut
IF (I2CAck=NAck) THEN Device_Addressing ' No acknowledge, send aagin

Send_Address:

temp = address ' Read current address
GOSUB SendOut
IF (I2CAck=NAck) THEN Send_Address ' No acknowledge, send again
RETURN



'
[ Subroutine - DeviceStart ]
DeviceStart: ' I2C start bit sequence
INPUT IO
INPUT CLK
LOW IO
Clock_hold:
DO : LOOP UNTIL(CLK=1)
RETURN

'
[ Subroutine - DeviceStop ]
DeviceStop: ' I2C stop bit sequence
LOW IO
INPUT CLK
INPUT IO
RETURN

'
[ Subroutine - SendOut ]
SendOut: ' Send out a byte and get acknowledge
SHIFTOUT IO, CLK, MSBFIRST, [Temp\8]
SHIFTIN IO, CLK, MSBPRE, [I2CAck\1]
RETURN

'
Receive a Byte
Receive_NAck: ' While recieving byte in random mode, the device require no 'Acknowledge
I2CAck = NAck

Receive:
SHIFTIN IO, CLK, MSBPRE, [Temp\8] 'Get a byte of data and send out a acknowledge
SHIFTOUT IO, CLK, MSBFIRST, [I2CAck\1]
RETURN

InvalidInsert:
GOSUB DeviceStop
HIGH 4 'Turn on Red LED
PAUSE 1000
SEROUT 16, 84, 10, [LF, CR, "Card Inserted incorrectly. Remove and Try Again", LF, CR]
LOW 4 'turn off Red LED
GOTO ExitProgram
Sign In or Register to comment.