• AVR Freaks

SPI Library or Sample code

Page: 12 > Showing page 1 of 2
Author
naeem1234
Super Member
  • Total Posts : 450
  • Reward points : 0
  • Joined: 2015/02/19 06:39:28
  • Location: 0
  • Status: offline
2018/01/30 13:41:07 (permalink)
0

SPI Library or Sample code

Hello everyone
 
My chip is PIC18F46K22 and I am using XC8 compiler.
I have to interface my chip with RC522 RFID module which communicates on SPI interface. I have never used SPI before but I am trying to understand it. I wonder if there is any library to read and write data on SPI port?
 
Is there is any sample code available for SPI or RC522?
 
Your help is highly appreciated.
#1

36 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1317
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 13:51:35 (permalink)
    0
    You can use MPLAB Code Configurator for your SPI library functions

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #2
    naeem1234
    Super Member
    • Total Posts : 450
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 14:00:13 (permalink)
    0
    katela
    You can use MPLAB Code Configurator for your SPI library functions




    I have not used MCC before. Can you tell me a good starting point to learn how to use MCC?
    #3
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 14:22:42 (permalink)
    0
    I wouldn't use MCC myself, but I think this is where Microchip would like you to start:
    http://microchipdeveloper.com/mcc:start
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #4
    katela
    Super Member
    • Total Posts : 1317
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 14:36:15 (permalink)
    0
    naeem1234
     
    I have not used MCC before. Can you tell me a good starting point to learn how to use MCC?


    You can start here: MCC

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #5
    naeem1234
    Super Member
    • Total Posts : 450
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 15:42:49 (permalink)
    0
    qɥb
    I wouldn't use MCC myself..




    Can you tell me where to find a library for SPI port or a sample code for it?
    #6
    katela
    Super Member
    • Total Posts : 1317
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 15:47:56 (permalink)
    0
    naeem1234
     
    Can you tell me where to find a library for SPI port or a sample code for it?


    You have been shown two options to start with, did you look at them?

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #7
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 16:09:26 (permalink)
    0
    naeem1234
    qɥb
    I wouldn't use MCC myself..


    Can you tell me where to find a library for SPI port or a sample code for it?

    Personally, I think it takes LONGER to try to work out how to use someone else's code, than to just read the datasheet for the PIC and the device, and write the code yourself.
    This thread is talking about a PIC16F1xxx chip, but the SPI peripheral is almost identical to a PIC18F chip.
    You should get an idea of the various ways to do it in the thread...
    http://www.microchip.com/forums/m1035062.aspx
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #8
    brownt
    Super Member
    • Total Posts : 296
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 16:37:20 (permalink)
    0
    If you don't understand SPI well and do not have a lot of experience with it, then using preconfigured functions is the quickest way to get it working. MCC (Microchip Code Configurator) is an add-on program to MPLAB. You download MCC and install it. Then you run it from within MPLAB. It is a Graphical User Unterface (GUI) where you can click on this and that to setup peripherals such as SPI. Unfortunately it does create a lot of code that you need to study to work out how to use the created functions. It also takes time to work out how to use the GUI, but it would still be quicker than creating the functions yourself if you have limited knowledge and experience. Also, once you have learned to use it, it makes future projects simpler and quicker.
     
    There is also the old peripheral libraries http://www.microchip.com/mplab/compilers. You would download it and install it. There are pre-made functions there that you use. But it is old and so you would probably need to modify the functions to suit your microcontroller and I am not sure that it would be any quicker than MCC.
     
    MCC is the way to go, if you can't write your own SPI code.
    #9
    Aussie Susan
    Super Member
    • Total Posts : 3607
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 19:01:13 (permalink)
    +3 (3)
    @brownt - I'm not sure that I would agree to your statement. For the IP or USB stacks I certainly agree that you need to use the available libraries, but for the simple peripherals such as UARTs, SPI amd PWM, then learning how to read the Microchip data sheets and understanding what is happening will make life much easier for you later on.
    For example, you really need to understand what a slave SPI device is expecting and will send (e.g sampling in relation to the SCK transitions) if you want to set up the master to correctly interact with it. No library will do that for you (even MCC).
    Also the SPI exchanges are really at the bottom of the stack. TO interact with an device you need to know what commands to send, what data etc. and again no library will help you with that.
    On the other hand, if the OP is just starting out then a 'flash a LED' project is better until you know you can get a program running (the learning curve is not trivial for many) and then move to something like this once you knwo you can successfully program the device and have it run.
    Susan
    #10
    brownt
    Super Member
    • Total Posts : 296
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/01/30 19:20:24 (permalink)
    +1 (1)
    my personal experience is different from that.
    #11
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/01/31 00:28:13 (permalink)
    0
    https://www.microchip.com/wwwproducts/en/SST26VF016B
     
    Open this URL.Go to documentation and then to SST26VF016B Software Driver
     
    #12
    naeem1234
    Super Member
    • Total Posts : 450
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/01/31 08:59:30 (permalink)
    0
    Thanks a lot for everyone's input.
     
    I wonder if its possible to compare the 2 peripherals namely UART Serial port and SPI port w.r.t their programing to send receive simple data?
     
     
    Which one is easy to program and use in XC8?
    What are some of the things that make one of the above 2 ports difficult to program and use in XC8?
    post edited by naeem1234 - 2018/01/31 10:26:29
    #13
    DavidBLit
    Super Member
    • Total Posts : 1574
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: SPI Library or Sample code 2018/01/31 11:04:08 (permalink)
    +3 (3)
    naeem1234
    ...
    What are some of the things that make one of the above 2 ports difficult to program and use in XC8?

    Refusal to study the Data Sheet and Errata.

    Yeah, "//Code and stuff".
    #14
    Aussie Susan
    Super Member
    • Total Posts : 3607
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: SPI Library or Sample code 2018/01/31 18:39:13 (permalink)
    +2 (2)
    It is not an issue of which is easier to program - they have completely different use cases.
    The UART can send and receive characters asynchronously and independently whereas SPI is an exchange process so sending and receiving values MUST occur at the same time. 
    It also depends on what you need to communicate with and how fast the interactions need to occur.
    Susan
    #15
    naeem1234
    Super Member
    • Total Posts : 450
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/02/01 06:37:21 (permalink)
    0
    Aussie Susan
    ... SPI is an exchange process so sending and receiving values MUST occur at the same time. 
     

     I am trying to understand this exchange process but not very clear about it so far.
    The chip datasheet provides 3 scenarios as follows:
     
    • Master sends useful data and slave sends dummy data.
    • Master sends useful data and slave sends useful data.
    • Master sends dummy data and slave sends useful data.
     
    Why do we need to send data from Master and Slave at the same time? Is it some sort of data acknowledgement process?
    #16
    mbrowning
    Just a Member
    • Total Posts : 1455
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: SPI Library or Sample code 2018/02/01 06:51:59 (permalink)
    +1 (1)
    SPI has no intelligence like a UART. It's just two shift registers (one in master, one in slave) with a clock and an enable. Minimal hardware for simple peripherals, but also faster as a result of it's simplicity. PIC puts data in its shift register (in parallel) and at each clock the bits are shifted serially one by one to the output. Simultaneously incoming bits (from the other side's shift register) are shifted serially into the shift register input and when the shifting is done, read out in parallel. Master/Slave just determines who has control of the clock and enable (chip select) lines.
     
    So every time data is shifted out, data is shifted in simultaneously. All the bits and controls in SPI are just to setup the transfer and let software know what the hardware has done.
     
    Thus the master has to send a byte (even if it's a "dummy" byte) to get a byte. And the slave has to put a byte into its shift register before the master sends that byte (if it needs to send data).
     
    It's pretty clear if you look at the block diagram in the datasheet. Maybe it's clear to me because as part of my education I studied digital logic, but it's about as basic a piece of digital logic as there is.
    post edited by mbrowning - 2018/02/01 06:56:01

    Oh well - there's always next year
    #17
    JorgeF
    Super Member
    • Total Posts : 3340
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: SPI Library or Sample code 2018/02/01 08:11:33 (permalink)
    +1 (1)
    Hi
    naeem1234
    I am trying to understand this exchange process but not very clear about it so far.

    naeem1234
    Why do we need to send data from Master and Slave at the same time? Is it some sort of data acknowledgement process?

    This is already covered in post #17 by mbrowning.
     
    Trying to clarify the scenarios with conceptual examples
    naeem1234
    • Master sends useful data and slave sends dummy data.
    • Master sends dummy data and slave sends useful data.

    -- Half duplex --
    * start with an idle slave *
    1 - Master sends a command and receives dummy data (idle line) from the slave.
    2 - Master keeps sending "dummy data" (idle line) to clock the system until it receives the reply from the slave.
    3 - back to 1 or proceed to 4
    4 - END
     
    naeem1234
    • Master sends useful data and slave sends dummy data.
    • Master sends useful data and slave sends useful data.
    • Master sends dummy data and slave sends useful data. 
     
    -- Full duplex --
    Available only with slaves that can pipe (buffer) commands and repplies.
    * starting with an idle slave *
    1 - Master sends a command and receives dummy data (idle line) from the slave.
    2 - Master sends a new command while receiving the reply to the previous.
    3 - Back to 1 or proceed to 4
    4 - Master keeps sending "dummy data" (idle line) to clock the system until it receives the reply to the last command sent.
    5 - END
     
     
    HIH
     
     
    Best regards
    Jorge
     
    post edited by JorgeF - 2018/02/01 08:12:56
    #18
    naeem1234
    Super Member
    • Total Posts : 450
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: SPI Library or Sample code 2018/02/02 19:29:50 (permalink)
    0
    Aussie Susan
    It is not an issue of which is easier to program - they have completely different use cases.
    The UART can send and receive characters asynchronously and independently whereas SPI is an exchange process so sending and receiving values MUST occur at the same time. 
    It also depends on what you need to communicate with and how fast the interactions need to occur.
    Susan




     
    The RFID module that i am trying to integrate with my PIC18F46K22 is RFID-RC522. It has 3 host interfaces available, SPI(default setting on board), UART and I2C. The UART and I2C can be selected by cutting tracks on the PCB using info from page 9 of the MFRC522 datasheet.
     
    So i was considering to use the UART interface instead of the SPI default interface. As i have some hands on knowledge of UART so if i could find some sample code for RFID-RC522 UART communication i can use that. But unfortunately i could not find that so far and all sample codes are for SPI. 
     
    Can anyone tell me if the UART host interface is possible to use or not for the RFID-RC522 module?
    #19
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: SPI Library or Sample code 2018/02/03 05:16:55 (permalink)
    +1 (1)
    naeem1234
     
    The RFID module that i am trying to integrate with my PIC18F46K22 is RFID-RC522. It has 3 host interfaces available, SPI(default setting on board), UART and I2C. The UART and I2C can be selected by cutting tracks on the PCB using info from page 9 of the MFRC522 datasheet.
    ...
    Can anyone tell me if the UART host interface is possible to use or not for the RFID-RC522 module?

    Did you just ask a question that you've already said it is supported?
     

    So i was considering to use the UART interface instead of the SPI default interface. As i have some hands on knowledge of UART so if i could find some sample code for RFID-RC522 UART communication i can use that. But unfortunately i could not find that so far and all sample codes are for SPI.

    Get your hands dirty and read the datasheet, instead of relying on library and sample code. You'll learn a LOT more that way.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5