请选择 进入手机版 | 继续访问电脑版
    查看: 518|回复: 2

    [经验分享] 基于ESP32 Arduino 的SPI程序赏析

    [复制链接]

    签到天数: 178 天

    [LV.7]化身百千

    发表于 2018-10-10 17:43:42 | 显示全部楼层 |阅读模式
    1. /* The ESP32 has four SPi buses, however as of right now only two of
    2. * them are available to use, HSPI and VSPI. Simply using the SPI API
    3. * as illustrated in Arduino examples will use HSPI, leaving VSPI unused.
    4. *
    5. * However if we simply intialise two instance of the SPI class for both
    6. * of these buses both can be used. However when just using these the Arduino
    7. * way only will actually be outputting at a time.
    8. *
    9. * Logic analyser capture is in the same folder as this example as
    10. * "multiple_bus_output.png"
    11. *
    12. * created 30/04/2018 by Alistair Symonds
    13. */
    14. #include <SPI.h>
    15. static const int spiClk = 1000000; // 1 MHz
    16. //uninitalised pointers to SPI objects
    17. SPIClass * vspi = NULL;
    18. SPIClass * hspi = NULL;
    19. void setup() {
    20. // 初始化 SPI 实例 VSPI、HSPI
    21. vspi = new SPIClass(VSPI);
    22. hspi = new SPIClass(HSPI);
    23. //clock miso mosi ss
    24. //使用默认 VSPI 引脚:SCLK = 18, MISO = 19, MOSI = 23, SS = 5
    25. vspi->begin();
    26. // alternatively route through GPIO pins of your choice
    27. //vspi->begin(0, 2, 4, 33); // SCLK, MISO, MOSI, SS
    28. //使用默认引脚初始化 HSPI
    29. //SCLK = 14, MISO = 12, MOSI = 13, SS = 15
    30. hspi->begin();
    31. //alternatively route through GPIO pins
    32. //hspi->begin(25, 26, 27, 32); //SCLK, MISO, MOSI, SS
    33. // 初始化 ss 片选引脚,默认为低电平
    34. pinMode(5, OUTPUT); //VSPI SS
    35. pinMode(15, OUTPUT); //HSPI SS
    36. }
    37. // the loop function runs over and over again until power down or reset
    38. void loop() {
    39. //use the SPI buses
    40. vspiCommand();
    41. hspiCommand();
    42. delay(100);
    43. }
    44. void vspiCommand() {
    45. // 模拟数据
    46. byte data = 0b01010101;
    47. // 启动 VSPI 传输
    48. vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
    49. digitalWrite(5, LOW);
    50. vspi->transfer(data);
    51. digitalWrite(5, HIGH);
    52. vspi->endTransaction();
    53. }
    54. void hspiCommand() {
    55. byte stuff = 0b11001100;
    56. hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
    57. digitalWrite(15, LOW);
    58. hspi->transfer(stuff);
    59. digitalWrite(15, HIGH);
    60. hspi->endTransaction();
    61. }
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    返回顶部