Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //i2c bus expander mcp23017 with driver ....raspberryPi
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #define GPIO_ADDR 496 // mcp base address
- enum dir{IN,OUT};
- void gpioExport(int gpio)
- {
- int fd;
- char buf[255];
- fd = open("/sys/class/gpio/export", O_WRONLY);
- sprintf(buf, "%d", gpio);
- write(fd, buf, strlen(buf));
- close(fd);
- }
- void gpioDirection(int gpio, int direction) // 1 for output, 0 for input
- {
- int fd;
- char buf[255];
- sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
- fd = open(buf, O_WRONLY);
- if (direction){
- printf ("gpio %d out\n",gpio);
- write(fd, "out", 3);
- }else{
- printf ("gpio %d in\n",gpio);
- write(fd, "in", 2);
- }
- close(fd);
- }
- void gpioSet(int gpio, int value)
- {
- int fd;
- char buf[255];
- sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
- fd = open(buf, O_WRONLY);
- sprintf(buf, "%d", value);
- write(fd, buf, 1);
- close(fd);
- }
- int main (){
- int offset=0;
- do{
- gpioExport(GPIO_ADDR+offset);
- gpioDirection ( GPIO_ADDR+offset ,offset <8 ?IN:OUT);
- }while (GPIO_ADDR+(offset++) <(GPIO_ADDR+15));
- //gpioExport(506);
- //gpioDirection(506,OUT);
- //gpioSet(506,1);
- do {//Set OUTs PORTB MCP23017
- gpioSet(GPIO_ADDR+offset,0);
- }while ((GPIO_ADDR+ (offset--)) >=GPIO_ADDR);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement