树莓派开发ADC芯片CS1237(C语言和python库开发)

源码地址https://github.com/dyingstraw/CS1237

一、准备工作

1.安装树莓派官方系统

安装树莓派官方系统

2.安装wiringpi库

sudo apt-get install git-core
sudo geit clone git://git.drogon.net/wiringPi
cd wieingPi
sudo ./build

安装结束之后
运行命令 gpio readall
树莓派开发ADC芯片CS1237(C语言和python库开发)_第1张图片
如果出现这个树莓派引脚图,说明安装成功。进行下一步
3.新建一个CS1237.c和CS1237.h文件

CS1237.h

#ifndef __CS1237__h__
#define __CS1237__h__
#include 
#include 
#define SCK 1
#define DOUT 4
#define SCK_LOW digitalWrite(SCK,0)
#define SCK_HIGN digitalWrite(SCK,1)
//树莓派wiring引脚对应关系如下

//AD转换数值与实际重量的增益
// #include 
static double CS1237P = 0.0078742;


//初始化
void CS1237Init()
{
    wiringPiSetup();
    pinMode(SCK,OUTPUT);
    pinMode(DOUT,INPUT);
    pullUpDnControl(DOUT,PUD_UP);
    SCK_LOW;
}
//是否转换完毕
int CS1237isReady()
{
    if(digitalRead(DOUT)==0)
    {
        return 1;
    }
    return 0;
}
//配置函数
void CS1237Config()
{
    //no op
}
//一次上升沿
void CS1237ClockUp()
{
    SCK_LOW;
    delayMicroseconds(10);
    SCK_HIGN;
    delayMicroseconds(10);
}
//读数据
int CS1237Read()
{
    int tmp=0;
    int now=0;
    int i;
    int num=100;
    while(!CS1237isReady())
    {
        ;
    }

    for(i=0;i<23;i++)
    {
        SCK_LOW;
        delayMicroseconds(10);
        SCK_HIGN;
        delayMicroseconds(10);
        now = digitalRead(DOUT);        
        //printf("%d,",now );
        tmp *=2;
        tmp = tmp+now;

        SCK_LOW;
    }

    SCK_LOW;
    CS1237ClockUp();
    CS1237ClockUp();
    CS1237ClockUp();
    SCK_LOW;



        return tmp;
}

//得到平均立波得到实际数值
//num:采样次数
int getValue(int num)
{
    int i=0;
    int tmp=0;
    long weight = 0;
    for(i=0;ireturn weight/num;
}
int getHelp(int num)
{
    return num*3;
}
#endif

CS1237.c

#include "CS1237.h"
int main()
{
    int tmp,Orign;
    printf("hello rasp  %s\n","dsc" );
    //初始化CS1237
    CS1237Init();
    //得到初试点(0点校正)
    Orign = getValue(200);
    printf("Orign value is %d\n", Orign);
    while(1)
    {
        tmp = getValue(100);
        printf("now is %lf\n", (tmp-Orign)*CS1237P);
    }
}

代码写好之后在同目录下编译:
gcc CS1237.c -o test -lwiringPi
运行
./test
树莓派开发ADC芯片CS1237(C语言和python库开发)_第2张图片

树莓派开发ADC芯片CS1237(C语言和python库开发)_第3张图片

树莓派开发ADC芯片CS1237(C语言和python库开发)_第4张图片

二、将程序封装为python可调用的库

python 调用C的方式:

新建一个CS1237.如下

#include 
#include 
#define SCK 1
#define DOUT 4
#define SCK_LOW digitalWrite(SCK,0)
#define SCK_HIGN digitalWrite(SCK,1)
//树莓派wiring引脚对应关系如下

//AD转换数值与实际重量的增益
#include 
static double CS1237P = 0.0078742;


//初始化
void CS1237Init()
{
    wiringPiSetup();
    pinMode(SCK,OUTPUT);
    pinMode(DOUT,INPUT);
    pullUpDnControl(DOUT,PUD_UP);
    SCK_LOW;
}
//是否转换完毕
int CS1237isReady()
{
    if(digitalRead(DOUT)==0)
    {
        return 1;
    }
    return 0;
}
//配置函数
void CS1237Config()
{
    //no op
}
//一次上升沿
void CS1237ClockUp()
{
    SCK_LOW;
    delayMicroseconds(10);
    SCK_HIGN;
    delayMicroseconds(10);
}
//读数据
int CS1237Read()
{
    int tmp=0;
    int now=0;
    int i;
    int num=100;
    while(!CS1237isReady())
    {
        ;
    }

    for(i=0;i<23;i++)
    {
        SCK_LOW;
        delayMicroseconds(10);
        SCK_HIGN;
        delayMicroseconds(10);
        now = digitalRead(DOUT);        
        //printf("%d,",now );
        tmp *=2;
        tmp = tmp+now;

        SCK_LOW;
    }

    SCK_LOW;
    CS1237ClockUp();
    CS1237ClockUp();
    CS1237ClockUp();
    SCK_LOW;



        return tmp;
}

//得到平均立波得到实际数值
//num:采样次数

int getValue(int num)
{
    int i=0;
    int tmp=0;
    long weight = 0;
    for(i=0;ireturn weight/num;
}

int getHelp(int num)
{
    return num*3;
}


PyObject * wrap_getValue(PyObject *self,PyObject *args)
{
    int num;
    int weight;
    if(!PyArg_ParseTuple(args,"i:getValue",&num))
        return NULL;
    weight = getValue(num);
    return Py_BuildValue("i",weight);
}
PyObject * wrap_getHelp(PyObject *self,PyObject *args)
{
    int num;
    int weight;
    if(!PyArg_ParseTuple(args,"i:getValue",&num))
        return NULL;
    weight = getHelp(num);
    return Py_BuildValue("i",weight);
}

PyObject * wrap_CS1237Init(PyObject *self,PyObject *args)
{
    int num;
    int weight;
    CS1237Init();
    return NULL;
}
static PyMethodDef CS1237Methods[]=
{
    {"getValue",wrap_getValue,METH_VARARGS,"get weignt adc valie"},
    {"getHelp",wrap_getHelp,METH_VARARGS,"get help adc valie"},
    {"CS1237Init",wrap_CS1237Init,METH_VARARGS,"get help adc valie"},
    {NULL,NULL}
};

void initCS1237()
{
    PyObject *m;
    CS1237Init();
    m = Py_InitModule("CS1237",CS1237Methods);
}

然后交叉编译:
命令代码(路径按照自己的)
gcc -fPIC CS1237.c -o CS1237.so -shared -I/usr/include/python2.7 -I/usr/lib/python2.7/config

然后在此目录下新建一个test.py验证一下:

# test.py
# coding=utf-8
import CS1237
tmp = CS1302.getValue(20)
print temp

执行python test.py 运行结果出来了:

33077

关于python引用C语言的若干种方式,请参考[Python调用C/C++的种种方法]1

你可能感兴趣的:(python学习笔记)