Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

pn532 with bascom

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 71

poland.gif
PostPosted: Sat Sep 01, 2018 7:22 pm    Post subject: pn532 with bascom Reply with quote

I did today reading rf-cards in pn532, here the link https://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=90_93&products_id=2242. This device is connected with Arduino Uno by i2c protocoll.
Perhaps the code isn't very beautiful but works (at least for me) Wink All needed telegrams (frames) I've found in youtube so I only copied them into my program. I would appreciate if someone will add a unit or code to create the checksum form telegrams. This program is written for i2c. Have a fun;)


Code:

$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 115200                                              ' use baud rate
$hwstack = 80                                               ' default use 32 for the hardware stack
$swstack = 80                                               ' default use 10 for the SW stack
$framesize = 90                                             ' default use 40 for the frame space


Dim M As Word

Dim Zapyt(9) As Byte
Dim ZapytID(11) As Byte    'inListPassiveTargets - read ID RF-cards
Dim ZapytEx(12) As Byte
Dim ZapytSAM(14) As Byte
Dim ZapytStat(9) As Byte
Dim ZapytRel(10) as Byte   'inRelease - The goal of this command is to release the target(s) Tg

Dim Wake_up(4) As Byte
Dim dane(21) As Byte
Dim daneID(28) As Byte
Dim dane_ack(6) As Byte

Dim addr_w as byte
Dim addr_r as byte


Wake_up(1) = &H55
Wake_up(2) = &H55
Wake_up(3) = &H00
Wake_up(4) = &H00


Zapyt(1) = &H00
Zapyt(2) = &H00
Zapyt(3) = &HFF
Zapyt(4) = &H02
Zapyt(5) = &HFE
Zapyt(6) = &HD4
Zapyt(7) = &H02
Zapyt(8) = &H2A
Zapyt(9) = &H00


ZapytRel(1) = &H00
ZapytRel(2) = &H00
ZapytRel(3) = &HFF
ZapytRel(4) = &H03
ZapytRel(5) = &HFD
ZapytRel(6) = &HD4
ZapytRel(7) = &H52
ZapytRel(8) = &H01
ZapytRel(9) = &HD9
ZapytRel(10) = &H00


ZapytID(1) = &H00
ZapytID(2) = &H00
ZapytID(3) = &HFF
ZapytID(4) = &H04
ZapytID(5) = &HFC
ZapytID(6) = &HD4
ZapytID(7) = &H4A
ZapytID(8) = &H01
ZapytID(9) = &H00
ZapytID(10) = &HE1
ZapytID(11) = &H00


ZapytStat(1) = &H00
ZapytStat(2) = &H00
ZapytStat(3) = &HFF
ZapytStat(4) = &H02
ZapytStat(5) = &HFE
ZapytStat(6) = &HD4
ZapytStat(7) = &H04
ZapytStat(8) = &H28
ZapytStat(9) = &H00


ZapytEx(1) = &H00
ZapytEx(2) = &H00
ZapytEx(3) = &HFF
ZapytEx(4) = &H05
ZapytEx(5) = &HFB
ZapytEx(6) = &HD4
ZapytEx(7) = &H40
ZapytEx(8) = &H01
ZapytEx(9) = &H30
ZapytEx(10) = &H00
ZapytEx(11) = &HBB
ZapytEx(12) = &H00


ZapytSAM(1) = &H55
ZapytSAM(2) = &H55
ZapytSAM(3) = &H00
ZapytSAM(4) = &H00
ZapytSAM(5) = &H00
ZapytSAM(6) = &H00
ZapytSAM(7) = &HFF
ZapytSAM(8) = &H03
ZapytSAM(9) = &HFD
ZapytSAM(10) = &HD4
ZapytSAM(11) = &H14
ZapytSAM(12) = &H01
ZapytSAM(13) = &H17
ZapytSAM(14) = &H00


'I2C
Config I2cdelay = 10
Config Twi = 100000
Config Scl = Portc.5
Config Sda = Portc.4

$lib "I2C_TWI.LBX"

addr_w=&H48
addr_r=&H49
I2cinit


print "Reading RF cards. Put over PN532 a card.."
print



 'SAM + WAKEUP!
 I2cstart
  I2cwbyte addr_w

  for m=1 to 14
    I2cwbyte zapytSAM(m)
  next m

  I2cstop

 waitms 500

 'ACK
 I2cstart
  I2cwbyte addr_r

  for m=1 to 5
   I2crbyte Dane_ack(m) , Ack
  next

  I2crbyte Dane_ack(6) , NAck
 I2cstop


' Print "ACK "; Hex(dane_ack(1));".";Hex(dane_ack(2));".";Hex(dane_ack(3));".";Hex(dane_ack(4));".";Hex(dane_ack(5));".";Hex(dane_ack(6))


 I2cstart
  I2cwbyte addr_r

  for m=1 to 8
   I2crbyte Dane(m) , Ack
  next

  I2crbyte Dane(9) , NAck
 I2cstop

  waitms 100

'--------------------------------------------------------------------------


Do

 I2cstart
  I2cwbyte addr_w

  for m=1 to 11
    I2cwbyte zapytID(m)
  next m

 I2cstop


 waitms 50


 I2cstart
  I2cwbyte addr_r

  for m=1 to 27
   I2crbyte DaneID(m) , Ack
  next

  I2crbyte DaneID(28) , NAck
 I2cstop


 'byte nr 12 changes when the rf-card is put over pn532
 'so I check the state of this byte

 if hex(daneID(12))<>"00" then
 I2cstart
  I2cwbyte addr_w

  for m=1 to 10
    I2cwbyte zapytRel(m)
  next m

  I2cstop

  Print "FOUND : " ;Hex(daneID(14));".";Hex(daneID(15));".";Hex(daneID(16));".";Hex(daneID(17));".";Hex(daneID(18));".";Hex(daneID(19));".";Hex(daneID(20))


 end if


 waitms 600


loop

End
 


(BASCOM-AVR version : 2.0.8.0 , Latest : 2.0.8.1 )
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Sun Sep 02, 2018 12:01 am    Post subject: Reply with quote

Bascom already has 6 types of CRC built in what is needed?

Regards Paul
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 71

poland.gif
PostPosted: Sun Sep 02, 2018 10:00 am    Post subject: Reply with quote

Hi,

It shames me to say, but I do not want to think about it. The point is (from a pn532 manual):

Quote:

- LEN 1 byte indicating the number of bytes in the data field
(TFI and PD0 to PDn),
- LCS 1 Packet Length Checksum LCS byte that satisfies the relation:
Lower byte of [LEN + LCS] = 0x00,

- TFI 1 byte frame identifier, the value of this byte depends
on the way of the message:
-- D4h in case of a frame from the host controller to the PN532,
-- D5h in case of a frame from the PN532 to the host controller.
- DATA LEN-1 bytes of Packet Data Information
The first byte PD0 is the Command Code,
- DCS 1 Data Checksum DCS byte that satisfies the relation:
Lower byte of [TFI + PD0 + PD1 + + PDn + DCS] = 0x00,



Regards,
Martin
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5093
Location: Holland

blank.gif
PostPosted: Sat Sep 08, 2018 8:31 pm    Post subject: Reply with quote

Thank you for sharing your code.

you can calculate LCS by adding value to the LEN till it becomes 0.
So if LEN is 200, and we add 55 we get 255, if we add another 1, the byte overflow into 0.
So the 'forumula' is LCS=256-LEN
That is, that is how i read it.

_________________
Mark
Back to top
View user's profile Visit poster's website
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 71

poland.gif
PostPosted: Sat Sep 08, 2018 8:33 pm    Post subject: Reply with quote

I did simply program to collect datas from pn532. Under this link https://www.youtube.com/watch?v=vNiot0elisQ you may see it.

Regards
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5093
Location: Holland

blank.gif
PostPosted: Sat Sep 08, 2018 8:45 pm    Post subject: Reply with quote

Thanks for the video Smile
It seems to work excellent. Well done.

_________________
Mark
Back to top
View user's profile Visit poster's website
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 71

poland.gif
PostPosted: Sat Sep 08, 2018 9:04 pm    Post subject: Reply with quote

Thanks Smile
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum