Étape 3: Code
#include & quot;core.h & quot ;
#include & quot ; Wire.h & quot ;
#include & quot ; LiquidCrystal.h & quot ;
#include & quot;string.h & quot ;
#define uchar unsigned char
#define uint unsigned int
Innovente lcd(0) ;
Définitions d’état de Bus SPI
#define SPI_RDY 0xF0 / / prêt
#define spibusy 0xaa / / occupé
#define spiread 0xbb / / écriture
#define spiwrite 0xcc / / lire
#define SCL_0 digitalWrite(13,LOW)
#define SCL_1 digitalWrite(13,HIGH)
#define MISO digitalRead(12)
#define MOSI_0 digitalWrite(11,LOW)
#define MOSI_1 digitalWrite(11,HIGH)
#define NSS_0 digitalWrite(10,LOW)
#define NSS_1 digitalWrite(10,HIGH)
#define SIG digitalRead(9)
#define succès 0
#define échec 1
g_cReceBuf UCHAR [10] ;
UCHAR [] ComPWRdwn = {0 x 02, 0 x 03} ;
UCHAR [] ComAutoSearchCard = {0 x 03, 0 x 13, 0 x 01} ;
UCHAR [] ComGetCardSn = {0 x 02, 0 x 20} ;
UCHAR [] ComHaltCard = {0 x 02, 0 x 12} ;
Sub port_init()
{
pinMode(13,OUTPUT) ;
pinMode(12,INPUT) ;
pinMode(11,OUTPUT) ;
pinMode(10,OUTPUT) ;
pinMode(9,INPUT) ;
}
unsigned char SPIRWByte (unsigned char cSendByte)
{
unsigned char j’ai = 8 ;
unsigned char cRecByte ;
tandis que (i)--
{
cRecByte * = 2 ;
SCL_0 ;
delayMicroseconds(2) ;
Si ((cSendByte & amp; 0 x 80) == 0 x 80) MOSI_1 ;
d’autre MOSI_0 ;
cSendByte * = 2 ;
cRecByte | = (unsigned char)(MISO) ;
SCL_1 ;
delayMicroseconds(2) ;
}
SCL_1 ;
Return cRecByte ;
}
unsigned char spi_cStatus(void)
{
unsigned char cStatus ;
NSS_0 ;
cStatus=SPIRWByte(spibusy) ;
cStatus=SPIRWByte(0xFF) ;
NSS_1 ;
Return cStatus ;
}
unsigned char SPI_Read (unsigned char * cP)
{
unsigned char cCnt, cStatus ;
unsigned char cCheckSum = 0 ;
pour (cCnt = 0 cCnt & lt ; 100 ; cCnt ++)
{
cStatus=spi_cStatus() ;
if(cStatus==0xF0)
{
cCnt = 253 ;
}
Delay(10) ;
}
if(cCnt==254)
{
NSS_0 ;
cCnt=SPIRWByte(spiread) ;
cP [0] = 0 x 01 ;
pour (cCnt = 0 cCnt & lt; cP [0]; cCnt ++)
{
cP [cCnt] = SPIRWByte(0xFF) ;
cCheckSum ^ = cP [cCnt] ;
Si (cP [0] & gt ; 32)
{
NSS_1 ;
retourne l’échec ;
}
}
cP [cCnt] = SPIRWByte(0xFF) ;
NSS_1 ;
Si (cCheckSum == cP[cCnt])
{
retourner un succès ;
}
}
retourne l’échec ;
}
unsigned char SPI_Write (unsigned char * cP)
{
unsigned char i, cStatus ;
unsigned char cCheckSum = 0 ;
NSS_0 ;
cStatus=SPIRWByte(spiwrite) ;
pour (i = 0; i & lt; cP [0]; i ++)
{
cCheckSum ^ = cP [i] ;
cStatus=SPIRWByte(cP[i]) ;
}
cStatus=SPIRWByte(cCheckSum) ;
NSS_1 ;
Return cStatus ;
}
void setup()
{
LCD.Begin (16, 2) ;
LCD.Clear() ;
LCD.Print (& quot ; lire la carte d’identité : & quot;) ;
lcd.setBacklight(HIGH) ;
port_init() ;
}
void loop()
{
UCHAR cStatus, i ;
UCHAR * ACP ;
while (1)
{
lcd.setCursor(0,0) ;
LCD.Print (& quot ; lire la carte d’identité : & quot;) ;
lcd.setCursor(0,1) ;
LCD.Print (& quot ; aucune carte, attente. & quot;) ;
if(SIG==Low)
{
Delay(100) ;
cPa = ComGetCardSn ;
SPI_Write(CPA) ;
Delay(100) ;
cStatus = SPI_Read(g_cReceBuf) ;
SPI_Write(ComHaltCard) ;
lcd.setCursor(0,1) ;
LCD.Print (& quot ; carte ID: & quot;) ;
pour (i = 2; & lt; 6; i ++)
LCD.Print(g_cReceBuf[i],hex) ;
while (!. SIG) ;
}
}
}
Vous pouvez télécharger le code ici : HY502B