Shop OBEX P1 Docs P2 Docs Learn Events
SmartCard and BasicStamp — Parallax Forums

SmartCard and BasicStamp

ftj405ftj405 Posts: 8
edited 2010-10-13 20:14 in BASIC Stamp
I'm using a Parallax smart card and parallax smart card reader board with my bs2.

I turn on and off Status LEDs depending on the situation.

1. If the card is not present. (RED)
2. Card Inserted (Yellow)
3. Reading or writing will be GREEN.

the issue i have is that the CD (Card Detector) only detects if the card is present or inserted, not if its in the wrong way. So if you go to read the card and its inserted wrong it hangs.

I'm modifying some of the sample code i download. Once the card is installed correctly can i get a signal from the SCard CLK pin?

Sample code

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

And if its not installed correctly could I not just stop waiting for the CLK to signal?

Thanks Ahead of time

Comments

  • ftj405ftj405 Posts: 8
    edited 2010-10-13 20:14
    I found an answer to my own question.

    Since the orientation of the card needs to be checked upon a CardDetect, the bus will not be busy and the card should be able to send a quick acknowledgement. If the card is put in incorrectly no acknowledgement is received and the user is notified and the program ends.


    I copied and modified the code below and placed the call after the card detect.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    'ReadDataFromSCToSerialOut_1010
    'read/write data over serial line to and from smart card.
    ' TJ Treinen 09/11/2010
    ' SER_1.BS2
    '

    [ 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
    rData VAR Byte ' Read data

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

    '
    [ Serial Vars ]

    CHOICE VAR Byte(8)
    MENUITEM VAR Nib
    POS VAR Nib
    idx VAR Byte
    value VAR Byte

    'DIRS = $00FF

    POS = 0
    CD = 0

    Main:

    DO
    GOSUB POLLFORCARD
    GOSUB CheckDevice
    GOSUB Page_Choice


    LOOP
    GOTO ExitProgram

    '
    [ Initialization ]
    ' Declares PSC and waits for a smart card to be inserted
    POLLFORCARD:
    DEBUG CLS
    DO ' Check for smart card,
    DEBUG HOME, "No card inserted!", CR, LF
    LOOP UNTIL(CD = 1)
    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
    RETURN
    ENDIF

    ExitProgram:
    END


    <<
    other code not part of the explanation was here
    >

    '
    [ Subroutine - Page_Choice ]
    ' During 'read' selection, allows user to browse through each 16-byte page of data
    Page_Choice:
    DO
    DEBUG CR, "Select new page [1-8], or press [9] to return to menu: "
    DEBUGIN DEC page

    SELECT page
    CASE 1 TO 8: GOTO Read_Data
    CASE 9: RETURN
    CASE ELSE: DEBUG "Invalid Selection", CLREOL
    ENDSELECT
    LOOP
    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:
    I2CAck = NAck

    InvalidInsert:
    GOSUB DeviceStop
    LOW 13
    HIGH 15
    PAUSE 1000
    SEROUT 16, 84, 10, [LF, CR, "Card Inserted incorrectly. Remove and Try Again", LF, CR]
    LOW 15
    GOTO ExitProgram
Sign In or Register to comment.