gauravsharma0190
Posts: 125
Joined: Tue Oct 28, 2014 6:36 am

MCP23017 code using C without WiringPi

Mon Feb 13, 2017 4:40 pm

hello there
i want to Interface mcp23017 without wiringPi because i added it with QT creator.
So i wrote the code and the code works fine when write on MCP23017 IC
but when comes to read the pin or port i can't understand how to read the pin
For eg. GPIOA GPA7 as input port and if i give it as high it tells me that Port is high or something like switch Condition as it is high then GPA1 will high...Something Like so Here is my code...

Code: Select all

#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
char buff[10];
char  buff1[10];
char arr[10];
int wbyte,wbyte1,io,rd;
void write_reg(int fh, uint8_t reg, uint8_t val);
int read_reg(int fh, uint8_t reg, int count);
int main()
{
int fd=open("/dev/i2c-1",O_RDWR);
if(fd<0){
perror("Cannot open fd\n");
exit(1);
}
else
{
printf("I2c working\n");
}
buff[0]=0x00;
buff[1]=0x80;
buff1[0]=0x12;
buff1[1]=0x01;
//arr[0]=0x12;
//arr[1]=0x00;
io=ioctl(fd,I2C_SLAVE,0x20);
if(io<0)
{
printf("ERROR IOCTL \n");
}
wbyte=write(fd,&buff,2);
if(wbyte!=2)
{
perror("cant write\n");
}

//write(fd,&buff1,2);
write_reg(fd,0x12,0x01);
usleep(1000*1000);
//write(fd,&arr,2);
//write_reg(fd,0x12,0x03);
//usleep(1000*1000);
rd=read_reg(fd,0x12,7);
if(rd!=2)
{
printf("No read\n");
}
printf("After : GPIOA 0x%02X\n",rd &0xff);

printf("Everything goes right\n");

return 1;
}

void write_reg(int fd, uint8_t reg, uint8_t val)
{
	uint8_t data[2];

	data[0] = reg;
	data[1] = val;

	if (write(fd, &data, 2) != 2) { 		 
		perror("write");
	
	}
}


int read_reg(int fh, uint8_t reg, int count)
{
	uint8_t data[2];
	data[0] = reg;

	if (write(fh, &data, 1) != 1) { 		 
		perror("write before read");
		return -1;
	}

	data[1] = 0;

	if (read(fh, &data, count) != count) {
		perror("read");
		return -1;	
	}

	return (data[1] << 8) + data[0];
}

Reply
Thanks

User avatar
joan
Posts: 14756
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP23017 code using C without WiringPi

Mon Feb 13, 2017 4:44 pm

Why not just look at how wiringPi does it and do something similar. Or look at a Python solution and re-engineer that in C.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

Re: MCP23017 code using C without WiringPi

Tue Feb 14, 2017 10:11 am

hello,
not sure if all your questions had been meanwhile answered, nonetheless I happened to come across this following link which provides fairly good documented MCP23017 libs;
one of them is about C (no wiringPi) and perhaps might be suitable for your purposes:
http://www.netzmafia.de/skripten/hardwa ... index.html
http://www.netzmafia.de/skripten/hardwa ... nder_lib.h
http://www.netzmafia.de/skripten/hardwa ... nder_lib.c
HTH!

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: MCP23017 code using C without WiringPi

Wed Feb 15, 2017 5:03 pm

gauravsharma0190 wrote:hello there
i want to Interface mcp23017 without wiringPi because i added it with QT creator.
Is this because you don't want to use wiringPi, or because you think it won't work with Qt? If the latter, then as far as I'm aware, there are many people using wiringPi with Qt without any problems.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “C/C++”