Difference between revisions of "How to install Arduino Library"
(→1、对中文支持很好) |
(→Control byte) |
||
(29 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | =''' | + | =='''Addressing'''== |
+ | EachPCF8591deviceinanI 2 C-bussystemisactivatedby sending a valid address to the device. The address consists of a fixed part and a programmable part. The programmable part must be set according to the address pins A0, A1 and A2. The address always has to be sent as the first byte after the start condition in the I 2 C-bus protocol. The last bit of the address byte is the read/write-bit which sets the direction of the following data transfer (see Figs 4). | ||
+ | [http://192.168.0.121/mediawiki/index.php/File:5.Car_avoidobstacle.zip lianjie] | ||
+ | *Figs 4 | ||
+ | https://www.arduino.cc/en/Main/Software | ||
− | ==' | + | =='''Control byte'''=== |
+ | The second byte sent to a PCF8591 device will be stored in its control register and is required to control the device function.Theuppernibbleofthecontrolregisterisusedfor enabling the analog output, and for programming the analog inputs as single-ended or differential inputs.The lower nibble selects one of the analog input channels defined by the upper nibble (see Fig.5).If the auto-increment flag is set, the channel number is incremented automatically after each A/D conversion.<br> | ||
+ | If the auto-increment mode is desired in applications where the internal oscillator is used, the analog output enable flag in the control byte (bit 6) should be set. This allows the internal oscillator to run continuously, thereby preventing conversion errors resulting from oscillator start-up delay. The analog output enable flag may be reset at other times to reduce quiescent power consumption.<br> | ||
+ | The selection of a non-existing input channel results in the highest available channel number being allocated. Therefore, if the auto-increment flag is set, the next selected channel will be always channel 0. The most significant bits of both nibbles are reserved for future functions and have to be set to logic 0. After a Power-on reset condition all bits of the control register are reset to logic 0. The D/A converter and the oscillator are disabled for power saving. The analog output is switched to a high-impedance state.<br> | ||
− | + | *Fig.5 | |
− | + | == '''D/A conversion'''== | |
+ | The third byte sent to a PCF8591 device is stored in the DAC data register and is converted to the corresponding analog voltage using the on-chip D/A converter. This D/A converter consists of a resistor divider chain connected to the external reference voltage with 256 taps and selection switches. The tap-decoder switches one of these taps to the DAC output line (see Fig.6).<br> | ||
+ | The analog output voltage is buffered by an auto-zeroed unity gain amplifier. This buffer amplifier may be switched on or off by setting the analog output enable flag of thecontrol register. In the active state the output voltage is held until a further data byte is sent.<br> | ||
+ | The on-chip D/A converter is also used for successive approximation A/D conversion. In order to release the DAC for an A/D conversion cycle the unity gain amplifier is equippedwithatrackandholdcircuit.Thiscircuitholdsthe output voltage while executing the A/D conversion.<br> | ||
+ | The output voltage supplied to the analog output AOUT is given by the formula shown in Fig.7. The waveforms of a D/A conversion sequence are shown in Fig.8. | ||
+ | {| border="1" class="wikitable" | ||
+ | |- | ||
+ | ! scope="col" | Pin | ||
+ | ! scope="col" | Name | ||
+ | ! scope="col" | Function and Note | ||
+ | |- | ||
+ | | 1 | ||
+ | | Reset | ||
+ | | A switch that would reset the SeeeduinoMega | ||
+ | |- | ||
+ | | 2 | ||
+ | | 3.3V | ||
+ | | 3.3V Source | ||
+ | |- | ||
+ | | 3 | ||
+ | | 5V | ||
+ | | 5V Source | ||
+ | |- | ||
+ | | 4 | ||
+ | | Gnd | ||
+ | | Ground | ||
+ | |- | ||
+ | | 5<br> | ||
+ | | Vin<br> | ||
+ | | A connection to the main source, this is used when the shield's supply is to be taken from the main power source | ||
+ | |- | ||
+ | | 0~7 | ||
+ | | ADC / GPIO:PF0-PF7 | ||
+ | | Analog to Digital channels multiplexed with Port-F, used to interface with analog sensors like potentiometers, voltage , current, temperature, pressure, humidity sensors as well as analog gyroscopes and accelerometers | ||
+ | |- | ||
+ | | 8~9 | ||
+ | | GPIO:PH5-PH6 | ||
+ | | General Purpose Input Output Pins<br> | ||
+ | |- | ||
+ | | 10~13<br> | ||
+ | | GPIO:PB4-PB7<br> | ||
+ | | General Purpose Input Output Pins<br> | ||
+ | |- | ||
+ | | 14<br> | ||
+ | | GND<br> | ||
+ | | A connection to the ground<br> | ||
+ | |- | ||
+ | | 15<br> | ||
+ | | AREF<br> | ||
+ | | The analog reference used as reference for the Seeeduino Mega’s ADC channels, Analog reference is decoupled to the ground using a capacitor for stability purposes.<br> | ||
+ | |- | ||
+ | | 0<br> | ||
+ | | GPIO:PE0/RX0<br> | ||
+ | | Receive channel for USART0<br> | ||
+ | |- | ||
+ | | 1<br> | ||
+ | | GPIO:PE1/TX0<br> | ||
+ | | Transmit channel for USART0<br> | ||
+ | |- | ||
+ | | 2~3<br> | ||
+ | | GPIO:PE4-PE5<br> | ||
+ | | General Purpose Input Output Pins<br> | ||
+ | |- | ||
+ | | 4<br> | ||
+ | | GPIO:PG5<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |- | ||
+ | | 5<br> | ||
+ | | GPIO:PE3<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |- | ||
+ | | 6~7<br> | ||
+ | | GPIO:PH3-PH4<br> | ||
+ | | General Purpose Input Output Pins<br> | ||
+ | |- | ||
+ | | <br> | ||
+ | | ICSP<br> | ||
+ | | <br> | ||
+ | |- | ||
+ | | 8~15<br> | ||
+ | | ADC / GPIO:PK0-PK7<br> | ||
+ | | 8 Analog to Digital channels multiplexed with Port-K<br><br> | ||
+ | |- | ||
+ | | 1<br> | ||
+ | | RXD1 / GPIO:PD2<br> | ||
+ | | Receive channel for USART1<br> | ||
+ | |- | ||
+ | | 2<br> | ||
+ | | TXD1 / GPIO:PD3 <br> | ||
+ | | Transmit channel for USART1<br> | ||
+ | |- | ||
+ | | 3<br> | ||
+ | | RXD2 / GPIO:PH0<br> | ||
+ | | Receive channel for USART2<br> | ||
+ | |- | ||
+ | | 4<br> | ||
+ | | TXD2 / GPIO:PH1<br> | ||
+ | | Transmit channel for USART2<br> | ||
+ | |- | ||
+ | | 5<br> | ||
+ | | RXD3 / GPIO:PJ0 <br> | ||
+ | | Receive channel for USART3<br> | ||
+ | |- | ||
+ | | 6<br> | ||
+ | | TXD3 / GPIO:PJ1<br> | ||
+ | | Transmit channel for USART3<br> | ||
+ | |- | ||
+ | | I2C<br> | ||
+ | | <br> | ||
+ | | Also known as the Two Wire Interface, I2C is an industry standard communication protocol that is used to communicate with ADCs, EEPROMs, DACs, sensors, and microcontrollers.<br> | ||
+ | |- | ||
+ | | 1<br> | ||
+ | | Vcc<br> | ||
+ | | <br> | ||
+ | |- | ||
+ | | 2<br> | ||
+ | | GND<br> | ||
+ | | <br> | ||
+ | |- | ||
+ | | 3<br> | ||
+ | | SCL / GPIO:PD0<br> | ||
+ | | I2C-Clock<br> | ||
+ | |- | ||
+ | | 4<br> | ||
+ | | SDA / GPIO:PD1<br> | ||
+ | | I2C-Serial Data<br> | ||
+ | |- | ||
+ | | 22~29<br> | ||
+ | | GPIO:PA0-PA7<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | 30-37<br> | ||
+ | | GPIO:PC0-PC7<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | 38<br> | ||
+ | | GPIO:PD7<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |- | ||
+ | | 39~41<br> | ||
+ | | GPIO:PG2 - PG0<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | 42~45<br> | ||
+ | | GPIO:PL7 - PL4<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | 46~49<br> | ||
+ | | GPIO:PL3 - PL0<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | SPI<br> | ||
+ | | <br> | ||
+ | | <br> | ||
+ | |- | ||
+ | | 50 <br> | ||
+ | | MISO / GPIO:PB3<br> | ||
+ | | SPI - Master In Slave Out<br> | ||
+ | |- | ||
+ | | 51<br> | ||
+ | | MOSI / GPIO:PB2<br> | ||
+ | | SPI - Master Out Slave In<br> | ||
+ | |- | ||
+ | | 52<br> | ||
+ | | SCK / GPIO:PB1<br> | ||
+ | | SPI - Clock<br> | ||
+ | |- | ||
+ | | 53<br> | ||
+ | | GPIO:PB0<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |- | ||
+ | | PH2<br> | ||
+ | | GPIO:PH2<br> | ||
+ | | General Purpose Input Output Pin | ||
+ | |- | ||
+ | | PH7<br> | ||
+ | | GPIO:PH7<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |- | ||
+ | | PJ2~PJ7<br> | ||
+ | | GPIO:PJ2-PJ7<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | PD4~PD6<br> | ||
+ | | GPIO:PD4-PD6<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | PG4~PG3<br> | ||
+ | | GPIO:PG4-PG3<br> | ||
+ | | General Purpose Input Output Pins <br> | ||
+ | |- | ||
+ | | PE7<br> | ||
+ | | GPIO:PE7<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |- | ||
+ | | PE6<br> | ||
+ | | GPIO:PE6<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |- | ||
+ | | PE2<br> | ||
+ | | GPIO:PE2<br> | ||
+ | | General Purpose Input Output Pin<br> | ||
+ | |} | ||
− | + | *Fig.6 | |
− | + | **Fig.7 | |
− | + | ***Fig.8 | |
+ | The A/D converter makes use of the successive approximation conversion technique. The on-chip D/A converter and a high-gain comparator are used temporarily during an A/D conversion cycle.<br> | ||
+ | An A/D conversion cycle is always started after sending a valid read mode address to a PCF8591 device. The A/D conversion cycle is triggered at the trailing edge of the acknowledge clock pulse and is executed while transmitting the result of the previous conversion (see Fig.9).<br> | ||
+ | Once a conversion cycle is triggered an input voltage sample of the selected channel is stored on the chip and is converted to the corresponding 8-bit binary code.Samples picked up from differential inputs are converted to an 8-bit twos complement code (see Figs 10 and 11).<br> | ||
+ | The conversion result is stored in the ADC data register and awaits transmission. If the auto-increment flag is set the next channel is selected.<br> | ||
+ | The first byte transmitted in a read cycle contains the conversion result code of the previous read cycle. After a Power-on reset condition the first byte read is a hexadecimal 80. The maximum A/D conversion rate is given by the actual speed of the I 2 C-bus.<br> | ||
− | + | #Fig.9 | |
+ | |||
+ | #*Fig.10 | ||
+ | |||
+ | #*Fig.11 | ||
+ | |||
+ | ;Reference voltage | ||
+ | :For the D/A and A/D conversion either a stable external voltage reference or the supply voltage has to be applied to the resistor divider chain (pins V REF and AGND). The AGND pin has to be connected to the system analog ground and may have a DC off-set with reference to V SS . A low frequency may be applied to the V REF and AGND pins. This allows the use of the D/A converter as a one-quadrant multiplier; see Chapter 15 and Fig.7. The A/D converter may also be used as a one or two quadrant analog divider. The analog input voltage is divided by the reference voltage. The result is converted to a binary code. In this application the user has to keep the reference voltage stable during the conversion cycle. | ||
+ | |||
+ | ===Oscillator=== | ||
+ | <pre>//Turns on and off a LED ,when pressings button attach to pin12 | ||
+ | /**********************************/ | ||
+ | const int keyPin = 12; //the number of the key pin | ||
+ | const int ledPin = 13;//the number of the led pin | ||
+ | /**********************************/ | ||
+ | void setup() | ||
+ | { | ||
+ | pinMode(keyPin,INPUT);//initialize the key pin as input | ||
+ | pinMode(ledPin,OUTPUT);//initialize the led pin as output | ||
+ | } | ||
+ | /**********************************/ | ||
+ | void loop() | ||
+ | { | ||
+ | //read the state of the key value | ||
+ | //and check if the kye is pressed | ||
+ | //if it is,the state is HIGH | ||
+ | if(digitalRead(keyPin) ==HIGH ) | ||
+ | { | ||
+ | digitalWrite(ledPin,HIGH);//turn on the led | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | digitalWrite(ledPin,LOW);//turn off the led | ||
+ | } | ||
+ | } | ||
+ | /************************************/</pre> | ||
+ | [[Getting Started:Arduino]] | ||
+ | [[Getting Started:Getting Started with Seeeduino]] | ||
+ | [[Category:Arduino Compatible]] | ||
+ | [[Category:MicroControllers]] |
Latest revision as of 03:47, 7 November 2015
Addressing
EachPCF8591deviceinanI 2 C-bussystemisactivatedby sending a valid address to the device. The address consists of a fixed part and a programmable part. The programmable part must be set according to the address pins A0, A1 and A2. The address always has to be sent as the first byte after the start condition in the I 2 C-bus protocol. The last bit of the address byte is the read/write-bit which sets the direction of the following data transfer (see Figs 4). lianjie
- Figs 4
https://www.arduino.cc/en/Main/Software
Control byte=
The second byte sent to a PCF8591 device will be stored in its control register and is required to control the device function.Theuppernibbleofthecontrolregisterisusedfor enabling the analog output, and for programming the analog inputs as single-ended or differential inputs.The lower nibble selects one of the analog input channels defined by the upper nibble (see Fig.5).If the auto-increment flag is set, the channel number is incremented automatically after each A/D conversion.
If the auto-increment mode is desired in applications where the internal oscillator is used, the analog output enable flag in the control byte (bit 6) should be set. This allows the internal oscillator to run continuously, thereby preventing conversion errors resulting from oscillator start-up delay. The analog output enable flag may be reset at other times to reduce quiescent power consumption.
The selection of a non-existing input channel results in the highest available channel number being allocated. Therefore, if the auto-increment flag is set, the next selected channel will be always channel 0. The most significant bits of both nibbles are reserved for future functions and have to be set to logic 0. After a Power-on reset condition all bits of the control register are reset to logic 0. The D/A converter and the oscillator are disabled for power saving. The analog output is switched to a high-impedance state.
- Fig.5
D/A conversion
The third byte sent to a PCF8591 device is stored in the DAC data register and is converted to the corresponding analog voltage using the on-chip D/A converter. This D/A converter consists of a resistor divider chain connected to the external reference voltage with 256 taps and selection switches. The tap-decoder switches one of these taps to the DAC output line (see Fig.6).
The analog output voltage is buffered by an auto-zeroed unity gain amplifier. This buffer amplifier may be switched on or off by setting the analog output enable flag of thecontrol register. In the active state the output voltage is held until a further data byte is sent.
The on-chip D/A converter is also used for successive approximation A/D conversion. In order to release the DAC for an A/D conversion cycle the unity gain amplifier is equippedwithatrackandholdcircuit.Thiscircuitholdsthe output voltage while executing the A/D conversion.
The output voltage supplied to the analog output AOUT is given by the formula shown in Fig.7. The waveforms of a D/A conversion sequence are shown in Fig.8.
Pin | Name | Function and Note |
---|---|---|
1 | Reset | A switch that would reset the SeeeduinoMega |
2 | 3.3V | 3.3V Source |
3 | 5V | 5V Source |
4 | Gnd | Ground |
5 |
Vin |
A connection to the main source, this is used when the shield's supply is to be taken from the main power source |
0~7 | ADC / GPIO:PF0-PF7 | Analog to Digital channels multiplexed with Port-F, used to interface with analog sensors like potentiometers, voltage , current, temperature, pressure, humidity sensors as well as analog gyroscopes and accelerometers |
8~9 | GPIO:PH5-PH6 | General Purpose Input Output Pins |
10~13 |
GPIO:PB4-PB7 |
General Purpose Input Output Pins |
14 |
GND |
A connection to the ground |
15 |
AREF |
The analog reference used as reference for the Seeeduino Mega’s ADC channels, Analog reference is decoupled to the ground using a capacitor for stability purposes. |
0 |
GPIO:PE0/RX0 |
Receive channel for USART0 |
1 |
GPIO:PE1/TX0 |
Transmit channel for USART0 |
2~3 |
GPIO:PE4-PE5 |
General Purpose Input Output Pins |
4 |
GPIO:PG5 |
General Purpose Input Output Pin |
5 |
GPIO:PE3 |
General Purpose Input Output Pin |
6~7 |
GPIO:PH3-PH4 |
General Purpose Input Output Pins |
|
ICSP |
|
8~15 |
ADC / GPIO:PK0-PK7 |
8 Analog to Digital channels multiplexed with Port-K |
1 |
RXD1 / GPIO:PD2 |
Receive channel for USART1 |
2 |
TXD1 / GPIO:PD3 |
Transmit channel for USART1 |
3 |
RXD2 / GPIO:PH0 |
Receive channel for USART2 |
4 |
TXD2 / GPIO:PH1 |
Transmit channel for USART2 |
5 |
RXD3 / GPIO:PJ0 |
Receive channel for USART3 |
6 |
TXD3 / GPIO:PJ1 |
Transmit channel for USART3 |
I2C |
|
Also known as the Two Wire Interface, I2C is an industry standard communication protocol that is used to communicate with ADCs, EEPROMs, DACs, sensors, and microcontrollers. |
1 |
Vcc |
|
2 |
GND |
|
3 |
SCL / GPIO:PD0 |
I2C-Clock |
4 |
SDA / GPIO:PD1 |
I2C-Serial Data |
22~29 |
GPIO:PA0-PA7 |
General Purpose Input Output Pins |
30-37 |
GPIO:PC0-PC7 |
General Purpose Input Output Pins |
38 |
GPIO:PD7 |
General Purpose Input Output Pin |
39~41 |
GPIO:PG2 - PG0 |
General Purpose Input Output Pins |
42~45 |
GPIO:PL7 - PL4 |
General Purpose Input Output Pins |
46~49 |
GPIO:PL3 - PL0 |
General Purpose Input Output Pins |
SPI |
|
|
50 |
MISO / GPIO:PB3 |
SPI - Master In Slave Out |
51 |
MOSI / GPIO:PB2 |
SPI - Master Out Slave In |
52 |
SCK / GPIO:PB1 |
SPI - Clock |
53 |
GPIO:PB0 |
General Purpose Input Output Pin |
PH2 |
GPIO:PH2 |
General Purpose Input Output Pin |
PH7 |
GPIO:PH7 |
General Purpose Input Output Pin |
PJ2~PJ7 |
GPIO:PJ2-PJ7 |
General Purpose Input Output Pins |
PD4~PD6 |
GPIO:PD4-PD6 |
General Purpose Input Output Pins |
PG4~PG3 |
GPIO:PG4-PG3 |
General Purpose Input Output Pins |
PE7 |
GPIO:PE7 |
General Purpose Input Output Pin |
PE6 |
GPIO:PE6 |
General Purpose Input Output Pin |
PE2 |
GPIO:PE2 |
General Purpose Input Output Pin |
- Fig.6
- Fig.7
- Fig.8
The A/D converter makes use of the successive approximation conversion technique. The on-chip D/A converter and a high-gain comparator are used temporarily during an A/D conversion cycle.
An A/D conversion cycle is always started after sending a valid read mode address to a PCF8591 device. The A/D conversion cycle is triggered at the trailing edge of the acknowledge clock pulse and is executed while transmitting the result of the previous conversion (see Fig.9).
Once a conversion cycle is triggered an input voltage sample of the selected channel is stored on the chip and is converted to the corresponding 8-bit binary code.Samples picked up from differential inputs are converted to an 8-bit twos complement code (see Figs 10 and 11).
The conversion result is stored in the ADC data register and awaits transmission. If the auto-increment flag is set the next channel is selected.
The first byte transmitted in a read cycle contains the conversion result code of the previous read cycle. After a Power-on reset condition the first byte read is a hexadecimal 80. The maximum A/D conversion rate is given by the actual speed of the I 2 C-bus.
- Fig.9
- Fig.10
- Fig.11
- Reference voltage
- For the D/A and A/D conversion either a stable external voltage reference or the supply voltage has to be applied to the resistor divider chain (pins V REF and AGND). The AGND pin has to be connected to the system analog ground and may have a DC off-set with reference to V SS . A low frequency may be applied to the V REF and AGND pins. This allows the use of the D/A converter as a one-quadrant multiplier; see Chapter 15 and Fig.7. The A/D converter may also be used as a one or two quadrant analog divider. The analog input voltage is divided by the reference voltage. The result is converted to a binary code. In this application the user has to keep the reference voltage stable during the conversion cycle.
Oscillator
//Turns on and off a LED ,when pressings button attach to pin12 /**********************************/ const int keyPin = 12; //the number of the key pin const int ledPin = 13;//the number of the led pin /**********************************/ void setup() { pinMode(keyPin,INPUT);//initialize the key pin as input pinMode(ledPin,OUTPUT);//initialize the led pin as output } /**********************************/ void loop() { //read the state of the key value //and check if the kye is pressed //if it is,the state is HIGH if(digitalRead(keyPin) ==HIGH ) { digitalWrite(ledPin,HIGH);//turn on the led } else { digitalWrite(ledPin,LOW);//turn off the led } } /************************************/
Getting Started:Arduino Getting Started:Getting Started with Seeeduino