accessing hardware in the. Net Micro Framework

Source: Internet
Author: User
Tags config

Summary: This article describes the simple and unique way of accessing hardware in the. Net Micro Framework. Involving I2c,spi and so on. A concise routine illustrates how to create and access I2C and SPI devices in. Net Mf

I2C bus

Although the MCU uses I2C (inter-integrated circuit) bus to communicate with peripherals is slower than many other bus systems (up to 100kbps), but because of its low cost, the pluggable equipment support better, On many occasions it is still supported as a standard feature.

So how do you access I2C-connected devices in the. Net Micro framework?

. Net MF Provides a Microsoft.SPOT.Hardware.I2CDevice class that implements the ability to access IIC devices. First, you need to create a I2cdevice instance for each IIC device, similar to the SerialPort mentioned earlier, and you need to pass in a I2cdevice.configuration configuration object for its constructor. In this configuration object you need to specify the address and communication speed (khz) of the device. Next, you need to create a set of transaction objects (Createreadtransaction and createwritetransaction) to read and write, and the specific transaction types will naturally depend on the role of your specific components. When you create a transaction, you need to pass in a buffer to be read (written).

Then you need to pass the set of transactions to the Execute method for execution and start the communication as an array. The Execute method returns when all transactions are completed or the method times out.

The following code demonstrates how to communicate with an I2C device with an address number of 58.

Using System;
Using Microsoft.spot;
Using Microsoft.SPOT.Hardware;
Namespace I2csample
{public
class program
{public
static void Main ()
{
i2cdevice.configuration config =
new i2cdevice.configuration (
58,//
//100khz clock frequency of address number
);
I2cdevice device = new I2cdevice (config);
byte[] Outbuffer = new byte[] {0xAA}; Pending write Buffer
byte[] Inbuffer = new byte[4];//to read buffer
i2cdevice.i2cwritetransaction writetransaction =
Device. Createwritetransaction (outbuffer);
I2cdevice.i2creadtransaction readtransaction =
device. Createreadtransaction (inbuffer);
Create a transaction array
i2cdevice.i2ctransaction[] transactions =
New i2cdevice.i2ctransaction[] {
Writetransaction,
readtransaction
};
int transferred = device. Execute (
transactions,
100//Timeout time set to 100ms
);
Transferred bytes should be 1 + 4 = 5
}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.