Use Python to talk to another Microcontroller via SPI

MENG XI

Mar 18, 2020
52
Joined
Mar 18, 2020
Messages
52
SPI is a highly important peripheral for microcontroller as it allows for high speed communication over short range, here is how we use python to send msg to another microcontroller over SPI

材料准备

  • Ameba x 1, Arduino UNO x 1

范例说明

SPI是一种快速且强大的通讯协议,并通常在微处理器中被用来接受传感器的数据或输出图像讯号。在这个示例中将示范ameba如何透过MicroPython以从属模式接收数据。

在通讯连接建立之前,需要先将以下代码烧录到Arduino UNO。




rtc = RTC()



///////////////////////



// SPI Master Write //



///////////////////////



 



#include



 



void setup (void) {



Serial.begin(115200); //set baud rate to 115200 for usart



digitalWrite(SS, HIGH); // disable Slave Select



SPI.begin ();



}



 



void loop (void) {



char c;



digitalWrite(SS, LOW); // enable Slave Select



// send test string



for (const char * p = "Hello, world!\r" ; c = *p; p++) {



SPI.transfer(c);



Serial.print(c);



}



Serial.println();



digitalWrite(SS, HIGH); // disable Slave Select



delay(2000);



}




Arduino UNO将以如下图所示的连接方式和Ameba连接,我们使用第“0”组SPI作为从机, 并将Arduino UNO当作SPI的主机。

1.jpg


然后复制以下代码并粘贴到REPL的粘贴模式窗口,并等待代码生效。



 




from machine import SPI



s1= SPI(0 , mode = SPI.SLAVE)



for i in range(14):



chr(s1.read())



 
Last edited by a moderator:
Top