c++ 如何在PC上通过USB与ATTiny 85/Digispark通信?

1sbrub3j  于 12个月前  发布在  Spark
关注(0)|答案(1)|浏览(147)

最近我尝试用ATTiny 85/Digispark制作一个简单的LED控制器。
我尝试使用DigiCDC lib来执行数据IO,但它在我的PC(win10 x64)上不起作用。
测试代码:

#include <DigiCDC.h>

void setup()
{
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);

  SerialUSB.begin();
  SerialUSB.println("hello world");
}

void loop()
{
  digitalWrite(0, HIGH);
  digitalWrite(1, HIGH);
  delay(200);
  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  delay(800);
  
  SerialUSB.println("ping");
  int ava = SerialUSB.available();
  int buffer[ava];

  // read buffer
  for(int step = 0; step < ava; step++)
    buffer[step] = SerialUSB.read();
  // write buffer back
  for(int step = 0; step < ava; step++)
    SerialUSB.print(buffer[step]);
  SerialUSB.println("==line end==");
  delete buffer;
}

字符串
官方演示mentioned here(Arduino IDE -文件-示例- DigiCDC - Echo)也不工作。
一旦程序被编译并上传到板上,Windows显示“未知USB设备”通知。并且找不到可用的串行端口设备。

  • 有没有少了几个司机?
  • 或者DigiCDC lib在Win10上不工作?
  • 或者我应该使用另一个lib来实现PC和ATTiny 85/Digispark之间通过USB的通信?
bq9c1y66

bq9c1y661#

你可以使用DigiKeyboard库来发送击键。我推荐Danjovic's fork,因为它也允许你check the status of the caps lock, num lock and scroll lock keys
以下是DigiKeyboard库附带的示例代码:

#include "DigiKeyboard.h"

void setup() {
  // don't need to set anything up to use DigiKeyboard
}

void loop() {
  // this is generally not necessary but with some older systems it seems to
  // prevent missing the first character after a delay:
  DigiKeyboard.sendKeyStroke(0);
  
  // Type out this string letter by letter on the computer (assumes US-style
  // keyboard)
  DigiKeyboard.println("Hello Digispark!");
  
  // It's better to use DigiKeyboard.delay() over the regular Arduino delay()
  // if doing keyboard stuff because it keeps talking to the computer to make
  // sure the computer knows the keyboard is alive and connected
  DigiKeyboard.delay(5000);
}

字符串

相关问题