Edison平台eclipse环境下通过MRAA库使用IIC、SPI、UART例程(BMI160、MS8607)

MRAA库IIC例程:

以下程序是操作MS8607传感器

mraa_init();
/* initialize I2C on bus 0 */
m_i2c = mraa_i2c_init(1);
mraa_i2c_frequency(m_i2c,MRAA_I2C_FAST);//400K
/* now run in a loop 10 times, reading the seconds */
/* register and printing it.*/
mraa_i2c_address(m_i2c, 0x76);
//mraa_i2c_address(m_i2c, 0x0f);
mraa_i2c_write_byte(m_i2c, 0x1e);
while(1)//iic1
{
char buf = 0;
char cdata = 0x00;
char buff[2];
mraa_i2c_address(m_i2c, 0x76);
mraa_i2c_write_byte(m_i2c, 0xa0);
buff[0] = mraa_i2c_read_byte(m_i2c);
buff[1] = mraa_i2c_read_byte(m_i2c);
printf("buff[0],buff[1]: 0x%x,0x%x\n", buff[0],buff[1]);
sleep(1);
}

MRAA库SPI例程:

以下程序是操作BMI160传感器,

int test_spi(void)
{
printf("test_spi\r\n");
mraa_init();
spi = mraa_spi_init(0);
if(NULL == spi)
{
  printf("Initialization of spi failed, check syslog for details, exit...\n");
}
printf("SPI initialised successfully\n");
mraa_spi_mode(spi,MRAA_SPI_MODE0);
mraa_spi_frequency(spi, 400000);
cs_init();
unsigned char address[3] = {0xC0,0x55,0xff};
unsigned char cmd[2] = {0xff,0xff};
unsigned char recv[3] = {0,0,0};
mraa_spi_transfer_buf(spi,cmd,recv,1);
mraa_gpio_write(d_pin, 1);
while (1) {
mraa_gpio_write(d_pin, 0);
//mraa_spi_write_buf(spi, recv, 2);
mraa_spi_transfer_buf(spi,address,recv,2);
mraa_gpio_write(d_pin, 1);
printf("spi_read_16bits-%x-%x\n", recv[0], recv[1]);
sleep(1);
}
}

MRAA库UART例程:

int uart_init(void)
{
    mraa_uart_context uart;
    uart = mraa_uart_init(0);
    printf("test_uart\r\n");
    if (uart == NULL) {
        fprintf(stderr, "UART failed to setup\n");
        return EXIT_FAILURE;
    }
    mraa_uart_set_baudrate(uart,115200);
    mraa_uart_set_mode(uart,8,MRAA_UART_PARITY_NONE,1);
    mraa_uart_set_flowcontrol(uart,0,0);
    char *buffer = "this is uart\n";
    int r = 0;
    while(1)
        {
                r = mraa_uart_write(uart, buffer,13);
                usleep(30000);
                printf("send return:%d\r\n",r);
        }
    mraa_uart_stop(uart);


    mraa_deinit();


    return EXIT_SUCCESS;
}

你可能感兴趣的:(嵌入式linux)