Stap 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
LiquidCrystal lcd(0);
SPI Bus staat definities
#define SPI_RDY 0xF0 / / klaar
#define spibusy 0xaa / / druk
#define spiread 0xbb / / schrijven
#define spiwrite 0xcc / / lezen
#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 succes 0
#define mislukking 1
UCHAR g_cReceBuf [10];
UCHAR [ComPWRdwn] = {0x02, 0x03};
UCHAR [ComAutoSearchCard] = {0x03, 0x13, 0x01};
UCHAR [ComGetCardSn] = {0x02, 0x20};
UCHAR [ComHaltCard] = {0x02, 0x12};
VOID 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 ik = 8;
unsigned char cRecByte;
terwijl (i)--
{
cRecByte * = 2;
SCL_0;
delayMicroseconds(2);
Als ((cSendByte & amp; 0x80) == 0x80) MOSI_1;
anders MOSI_0;
cSendByte * = 2;
cRecByte | = (niet-ondertekende 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;
voor (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] = 0x01;
voor (cCnt = 0 cCnt & lt; cP [0]; cCnt ++)
{
cP [cCnt] = SPIRWByte(0xFF);
cCheckSum ^ = cP [cCnt];
Als (cP [0] & gt; 32)
{
NSS_1;
Terug falen;
}
}
cP [cCnt] = SPIRWByte(0xFF);
NSS_1;
Als (cCheckSum == cP[cCnt])
{
terug van succes;
}
}
Terug falen;
}
unsigned char SPI_Write (unsigned char * cP)
{
unsigned char i, cStatus;
unsigned char cCheckSum = 0;
NSS_0;
cStatus=SPIRWByte(spiwrite);
voor (ik = 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; Lees kaart id: & quot;);
lcd.setBacklight(HIGH);
port_init();
}
void loop
{
UCHAR cStatus, i;
UCHAR * cPa;
terwijl (1)
{
lcd.setCursor(0,0);
LCD.Print (& quot; Lees kaart id: & quot;);
lcd.setCursor(0,1);
LCD.Print (& quot; geen kaart, wachtende. & 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; ID-kaart: & quot;);
voor (ik = 2 i & lt; 6; i ++)
LCD.Print(g_cReceBuf[i],hex);
terwijl (!. SIG);
}
}
}
U kunt de code hier downloaden: HY502B