mcp3208 和 树莓派3b板子的配置以及对应代码

2024-06-07 12:58

本文主要是介绍mcp3208 和 树莓派3b板子的配置以及对应代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先上连接图。
在这里插入图片描述
MCP3208 在2013年一位日本大佬的代码:
https://seinzumtode.hatenadiary.jp/entry/20130918/1379501130
针对MQ-X这类气体模块,用MQ-2去做实验(这个模块是可燃气体,那个打火机就能看出程序里的读数变化)。所以MQ2的AO的小针就是对应MCP3208上面的CH0的。
最后 如果再修改的好一点的话,关于pot move的这类代码其实在气体模块这里是用不到的,然后volume的意义针对气体模块其实应该重新整理公式去换算。

import time
import os
import RPi.GPIO as GPIOGPIO.setmode(GPIO.BCM)
DEBUG = 1# read SPI data from MCP3208 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):if ((adcnum > 7) or (adcnum < 0)):return -1GPIO.output(cspin, True)GPIO.output(clockpin, False)  # start clock lowGPIO.output(cspin, False)     # bring CS lowcommandout = adcnumcommandout |= 0x18  # start bit + single-ended bitcommandout <<= 3    # we only need to send 5 bits herefor i in range(5):if (commandout & 0x80):GPIO.output(mosipin, True)else:GPIO.output(mosipin, False)commandout <<= 1GPIO.output(clockpin, True)GPIO.output(clockpin, False)adcout = 0# read in one empty bit, one null bit and 10 ADC bitsfor i in range(12):GPIO.output(clockpin, True)GPIO.output(clockpin, False)adcout <<= 1if (GPIO.input(misopin)):adcout |= 0x1GPIO.output(cspin, True)adcout >>= 1       # first bit is 'null' so drop itreturn adcout# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
#SPICLK = 18
#SPIMISO = 23
#SPIMOSI = 24
#SPICS = 25
# Above pin number seems not working. I modified the pin as below.
# See also: http://elinux.org/RPi_Low-level_peripherals
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)# 10k trim pot connected to adc #0
potentiometer_adc = 0;last_read = 0       # this keeps track of the last potentiometer value
tolerance = 5       # to keep from being jittery we'll only change# volume when the pot has moved more than 5 'counts'while True:# we'll assume that the pot didn't movetrim_pot_changed = False# read the analog pintrim_pot = readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)# how much has it changed since the last read?pot_adjust = abs(trim_pot - last_read)if DEBUG:print "trim_pot:", trim_potprint "pot_adjust:", pot_adjustprint "last_read", last_readif ( pot_adjust > tolerance ):trim_pot_changed = Trueif DEBUG:print "trim_pot_changed", trim_pot_changedif ( trim_pot_changed ):# convert 12bit adc0 (0-4096) trim pot read into 0-100 volume levelset_volume = trim_pot / (10.24 * 4)set_volume = round(set_volume)          # round out decimal valueset_volume = int(set_volume)            # cast volume as integerprint 'Volume = {volume}%' .format(volume = set_volume)set_vol_cmd = 'sudo amixer cset numid=1 -- {volume}% > /dev/null' .format(volume = set_volume)os.system(set_vol_cmd)  # set volumeif DEBUG:print "set_volume", set_volumeprint "tri_pot_changed", set_volume# save the potentiometer reading for the next looplast_read = trim_pot# hang out and do nothing for a half secondtime.sleep(0.5)

这篇关于mcp3208 和 树莓派3b板子的配置以及对应代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1039242

相关文章

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的