Advanced I/O
Update time:2018-04-13 Views:6651
Operating Environment
Operating system : windows 7 Ultimate 64-bit
IDE : Arduino1.6.6
tone()
Description
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.
Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.
Syntax
tone(pin,frequency) tone(pin,frequency,duration)
Parameters
pin:the pin on which to generate the tone frequency: the frequency of the tone in hertz - unsigned int duration:the duration of the tone in milliseconds (optional) - unsigned long
Returns
None
Note
if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.
noTone()
Description
Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.
Syntax
noTone(pin)
Parameters
pin:the pin on which to stop generating the tone
Returns
None
Note
if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.
shiftOut()
Description
Shifts out a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available.
Syntax
shiftOut(dataPin, clockPin, bitOrder, value)
Parameters
dataPin:the pin on which to output each bit (int) clockPin:the pin to toggle once the dataPin has been set to the correct value (int) bitOrder:which order to shift out the bits; either MSBFIRST or LSBFIRST.(Most Significant Bit First, or, Least Significant Bit First) value: the data to shift out. (byte)
Returns
None
Note
1.if you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the call to shiftOut(), e.g. with a call to digitalWrite(clockPin, LOW). 2.This is a software implementation; see also the SPI library, which provides a hardware implementation that is faster but works only on specific pins.
instance
// Do this for MSBFIRST serialint data = 500;// shift out highbyteshiftOut(dataPin, clock, MSBFIRST, (data >> 8)); // shift out lowbyteshiftOut(dataPin, clock, MSBFIRST, data);// Or do this for LSBFIRST serialdata = 500;// shift out lowbyteshiftOut(dataPin, clock, LSBFIRST, data); // shift out highbyte shiftOut(dataPin, clock, LSBFIRST, (data >> 8));
Example
//**************************************************************//// Name : shiftOutCode, Hello World //// Author : Carlyn Maw,Tom Igoe //// Date : 25 Oct, 2006 //// Version : 1.0 //// Notes : Code for using a 74HC595 Shift Register //// : to count from 0 to 255 ////****************************************************************//Pin connected to ST_CP of 74HC595int latchPin = 8;//Pin connected to SH_CP of 74HC595int clockPin = 12;////Pin connected to DS of 74HC595int dataPin = 11;void setup() { //set pins to output because they are addressed in the main loop pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT);}void loop() { //count up routine for (int j = 0; j < 256; j++) {//ground latchPin and hold low for as long as you are transmittingdigitalWrite(latchPin, LOW);shiftOut(dataPin, clockPin, LSBFIRST, j); //return the latch pin high to signal chip that it //no longer needs to listen for informationdigitalWrite(latchPin, HIGH);delay(1000); }}
shiftIn()
Description
Shifts in a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. For each bit, the clock pin is pulled high, the next bit is read from the data line, and then the clock pin is taken low. If you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the first call to shiftIn(), e.g. with a call to digitalWrite(clockPin, LOW).
Syntax
shiftIn(dataPin,clockPin,bitOrder)
Parameters
dataPin:the pin on which to input each bit (int) clockPin:the pin to toggle to signal a read from dataPin bitOrder:which order to shift in the bits; either MSBFIRST or LSBFIRST.
Returns
the value read (byte)
Note
this is a software implementation; Arduino also provides an SPI library that uses the hardware implementation, which is faster but only works on specific pins.
pulseIn()
Description
Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or 0 if no complete pulse was received within the timeout. The timing of this function has been determined empirically and will probably show errors in shorter pulses. Works on pulses from 10 microseconds to 3 minutes in length. Please also note that if the pin is already high when the function is called, it will wait for the pin to go LOW and then HIGH before it starts counting. This routine can be used only if interrupts are activated. Furthermore the highest resolution is obtained with short intervals.
Syntax
pulseIn(pin, value) pulseIn(pin, value, timeout)
Parameters
pin: the number of the pin on which you want to read the pulse. (int) value:type of pulse to read: either HIGH or LOW. (int)。 timeout (optional): the number of microseconds to wait for the pulse to be completed: the function returns 0 if no complete pulse was received within the timeout. Default is one second (unsigned long).
Returns
the length of the pulse (in microseconds) or 0 if no pulse is completed before the timeout (unsigned long)
Example
int pin = 7;unsigned long duration;void setup(){ pinMode(pin, INPUT);}void loop(){ duration = pulseIn(pin, HIGH);}