源码地址/dyingstraw/CS1237
一、准备工作
1.安装树莓派官方系统
安装树莓派官方系统
2.安装wiringpi库
sudo apt-get install git-coresudo geit clone git:///wiringPicd wieingPisudo ./build
安装结束之后
运行命令 gpio readall
如果出现这个树莓派引脚图,说明安装成功。进行下一步
3.新建一个CS1237.c和CS1237.h文件
CS1237.h
#ifndef __CS1237__h__#define __CS1237__h__#include <stdio.h>#include <wiringPi.h>#define SCK 1#define DOUT 4#define SCK_LOW digitalWrite(SCK,0)#define SCK_HIGN digitalWrite(SCK,1)//树莓派wiring引脚对应关系如下//AD转换数值与实际重量的增益// #include <Python.h>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;i<num;i++){tmp = CS1237Read();weight =weight + tmp;}return 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" );//初始化CS1237CS1237Init();//得到初试点(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
二、将程序封装为python可调用的库
python 调用C的方式:
新建一个CS1237.如下
#include <stdio.h>#include <wiringPi.h>#define SCK 1#define DOUT 4#define SCK_LOW digitalWrite(SCK,0)#define SCK_HIGN digitalWrite(SCK,1)//树莓派wiring引脚对应关系如下//AD转换数值与实际重量的增益#include <Python.h>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;i<num;i++){tmp = CS1237Read();weight =weight + tmp;}return 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-8import CS1237tmp = CS1302.getValue(20)print temp
执行python test.py
运行结果出来了:
33077
关于python引用C语言的若干种方式,请参考[Python调用C/C++的种种方法]1