I need an example of using SPI to receive data



  • Hello everyone! I need an example of using SPI to receive data. Lowj is the master and receives data from the slave. Can anyone provide the code that runs correctly.I have a code, but it can’t receive data, it returns all 0.

    var spi = require("spi");
    var s = new spi.SPI({mode: 0, clockHz: 20000, pinSCLK: 18, pinMISO: 19, pinMOSI: 23});
    let buf = Buffer.alloc(2);
    s.addCS(5);
    s.transfer(5, buf, function (err, data) {
        console.log(err);
        console.log(data.readUInt8(0));
        console.log(data.readUInt8(1));
    });
    


  • 2021-10-30 17-06-44 的屏幕截图.png

    In order to ensure the hardware connection is correct, I use micropython to read. micropython can indeed read the data correctly



  • Have you tried

    clockHz: 20 instead of clockHz: 20000
    

    since the frequency is set in kHz



  • Hi! @Frida
    After your prompt, I tested clockHz=20, but he did use Hz as the unit instead of kHz. I have a logic analyzer, and I can get an accurate 20kHz when I set clockHz=20000. And when setting clockHz=20 I get 156.625Hz.

    你好! @Frida
    在你的提示后我测试了clockHz=20,但他确实是以Hz为单位而不是kHz为单位。我有一个逻辑分析仪,我设置clockHz=20000时我能得到准确的20kHz。而设置clockHz=20时我得到156.625Hz.
    2021-11-01 08-15-38 的屏幕截图.png 2021-11-01 08-14-07 的屏幕截图.png

    Thank you for replying



  • I guess there is no hardware mode for 20 Hz and it is taken the nearest one. But 20 kHz is what you need, so that is fine.

    Can you post the Micropython code? Then I can quickly compare and see if there is a difference, or low.js has a bug.



  • Hi @neoniousTR

    from machine import SPI
    from machine import Pin
    cs = Pin(5,Pin.OUT,Pin.PULL_UP)
    vspi = SPI(2,baudrate=20000)
    cs.value(0)
    vspi.read(2)
    cs.value(1)
    

    This is the code I use, I only experiment with this code in the serial terminal in micropython

    Quick reference for the ESP32 https://docs.micropython.org/en/latest/esp32/quickref.html#hardware-spi-bus



  • Can you try HSPI (SCLK: 14, MISO: 12, MOSI: 13) ?

    If there is any success with HSPI, I know what the problem is and will explain.

    If not, I will have to start open the debugger.



  • @neoniousTR Still failed, only received 0



  • Hi!
    I want to ask if there is any solution to this problem.


Log in to reply