2.1.28, initiation update to PXT v5.28.24 (#54)
This commit is contained in:
committed by
Peli de Halleux
parent
38a964516e
commit
5c114a0c57
@ -1,31 +1,33 @@
|
||||
# Analog Pitch
|
||||
|
||||
Emits a Pulse With Modulation (PWM) signal to the current pitch [pin](/device/pins). Use [analog set pitch pin](/reference/pins/analog-set-pitch-pin) to set the current pitch pin.
|
||||
Emits a Pulse With Modulation (PWM) signal to the pin ``P0``.
|
||||
Use [analog set pitch pin](/reference/pins/analog-set-pitch-pin) to set the current pitch pin.
|
||||
|
||||
```sig
|
||||
pins.analogPitch(440, 300)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* `frequency` : [Number](/reference/types/number)
|
||||
* `ms`: [Number](/reference/types/number)
|
||||
* `frequency` : [Number](/types/number)
|
||||
* `ms`: [Number](/types/number)
|
||||
|
||||
### Example
|
||||
## Example
|
||||
|
||||
```blocks
|
||||
pins.analogSetPitchPin("P0")
|
||||
pins.analogSetPitchPin(AnalogPin.P0);
|
||||
let frequency1 = 440
|
||||
let duration = 1000
|
||||
pins.analogSetPitchPin(AnalogPin.P1);
|
||||
pins.analogPitch(frequency1, duration)
|
||||
```
|
||||
|
||||
### Some common notes
|
||||
## Some common notes
|
||||
|
||||
* 440 = A4 on piano
|
||||
* see [piano key frequencies ](https://en.wikipedia.org/wiki/Piano_key_frequencies) for more information
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins), [analog set period](/reference/pins/analog-set-period), [analog set pitch pin](/reference/pins/analog-set-pitch-pin)
|
||||
|
||||
|
@ -4,17 +4,17 @@ Read an **analog** signal (`0` through `1023`) from the
|
||||
[pin](/device/pins) you say.
|
||||
|
||||
```sig
|
||||
pins.analogReadPin(AnalogPin.P1)
|
||||
pins.analogReadPin(AnalogPin.P0)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name`` is a [string](/reference/types/string) with the name of the pin
|
||||
* ``name`` is a [string](/types/string) with the name of the pin
|
||||
you say (`P0` through `P4`, or `P10`)
|
||||
|
||||
### Returns
|
||||
## Returns
|
||||
|
||||
* a [number](/reference/types/number) from `0` through `1023`
|
||||
* a [number](/types/number) from `0` through `1023`
|
||||
|
||||
This program reads pin `P1` and shows the number
|
||||
on the LED screen.
|
||||
@ -26,13 +26,13 @@ basic.forever(() => {
|
||||
});
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
### ~hint
|
||||
|
||||
If you are using **analog read pin** with another @boardname@ running **analog write pin**, then things can get tricky. Remember that the @boardname@ that runs **analog set pin** writes 0's and 1's at a very high frequency to achieve an average of the desired value. Sadly, if you try to read that average from another @boardname@, then the @boardname@ will either read 0 or 1023. You could try to read a higher number of values (e.g. a million) in a loop, then computer then average. Alternatively, you can plug in a capacitor in-between the two @boardname@s.
|
||||
|
||||
#### ~
|
||||
### ~
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins),
|
||||
[on pin pressed](/reference/input/on-pin-pressed),
|
||||
|
@ -5,23 +5,23 @@ analog [pin](/device/pins).
|
||||
Before you call this function, you should set the specified pin as analog.
|
||||
|
||||
```sig
|
||||
pins.analogSetPeriod(AnalogPin.P1, 20000)
|
||||
pins.analogSetPeriod(AnalogPin.P0, 20000)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name``: a [string](/reference/types/string) that specifies the pin to configure (`P0` through `P4`, or `P10`)
|
||||
* ``micros``: a [number](/reference/types/number) that specifies the analog period in microseconds.
|
||||
* ``name``: a [string](/types/string) that specifies the pin to configure (`P0` through `P4`, or `P10`)
|
||||
* ``micros``: a [number](/types/number) that specifies the analog period in microseconds.
|
||||
|
||||
The following code first sets `P0` to analog with **analog write
|
||||
pin**, and then sets the PWM period of `P0` to 20,000 microseconds.
|
||||
|
||||
```blocks
|
||||
pins.analogWritePin(AnalogPin.P1, 512)
|
||||
pins.analogSetPeriod(AnalogPin.P1, 20000)
|
||||
pins.analogWritePin(AnalogPin.P0, 512)
|
||||
pins.analogSetPeriod(AnalogPin.P0, 20000)
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins),
|
||||
[on pin pressed](/reference/input/on-pin-pressed),
|
||||
|
@ -3,28 +3,28 @@
|
||||
Specify which [pin](/device/pins) (P0, P1, P2) is used to generate tones.
|
||||
|
||||
```sig
|
||||
pins.analogSetPitchPin(AnalogPin.P1)
|
||||
pins.analogSetPitchPin(AnalogPin.P0)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* `name` - [String](/reference/types/string); the pin name ("P0", "P1", or "P2")
|
||||
* `name` - [String](/types/string); the pin name ("P0", "P1", or "P2")
|
||||
|
||||
### Example
|
||||
## Example
|
||||
|
||||
```blocks
|
||||
pins.analogSetPitchPin(AnalogPin.P1)
|
||||
pins.analogSetPitchPin(AnalogPin.P0)
|
||||
let frequency = 440
|
||||
let duration = 1000
|
||||
pins.analogPitch(frequency, duration)
|
||||
```
|
||||
|
||||
### Some common notes
|
||||
## Some common notes
|
||||
|
||||
* 440 = A4 on piano
|
||||
* see [piano key frequencies ](https://en.wikipedia.org/wiki/Piano_key_frequencies) for more information
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins), [analog set period](/reference/pins/analog-set-period), [analog pitch](/reference/pins/analog-pitch)
|
||||
|
||||
|
@ -4,31 +4,31 @@ Write an **analog** signal (`0` through `1023`) to the
|
||||
[pin](/device/pins) you say.
|
||||
|
||||
```sig
|
||||
pins.analogWritePin(AnalogPin.P1, 400)
|
||||
pins.analogWritePin(AnalogPin.P0, 400)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name`` is a [string](/reference/types/string) that is the pin name you say (`P0` through `P4`, or `P10`)
|
||||
* ``value`` is a [number](/reference/types/number) from `0` through `1023`
|
||||
* ``name`` is a [string](/types/string) that is the pin name you say (`P0` through `P4`, or `P10`)
|
||||
* ``value`` is a [number](/types/number) from `0` through `1023`
|
||||
|
||||
### Example
|
||||
## Example
|
||||
|
||||
This program writes `1023` to pin `P0`.
|
||||
|
||||
```blocks
|
||||
pins.analogWritePin(AnalogPin.P1, 1023)
|
||||
pins.analogWritePin(AnalogPin.P0, 1023)
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
### ~hint
|
||||
|
||||
When you tell it to write `256` (for example), this function does not
|
||||
_really_ write `256`. Instead, it writes a lot of different numbers,
|
||||
and their average is `256`.
|
||||
|
||||
#### ~
|
||||
### ~
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins), [on pin pressed](/reference/input/on-pin-pressed), [analog read pin](/reference/pins/analog-read-pin), [digital read pin](/reference/pins/digital-read-pin), [digital write pin](/reference/pins/digital-write-pin)
|
||||
|
||||
|
@ -7,22 +7,22 @@ the @boardname@ board.
|
||||
pins.digitalReadPin(DigitalPin.P3)
|
||||
```
|
||||
|
||||
### ~avatar
|
||||
## ~avatar
|
||||
|
||||
Some pins are also used by the [LED screen](/device/screen).
|
||||
Please read the [page about pins](/device/pins) carefully.
|
||||
|
||||
### ~
|
||||
## ~
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name`` is a [string](/reference/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
|
||||
* ``name`` is a [string](/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
|
||||
|
||||
### Returns
|
||||
## Returns
|
||||
|
||||
* a [number](/reference/types/number) that can be `0` or `1`
|
||||
* a [number](/types/number) that can be `0` or `1`
|
||||
|
||||
### Example: football score keeper
|
||||
## Example: football score keeper
|
||||
|
||||
This program reads pin `P0` to find when a goal is scored. When `P0`
|
||||
is `1`, the program makes the score bigger and plays a buzzer sound
|
||||
@ -54,13 +54,13 @@ input.onButtonPressed(Button.B, () => {
|
||||
pins.digitalWritePin(DigitalPin.P1, 0);
|
||||
});
|
||||
```
|
||||
#### ~hint
|
||||
### ~hint
|
||||
|
||||
Remember to connect `GND` on both @boardname@s together!
|
||||
|
||||
#### ~
|
||||
### ~
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins),
|
||||
[digital write pin](/reference/pins/digital-write-pin),
|
||||
|
@ -7,19 +7,19 @@ the @boardname@ board.
|
||||
pins.digitalWritePin(DigitalPin.P1, 1)
|
||||
```
|
||||
|
||||
### ~avatar
|
||||
## ~avatar
|
||||
|
||||
Some pins are also used by the [LED screen](/device/screen).
|
||||
Please read the [page about pins](/device/pins) carefully.
|
||||
|
||||
### ~
|
||||
## ~
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name`` is a [string](/reference/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
|
||||
* ``value`` is a [number](/reference/types/number) that can be either `0` or `1`
|
||||
* ``name`` is a [string](/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
|
||||
* ``value`` is a [number](/types/number) that can be either `0` or `1`
|
||||
|
||||
### Example: football score keeper
|
||||
## Example: football score keeper
|
||||
|
||||
This program reads pin `P0` to find when a goal is scored. When `P0`
|
||||
is `1`, the program makes the score bigger and plays a buzzer sound
|
||||
@ -53,7 +53,7 @@ input.onButtonPressed(Button.B, () => {
|
||||
});
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins),
|
||||
[digital read pin](/reference/pins/digital-read-pin),
|
||||
|
57
docs/reference/pins/i2c-read-buffer.md
Normal file
57
docs/reference/pins/i2c-read-buffer.md
Normal file
@ -0,0 +1,57 @@
|
||||
# i2c Read Buffer
|
||||
|
||||
Read data into a buffer from a device at an I2C address.
|
||||
|
||||
```sig
|
||||
pins.i2cReadBuffer(0, 0, false);
|
||||
```
|
||||
|
||||
A device connected to the I2C pins on the @boardname@ at the address is selected to read data from. If it has data available to transfer, the data is received and copied into a buffer for your program to use. Your program says how big (how many bytes to receive) the buffer should be. You won't get back that many bytes of data if the connected device has less to send than what you asked for.
|
||||
|
||||
### ~ hint
|
||||
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### ~
|
||||
|
||||
## Parameters
|
||||
|
||||
* **address**: the 7-bit I2C address to read the data from.
|
||||
* **size**: the [number](/types/number) of bytes to read into the buffer from the device.
|
||||
* **repeated**: if `true`, don't send a stop condition after the read. Otherwise, a stop condition is sent when `false` (the default).
|
||||
|
||||
### ~ hint
|
||||
|
||||
#### Repeated start
|
||||
|
||||
A [repeated start condition](http://www.i2c-bus.org/repeated-start-condition/) is set to help make sure that when you want to read data miltiple times from the device at once, it can happen without interruption. A start conditon is sent (if **repeated** is `true`) each time a buffer is read without a matching stop condition. When the last buffer is read, the stop conditon can be sent by setting **repeated** to `false`. For single reads, don't use **repeated** or set it to `false`.
|
||||
|
||||
#### Reserved addresses
|
||||
|
||||
Some sensors on your @boardname@ use the same I2C bus that is connected to the pins that you program. This means that you should be careful to **NOT** use an address for your device that is the same as the any of the ones used by the sensors on the board. Check the [I2C sensor addresses](https://tech.microbit.org/hardware/i2c/) list before you assign one to your device. This will help you keep the addresses separate.
|
||||
|
||||
#### Bus address format
|
||||
|
||||
The @boardname@ uses 7-bit values to address the devices connected on the I2C bus. Before an address is transmitted, it is adjusted temporarily to an 8-bit value so that the valid address bits are sent properly. This means that the value of an 8-bit address present on the bus will appear as twice that of what you specified. This is fine though, since the device you are addressing will decode it to match the address you gave. If your device address is specified as an 8-bit address, you will need to use an address that is half that value when you read to or write from it.
|
||||
|
||||
### ~
|
||||
|
||||
## Returns
|
||||
|
||||
* a [buffer](/types/buffer) that contains the data read from the device at the I2C address. The number of bytes returned to you is less than or equal to amount you asked for in the **size** parameter.
|
||||
|
||||
## Example
|
||||
|
||||
Tell a device connected to the I2C pins with the address of `141` to respond to a _read status_ command. The device sends the status data on the I2C wires if receives a command byte equal to `0`. `32` bytes of status data are read into a buffer.
|
||||
|
||||
```blocks
|
||||
const i2cDevice = 141;
|
||||
pins.i2cWriteNumber(i2cDevice, NumberFormat.UInt8LE, 0)
|
||||
let i2cBuffer = pins.i2cReadBuffer(i2cDevice, 32, false);
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[i2c write buffer](/reference/pins/i2c-write-buffer), [buffer](/types/buffer)
|
||||
|
||||
[What's I2C?](http://www.i2c-bus.org/)
|
@ -1,38 +1,70 @@
|
||||
# I2C Read Number
|
||||
# i2c Read Number
|
||||
|
||||
Read one number from the specified 7-bit I2C address, in the specified
|
||||
number format.
|
||||
Read one number from an I2C address using a specified number format.
|
||||
|
||||
```sig
|
||||
pins.i2cReadNumber(0, NumberFormat.Int8LE);
|
||||
pins.i2cReadNumber(0, NumberFormat.Int8LE, false);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
### ~ hint
|
||||
|
||||
* ``address``: the 7-bit I2C address from which to read the number.
|
||||
* ``format``: the number format. Formats include
|
||||
**Int8LE**, **UInt8LE**, **Int16LE**, **UInt16LE**, **Int32LE**,
|
||||
**Int8BE**, **UInt8BE**, **Int16BE**, **UInt16BE**, and
|
||||
**Int32BE**.
|
||||
* **Int** stands for "integer", and **UInt** stands for "unsigned integer".
|
||||
* **LE** stands for "little-endian" and **BE** stands for "big-endian".
|
||||
* The number in each format name stands for the number of bits in the format.
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### Example
|
||||
### ~
|
||||
|
||||
## Parameters
|
||||
|
||||
* **address**: the 7-bit I2C address of the device you want to read a number from.
|
||||
* **format**: the [NumberFormat](/types/buffer/number-format) of the number value to read.
|
||||
* **repeated**: if `true`, don't send a stop condition after the read. Otherwise, a stop condition is sent when `false` (the default).
|
||||
|
||||
### ~ hint
|
||||
|
||||
#### Repeated start
|
||||
|
||||
A [repeated start condition](http://www.i2c-bus.org/repeated-start-condition/) is set to help make sure that when you want to read multiple numbers from the device at one time, it can happen without interruption. A start conditon is sent (if **repeated** is `true`) each time a number is read without a matching stop condition. When the last number is read, the stop conditon can be sent by setting **repeated** to `false`. For single reads, don't use **repeated** or set it to `false`.
|
||||
|
||||
#### Reserved addresses
|
||||
|
||||
Some sensors on your @boardname@ use the same I2C bus that is connected to the pins that you program. This means that you should be careful to **NOT** use an address for your device that is the same as the any of the ones used by the sensors on the board. Check the [I2C sensor addresses](https://tech.microbit.org/hardware/i2c/) list before you assign one to your device. This will help you keep the addresses separate.
|
||||
|
||||
#### Bus address format
|
||||
|
||||
The @boardname@ uses 7-bit values to address the devices connected on the I2C bus. Before an address is transmitted, it is adjusted temporarily to an 8-bit value so that the valid address bits are sent properly. This means that the value of an 8-bit address present on the bus will appear as twice that of what you specified. This is fine though, since the device you are addressing will decode it to match the address you gave. If your device address is specified as an 8-bit address, you will need to use an address that is half that value when you read to or write from it.
|
||||
|
||||
### ~
|
||||
|
||||
## Returns
|
||||
|
||||
* a number from the device with the [NumberFormat](/types/buffer/number-format) you asked for.
|
||||
|
||||
## Examples
|
||||
|
||||
### Read a big endian number from a device
|
||||
|
||||
The following example reads a number in big-endian, 16-bit, unsigned integer
|
||||
format from the 7-bit I2C address `32`.
|
||||
|
||||
Read a number from the device at a 7-bit I2C address as a 16-bit number. The `16`, big-endian, and integer chosen for the format.
|
||||
|
||||
```blocks
|
||||
pins.i2cReadNumber(32, NumberFormat.UInt16BE);
|
||||
let inValue = pins.i2cReadNumber(32, NumberFormat.UInt16BE, false);
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
### Repeated reads
|
||||
|
||||
This function is not supported in the simulator.
|
||||
Read three bytes from a device at address `33` at one time.
|
||||
|
||||
#### ~
|
||||
```blocks
|
||||
let nums: number[] = []
|
||||
|
||||
### See also
|
||||
nums[0] = pins.i2cReadNumber(33, NumberFormat.UInt8LE, true)
|
||||
nums[1] = pins.i2cReadNumber(33, NumberFormat.UInt8LE, true)
|
||||
nums[2] = pins.i2cReadNumber(33, NumberFormat.UInt8LE, false)
|
||||
```
|
||||
|
||||
[I2C](https://en.wikipedia.org/wiki/I%C2%B2C)
|
||||
## See also
|
||||
|
||||
[i2c write number](/reference/pins/i2c-write-number)
|
||||
|
||||
[What's I2C?](http://www.i2c-bus.org/), [number format](/types/buffer/number-format)
|
||||
|
57
docs/reference/pins/i2c-write-buffer.md
Normal file
57
docs/reference/pins/i2c-write-buffer.md
Normal file
@ -0,0 +1,57 @@
|
||||
# i2c Write Buffer
|
||||
|
||||
Write data from buffer to a device at an I2C address.
|
||||
|
||||
```sig
|
||||
pins.i2cWriteBuffer(0, null, false);
|
||||
```
|
||||
|
||||
A device connected to the I2C pins on the @boardname@ at the address is selected to write data to. If the device is ready to take in your data, some or all of the data in your buffer is written to it.
|
||||
|
||||
### ~ hint
|
||||
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### ~
|
||||
|
||||
## Parameters
|
||||
|
||||
* **address**: the 7-bit I2C address to read the data from.
|
||||
* **buffer**: a [buffer](/types/buffer) that contains the data to write to the device at the I2C address.
|
||||
* **repeated**: if `true`, don't send a stop condition after the write. Otherwise, a stop condition is sent when `false` (the default).
|
||||
|
||||
### ~ hint
|
||||
|
||||
#### Repeated start
|
||||
|
||||
A [repeated start condition](http://www.i2c-bus.org/repeated-start-condition/) is set to help make sure that when you want to write data miltiple times from the device at once, it can happen without interruption. A start conditon is sent (if **repeated** is `true`) each time a buffer is written without a matching stop condition. When the last buffer is written, the stop conditon can be sent by setting **repeated** to `false`. For single writes, don't use **repeated** or set it to `false`.
|
||||
|
||||
#### Reserved addresses
|
||||
|
||||
Some sensors on your @boardname@ use the same I2C bus that is connected to the pins that you program. This means that you should be careful to **NOT** use an address for your device that is the same as the any of the ones used by the sensors on the board. Check the [I2C sensor addresses](https://tech.microbit.org/hardware/i2c/) list before you assign one to your device. This will help you keep the addresses separate.
|
||||
|
||||
#### Bus address format
|
||||
|
||||
The @boardname@ uses 7-bit values to address the devices connected on the I2C bus. Before an address is transmitted, it is adjusted temporarily to an 8-bit value so that the valid address bits are sent properly. This means that the value of an 8-bit address present on the bus will appear as twice that of what you specified. This is fine though, since the device you are addressing will decode it to match the address you gave. If your device address is specified as an 8-bit address, you will need to use an address that is half that value when you read to or write from it.
|
||||
|
||||
### ~
|
||||
|
||||
## Example
|
||||
|
||||
Tell a device connected to the I2C pins with the address of `141` to respond to a _read status_ command. The device sends the status data on the I2C wires if receives a command byte equal to `0`. `32` bytes of status data is read into a buffer.
|
||||
|
||||
The third byte is changed and the buffer is sent back to have the device update its status.
|
||||
|
||||
```blocks
|
||||
const i2cDevice = 141;
|
||||
pins.i2cWriteNumber(i2cDevice, NumberFormat.UInt8LE, 0)
|
||||
let i2cBuffer = pins.i2cReadBuffer(i2cDevice, 32, false);
|
||||
i2cBuffer.setNumber(NumberFormat.UInt8LE, 2, 0)
|
||||
pins.i2cWriteBuffer(i2cDevice, i2cBuffer, false)
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[i2c read buffer](/reference/pins/i2c-read-buffer), [buffer](/types/buffer)
|
||||
|
||||
[What's I2C?](http://www.i2c-bus.org/)
|
@ -1,39 +1,61 @@
|
||||
# I2C Write Number
|
||||
# i2c Write Number
|
||||
|
||||
Write the specified number to the specified 7-bit I2C address in the
|
||||
specified number format.
|
||||
Write a number to a device at an I2C address using a specified number format.
|
||||
|
||||
```sig
|
||||
pins.i2cWriteNumber(0, 0, NumberFormat.Int8LE);
|
||||
pins.i2cWriteNumber(0, 0, NumberFormat.Int8LE, true);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
### ~ hint
|
||||
|
||||
* ``address``: the 7-bit I2C address to which to send ``value``
|
||||
* ``value``: the number to send to ``address``
|
||||
* ``format``: the number format for ``value``. Formats include
|
||||
**Int8LE**, **UInt8LE**, **Int16LE**, **UInt16LE**, **Int32LE**,
|
||||
**Int8BE**, **UInt8BE**, **Int16BE**, **UInt16BE**, and
|
||||
**Int32BE**.
|
||||
* **Int** stands for "integer", and **UInt** stands for "unsigned integer".
|
||||
* **LE** stands for "little-endian" and **BE** stands for "big-endian".
|
||||
* The number in each format name stands for the number of bits in the format.
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### Example
|
||||
### ~
|
||||
|
||||
The following example sends the value `2055` to the 7-bit I2C
|
||||
address `32` in big-endian 32-bit integer format.
|
||||
## Parameters
|
||||
|
||||
* **address**: the 7-bit I2C address of the device to send to send **value** to.
|
||||
* **value**: the number to send to **address**.
|
||||
* **format**: the [NumberFormat](/types/buffer/number-format) for **value**.
|
||||
* **repeated**: if `true`, don't send a stop condition after the write. Otherwise, a stop condition is sent when `false` (the default).
|
||||
|
||||
### ~ hint
|
||||
|
||||
#### Repeated start
|
||||
|
||||
A [repeated start condition](http://www.i2c-bus.org/repeated-start-condition/) is set to help make sure that when you want to write multiple numbers from the device at one time, it can happen without interruption. A start conditon is sent (if **repeated** is `true`) each time a number is written without a matching stop condition. When the last number is written, the stop conditon can be sent by setting **repeated** to `false`. For single writes, don't use **repeated** or set it to `false`.
|
||||
|
||||
#### Reserved addresses
|
||||
|
||||
Some sensors on your @boardname@ use the same I2C bus that is connected to the pins that you program. This means that you should be careful to **NOT** use an address for your device that is the same as the any of the ones used by the sensors on the board. Check the [I2C sensor addresses](https://tech.microbit.org/hardware/i2c/) list before you assign one to your device. This will help you keep the addresses separate.
|
||||
|
||||
#### Bus address format
|
||||
|
||||
The @boardname@ uses 7-bit values to address the devices connected on the I2C bus. Before an address is transmitted, it is adjusted temporarily to an 8-bit value so that the valid address bits are sent properly. This means that the value of an 8-bit address present on the bus will appear as twice that of what you specified. This is fine though, since the device you are addressing will decode it to match the address you gave. If your device address is specified as an 8-bit address, you will need to use an address that is half that value when you read to or write from it.
|
||||
### ~
|
||||
|
||||
## Examples
|
||||
|
||||
### Write a big endian number to a device
|
||||
|
||||
Send the value `2055` to the 7-bit I2C address as a 32-bit number. The `32`, big-endian, and integer chosen for the format.
|
||||
|
||||
```blocks
|
||||
pins.i2cWriteNumber(32, 2055, NumberFormat.Int32BE);
|
||||
pins.i2cWriteNumber(32, 2055, NumberFormat.Int32BE, false);
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
### Repeated writes
|
||||
|
||||
This function is not supported in the simulator.
|
||||
Send three byte values to a device at address `33`.
|
||||
|
||||
#### ~
|
||||
```blocks
|
||||
pins.i2cWriteNumber(33, 19, NumberFormat.Int32BE, true);
|
||||
pins.i2cWriteNumber(33, 61, NumberFormat.Int32BE, true);
|
||||
pins.i2cWriteNumber(33, 87, NumberFormat.Int32BE, false);
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[I2C](https://en.wikipedia.org/wiki/I%C2%B2C)
|
||||
[i2c read number](/reference/pins/i2c-read-number)
|
||||
|
||||
[What's I2C?](http://www.i2c-bus.org/), [number format](/types/buffer/number-format)
|
||||
|
@ -14,13 +14,13 @@ calling this function.
|
||||
pins.map(0, 0, 4, 0, 1023);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``value``: a [number](/reference/types/number) that specifies the value to map
|
||||
* ``fromLow``: a [number](/reference/types/number) that specifies the lower bound of the origin interval
|
||||
* ``fromHigh``: a [number](/reference/types/number) that specifies the upper bound of the origin interval
|
||||
* ``toLow``: a [number](/reference/types/number) that specifies the lower bound of the target interval
|
||||
* ``toHigh``: a [number](/reference/types/number) that specifies the upper bound of the target interval
|
||||
* ``value``: a [number](/types/number) that specifies the value to map
|
||||
* ``fromLow``: a [number](/types/number) that specifies the lower bound of the origin interval
|
||||
* ``fromHigh``: a [number](/types/number) that specifies the upper bound of the origin interval
|
||||
* ``toLow``: a [number](/types/number) that specifies the lower bound of the target interval
|
||||
* ``toHigh``: a [number](/types/number) that specifies the upper bound of the target interval
|
||||
|
||||
## Example
|
||||
|
||||
@ -28,12 +28,12 @@ This example maps the value read from the analog pin `P0` to an LED
|
||||
coordinate between `0` and `4`.
|
||||
|
||||
```blocks
|
||||
let value1 = pins.analogReadPin(AnalogPin.P1)
|
||||
let value1 = pins.analogReadPin(AnalogPin.P0)
|
||||
let index = pins.map(value1, 0, 1023, 0, 4)
|
||||
led.plot(0, index)
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[analog read pin](/reference/pins/analog-read-pin)
|
||||
|
||||
|
@ -1,22 +1,26 @@
|
||||
# On Pulsed
|
||||
# on Pulsed
|
||||
|
||||
Configure the specified pin for digital input, and then
|
||||
execute the associated code block whenever the pin
|
||||
pulses **High** or **Low** (as specified).
|
||||
Set a pin to use as a digital input and then run some code when the pin pulses either ``high`` or ``low``.
|
||||
|
||||
```sig
|
||||
pins.onPulsed(DigitalPin.P0, PulseValue.High, () => { });
|
||||
```
|
||||
|
||||
### Parameters
|
||||
### ~ hint
|
||||
|
||||
* ``name``: The @boardname@ hardware pin to configure (``P0`` through ``P20``)
|
||||
* ``pulse``: Which state will cause the associated block to execute (**High** or **Low**)
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### Example
|
||||
### ~
|
||||
|
||||
The following example configures pin ``P2`` for digital input,
|
||||
and then displays the string `LOW` whenever ``P2`` pulses low.
|
||||
## Parameters
|
||||
|
||||
* **name**: the @boardname@ hardware pin to set for digital input (``P0`` through ``P20``).
|
||||
* **pulse**: the state that will cause the code inside the block to run, either ``high`` or ``low``.
|
||||
* **body**: the code to run when the pin in **name** is pulsed to the state set in **pulse**.
|
||||
|
||||
## Example
|
||||
|
||||
Configure pin ``P2`` for digital input. Display the string `"LOW"` whenever ``P2`` pulses ``low``.
|
||||
|
||||
```blocks
|
||||
pins.onPulsed(DigitalPin.P2, PulseValue.Low, () => {
|
||||
@ -24,7 +28,7 @@ pins.onPulsed(DigitalPin.P2, PulseValue.Low, () => {
|
||||
});
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[servo set pulse](/reference/pins/servo-set-pulse),
|
||||
[pulse duration](/reference/pins/pulse-duration),
|
||||
|
@ -1,21 +1,26 @@
|
||||
# Pulse Duration
|
||||
# pulse Duration
|
||||
|
||||
Gets the duration of the last pulse in microseconds.
|
||||
|
||||
This function should be called from an **on pulsed** handler.
|
||||
Get the duration of the last pulse in microseconds.
|
||||
|
||||
```sig
|
||||
pins.pulseDuration();
|
||||
```
|
||||
|
||||
### Returns
|
||||
A pin pulse is detected in the [onPulsed](/reference/pins/on-pulsed) event. You use **pulseDuration** inside that event to get the duration of the pulse that triggered the event.
|
||||
|
||||
The duration of the last pulse, measured in microseconds.
|
||||
### ~ hint
|
||||
|
||||
### Example
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
The following example waits for pin ``P0`` to be pulsed high, and then
|
||||
displays the duration of the pulse in microseconds on the LED screen.
|
||||
### ~
|
||||
|
||||
## Returns
|
||||
|
||||
* a [number](/types/number) that is the duration of the last pulse, measured in microseconds.
|
||||
|
||||
## Example
|
||||
|
||||
Wait for pin ``P0`` to be pulsed high. Display the duration of the pulse in microseconds on the LED screen.
|
||||
|
||||
```blocks
|
||||
pins.onPulsed(DigitalPin.P0, PulseValue.High, () => {
|
||||
@ -23,7 +28,7 @@ pins.onPulsed(DigitalPin.P0, PulseValue.High, () => {
|
||||
});
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[servo set pulse](/reference/pins/servo-set-pulse),
|
||||
[on pulsed](/reference/pins/on-pulsed),
|
||||
|
@ -1,32 +1,37 @@
|
||||
# Pulse In
|
||||
# pulse In
|
||||
|
||||
Returns the duration of a pulse (high or low) from a [pin](/device/pins) on
|
||||
the @boardname@ board in microseconds.
|
||||
Get the duration, in microseconds, of a pulse (high or low) from one of the pins.
|
||||
|
||||
```sig
|
||||
pins.pulseIn(DigitalPin.P0, PulseValue.High)
|
||||
```
|
||||
|
||||
### ~avatar
|
||||
## ~avatar
|
||||
|
||||
Some pins are also used by the [LED screen](/device/screen).
|
||||
Please read the [page about pins](/device/pins) carefully.
|
||||
|
||||
## ~
|
||||
|
||||
### ~ hint
|
||||
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### ~
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name`` is a [string](/reference/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
|
||||
* ``value`` is the value of the pulse, ``high`` or ``low``
|
||||
* ``maxDuration``, maximum duration in micro-seconds. If no pulse is received
|
||||
* ``name`` the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``).
|
||||
* ``value`` the value of the pulse, either ``high`` or ``low``.
|
||||
* ``maxDuration``, maximum duration to wait for the pulse in microseconds. If no pulse is received, the duration returned is `0`.
|
||||
|
||||
### Returns
|
||||
## Returns
|
||||
|
||||
* a [number](/reference/types/number) that represents the pulse duration in micro-seconds
|
||||
* a [number](/types/number) that is the pulse duration in microseconds.
|
||||
|
||||
### Example: Measuring distance with a sonar
|
||||
## Example: Measuring distance with a sonar
|
||||
|
||||
The following script sends a pulse on ``P0`` and reads the pulse returned by a HC-SR04 sonar to determine the distance of the object in front of the sensor.
|
||||
Send a pulse on ``P0`` and read a pulse returned by a HC-SR04 sonar ultrasonic sensor. The sensor determines the distance of the object in front of it.
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
@ -43,12 +48,6 @@ basic.forever(() => {
|
||||
})
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
## See also
|
||||
|
||||
This function is not supported in the simulator.
|
||||
|
||||
#### ~
|
||||
|
||||
### See also
|
||||
|
||||
[digital write pin](/reference/pins/digital-write-pin),
|
||||
[digital write pin](/reference/pins/digital-write-pin)
|
||||
|
@ -7,23 +7,26 @@ ms, and set the pulse width to the specified value.
|
||||
pins.servoSetPulse(AnalogPin.P1, 1500)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name``: a [string](/reference/types/string) that specifies the pin to configure (`P0` through `P4`, or `P10`)
|
||||
* ``micros``: a [number](/reference/types/number) that specifies the analog period in microseconds.
|
||||
* ``name``: a [string](/types/string) that specifies the pin to configure (`P0` through `P4`, or `P10`)
|
||||
* ``micros``: a [number](/types/number) that specifies the analog period in microseconds.
|
||||
|
||||
### Example
|
||||
## Example
|
||||
|
||||
The following code sets the servo pulse to `1000` microseconds.
|
||||
|
||||
```blocks
|
||||
pins.servoSetPulse(AnalogPin.P1, 1000)
|
||||
pins.servoSetPulse(AnalogPin.P0, 1000)
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins),
|
||||
[on pin pressed](/reference/input/on-pin-pressed),
|
||||
[analog read pin](/reference/pins/analog-read-pin),
|
||||
[digital read pin](/reference/pins/digital-read-pin),
|
||||
[digital write pin](/reference/pins/digital-write-pin)
|
||||
|
||||
[Brief Guide to Servos](https://www.kitronik.co.uk/pdf/a-brief-guide-to-servos.pdf)
|
||||
|
||||
|
@ -9,40 +9,41 @@ full speed in one direction, `180` specifies full speed in the other,
|
||||
and approximately `90` specifies no movement.)
|
||||
|
||||
```sig
|
||||
pins.servoWritePin(AnalogPin.P1, 180)
|
||||
pins.servoWritePin(AnalogPin.P0, 180)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
## Parameters
|
||||
|
||||
* ``name``: a [string](/reference/types/string) that specifies the pin name (`P0` through `P4`, or `P10`)
|
||||
* ``value``: a [number](/reference/types/number) from `0` through `180`
|
||||
* ``name``: a [string](/types/string) that specifies the pin name (`P0` through `P4`, or `P10`)
|
||||
* ``value``: a [number](/types/number) from `0` through `180`
|
||||
|
||||
### Examples
|
||||
## Examples
|
||||
|
||||
#### Setting the shaft angle to midpoint on a servo
|
||||
### Setting the shaft angle to midpoint on a servo
|
||||
|
||||
```blocks
|
||||
pins.servoWritePin(AnalogPin.P1, 90)
|
||||
pins.servoWritePin(AnalogPin.P0, 90)
|
||||
```
|
||||
|
||||
#### Controlling the shaft by using the tilt information of the accelerometer
|
||||
### Controlling the shaft by using the tilt information of the accelerometer
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
let millig = input.acceleration(Dimension.X)
|
||||
// map accelerometer readings to angle
|
||||
let angle = pins.map(millig, -1023, 1023, 0, 180)
|
||||
pins.servoWritePin(AnalogPin.P1, angle)
|
||||
pins.servoWritePin(AnalogPin.P0, angle)
|
||||
})
|
||||
```
|
||||
|
||||
#### Setting the full speed on a continuous servo
|
||||
### Setting the full speed on a continuous servo
|
||||
|
||||
```blocks
|
||||
pins.servoWritePin(AnalogPin.P1, 0)
|
||||
pins.servoWritePin(AnalogPin.P0, 0)
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ pins](/device/pins), [servo set pulse](/reference/pins/servo-set-pulse)
|
||||
|
||||
[Brief Guide to Servos](https://www.kitronik.co.uk/pdf/a-brief-guide-to-servos.pdf)
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Set Events
|
||||
# set Events
|
||||
|
||||
Configure the type of events emitted by a given pin.
|
||||
|
||||
@ -6,15 +6,20 @@ Configure the type of events emitted by a given pin.
|
||||
pins.setEvents(DigitalPin.P0, PinEventType.Edge);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
### ~ hint
|
||||
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### ~
|
||||
|
||||
## Parameters
|
||||
|
||||
* ``name``: The @boardname@ hardware pin to configure (``P0`` through ``P20``)
|
||||
* ``type``: The type of events this pin should emit
|
||||
|
||||
### Example
|
||||
## Example
|
||||
|
||||
The following example configures pin ``P0`` and then
|
||||
subscribes to the rise and fall events.
|
||||
The following example configures pin ``P0`` and then subscribes to the rise and fall events.
|
||||
|
||||
```blocks
|
||||
control.onEvent(control.eventSourceId(EventBusSource.MICROBIT_ID_IO_P0), control.eventValueId(EventBusValue.MICROBIT_PIN_EVT_RISE), () => {
|
||||
|
@ -14,12 +14,14 @@ button press.
|
||||
pins.setPull(DigitalPin.P9, PinPullMode.PullDown);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
The pull-up and -down resistors are about 13kOhm.
|
||||
|
||||
## Parameters
|
||||
|
||||
* ``name``: The @boardname@ hardware pin to configure (``P0``-``P20``)
|
||||
* ``pull``: The pull to which to set the pin (**down**, **up**, or **none**)
|
||||
|
||||
### Example
|
||||
## Example
|
||||
|
||||
The following example sets the pull of pin ``P0`` to **up** (high).
|
||||
|
||||
@ -27,6 +29,6 @@ The following example sets the pull of pin ``P0`` to **up** (high).
|
||||
pins.setPull(DigitalPin.P0, PinPullMode.PullUp);
|
||||
```
|
||||
|
||||
### See also
|
||||
## See also
|
||||
|
||||
[@boardname@ | mbed](https://developer.mbed.org/platforms/Microbit/)
|
||||
|
43
docs/reference/pins/spi-format.md
Normal file
43
docs/reference/pins/spi-format.md
Normal file
@ -0,0 +1,43 @@
|
||||
# spi Format
|
||||
|
||||
Set the Serial Peripheral Interface (SPI) format.
|
||||
|
||||
```sig
|
||||
pins.spiFormat(8, 3);
|
||||
```
|
||||
|
||||
The data sent over a SPI connection has a number of _bits_ to represent each value. Also, position of the clock signal (SCK), ``high`` or ``low``, and the exact time when a data value is read is determined by the SPI _mode_. Both the bits and mode values together are called the _SPI format_.
|
||||
|
||||
### ~ hint
|
||||
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### ~
|
||||
|
||||
The default number of bits is `8` and the default mode value is `3`.
|
||||
|
||||
## Parameters
|
||||
|
||||
* **bits**: the [number](types/number) of bits for each data value sent and received on the SPI connection. This value must be ``8`` since only 8 bits is currently supported for SPI data values.
|
||||
* **mode**: a [number](/types/number) that is the mode value for the SPI clock (**SCK**) signalling. The different modes are:
|
||||
>* `0`: the data line is active when **SCK** goes to ``high`` and the data values are read when **SCK** goes to ``high``
|
||||
>* `1`: the data line is active when **SCK** goes to ``high`` and the data values are read when **SCK** goes to ``low``
|
||||
>* `2`: the data line is active when **SCK** goes to ``low`` and the data values are read when **SCK** goes to ``high``
|
||||
>* `3`: the data line is active when **SCK** goes to ``low`` and the data values are read when **SCK** goes to ``low``
|
||||
|
||||
## Example
|
||||
|
||||
Set the pins and format for the SPI connection.
|
||||
|
||||
```blocks
|
||||
pins.spiPins(DigitalPin.P15, DigitalPin.P14, DigitalPin.P13);
|
||||
pins.spiFormat(8, 3);
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[spi write](/reference/pins/spi-write),
|
||||
[spi pins](/reference/pins/spi-pins),
|
||||
[spi frequency](/reference/pins/spi-frequency)
|
||||
|
||||
[SPI Programming](https://developer.mbed.org/handbook/SPI)
|
46
docs/reference/pins/spi-frequency.md
Normal file
46
docs/reference/pins/spi-frequency.md
Normal file
@ -0,0 +1,46 @@
|
||||
# spi Frequency
|
||||
|
||||
Set the Serial Peripheral Interface (SPI) clock frequency.
|
||||
|
||||
```sig
|
||||
pins.spiFrequency(1000000);
|
||||
```
|
||||
|
||||
The @boardname@ sets the rate of data transfer and control timing for a SPI connection. This data rate and timing signal is controlled by the **SCK** pin. The signal on this pin is _clocked_ using the frequency set for it.
|
||||
|
||||
### ~ hint
|
||||
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### ~
|
||||
|
||||
The default clock frequency is 1 Mhz (10000000 Hz). You can set the frequency for the SPI connection to some other value if you need a different data rate.
|
||||
|
||||
## Parameters
|
||||
|
||||
* **frequency**: a [number](/types/number) to set as the frequency for SPI bus clock. This value is the number of clock changes per second (Hz).
|
||||
|
||||
## Example
|
||||
|
||||
Read the value of the _WHOAMI_ register from the device connected to the SPI bus. The chip select line is connected to pin **0** and the SPI signals use pins **P13**, **P14**, and **P15**.
|
||||
|
||||
```blocks
|
||||
pins.digitalWritePin(DigitalPin.P0, 1);
|
||||
pins.spiPins(DigitalPin.P15, DigitalPin.P14, DigitalPin.P13);
|
||||
pins.spiFormat(8, 3);
|
||||
pins.spiFrequency(1000000);
|
||||
pins.digitalWritePin(DigitalPin.P0, 0);
|
||||
let command = pins.spiWrite(143);
|
||||
let whoami = pins.spiWrite(0);
|
||||
pins.digitalWritePin(DigitalPin.P0, 1);
|
||||
basic.showNumber(whoami);
|
||||
serial.writeLine("WHOAMI register value: " + whoami)
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[spi write](/reference/pins/spi-write),
|
||||
[spi pins](/reference/pins/spi-pins),
|
||||
[spi format](/reference/pins/spi-format)
|
||||
|
||||
[SPI Programming](https://developer.mbed.org/handbook/SPI)
|
43
docs/reference/pins/spi-pins.md
Normal file
43
docs/reference/pins/spi-pins.md
Normal file
@ -0,0 +1,43 @@
|
||||
# spi Pins
|
||||
|
||||
Set the Serial Peripheral Interface (SPI) signalling pins
|
||||
|
||||
```sig
|
||||
pins.spiPins(DigitalPin.P0, DigitalPin.P1, DigitalPin.P2);
|
||||
```
|
||||
|
||||
To configure the @boardname@ to write to an external device using a SPI connection, each SPI signal line is assigned to unique a pin. A SPI connection uses 3 signalling lines called **MOSI**, **MISO**, and **SCK**.
|
||||
|
||||
### ~ hint
|
||||
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
### ~
|
||||
|
||||
If you don't set the pins for the SPI connection, the default pin assignments are used:
|
||||
|
||||
* **P15** = **MOSI**, @boardname@ SPI data output pin
|
||||
* **P14** = **MISO**, @boardname@ SPI data input pin
|
||||
* **P13** = **SCK**, @boardname@ SPI serial clock output pin
|
||||
|
||||
## Parameters
|
||||
|
||||
* **mosi**: the pin for SPI data output, the **MOSI** signal pin.
|
||||
* **miso**: the pin for SPI data input, the **MISO** signal pin.
|
||||
* **sck**: the pin for SPI serial clock output, the **SCK** signal pin.
|
||||
|
||||
## Example
|
||||
|
||||
Set the pin assignments for a SPI connection to the default pins.
|
||||
|
||||
```blocks
|
||||
pins.spiPins(DigitalPin.P15, DigitalPin.P14, DigitalPin.P13);
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[spi write](/reference/pins/spi-write),
|
||||
[spi frequency](/reference/pins/spi-frequency),
|
||||
[spi format](/reference/pins/spi-format)
|
||||
|
||||
[SPI Programming](https://developer.mbed.org/handbook/SPI)
|
@ -1,19 +1,48 @@
|
||||
# SPI Write
|
||||
# spi Write
|
||||
|
||||
Write to the SPI Slave and return the response.
|
||||
Write a data value to the SPI slave device and return its response.
|
||||
|
||||
```sig
|
||||
pins.spiWrite(0);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
Data values are written to a SPI slave device connected to the @boardname@ by the [SPI pins](/reference/pins/spi-pins). The data value might be either a command for the connected device or some value for its use. If the write operation causes the connected device to send a value back to the @boardname@, this will be the return value for the **spiWrite** function.
|
||||
|
||||
* ``value``: value Data to be sent to the SPI slave
|
||||
### ~ hint
|
||||
|
||||
### Returns
|
||||
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
|
||||
|
||||
* a [number](/reference/types/number) Response from the SPI slave
|
||||
### ~
|
||||
|
||||
### See also
|
||||
## Parameters
|
||||
|
||||
[SPI](https://developer.mbed.org/handbook/SPI)
|
||||
* ``value``: a [number](/types/number) which is the data value to send to the SPI slave device.
|
||||
|
||||
## Returns
|
||||
|
||||
* a [number](/types/number) value which is the response from the SPI slave device.
|
||||
|
||||
## Example
|
||||
|
||||
Send the command to read the value of the _WHOAMI_ register from the device connected to the SPI bus. The chip select line is connected to pin **0** and the SPI signals use pins **P13**, **P14**, and **P15**.
|
||||
|
||||
```blocks
|
||||
pins.digitalWritePin(DigitalPin.P0, 1);
|
||||
pins.spiPins(DigitalPin.P15, DigitalPin.P14, DigitalPin.P13);
|
||||
pins.spiFormat(8, 3);
|
||||
pins.spiFrequency(1000000);
|
||||
pins.digitalWritePin(DigitalPin.P0, 0);
|
||||
let command = pins.spiWrite(143);
|
||||
let whoami = pins.spiWrite(0);
|
||||
pins.digitalWritePin(DigitalPin.P0, 1);
|
||||
basic.showNumber(whoami);
|
||||
serial.writeLine("WHOAMI register value: " + whoami)
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[spi pins](/reference/pins/spi-pins),
|
||||
[spi frequency](/reference/pins/spi-frequency),
|
||||
[spi format](/reference/pins/spi-format)
|
||||
|
||||
[SPI Programming](https://developer.mbed.org/handbook/SPI)
|
||||
|
Reference in New Issue
Block a user