feat: 全量同步 254 个常用的 Arduino 扩展库文件
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// I2C Digital Potentiometer
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/>
|
||||
|
||||
// Demonstrates use of the Wire library
|
||||
// Controls AD5171 digital potentiometer via I2C/TWI
|
||||
|
||||
// Created 31 March 2006
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
// ---------------------------------
|
||||
// Example from : http://www.arduino.cc/en/Tutorial/DigitalPotentiometer
|
||||
// Adapted to show usage of the SoftwareWire library
|
||||
// ---------------------------------
|
||||
|
||||
|
||||
#include <SoftwareWire.h>
|
||||
|
||||
// SoftwareWire constructor.
|
||||
// Parameters:
|
||||
// (1) pin for the software sda
|
||||
// (2) pin for the software scl
|
||||
// (3) use internal pullup resistors. Default true. Set to false to disable them.
|
||||
// (4) allow the Slave to stretch the clock pulse. Default true. Set to false for faster code.
|
||||
//
|
||||
// Using pin 2 (software sda) and 3 (software scl) in this example.
|
||||
|
||||
SoftwareWire myWire( 2, 3);
|
||||
|
||||
void setup()
|
||||
{
|
||||
myWire.begin(); // join i2c bus (address optional for master)
|
||||
}
|
||||
|
||||
byte val = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
myWire.beginTransmission(44); // transmit to device #44 (0x2c)
|
||||
// device address is specified in datasheet
|
||||
myWire.write(byte(0x00)); // sends instruction byte
|
||||
myWire.write(val); // sends potentiometer value byte
|
||||
myWire.endTransmission(); // stop transmitting
|
||||
|
||||
val++; // increment value
|
||||
if(val == 64) // if reached 64th position (max)
|
||||
{
|
||||
val = 0; // start over from lowest value
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// Stress test for SoftwareWire library.
|
||||
// Tested with an Arduino Uno connected to an Arduino Uno.
|
||||
// This is the sketch for the Master Arduino using the software i2c.
|
||||
|
||||
// Use define to switch between the Arduino Wire and the SoftwareWire.
|
||||
#define TEST_SOFTWAREWIRE
|
||||
|
||||
|
||||
#ifdef TEST_SOFTWAREWIRE
|
||||
|
||||
#include "SoftwareWire.h"
|
||||
|
||||
// SoftwareWire constructor.
|
||||
// Parameters:
|
||||
// (1) pin for the software sda
|
||||
// (2) pin for the software scl
|
||||
// (3) use internal pullup resistors. Default true. Set to false to disable them.
|
||||
// (4) allow the Slave to stretch the clock pulse. Default true. Set to false for faster code.
|
||||
|
||||
// This stress test uses A4 and A5, that makes it easy to switch between the (hardware) Wire
|
||||
// and the (software) SoftwareWire libraries.
|
||||
// myWire: sda = A4, scl = A5, turn on internal pullups, allow clock stretching by Slave
|
||||
SoftwareWire myWire( A4, A5);
|
||||
|
||||
#else
|
||||
|
||||
// Make code work with normal Wire library.
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#define myWire Wire // use the real Arduino Wire library instead of the SoftwareWire.
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600); // start serial port
|
||||
Serial.println(F("\nMaster"));
|
||||
|
||||
myWire.begin(); // join i2c bus as master
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println(F("Test with 200 transmissions of writing 10 bytes each"));
|
||||
byte buf[20] = { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, };
|
||||
int err = 0;
|
||||
unsigned long millis1 = millis();
|
||||
boolean firsterr = false;
|
||||
for( int i=0; i<200; i++)
|
||||
{
|
||||
myWire.beginTransmission(4);
|
||||
myWire.write( buf, 10);
|
||||
if( myWire.endTransmission() != 0)
|
||||
{
|
||||
err++;
|
||||
if( !firsterr)
|
||||
{
|
||||
Serial.print(F("first error at i = "));
|
||||
Serial.println(i);
|
||||
firsterr = true;
|
||||
}
|
||||
}
|
||||
delayMicroseconds(100); // Even the normal Arduino Wire library needs some delay when the Slave disables interrupts.
|
||||
}
|
||||
unsigned long millis2 = millis();
|
||||
Serial.print(F("total time: "));
|
||||
Serial.print(millis2 - millis1);
|
||||
Serial.print(F(" ms, total errors: "));
|
||||
Serial.println(err);
|
||||
|
||||
delay(2000);
|
||||
|
||||
Serial.println(F("Sending data"));
|
||||
static byte x = 0;
|
||||
myWire.beginTransmission(4); // transmit to i2c slave device #4
|
||||
myWire.write(x++); // counter, to check that everything was transmitted.
|
||||
for( int i=0; i<random( 32); i++) // 0 to 31 bytes (1 byte has been send already)
|
||||
{
|
||||
myWire.write(random(256));
|
||||
}
|
||||
int error = myWire.endTransmission(); // stop transmitting
|
||||
Serial.print(F("transmission status error="));
|
||||
Serial.println(error);
|
||||
|
||||
delay(2000);
|
||||
|
||||
Serial.println(F("Requesting data"));
|
||||
int n = myWire.requestFrom(4, 10); // request bytes from Slave
|
||||
Serial.print(F("n="));
|
||||
Serial.print(n);
|
||||
Serial.print(F(", available="));
|
||||
Serial.println(myWire.available());
|
||||
|
||||
// myWire.printStatus(Serial); // This shows information about the SoftwareWire object.
|
||||
|
||||
byte buffer[40];
|
||||
// for( int j=0; j<n; j++)
|
||||
// buffer[j] = myWire.read();
|
||||
myWire.readBytes( buffer, n);
|
||||
|
||||
for( int k=0; k<n; k++)
|
||||
{
|
||||
if( k == 0)
|
||||
Serial.print(F("*")); // indicate the number of the counter
|
||||
Serial.print( (int) buffer[k]);
|
||||
Serial.print(F(", "));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// Stress test for SoftwareWire library.
|
||||
// Tested with an Arduino Uno connected to an Arduino Uno.
|
||||
// This is the sketch for the Slave Arduino using the hardware I2C.
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
volatile byte buffer[40];
|
||||
volatile int rxHowMany;
|
||||
volatile int rxInterrupts = 0;
|
||||
volatile boolean flagRequest;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600); // start serial for output
|
||||
Serial.println("\nSlave");
|
||||
|
||||
Wire.begin(4); // join i2c bus as slave with address #4
|
||||
Wire.onReceive(receiveEvent); // interrupt handler for receiving i2c data
|
||||
Wire.onRequest(requestEvent); // interrupt handler for when data is requested by i2c
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
noInterrupts();
|
||||
int rxInterruptsCopy = rxInterrupts;
|
||||
rxInterrupts = 0;
|
||||
interrupts();
|
||||
|
||||
// Using all the text output to the Serial port is part of the stress test.
|
||||
// That causes delays and interrupts.
|
||||
if( rxInterruptsCopy > 0)
|
||||
{
|
||||
Serial.print("Receive: ");
|
||||
if( rxInterruptsCopy > 1)
|
||||
{
|
||||
// Printing to the serial port at 9600 is slow.
|
||||
// Therefor it is normal that this sketch misses received data,
|
||||
// if too much data was received.
|
||||
// As long as the i2c data is correct, everything is okay. It is a stress test.
|
||||
Serial.print("Missed:");
|
||||
Serial.print( rxInterruptsCopy);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.print("howMany:");
|
||||
Serial.print( rxHowMany);
|
||||
|
||||
Serial.print(", data:");
|
||||
for(int i=0; i<rxHowMany; i++)
|
||||
{
|
||||
if( i == 0)
|
||||
Serial.print(F("*")); // indicate the first number (sometimes used for a counter value).
|
||||
|
||||
Serial.print((unsigned int) buffer[i], DEC);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
noInterrupts();
|
||||
boolean flagRequestCopy = flagRequest;
|
||||
flagRequest = false;
|
||||
interrupts();
|
||||
|
||||
if( flagRequestCopy)
|
||||
{
|
||||
Serial.println("Request: Data was requested and send");
|
||||
}
|
||||
|
||||
// Stress the master by disabling interrupts.
|
||||
// A value of 500 microseconds will even corrupt the transmission with the normal Arduino Wire library.
|
||||
noInterrupts();
|
||||
delayMicroseconds(50);
|
||||
interrupts();
|
||||
}
|
||||
|
||||
void receiveEvent(int howMany)
|
||||
{
|
||||
for( int i=0; i<howMany; i++)
|
||||
buffer[i] = Wire.read();
|
||||
|
||||
rxHowMany = howMany;
|
||||
rxInterrupts++;
|
||||
}
|
||||
|
||||
void requestEvent()
|
||||
{
|
||||
static byte x = 0;
|
||||
|
||||
// Fill array with numbers.
|
||||
char TXbuf[] = { 188, 0, 255, 1, 120, 150, 44, 2, 131, 72 };
|
||||
TXbuf[0] = x++; // overwrite the first with a counter.
|
||||
Wire.write(TXbuf, sizeof(TXbuf));
|
||||
|
||||
flagRequest = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user