Stm32F2xx与arduino之间的I2C通信
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Stm32F2xx与arduino之间的I2C通信相关的知识,希望对你有一定的参考价值。
哥哥,能给点提示吗
参考技术A 串口通讯是靠DIO(直接输入/输出)和CLK1(bit时钟),CLK2(BYTE时钟)进行的比如arduino要传一段英文到stm32
arduino端:
通过shitOut指令传出,传出一次CLK2高电平脉冲一次
stm32端:
初始化引脚
循环检测DIO角是否有信号
有就记录,每次记录一次位移一次,以CLK1脉冲一次为一个跳变结束,以CLK2脉冲为一个字节结束
配上数组就可以实现字符串发送接受
python和arduino之间的串行通信
我正在研究物体跟踪机器人。我正在使用python和OpenCV来检测对象并将正确的数据发送到控制两个伺服电机的arduino。应发送到arduino的数据是伺服电机角度范围在0-180之间。我正在使用示例代码来理解python和arduino如何使用串行总线进行通信。当我发送一个数字时,arduino接收它并按预期工作,但是当我发送多个数字时没有任何反应。这是arduino代码:
#include <Servo.h>
int data;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(LED_BUILTIN, OUTPUT); //make the LED pin (13) as output
digitalWrite (LED_BUILTIN, LOW);
myservo.attach(9);
Serial.println("Hi!, I am Arduino");
}
void loop() {
while (Serial.available()){
//to receive more than one character
char buffer[] = {' ',' ',' ',' ',' ',' ',' '}; // Receive up to 7 bytes
while (!Serial.available()); // Wait for characters
Serial.readBytesUntil('n', buffer, 7);
data = atoi(buffer);
}
myservo.write(data);
}
这是python代码:
import serial
import time # Required to use delay functions
arduinoSerialData = serial.Serial('com14', 9600) # Create Serial port
object called arduinoSerialData
time.sleep(2) # wait for 2 secounds for the communication to get
established
print arduinoSerialData.readline() # read the serial data and print it as
line
print ("Enter 1 to turn ON LED and 0 to turn OFF LED")
while 1: # Do this forever
var = raw_input() # get input from user
print "you entered", var # print the intput for confirmation
arduinoSerialData.write(var)
readBytesUntil()中的'n'应为' n'。
readBytesUntil()在一个字节后终止,因为串行缓冲区将为空。如果希望readBytesUntil()读取整个串行缓冲区,请添加Serial.setTimeout(),默认为1000毫秒等待数据。这给了填充串行缓冲区的时间。 1秒是不必要的长,但readBytesUntil()将在找到' n'后终止。
while(!Serial.available());可以删除。
以上是关于Stm32F2xx与arduino之间的I2C通信的主要内容,如果未能解决你的问题,请参考以下文章
Arduino STM32F103C8T6 驱动ssd1306 I2C oled
STM32第七章: IIC(I2C Inter-Intergrated Circuit 集成电路总线) IIC时序图(IIC协议) I2C模拟时序STM32F4XX的IIC通信重载print
: IIC(I2C Inter-Intergrated Circuit 集成电路总线) IIC时序图(IIC协议) I2C模拟时序STM32F4XX的IIC通信重载print