Arduino简易示波器检测树莓派产生的方波
在使用超声测距模块时,需要给超声模块一个方波信号。于是可以用树莓派的的PWM功能产生一个低频的方波信号。 产生方波信号后,如果手边没有示波器,还可以使用Arduino的ADC采样功能,做一个简单的示波器。 树莓派产生方波 树莓派的pin12、pin33(GPIO_18、GPIO_13)是树莓派提供的PWM硬件接口,可以产生高频的PWM信号。 由于我只需要产生一个大约50Hz的方波信号。用最简单的GPIO库就可以产生可用的方波。 ...
在使用超声测距模块时,需要给超声模块一个方波信号。于是可以用树莓派的的PWM功能产生一个低频的方波信号。 产生方波信号后,如果手边没有示波器,还可以使用Arduino的ADC采样功能,做一个简单的示波器。 树莓派产生方波 树莓派的pin12、pin33(GPIO_18、GPIO_13)是树莓派提供的PWM硬件接口,可以产生高频的PWM信号。 由于我只需要产生一个大约50Hz的方波信号。用最简单的GPIO库就可以产生可用的方波。 ...
Arduino可以使用PWM产生方波信号,在我的Arduino UNO R3上,支持PWM的输出口是pin 3,5,6,9,10,11这几个引脚,支持大约980Hz的PWM输出。这方面不再赘述。 本文介绍另一种产生方波的方法,可以使用任何引脚产生方波信号。功能: 固定频率,占空比,偏移量的方波 通过模拟口连接可调电位器,产生可变频率、占空比、偏移量的方波 // High-accuracy square wave generator // based on Arduino UNO // with runtime adjustable frequency, PWM width and offset // Output wave at pin 13 double freq; // Hz double offset; // percent (0.0 to 1.0) double width; // percent (0.0 to 1.0) // unit: microsecond unsigned long cycle_time; unsigned long raising_edge; unsigned long falling_edge; unsigned long prev_micros; // compare 2 unsigned value // true if X > Y while for all possible (X, Y), X - Y < Z #define TIME_CMP(X, Y, Z) (((X) - (Y)) < (Z)) inline void setHigh() { // 2 CPU cycles to balance execution time with setLow() // this is based on measurement on Arduino UNO R3, your mileage may vary PORTB = B00100000; PORTB = B00100000; } inline void setLow() { PORTB = B00000000; } void setup() { DDRB = B00100000; prev_micros = micros(); while(1) { // read everything from analog input (potentiometer) // frequency: 0.1-102.4 Hz // width: 0-100% // offset: 0-100% //freq = (double)(analogRead(1) + 1) / 10; //width = (double)(analogRead(0) + 1) / 1024; //offset = (double)analogRead(2) / 1024; // OR manual settings // max possible frequency is around 55000Hz with <1KHz deviation // based on measurements on Arduino UNO R3 // you may get to ~77500Hz with significantly larger deviation // note: please uncomment the next 3 expressions, then // move the following 6 expressions ahead of while loop // if you are going to use manual settings, because it is no worth // to recalculate them. freq = 50; width = 0.3; offset = 0.0; cycle_time = 1000000 / freq; raising_edge = (unsigned long)(offset * cycle_time) % cycle_time; falling_edge = (unsigned long)((offset + width) * cycle_time) % cycle_time; if (width + offset < 1) { // raising edge should appear earlier while (TIME_CMP(micros(), prev_micros + raising_edge, cycle_time)); setHigh(); while (TIME_CMP(micros(), prev_micros + falling_edge, cycle_time)); setLow(); } else { // falling edge should appear earlier while (TIME_CMP(micros(), prev_micros + falling_edge, cycle_time)); setLow(); while (TIME_CMP(micros(), prev_micros + raising_edge, cycle_time)); setHigh(); }pin prev_micros += cycle_time; } } 解释: ...
安装LCD库 使用AdaFruit库来控制lcd库,这个库支持AdaFruit屏幕和使用HD44780的显示屏。 通过源码安装: git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git cd ./Adafruit_Python_CharLCD sudo python setup.py install 将树莓派和LCD1602连接 连接的图如下所示: LCD电子钟程序 #!/usr/bin/python3 # -*- coding: utf-8 -*- import RPi.GPIO as gpio #to add the LCD library import Adafruit_CharLCD as LCD import time gpio.setmode(gpio.BCM) #声明 LCD pins(对应BCM引脚) lcd_rs = 17 lcd_en = 18 lcd_d4 = 27 lcd_d5 = 22 lcd_d6 = 23 lcd_d7 = 10 lcd_backlight = 2 lcd_columns = 16 #Lcd column lcd_rows = 2 #number of LCD rows lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, \ lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows,\ lcd_backlight) lcd.set_cursor(0,0) lcd.message(' CLOCK') while True: lcd.set_cursor(0,1) localtime = time.asctime( time.localtime(time.time()) )[4:-5] print(localtime) lcd.message(localtime) time.sleep(1) 效果图 显示的效果图如下,可以显示日期和时间,每秒钟刷新屏幕一次:
在树莓派3B+里启用串口,并通过UART读取GPS模块的数据帧。 树莓派3启用UART #####先更新系统 sudo apt-get update sudo apt-get upgrade sudo raspi-config 在raspi-config中设置: disable login shell over serial enable serial hardware port #####然后重启 sudo reboot #####编辑配置文件 sudo nano /boot/config.txt 在最后一段加上: dtparam=spi=on dtoverlay=pi3-disable-bt core_freq=250 enable_uart=1 force_turbo=1 然后编辑cmdline.txt sudo cp boot/cmdline.txt boot/cmdline_backup.txt sudo nano /boot.cmdline.txt 将cmdline.txt的内容替换为: dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles ...
本文介绍树莓派上GPIO引脚的三种编号方式,同时介绍控制引脚的方式。并以BCM-17引脚为例,动手点亮led灯并使其闪烁。 GPIO是通用输入输出接口。树莓派上有40个引脚,对这40个引脚主要有两种编号方式。 BOARD 和引脚物理顺序一致 BCM wiringpi编号 wiringPi是一个用来控制GPIO的库,它对40个引脚的编号方式如下: ...
Python3实现AES和DES对称加密算法的 ...
Python3实现哈希散列算法,包含MD5和sha256。 Hash函数算法编写 本实验是使用python来编写MD5和SHA256加密函数,并对加密函数的正确性进行验证。 验证的方式是通过和已有的标准库加密结果进行比较,如果结果相同,则加密函数正确。 1.实验目的 熟悉MD5和SHA256加密函数的原理和应用 实现MD5和SHA256加密函数并验证 2.实验工具 Jupyter Notebook Python3.5 3.实验环境 Ubuntu16.04LTS操作系统 Python3标准库 4.实验步骤 4.1 回顾课程,查阅资料 4.2 熟悉MD5的原理 MD5(Message-Digest Algorithm)消息摘要算法是一种广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。 MD5将可变长度的消息处理成128位的固定长度输出。输入消息被分解成512位块(16个32位字)块;该消息被填充以使其长度可以被512整除。 填充的工作原理如下: ...
程序效果图如下: 程序的效果就是可以在Terminal浏览一下每天知乎日报的标题和url,然后根据兴趣选择是否继续阅读。 程序十分简单,只十几行代码。使用python3,需要安装requests包。 源代码如下,也可以从我的github下载。 #!/usr/bin/python3 #-*- coding: utf-8 -*- import requests import json headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'} def get_daily(): page = requests.get('http://news-at.zhihu.com/api/3/news/latest', headers=headers).text response = json.loads(page) date = response['date'] stories = response['stories'] date = '\n%s年%s月%s日' % (date[:4], date[4:6], date[6:]) print('%s 共%d条日报\n' % (date, len(stories))) for index,story in enumerate(stories, 1): print('{0:<2d}:{1:s}\n url:http://news-at.zhihu.com/story/{2:d}'.format(index, story['title'], story['id'])) if __name__ == '__main__': get_daily() 保存为zhdaily.py文件,然后将文件放到/usr/local/bin/目录下,并给zhdaily.py增加执行权限: ...
上一节中,IntegralOccupancyMap()函数用来确定单词位置,其中调用了query_integral_image()方法。而query_integral_image是用来Cython。下边介绍Cython。 Cython介绍 Cython 的本质可以总结如下:Cython 是包含C 数据类型的Python。 Cython可以将Python代码编译成动态链接库,在某些情况下,可以极大提高Python程序的运行效率。 ...
wordcloud是python用来生成词云的第三方库,github地址是word_cloud 下载源码: git clone https://github.com/amueller/word_cloud 然后,直接看最老的版本,有精力的话看完最老版本可以再看最新的版本。 git tag git checkout 1.2.1 现在我们的目录结构如下: 可以看到,核心代码都在wordcloud目录下: wordcloud项目主要用了以下第三方库: Numpy PIL Cython 储备知识: ...
Python模块wordcloud参考文档的中文翻译 官网链接:wordcloud api reference Github链接:wordcloud 所有函数均封装在WordCloud类里: WordCloud([…]) 生成并绘制WordCloud对象 ImageColorGenerator(image) 词云颜色生成器(基于图片颜色) random_color_func([]) 词云颜色随机生成 wordcloud.WordCloud class wordcloud.WordCloud(font_path=None, width=400, height=200, margin=2, ranks_only=None, prefer_horizontal=0.9, mask=None, scale=1, color_func=None, max_words=200, min_font_size=4, stopwords=None, random_state=None, background_color='black', max_font_size=None, font_step=1, mode='RGB', relative_scaling=0.5, regexp=None, collocations=True, colormap=None, normalize_plurals=True) 参数: font_path : string 需要使用的字体路径(支持OTF和TTF)。Linux系统上默认指向DroidSansMono路径。若使用其他操作系统或者没有DroidSansMono字体,需要指定字体路径。 ...