分享

Arduino的崇高感和Lilytiny的优美感

 新用户5228KeDY 2021-10-10

Arduino的崇高感和Lilytiny的优美感

Arduino的易用性让我们设计物理实验的想法成为现实,这说明以方便易用为目的的单片机产品发展方向是崇高的;而Arduino家庭多数使用的是Atmel芯片,基于ATtiny的微小型芯片除了Gemma,还有Lilytiny和LilyPad,从名字就能看出她们的优美感,如果一定要有中文的名字,也许叫她们莉莉蒂妮和莉莉帕德的好。

不过,Arduino是开源的,因此这种开发板规格非常的混乱,不易使用。

第一乱,是这种开发板的名称:有的称为CJMCU Lilytiny,有的称为MCU Lilytiny,甚至LilyUSB等,总之是CJMCU/MCU与Lilytiny/LilyUSB等组合出来的名称,这会让我们在中英文网站里寻找资料时,都很困难。

第二乱,是我们难于辨认在淘宝上的这种开发板是国内自制的,还是原产的。当然从价格与包装上往往能够识别,原产的开发板包装一般非常精致,而仿制的一般只用防静电袋包装且大约7元左右的价格。

这些都不是最重要的,重要的是我确实看不到这种开发板的DataSheets或者PCB图,于是P0至P5这6个引脚一直没弄明白到底有什么功能和区别。昨天搞明白了手里的Gemma,也烧录了代码——Gemma只有3只引脚可用,直径28mm,而Lilytiny的直径,则是25mm。做一些事情时,使用Lily的优势是显然的。

Lilytiny与Gemma的烧录有微微的不同。记录如下。

为烧录Lily的代码,网上有一种Arduino IDE,是从很久以前的一种Arduino IDE改造的版本演化来的,一切使用都正常,只是IDE还是1.04的版本,同样的也要安装libusb-win32 devices这个驱动,否则Lilytiny也不能正常烧写,因为显然它也不需要Com号。Lilytiny烧写和Gemma又有不同,必须在烧写之前拔掉USB线,然后烧写,IDE提示插入,这时要在60s内插入才能正常烧写。

为了测试Lilytiny的引脚,我们改造Blink:

void setup() {                
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  digitalWrite(0, HIGH);
  digitalWrite(1, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  delay(50);
  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  delay(50);
}

找个LED分别插到P0至P5与GND,发现:P0、P1、P2、P3、P4均可作为输出,P5为复位RST。

给每个引脚写PWM,只有P0、P1能正常实现。P0、P1为PWM引脚。

同时测试到,板载LED为P1引脚,与UNO的A13类似。

在琢磨Analog的读和写的时候,发现在http:///wiki/digispark/tutorials/basics有一些关于LilyTiny的介绍,复制粘贴,整理如下。

Differences (from the Arduino) and limitations

The Digispark is compatible with the Arduino IDE - that does not, however, mean it can do everything an Arduino can do. In order to achieve the low cost and small size of the Digispark some compromises had to be made.

Here is a list of some of the differences worth considering when designing or troubleshooting (pin differences are outlined in the sections below this list):

The Digispark is powered by an Atmel Attiny85 MCU - this has many differences from an Arduino's ATmega328 and some libraries may not work correctly on it.

The Digispark only has about 6 KB of flash memory for storing your code.

Pin 3 and Pin 4 (P3 and P4) are used for USB communication and programming, while you can use them in your circuit if you are not using USB communication, you may have to unplug your circuit during programming if the circuit would impede the pin states or dramatically affect the voltage levels on these pins.

Pin 3 (P3) has a 1.5 kΩ pull-up resistor attached to it which is required for when P3 and P4 are used for USB communication (including programming). Your design may need to take into account that you'd have to overpower this to pull this pin low.

The Digispark does not have a hardware serial port nor a hardware serial to USB converter. An example library (DigiUSB) is provided, as well as some example code and a serial monitor like program, but communication with the computer will not always be plug and play, especially when other libraries are involved. 这里P3、P4虽然可以用软串口库模拟,但是模拟之后,错误的时间比正常的时间多太多,大概是不能实用。

DigitalWrite:

void setup() {
    //All pins are capable of Digital output, though P5 is 3 V at HIGH instead of 5 V
//是3V而不是5V,因此可以直接不限流不分压直接串LED
    pinMode(0, OUTPUT); //0 is P0, 1 is P1, 2 is P2, etc. - unlike the analog inputs, for digital outputs the pin number matches.
}

void loop() {
    digitalWrite(0,HIGH); //Turn the pin HIGH (5 V)
    delay(1000);
    digitalWrite(0,LOW); //Turn the pin LOW (GND)
    delay(1000);
}

Digital Read:

NOTE: The internal pull-up resistor (turned on by calling digitalWrite(0) after setting the pin to output, where 0 is the pin number) are much weaker (about 25 kohm) on an ATtiny than on an Arduino, so the onboard LED interferes with them. If you need them, you can use a different port. Change your circuit to not need the internal pull-up, or cut the LED trace. For Model A this would apply to P1 for Model B this would apply to P0.(Model Identification)

int sensorValue = 0;

void setup() {
    //All pins are capable of digital input.
    pinMode(0, INPUT); //0 is P0, 1 is P1, 2 is P2, etc. - unlike the analog inputs, for digital inputs the pin number matches.
}

void loop() {
    sensorValue = digitalRead(1); //Returns HIGH or LOW (true or false / 1 or 0).
}

Analog Read:

int sensorValue = 0;

void setup() {
    //You need not set pin mode for analogRead - though if you have set the pin to
    //output and later want to read from it then you need to set pinMode(0,INPUT);
    //where 0 is the physical pin number not the analog input number.
    //
    //See below for the proper pinMode statement to go with each analog read.
}

void loop() {
    // The analog pins are referenced by their analog port number, not their pin
    //number and are as follows:

    sensorValue = analogRead(1); //Read P2
    //To set to input: pinMode(2, INPUT);
    //THIS IS P2, P2 is analog input 1, so when you are using analog read, you refer to it as 1.

    //sensorValue = analogRead(2); //Read P4
    //To set to input: pinMode(4, INPUT);
    //THIS IS P4, P4 is analog input 2, so when you are using analog read, you refer to it as 2.

    //sensorValue = analogRead(3); //Read P3
    //To set to input: pinMode(3, INPUT);
    //THIS IS P3, P3 is analog input 3, so when you are using analog read, you refer to it as 3.

    //sensorValue = analogRead(0); //Read P5
    //To set to input: pinMode(5, INPUT);
    //THIS IS P5, P5 is analog input 0, so when you are using analog read, you refer to it as 0.
}

Analog Write: (AKA PWM)

void setup() {
    //P0, P1, and P4 are capable of hardware PWM (analogWrite).
    pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs, 
                        //for analog (PWM) outputs the pin number matches the port number.
}

void loop() {
    analogWrite(0,255); //Turn the pin on full (100%)
    delay(1000);
    analogWrite(0,128); //Turn the pin on half (50%)
    delay(1000);
    analogWrite(0,0);   //Turn the pin off (0%)
    delay(1000);
}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多