树莓派通过ADC0832采集模拟量

树莓派通过ADC0832采集模拟量

       花费了近两个小时的时间,终于成功地将基于51MCU平台的ADC0832代码移植到了树莓派上。

       ADC0832芯片的具体参数和用法我就不做解释了,网上一抓一大把。现在,将我写的代码呈现出来,供大家参考,本人才疏学浅,代码如有僻陋,敬请拍砖!

      下图是ADC0832与树莓派的连接图,只需要三根线:

树莓派通过ADC0832采集模拟量_第1张图片


#include 
#include 

typedef unsigned char uchar;
typedef unsigned int uint;

#define     ADC_CS    0
#define     ADC_CLK   1
#define     ADC_DIO   2

uchar get_ADC_Result(void)
{
	uchar i;
	uchar dat1=0, dat2=0;

	digitalWrite(ADC_CS, 0);
	digitalWrite(ADC_CLK,0);
	digitalWrite(ADC_DIO,1);	delayMicroseconds(2);
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);

	digitalWrite(ADC_CLK,0);	
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);

	digitalWrite(ADC_CLK,0);	
	digitalWrite(ADC_DIO,0);	delayMicroseconds(2);//这里若写ADC_DIO为1,则选用通道1进行AD转换
	digitalWrite(ADC_CLK,1);	
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
	digitalWrite(ADC_CLK,0);	
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
	
	for(i=0;i<8;i++)
	{
		digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);

		pinMode(ADC_DIO, INPUT);
		dat1=dat1<<1 | digitalRead(ADC_DIO);
	}
	
	for(i=0;i<8;i++)
	{
		dat2 = dat2 | ((uchar)(digitalRead(ADC_DIO))<


你可能感兴趣的:(【Raspberry,Pi】,【开源硬件】)