树莓派+PCF8591--02--模拟输入

树莓派没有AD芯片

正好学习AD&DA芯片 和 I2C

硬件

51开发板端

在这里插入图片描述
J31与J32用于切换AD输入端口,不用电位计。直接将AD0接到光线传感器上。

(这个开发板模块的意思就是可以用电位器的模拟量,也可以接其他东西。)
在这里插入图片描述
J33是DA输入模拟LED等选择开关,用跳帽跳上之后LED起作用。
(意思就是模拟输出控制灯的亮度)
在这里插入图片描述

I2C接口

SCL------时钟
SDA-----数据

树莓派端

wiringPi上的I2C-pcf8591说明
寥寥几句,字字如金。

先在开起I2C
	sudo raspi-config
	-->  5 Interfacing Options
	--> p5 I2C
	--> yes
	-->reboot
	

能看出有两个I2C接口
在这里插入图片描述

安装I2C工具

pi@raspberrypi:~ $  sudo apt-get install i2c-tools
pi@raspberrypi:~ $  ls /dev/*i2c*
	/dev/i2c-1		
pi@raspberrypi:~ $ i2cdetect -y 1
  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --     

杜邦线连接后

pi@raspberrypi:~ $ i2cdetect -y 1
  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --     

48是pcf8591的。57可能是板子上连的eeprom

以下是qtcreator源码

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <wiringPi.h>
#include <pcf8591.h>
#include <QTimerEvent>

//宏定义一些必须的
#define Address 0x48    //i2cdetect -y 1中找到的
#define BASE 64         //pcf8591的配置
#define A0 BASE+0       
#define A1 BASE+1
#define A2 BASE+2
#define A3 BASE+3

namespace Ui {
class MainWindow;
}

long map(long value, long fromStart, long fromEnd, long toStart, long toEnd);

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void timerEvent(QTimerEvent *event);    //定义了一个定时器
private:

    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    wiringPiSetup();
    pcf8591Setup(BASE,Address);

    startTimer(100);    //定时100毫秒。



}

MainWindow::~MainWindow()
{
    delete ui;
}


/*使用qt的定时器*/
void MainWindow::timerEvent(QTimerEvent *event)
{
    ui->progressBar->setValue(map(analogRead(A0),0,255,0,100));
}


/*这个是模仿arduino的map函数*/
long map(long value, long fromStart, long fromEnd, long toStart, long toEnd)
{
    return ((value-fromStart)*(toEnd-toStart)/(fromEnd-fromStart)+toStart);
}

ui上只放一个Progress Bar

致谢:

https://blog.csdn.net/wwb74110/article/details/67119781
https://learn.sparkfun.com/tutorials/raspberry-pi-spi-and-i2c-tutorial#i2c-on-pi
http://www.mamicode.com/info-detail-1791255.html
https://book.2cto.com/201311/36122.html
https://blog.csdn.net/xukai871105/article/details/18033945

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值