stamechad11
Posts: 1
Joined: Wed Jun 27, 2018 1:41 pm

C setup to Control the I2C GPIO pins.

Wed Jun 27, 2018 2:26 pm

Hello there!

I am totally new to Raspberry-Pi, I am trying to use it for the very first time!
I am trying to use the Raspberry Pi with C language files that will control the I2C GPIO pins.
Does anyone know how to make this possible?

I have used the shell codes:
"sudo apt-get update" and "sudo apt-get install gcc".

I am not going to do anything advanced other than writing a basic C program.

1: What other shell codes do I need?
2: What C library is good to use for I2C control?

Thanx alot for anyone who can help! This is an absolute life saver for me!
Last edited by stamechad11 on Wed Jun 27, 2018 3:39 pm, edited 1 time in total.

User avatar
Arjan
Posts: 262
Joined: Sat Sep 08, 2012 1:59 pm

Re: C++ setup to Control the I2C GPIO pins.

Wed Jun 27, 2018 3:30 pm

stamechad11 wrote:
Wed Jun 27, 2018 2:26 pm
2: What C library is good to use for I2C control?
C library for Broadcom BCM 2835 as used in Raspberry Pi
http://www.airspayce.com/mikem/bcm2835/
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

dsyleixa123
Posts: 341
Joined: Mon Jun 11, 2018 11:22 am

Re: C setup to Control the I2C GPIO pins.

Wed Jun 27, 2018 5:53 pm

hello,
I am using wiringPi: http://wiringpi.com/reference/i2c-library/
(gcc is already installed in Jessie and Stretch, and so is wiringPi.)

User avatar
DougieLawson
Posts: 35796
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: C setup to Control the I2C GPIO pins.

Wed Jun 27, 2018 8:19 pm

stamechad11 wrote:
Wed Jun 27, 2018 2:26 pm

2: What C library is good to use for I2C control?
You don't need a library for it. Everything is a built-in part of Linux.

Code: Select all

#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include <sys/ioctl.h>
#include "linux/i2c-dev.h"

int main(int argc, char *argv[])
{
    int fd;
    struct timespec _500ms;
    _500ms.tv_sec = 0;
    _500ms.tv_nsec = 5000000L;
    fd = open("/dev/i2c-1", O_RDWR);
    if(fd < 0) {
        fprintf(stderr, "Error opening device\n");
        exit(EXIT_FAILURE);
    }
    if(ioctl(fd, I2C_SLAVE, 0x20) < 0) {
        fprintf(stderr, "Error setting slave address\n");
        close(fd);
        exit(EXIT_FAILURE);
    }

    char buf[10];
    buf[0] = 0x07; // IODIR PORT1
    buf[1] = 0x00; // all output
    if(write(fd, buf, 2) != 2) {
        fprintf(stderr, "Error writing (1)\n");
        close(fd);
        exit(EXIT_FAILURE);
    }
    printf("Write success!\n");

    nanosleep(&_500ms, NULL);

    buf[0] = 0x01; // PORT1
    buf[1] = 0xff; // output high
    if(write(fd, buf, 2) != 2) {
        fprintf(stderr, "Error writing (2)\n");
        close(fd);
        exit(EXIT_FAILURE);
    }

    close(fd);
    printf("Done.\n");
    exit(EXIT_SUCCESS);
}
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “C/C++”