Arduino Libraries库函数大全(官方网站)

2018-11-12 13:46

Libraries 目录

Libraries provide extra functionality for use in sketches, e.g. working with hardware or manipulating data. To use a library in a sketch, select it from Sketch > Import Library.

Standard Libraries

? ? ? ? ? ? ? ? ? ? ?

EEPROM - reading and writing to \Ethernet - for connecting to the internet using the Arduino Ethernet Shield Firmata - for communicating with applications on the computer using a standard serial protocol.

LiquidCrystal - for controlling liquid crystal displays (LCDs) SD - for reading and writing SD cards Servo - for controlling servo motors SPI - for communicating with devices using the Serial Peripheral Interface (SPI) Bus

SoftwareSerial - for serial communication on any digital pins. Version 1.0 and later of Arduino incorporate Mikal Hart's NewSoftSerial library as SoftwareSerial. Stepper - for controlling stepper motors WiFi - for connecting to the internet using the Arduino WiFi shield Wire - Two Wire Interface (TWI/I2C) for sending and receiving data over a net of devices or sensors.

The Matrix and Sprite libraries are no longer part of the core distribution.

Leonardo Only Libraries

? ?

Keyboard - Send keystrokes to an attached computer. Mouse - Control cursor movement on a connected computer. Contributed Libraries

If you're using one of these libraries, you need to install it first. To do so, download the library and unzip it. It should be in a folder of its own, and will typically contain at least two files, one with a .h suffix and one with a .cpp suffix. Open your Arduino sketchbook folder. If there is already a folder there called libraries, place the library folder in there. If not, create a folder called libraries in the sketchbook folder, and drop the library

folder in there. Then re-start the Arduino programming environment, and you should see your new library in the Sketch > Import Library menu. For details, see the page on the Arduino environment. Communication (networking and protocols):

Messenger - for processing text-based messages from the computer NewSoftSerial - an improved version of the SoftwareSerial library OneWire - control devices (from Dallas Semiconductor) that use the One Wire protocol.

1

? ? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ?

?

? ? ? ? ? ?

PS2Keyboard - read characters from a PS2 keyboard. Simple Message System - send messages between Arduino and the computer SSerial2Mobile - send text messages or emails using a cell phone (via AT commands over software serial)

Webduino - extensible web server library (for use with the Arduino Ethernet Shield)

X10 - Sending X10 signals over AC power lines XBee - for communicating with XBees in API mode SerialControl - Remote control other Arduinos over a serial connection Sensing:

Capacitive Sensing - turn two or more pins into capacitive sensors Debounce - for reading noisy digital inputs (e.g. from buttons) Displays and LEDs:

Improved LCD library fixes LCD initialization bugs in official Arduino LCD library

GLCD - graphics routines for LCD based on the KS0108 or equivalent chipset. LedControl - for controlling LED matrices or seven-segment displays with a MAX7221 or MAX7219.

LedControl - an alternative to the Matrix library for driving multiple LEDs with Maxim chips.

LedDisplay - control of a HCMS-29xx scrolling LED display. These libraries are compatible Wiring versions, and the links below point to the (excellent) Wiring documentation.

Matrix - Basic LED Matrix display manipulation library Sprite - Basic image sprite manipulation library for use in animations with an LED matrix

Frequency Generation and Audio:

Tone - generate audio frequency square waves in the background on any microcontroller pin Motors and PWM:

TLC5940 - 16 channel 12 bit PWM controller. Timing:

DateTime - a library for keeping track of the current date and time in software. Metro - help you time actions at regular intervals MsTimer2 - uses the timer 2 interrupt to trigger an action every N milliseconds. Utilities:

PString - a lightweight class for printing to buffers Streaming - a method to simplify print statements For a guide to writing your own libraries, see this tutorial

2

Writing a Library for Arduino

This document explains how to create a library for Arduino. It starts with a sketch with a sketch for flashing Morse code and explains how to convert its functions into a library. This allows other people to easily use the code that you've written and to easily update it as you improve the library.

We start with a sketch that does simple Morse code: int pin = 13;

void setup() {

pinMode(pin, OUTPUT); }

void loop() {

dot(); dot(); dot(); dash(); dash(); dash(); dot(); dot(); dot(); delay(3000); }

void dot() {

digitalWrite(pin, HIGH); delay(250);

digitalWrite(pin, LOW); delay(250); }

void dash() {

digitalWrite(pin, HIGH); delay(1000);

digitalWrite(pin, LOW); delay(250); }

3

[Get Code] If you run this sketch, it will flash out the code for SOS (a distress call) on pin 13. The sketch has a few different parts that we'll need to bring into our library. First, of course, we have the dot() and dash()functions that do the actual blinking. Second,

there's the ledPin variable which the functions use to determine which pin to use. Finally, there's the call to pinMode() that initializes the pin as an output. Let's start turning the sketch into a library!

You need at least two files for a library: a header file (w/ the extension .h) and the source file (w/ extension .cpp). The header file has definitions for the library: basically a listing of everything that's inside; while the source file has the actual code. We'll call our library \seem a bit strange at first, but it will make more sense once you see the source file that goes with it.

The core of the header file consists of a line for each function in the library, wrapped up in a class along with any variables you need: class Morse {

public:

Morse(int pin); void dot(); void dash(); private: int _pin; };

[Get Code] A class is simply a collection of functions and variables that are all kept together in one place. These functions and variables can be public, meaning that they can be accessed by people using your library, or private, meaning they can only be accessed from within the class itself. Each class has a special function known as a constructor, which is used to create aninstance of the class. The constructor has the same name as the class, and no return type.

You need a couple of other things in the header file. One is an #include statement that gives you access to the standard types and constants of the Arduino language (this is automatically added to normal sketches, but not to libraries). It looks like this (and goes above the class definition given previously): #include \

[Get Code] Finally, it's common to wrap the whole header file up in a weird looking construct: #ifndef Morse_h #define Morse_h

// the #include statment and code go here...

#endif

4

[Get Code] Basically, this prevents problems if someone accidently #include's your library twice. Finally, you usually put a comment at the top of the library with its name, a short description of what it does, who wrote it, the date, and the license. Let's take a look at the complete header file: /*

Morse.h - Library for flashing Morse code. Created by David A. Mellis, November 2, 2007. Released into the public domain. */

#ifndef Morse_h #define Morse_h

#include \

class Morse {

public:

Morse(int pin); void dot(); void dash(); private: int _pin; };

#endif

[Get Code] Now let's go through the various parts of the source file, Morse.cpp.

First comes a couple of #include statements. These give the rest of the code access to the standard Arduino functions, and to the definitions in your header file: #include \#include \

[Get Code] Then comes the constructor. Again, this explains what should happen when someone creates an instance of your class. In this case, the user specifies which pin they would like to use. We configure the pin as an output save it into a private variable for use in the other functions:

Morse::Morse(int pin) {

pinMode(pin, OUTPUT); _pin = pin; }

[Get Code] 5

There are a couple of strange things in this code. First is the Morse:: before the name of the function. This says that the function is part of the Morse class. You'll see this again in the other functions in the class. The second unusual thing is the underscore in the name of our private variable, _pin. This variable can actually have any name you want, as long as it matches the definition in the header file. Adding an underscore to the start of the name is a common convention to make it clear which variables are private, and also to distinguish the name from that of the argument to the function (pin in this case).

Next comes the actual code from the sketch that you're turning into a library (finally!). It looks pretty much the same, except with Morse:: in front of the names of the functions, and _pin instead of pin: void Morse::dot() {

digitalWrite(_pin, HIGH); delay(250);

digitalWrite(_pin, LOW); delay(250); }

void Morse::dash() {

digitalWrite(_pin, HIGH); delay(1000);

digitalWrite(_pin, LOW); delay(250); }

[Get Code] Finally, it's typical to include the comment header at the top of the source file as well. Let's see the whole thing: /*

Morse.cpp - Library for flashing Morse code. Created by David A. Mellis, November 2, 2007. Released into the public domain. */

#include \#include \

Morse::Morse(int pin) {

pinMode(pin, OUTPUT); _pin = pin; }

void Morse::dot()

6

{

digitalWrite(_pin, HIGH); delay(250);

digitalWrite(_pin, LOW); delay(250); }

void Morse::dash() {

digitalWrite(_pin, HIGH); delay(1000);

digitalWrite(_pin, LOW); delay(250); }

[Get Code] And that's all you need (there's some other nice optional stuff, but we'll talk about that later). Let's see how you use the library.

First, make a Morse directory inside of the libraries sub-directory of your sketchbook directory. Copy or move the Morse.h and Morse.cpp files into that directory. Now launch the Arduino environment. If you open the Sketch > Import Library menu, you should see Morse inside. The library will be compiled with sketches that use it. If the library

doesn't seem to build, make sure that the files really end in .cpp and .h (with no extra .pde or .txt extension, for example).

Let's see how we can replicate our old SOS sketch using the new library: #include

Morse morse(13);

void setup() { }

void loop() {

morse.dot(); morse.dot(); morse.dot();

morse.dash(); morse.dash(); morse.dash(); morse.dot(); morse.dot(); morse.dot(); delay(3000); }

[Get Code] There are a few differences from the old sketch (besides the fact that some of the code has moved to a library).

First, we've added an #include statement to the top of the sketch. This makes the Morse library available to the sketch and includes it in the code sent to the board. That means if

7

you no longer need a library in a sketch, you should delete the #include statement to save space.

Second, we now create an instance of the Morse class called morse:

Morse morse(13);

[Get Code] When this line gets executed (which actually happens even before the setup() function), the constructor for the Morse class will be called, and passed the argument you've given here (in this case, just 13).

Notice that our setup() is now empty; that's because the call to pinMode() happens inside the library (when the instance is constructed).

Finally, to call the dot() and dash() functions, we need to prefix them with morse. - the name of the instance we want to use. We could have multiple instances of the Morse class, each on their own pin stored in the _pin private variable of that instance. By calling a function on a particular instance, we specify which instance's variables should be used during that call to a function. That is, if we had both:

Morse morse(13); Morse morse2(12);

[Get Code]

then inside a call to morse2.dot(), _pin would be 12.

If you tried the new sketch, you probably noticed that nothing from our library was recognized by the environment and highlighted in color. Unfortunately, the Arduino

software can't automatically figure out what you've define in your library (though it would be a nice feature to have), so you have to give it a little help. To do this, create a file called keywords.txtin the Morse directory. It should look like this:

Morse KEYWORD1 dash KEYWORD2 dot KEYWORD2

[Get Code] Each line has the name of the keyword, followed by a tab (not spaces), followed by the kind of keyword. Classes should beKEYWORD1 and are colored orange; functions should be KEYWORD2 and will be brown. You'll have to restart the Arduino environment to get it to recognize the new keywords.

It's also nice to provide people with an example sketch that uses your library. To do this, create an examples directory inside the Morse directory. Then, move or copy the directory containing the sketch (let's call it SOS) we wrote above into the examples

directory. (You can find the sketch using the Sketch > Show Sketch Folder command.) If you restart the Arduino environment (this is the last time, I promise) - you'll see

a Library-Morse item inside the File > Sketchbook > Examples menu containing your example. You might want to add some comments that better explain how to use your library.

If you'd like to check out the complete library (with keywords and example), you can download it: Morse.zip. 8

That's all for now but I'll probably write an advanced library tutorial soon. In the meantime, if you have any problems or suggestions, please post them to the Software Development forum. More Sharing ServicesShare|Share on emailShare on favoritesShare on printShare on facebookShare on twitter

EEPROM Library

The microcontroller on the Arduino board has EEPROM: memory whose values are kept when the board is turned off (like a tiny hard drive). This library enables you to read and write those bytes.

The microcontrollers on the various Arduino boards have different amounts of EEPROM: 1024 bytes on the ATmega328, 512 bytes on the ATmega168 and ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560.

? ?

Functions

read() write() read()

Description

Reads a byte from the EEPROM. Locations that have never been written to have the value of 255.

Syntax

EEPROM.read(address)

Parameters

address: the location to read from, starting from 0 (int) Returns

the value stored in that location (byte) Example

#include

int a = 0; int value;

void setup() {

Serial.begin(9600);

9

}

void loop() {

value = EEPROM.read(a);

Serial.print(a); Serial.print(\ Serial.print(value); Serial.println();

a = a + 1;

if (a == 512) a = 0;

delay(500); }

write()

Description

Write a byte to the EEPROM. Syntax

EEPROM.write(address, value)

Parameters

address: the location to write to, starting from 0 (int) value: the value to write, from 0 to 255 (byte) Returns none

Note

An EEPROM write takes 3.3 ms to complete. The EEPROM memory has a specified life of 100,000 write/erase cycles, so you may need to be careful about how often you write to it. Example

#include

void setup() {

for (int i = 0; i < 512; i++) EEPROM.write(i, i); }

void loop() { }

10

Ethernet library

With the Arduino Ethernet Shield, this library allows an Arduino board to connect to the internet. It can serve as either a server accepting incoming connections or a client making outgoing ones. The library supports up to four concurrent connection (incoming or outgoing or a combination).

Arduino communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53, is not used to select the W5100, but it must be kept as an output or the SPI interface won't work.

11

Ethernet class

The Ethernet class initializes the ethernet library and network settings.

begin() localIP() maintain() IPAddress class

The IPAddress class works with local and remote IP addressing.

IPAddress() Server class

The Server class creates servers which can send data to and receive data from connected clients (programs running on other computers or devices).

Server EthernetServer() begin() available() write() print() println() Client class

The client class creates clients that can connect to servers and send and receive data.

Client EthernetClient() if (EthernetClient) connected() connect() write() print() println() available() read()

12

? ? ?

?

? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ?

? ? flush() stop() EthernetUDP class

The EthernetUDP class enables UDP message to be sent and received.

begin() read() write() beginPacket() endPacket() parsePacket() available() remoteIP() remotePort() Reference Home Corrections, sugges

Ethernet class

The Ethernet class initializes the ethernet library and network settings.

begin() localIP() maintain() ? ? ? ? ? ? ? ? ?

? ? ?

Ethernet.begin()

Description

Initializes the ethernet library and network settings.

With version 1.0, the library supports DHCP. Using Ethernet.begin(mac) with the proper network setup, the Ethernet shield will automatically obtain an IP address. This increases the sketch size significantly.

Syntax

Ethernet.begin(mac); Ethernet.begin(mac, ip);

Ethernet.begin(mac, ip, dns);

Ethernet.begin(mac, ip, dns, gateway);

Ethernet.begin(mac, ip, dns, gateway, subnet);

Parameters

mac: the MAC (Media access control) address for the device (array of 6 bytes). this is the Ethernet hardware address of your shield. Newer Arduino Ethernet Shields include a sticker with the device's MAC address. For older shields, choose your own. ip: the IP address of the device (array of 4 bytes) dns: the address for a DNS server.

gateway: the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1

subnet: the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0

13

Returns

The DHCP version of this function, Ethernet.begin(mac), returns an int: 1 on a successful DHCP connection, 0 on failure. The other versions don't return anything.

Example

#include // the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the IP address for the shield: byte ip[] = { 10, 0, 0, 177 };

void setup() {

Ethernet.begin(mac, ip); }

void loop () {}

[Get Code]

Reference Home Ethernet.localIP()

Description

Obtains the IP address of the Ethernet shield. Useful when the address is auto assigned through DHCP. Syntax

Ethernet.localIP(); Parameters none

Returns the IP address

Example

#include

#include

// Enter a MAC address for your controller below.

// Newer Ethernet shields have a MAC address printed on a sticker on the shield

byte mac[] = {

0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// Initialize the Ethernet client library

// with the IP address and port of the server

// that you want to connect to (port 80 is default for HTTP): EthernetClient client;

14

void setup() {

// start the serial library: Serial.begin(9600);

// start the Ethernet connection: if (Ethernet.begin(mac) == 0) {

Serial.println(\ // no point in carrying on, so do nothing forevermore: for(;;) ; }

// print your local IP address:

Serial.println(Ethernet.localIP()); }

void loop() { }

Ethernet.maintain()

Description

Allows for the renewal of DHCP leases. When assigned an IP address via DHCP, ethernet devices are given a lease on the address for an amount of time. With Ethernet.maintain(), it is possible to request a renewal from the DHCP server. Depending on the server's configuration, you may receive the same address, a new one, or none at all. Ethernet.maintain() was added to Arduino 1.0.1. Syntax

Ethernet.maintain(); Parameters none

Returns byte:

0: nothing happened 1: renew failed 2: renew success 3: rebind fail 4: rebind success

IPAddress class

The IPAddress class works with local and remote IP addressing.

IPAddress() ?

IPAddress()

15

Description

Defines an IP address. It can be used to declare both local and remote addresses.

Syntax

IPAddress(address);

Parameters

address: a comma delimited list representing the address (4 bytes, ex. 192, 168, 1, 1)

Returns

None

Example

#include

// network configuration. gateway and subnet are optional. // the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // the router's gateway address: byte gateway[] = { 10, 0, 0, 1 }; // the subnet:

byte subnet[] = { 255, 255, 0, 0 };

EthernetServer server = EthernetServer(23);

//the IP address is dependent on your network IPAddress ip(192,168,1,1);

void setup() {

// initialize the ethernet device

Ethernet.begin(mac, ip, gateway, subnet);

// start listening for clients server.begin(); }

void loop() {

//print out the IP address Serial.println(myIPaddress); }

16

Server class

The Server class creates servers which can send data to and receive data from connected

clients (programs running on other computers or devices).

? Server ? EthernetServer() ? begin() ? available() ? write() ? print() ? println() Server()

Description

Create a server that listens for incoming connections on the specified port. Syntax Server(port);

Parameters

port: the port to listen on (int) Returns None

Example

#include

// network configuration. gateway and subnet are optional.

// the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the IP address for the shield: byte ip[] = { 10, 0, 0, 177 }; // the router's gateway address: byte gateway[] = { 10, 0, 0, 1 }; // the subnet:

byte subnet[] = { 255, 255, 0, 0 };

// telnet defaults to port 23 Server server = Server(23);

void setup() {

// initialize the ethernet device

Ethernet.begin(mac, ip, gateway, subnet);

// start listening for clients

17

server.begin(); }

void loop() {

// if an incoming client connects, there will be bytes available to read: Client client = server.available(); if (client == true) {

// read bytes from the incoming client and write them back // to any clients connected to the server: server.write(client.read()); } }

Refer EthernetServer()

Description

Create a server that listens for incoming connections on the specified port.

Syntax

Server(port);

Parameters

port: the port to listen on (int)

Returns

None

Example

#include

#include

// network configuration. gateway and subnet are optional. // the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the IP address for the shield: byte ip[] = { 10, 0, 0, 177 }; // the router's gateway address: byte gateway[] = { 10, 0, 0, 1 }; // the subnet:

byte subnet[] = { 255, 255, 0, 0 };

// telnet defaults to port 23

EthernetServer server = EthernetServer(23);

18

void setup() {

// initialize the ethernet device

Ethernet.begin(mac, ip, gateway, subnet);

// start listening for clients server.begin(); }

void loop() {

// if an incoming client connects, there will be bytes available to read:

EthernetClient client = server.available(); if (client == true) {

// read bytes from the incoming client and write them back // to any clients connected to the server: server.write(client.read()); } }

Ethernet : Server class

begin()

Description

Tells the server to begin listening for incoming connections.

Syntax

server.begin()

Parameters

None

Returns

None

Example

#include

#include

// the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the IP address for the shield: byte ip[] = { 10, 0, 0, 177 }; // the router's gateway address: byte gateway[] = { 10, 0, 0, 1 }; // the subnet:

byte subnet[] = { 255, 255, 0, 0 };

19

// telnet defaults to port 23

EthernetServer server = EthernetServer(23);

void setup() {

// initialize the ethernet device

Ethernet.begin(mac, ip, gateway, subnet);

// start listening for clients server.begin(); }

void loop() {

// if an incoming client connects, there will be bytes available to read:

EthernetClient client = server.available(); if (client == true) {

// read bytes from the incoming client and write them back // to any clients connected to the server: server.write(client.read()); } }

available()

Description

Gets a client that is connected to the server and has data available for reading. The connection persists when the returned client object goes out of scope; you can close it by calling client.stop(). available() inherits from the Stream utility class.

Syntax

server.available()

Parameters

None

Returns

a Client object; if no Client has data available for reading, this object will evaluate to false in an if-statement (see the example below)

Example

#include #include

// the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the IP address for the shield:

20

begin()

start the library

begin(long)

start the library and override the default baud rate

printVersion()

send the protocol version to the host computer

blinkVersion()

blink the protocol version on pin 13

printFirmwareVersion()

send the firmware name and version to the host computer

setFirmwareVersion(byte major, byte minor)

set the firmware name and version, using the sketch's filename, minus the .pde

Sending Messages

sendAnalog(byte pin, int value)

send an analog message

sendDigitalPorts(byte pin, byte firstPort, byte secondPort)

send digital ports as individual bytes

sendDigitalPortPair(byte pin, int value)

send digital ports as one int

sendSysex(byte command, byte bytec, byte* bytev)

send a command with an arbitrary array of bytes

sendString(const char* string)

send a string to the host computer

sendString(byte command, const char* string)

send a string to the host computer using a custom command type

Receiving Messages available()

check to see if there are any incoming messages in the buffer

processInput()

process incoming messages from the buffer, sending the data to any registered callback functions

attach(byte command, callbackFunction myFunction)

attach a function to an incoming message type

detach(byte command)

detach a function from an incoming message type

Callback Functions

In order to attach your function to a message type, your function must match the standard callback function. There are currently three types of callback functions in Firmata: generic, string, and sysex. generic

void callbackFunction(byte pin, int value);

system_reset

void systemResetCallbackFunction(void);

46

string

void stringCallbackFunction(char *myString);

sysex

void sysexCallbackFunction(byte pin, byte byteCount, byte *arrayPointer);

Message Types

These are the various message types that you can attach functions to.

ANALOG_MESSAGE

the analog value for a single pin

DIGITAL_MESSAGE

8-bits of digital pin data (one port)

REPORT_ANALOG

enable/disable the reporting of analog pin

REPORT_DIGITAL

enable/disable the reporting of a digital port

SET_PIN_MODE

change the pin mode between INPUT/OUTPUT/PWM/etc.

FIRMATA_STRING

C-style strings, uses stringCallbackFunction for the function type

SYSEX_START

generic, arbitrary length messages (via MIDI SysEx protocol), uses sysexCallbackFunction for the function type

SYSTEM_RESET

message to reset firmware to its

uses systemResetCallbackFunction for the function type

default state,

Example

This example shows how to send and receive analog messages using Firmata.

#include

byte analogPin;

void analogWriteCallback(byte pin, int value) {

pinMode(pin,OUTPUT); analogWrite(pin, value); }

void setup() {

Firmata.setFirmwareVersion(0, 1);

Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); Firmata.begin(); }

47

void loop() {

while(Firmata.available()) { Firmata.processInput(); }

for(analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) { Firmata.sendAnalog(analogPin, analogRead(analogPin)); } }

LiquidCrystal Library

This library allows an Arduino board to control LiquidCrystal displays (LCDs) based on the Hitachi HD44780 (or a compatible) chipset, which is found on most text-based LCDs. The library works with in either 4- or 8-bit mode (i.e. using 4 or 8 data lines in addition to the rs, enable, and, optionally, the rw control lines).

? ? ? ? ? ? ? ? ? ? ? ?

Function

LiquidCrystal() begin() clear() home() setCursor() write() print() cursor() noCursor() blink() noBlink() display()

48

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

noDisplay() scrollDisplayLeft() scrollDisplayRight() autoscroll() noAutoscroll() leftToRight() rightToLeft() createChar() Examples

Hello World Blink Cursor Display Text Direction Autoscroll Serial input SetCursor Scroll Reference Home Corrections, sugge

LiquidCrystal()

Description

Creates a variable of type LiquidCrystal. The display can be controlled using 4 or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those lines unconnected. The RW pin can be tied to ground instead of connected to a pin on the Arduino; if so, omit it from this function's parameters.

Syntax

LiquidCrystal(rs, enable, d4, d5, d6, d7) LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)

LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7) LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)

Parameters

rs: the number of the Arduino pin that is connected to the RS pin on the LCD

rw: the number of the Arduino pin that is connected to the RW pin on the LCD (optional) enable: the number of the Arduino pin that is connected to the enable pin on the LCD d0, d1, d2, d3, d4, d5, d6, d7: the numbers of the Arduino pins that are connected to the corresponding data pins on the LCD. d0, d1, d2, and d3 are optional; if omitted, the LCD will be controlled using only the four data lines (d4, d5, d6, d7). Example

#include

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

49

void setup() {

lcd.print(\}

void loop() {}

begin()

Description

Specifies the dimensions (width and height) of the display. Syntax

lcd.begin(cols, rows)

Parameters

lcd: a variable of type LiquidCrystal

cols: the number of columns that the display has rows: the number of rows that the display has

clear()

Description

Clears the LCD screen and positions the cursor in the upper-left corner. Syntax lcd.clear()

Parameters

lcd: a variable of type LiquidCrystal

home()

Description

Positions the cursor in the upper-left of the LCD. That is, use that location in outputting subsequent text to the display. To also clear the display, use the clear() function instead. Syntax lcd.home()

Parameters

lcd: a variable of type LiquidCrystal

setCursor()

Description

Position the LCD cursor; that is, set the location at which subsequent text written to the LCD will be displayed. Syntax

lcd.setCursor(col, row)

Parameters

lcd: a variable of type LiquidCrystal

col: the column at which to position the cursor (with 0 being the first column)

50


Arduino Libraries库函数大全(官方网站).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:远程学习方法与技术-练习题目与答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: