Jump to content
Electronics-Lab.com Community

Use Python to talk to another Microcontroller via SPI


Recommended Posts

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

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

 
from machine import SPI
s1= SPI(0 , mode = SPI.SLAVE)
for i in range(14):
chr(s1.read())
Link to comment
Share on other sites

  • MENG XI changed the title to Use Python to talk to another Microcontroller via SPI

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.