Hi
I bougth a smartcardreader and some cards, but i dont know how to program it... I have a arduino duemilanove, but i found some code to another microcontroller, is there someone there can help me rewrite it to "arduino language"?
' SC4442_Demo.bs2
' {$STAMP BS2}
' {$PBASIC 2.5}
' This program demonstrates the basics of reading and writing data to the
' IS23SC4442 smart card. Since this card has a Programmable Security Code
' (PSC), the user is required to enter a password before writing data to
' the card.
' This program simply declares the password as a constant (default $FFFFFF)
' and when 'authenticate' is selected, it compares that password to the one
' on this card. This MUST be selected before the user is able to write
' data to the card.
' -----[ I/O Definitions ]-------------------------------------------------
IO PIN 3 ' Serial Data IO
CD PIN 2 ' Card Detect
CLK PIN 1 ' Serial Data Clock
RST PIN 0 ' Reset
' -----[ Constants ]-------------------------------------------------------
ReadMain CON $30 ' Read main memory
ReadProtection CON $34 ' Read protection bit
ReadSecurity CON $31 ' Read PSC
UpdateMain CON $38 ' Update main memory
UpdateSecurity CON $39 ' Update PSC
UpdateProtection CON $3C ' Update Protection bits
CompareData CON $33 ' Compare Verification Data
' Programmable Security Code - REMEMBER TO CHANGE IF PASSWORD IS UPDATED!!
Password1 CON $FF ' 1st byte of PSC
Password2 CON $FF ' 2nd byte of PSC
Password3 CON $FF ' 3rd byte of PSC
' -----[ Variables ]-------------------------------------------------------
index VAR Byte ' Variable space for FOR...NEXT loop
command VAR Byte ' Command format
dat VAR Byte ' Variable space for data
address VAR Byte ' Address location in memory
PSW1 VAR Byte ' Variable space for PSC comparison
PSW2 VAR Byte
PSW3 VAR Byte
temp VAR Byte
errCounter VAR Byte ' Error counter for PSC
choice VAR Nib ' Main menu selection
keyboard VAR Bit ' Input from keyboard
' -----[ Initialization ]--------------------------------------------------
' Declares PSC and waits for a smart card to be inserted
Program_Start:
DEBUG CLS
DO ' Check for smart card,
DEBUG HOME, "No card inserted!" ' loop until card detected
LOOP UNTIL(CD = 1)
GOSUB Reset ' Reset smart card
' -----[ Program Code ]----------------------------------------------------
Main:
DO
DEBUG HOME, "Remember to authenticate before writing data to the card!", CR,
CR, "Select:", CR,
" 1) Read Data from the card", CR,
" 2) Authenticate", CR,
" 3) Write Data to the card", CR, CR,
"Selection: ", CLREOL
DEBUGIN DEC Choice
SELECT Choice
CASE 1: GOSUB Read_Data_From_Card
CASE 2: GOSUB Authenticate
CASE 3: GOSUB Write_Data_To_Card
ENDSELECT
LOOP
' -----[ Subroutine - Read_Data_From_Card ]--------------------------------
' Reads all data from memory
Read_Data_From_Card:
' Display memory map heading
DEBUG HOME, 0, 13, REP "-"\36, "[ DATA ]", REP "-"\36,
CR, "Addr. *0 *1 *2 *3 *4 *5 *6 *7 *8 *9 *A *B *C *D *E *F",
CR, REP "_"\84
address = 0 ' Set address location to 0
command = ReadMain ' Set command format to read main memory
GOSUB Read_Main ' Call the Read_Main subroutine
DEBUG CR, REP "_"\84, CR ' Finish memory map "box"
DEBUG "Press any key to return to menu..."
DEBUGIN keyboard ' Receive input from user
DEBUG CLS
RETURN
' -----[ Subroutine - Authenticate ]---------------------------------------
' Compares password delcared in 'Constants' to PSC in memory
Authenticate:
DEBUG CLS, "Authenticating...", CR
GOSUB Compare
SELECT errCounter
CASE %000: DEBUG HOME, "Card is locked.", CLREOL, CR, CLREOL
CASE %001: DEBUG HOME, "Invalid PSC, one try remaining."
CASE %011: DEBUG HOME, "Invalid PSC, two tries remaining."
CASE %111: DEBUG HOME, "PSC verified, you may now write data to the card."
ENDSELECT
DEBUG CR, CR, "Press any key to return to menu..."
DEBUGIN keyboard ' Receive input from user
DEBUG CLS
RETURN
' -----[ Subroutine - Write_Data_To_Card ]---------------------------------
' Writes data to specified address location
Write_Data_To_Card:
DEBUG CLS, "Write Data to Address", CR, REP "-"\25, CR, CR,
"Input address location (HEX): ", CLREOL
DEBUGIN HEX address ' Receive address location from user
DEBUG "Input data (HEX): ", CLREOL
DEBUGIN HEX Dat ' Receive data from user
command = UpdateMain ' Set command format to update main memory
GOSUB Update_Main ' Call the Update_Main subroutine
GOSUB Read_Data_From_Card ' Display memory map for verification
RETURN
' -----[ Subroutine - Read_Main ]------------------------------------------
' Call I2C protocol to read main memory
Read_Main:
GOSUB Send_Command
FOR index = 0 TO 255
IF (index//16 = 0) THEN DEBUG CR, HEX1 index/16, "* |"
GOSUB Recieve_Byte
DEBUG " ", HEX2 Temp, " "
NEXT
RETURN
' -----[ Subroutine - Update_Main ]----------------------------------------
' Calls I2C protocol to update main memory
Update_Main:
GOSUB Send_Command
GOSUB Process
RETURN