RP2040 CXX SDK PIO应用例程

RP2040 CXX SDK PIO应用例程


  • 📍DS18B20 PIO参考项目例程:https://github.com/jondurrant/RP2040PIO-DS18B20
  • 📍DHT11 PIO 参考项目例程:https://github.com/vmilea/pico_dht
    在官方的SDK pico-examples中有关PIO的例程有20个:https://github.com/raspberrypi/pico-examples
    在这里插入图片描述

📗RP2040 PIO功能介绍

📝RP2040中有2个相同的PIO块。每个PIO模块都有到总线结构、GPIO和中断控制器的专用连接。单个PIO模块的示意图如图38所示:
在这里插入图片描述

  • ✨使用PIO功能模拟外设,需要使用PIO专门的汇编指令集进行编程。
  • 📑PIO指令集:
    在这里插入图片描述

不懂得汇编的,只能先拿现有PIO开发出来的模拟驱动外设模块,直接使用。我们不需要了解驱动模块内部是如何实现的。RP2040只集成了2路PIO资源,一般只能用来模拟单线或双线的信号。

📘DS18B20 PIO驱动例程

  • 📍例程项目:https://github.com/jondurrant/RP2040PIO-DS18B20
  • DS1820.PIO引脚7。
  • 工程结构:
    在这里插入图片描述
  • 🔖CMakeLists.txt相关内容:
set(PICO_CXX_ENABLE_EXCEPTIONS 1) # Enable C++ exceptions
add_executable(RP2040_PIO_DS18B20RP2040_PIO_DS18B20.cppDS18B20.hDS18B20.cpp)
# Add any user requested libraries
pico_generate_pio_header(RP2040_PIO_DS18B20 ${CMAKE_CURRENT_LIST_DIR}/DS1820.pio)
  • 程序
/*CMSIS-DAP烧录命令:openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c  "adapter speed 5000"-c "program RP2040_PIO_DS18B20.elf verify reset exit"jlink命令: openocd -f interface/jlink.cfg -f target/rp2040.cfg  -c  "adapter speed 2000" -c  "program RP2040_PIO_DS18B20.elf verify reset exit"
*/#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "DS1820.pio.h"
#include "pico/time.h"
#include "DS18B20.h"
#include "hardware/adc.h"//读取内部温度需要#define BUILTIN_LED PICO_DEFAULT_LED_PIN // LED is on the same pin as the default LED 25
#define DS18B20_PIN 7
#define DS18B20_PIO pio0
//芯片内部温度读取函数
float getTemp(){const float conversion_factor = 3.3f / (1 << 12);adc_select_input(4);float v = (float)adc_read() * conversion_factor;float t = 27.0 - ((v - 0.706)/0.001721);return t;
}int main()
{uint32_t time;float temp;DS18B20 ds = DS18B20(DS18B20_PIO, DS18B20_PIN);stdio_init_all();//setup_default_uart();gpio_init(BUILTIN_LED);gpio_set_dir(BUILTIN_LED, GPIO_OUT);gpio_pull_up(BUILTIN_LED);adc_init();adc_set_temp_sensor_enabled(true);sleep_ms(3000);printf("GO\n");while (1) {time = to_ms_since_boot (get_absolute_time ());ds.convert();time = to_ms_since_boot (get_absolute_time ()) - time;printf("Covert time %d ms \n", time);sleep_ms(1000);time = to_ms_since_boot (get_absolute_time ());temp = ds.getTemperature();time = to_ms_since_boot (get_absolute_time ()) - time;printf("DS18B20 Temp %f in %d ms\r\n", temp, time);printf("Pico Temp was %f\n", getTemp());sleep_ms(500);gpio_xor_mask(1ul << BUILTIN_LED); // Toggle the LED}return 0;
}

在这里插入图片描述

📘DHT11 PIO驱动例程

  • 📍dht11驱动例程参考:https://github.com/vmilea/pico_dht
  • 🔖DHT11.PIO引脚7。
  • 🍁工程结构:
    在这里插入图片描述
  • 🔖CMakeLists.txt相关内容:
add_subdirectory(dht)
# Add executable. Default name is the project name, version 0.1add_executable(RP2040_DHT11_PIO  RP2040_DHT11_PIO.cdht/dht.c)
# Add any user requested libraries
pico_generate_pio_header(RP2040_DHT11_PIO ${CMAKE_CURRENT_LIST_DIR}/dht/dht.pio)# Add the standard library to the build
target_link_libraries(RP2040_DHT11_PIOpico_stdlibhardware_adchardware_piohardware_gpiohardware_dma)# Add the standard include files to the build
target_include_directories(RP2040_DHT11_PIO PRIVATE${CMAKE_CURRENT_LIST_DIR}${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required${CMAKE_CURRENT_LIST_DIR}/dht/include
)
  • 📝程序:
/*CMSIS-DAP烧录命令:openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c  "adapter speed 5000"-c "program RP2040_DHT11_PIO.elf verify reset exit"jlink命令: openocd -f interface/jlink.cfg -f target/rp2040.cfg  -c  "adapter speed 2000" -c  "program RP2040_DHT11_PIO.elf verify reset exit"*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/timer.h"
#include "hardware/clocks.h"
#include "hardware/adc.h" //读取内部温度需要
#include "dht.h"#define BUILTIN_LED PICO_DEFAULT_LED_PIN // LED is on the same pin as the default LED 25// change this to match your setup
static const dht_model_t DHT_MODEL = DHT11;
static const uint DATA_PIN = 7;//DHT11数据引脚定义static float celsius_to_fahrenheit(float temperature)
{return temperature * (9.0f / 5) + 32;
}//芯片内部温度读取函数
float getTemp()
{const float conversion_factor = 3.3f / (1 << 12);adc_select_input(4);float v = (float)adc_read() * conversion_factor;float t = 27.0 - ((v - 0.706) / 0.001721);return t;
}int main()
{stdio_init_all();// setup_default_uart();gpio_init(BUILTIN_LED);gpio_set_dir(BUILTIN_LED, GPIO_OUT);gpio_pull_up(BUILTIN_LED);adc_init();adc_set_temp_sensor_enabled(true);puts("\nDHT test");dht_t dht;dht_init(&dht, DHT_MODEL, pio0, DATA_PIN, true /* pull_up */);do{dht_start_measurement(&dht);float humidity;float temperature_c;dht_result_t result = dht_finish_measurement_blocking(&dht, &humidity, &temperature_c);if (result == DHT_RESULT_OK){printf("%.1f C (%.1f F), %.1f%% humidity\n", temperature_c, celsius_to_fahrenheit(temperature_c), humidity);}else if (result == DHT_RESULT_TIMEOUT){puts("DHT sensor not responding. Please check your wiring.");}else{assert(result == DHT_RESULT_BAD_CHECKSUM);puts("Bad checksum");}printf("Pico Temp was %f\n", getTemp()); //打印芯片内部温度gpio_xor_mask(1ul << BUILTIN_LED); // Toggle the LEDsleep_ms(2000);} while (true);return 0;
}

在这里插入图片描述

📘PWM PIO驱动例程

  • 🔖来自自带例程:pico-examples\pio\pwm
  • 📝测试代码:
#include <stdio.h>
/*CMSIS-DAP烧录命令:openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c  "adapter speed 5000"-c "program RP2040_PIO_PWM.elf verify reset exit"jlink命令: openocd -f interface/jlink.cfg -f target/rp2040.cfg  -c  "adapter speed 2000" -c  "program RP2040_PIO_PWM.elf verify reset exit"
Sysclk = 125MHz  PIO_clk =41,667,788.8Hz
Sysclk = 270MHz  PIO_clk =44,892,160Hz
PWM输出频率 = 41,667,788.8Hz / ( PWM_period+1)
*/
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "pwm.pio.h"#define BUILTIN_LED PICO_DEFAULT_LED_PIN // LED is on the same pin as the default LED 25
#define PWM_PIN 7// Write `period` to the input shift register
void pio_pwm_set_period(PIO pio, uint sm, uint32_t period)
{pio_sm_set_enabled(pio, sm, false);pio_sm_put_blocking(pio, sm, period);pio_sm_exec(pio, sm, pio_encode_pull(false, false));pio_sm_exec(pio, sm, pio_encode_out(pio_isr, 32));pio_sm_set_enabled(pio, sm, true);
}// Write `level` to TX FIFO. State machine will copy this into X.
void pio_pwm_set_level(PIO pio, uint sm, uint32_t level)
{pio_sm_put_blocking(pio, sm, level);
}int main()
{// set_sys_clock_khz(270000, true);//270000KHzstdio_init_all();// setup_default_uart();puts("PIO PWM Example");gpio_init(BUILTIN_LED);gpio_set_dir(BUILTIN_LED, GPIO_OUT);gpio_pull_up(BUILTIN_LED);// todo get free smPIO pio = pio0;int sm = 0;uint offset = pio_add_program(pio, &pwm_program);printf("Loaded program at %d\n", offset);pwm_program_init(pio, sm, offset, PWM_PIN);// pio_pwm_set_period(pio, sm, (1u << 16) - 1); // 65535pio_pwm_set_period(pio, sm, 4167 - 1);int level = 4166/2;//32768;//      printf("Level = %d\n", level);pio_pwm_set_level(pio, sm, level);//   level = (level + 1) % 256;while (true){printf("Level = %d\n", level);sleep_ms(1000);gpio_xor_mask(1ul << BUILTIN_LED); // Toggle the LED}return 0;
}
  • 📏PWM输出10KHz.占空比:50%
    在这里插入图片描述
  • 🎉超频情况下:set_sys_clock_khz(270000, true);//270000KHz
    在这里插入图片描述
  • 🌿也就是说通过PIO 产生的PWM输出频率,在没有超频的情况下。RP2040默认时钟频率125MHz情况下,PWM输出最大频率:41,667,788Hz / ( 1+1)=20,833,894Hz
  • 🌿时钟频率超频到270MHz情况下,PWM输出最大频率:44,892,160/2=22,446,080Hz

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1543759.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

828华为云征文 | 云服务器Flexus X实例,Docker集成搭建Halo博客平台

828华为云征文 | 云服务器Flexus X实例&#xff0c;Docker集成搭建Halo博客平台 Halo博客平台是一款基于Java的开源博客系统&#xff0c;以其简单易用、功能强大、美观大方等特点而受到广泛欢迎&#xff0c;采用了多种先进的技术框架&#xff0c;包括Freemarker模板引擎、Vue.j…

【STM32】【rt-thread】startup_stm32f405xx.S文件解读

startup_stm32f405xx.S文件解读 一、代码全文 /********************************************************************************* file startup_stm32f405xx.s* author MCD Application Team* brief STM32F405xx Devices vector table for GCC based toolcha…

每日OJ题_牛客_ 游游的you(贪心+模拟)

目录 牛客_ 游游的you&#xff08;贪心模拟&#xff09; 解析代码 牛客_ 游游的you&#xff08;贪心模拟&#xff09; 游游现在有a个y&#xff0c;b个o&#xff0c;c个u&#xff0c;他想用这些字母拼成一个字符串。 三个相邻的字母是"you"可以获得2分&#xff0c…

室内院内常见的不知名蚊虫(昆虫)图鉴和防治方法

文章目录 蟑螂形态特征出现源头危害性防治方法 跳蚤形态特征出现源头危害性防治方法 臭虫&#xff0c;又名木蚤、床虱、壁虱形态特征出现源头危害性防治方法 尘螨形态特征出现源头危害性防治方法 蛾蚋&#xff08;ru&#xff09;&#xff0c;又名蛾蠓&#xff08;měng&#xf…

解密.baxia勒索病毒:.baxia勒索病毒的攻击手法及防护建议

导言 在当前网络安全形势日益严峻的背景下&#xff0c;勒索软件的威胁正不断升级&#xff0c;其中.baxia勒索病毒尤为突出。作为一种新型恶意软件&#xff0c;.baxia病毒通过加密用户的文件并要求支付赎金来获取解密密钥&#xff0c;对个人和企业的安全构成了严重威胁。随着其…

医院预约|基于springBoot的医院预约挂号系统设计与实现(附项目源码+论文+数据库)

私信或留言即免费送开题报告和任务书&#xff08;可指定任意题目&#xff09; 目录 一、摘要 二、相关技术 三、系统设计 四、数据库设计 五、核心代码 六、论文参考 七、源码获取 一、摘要 近年来&#xff0c;信息化管理行业的不断兴起&#xff0c;使得人们的日…

国庆节适合买什么东西?精选五款实用又优惠的多功能好物!

临近国庆&#xff0c;我猜很多朋友已经开始为假期做好准备&#xff0c;计划开启出游和购物的节奏了&#xff01;大家都希望在国庆期间&#xff0c;买到一些平时因为价格太贵而舍不得下单的好物&#xff01;作为一名家居兼数码博主&#xff0c;每年国庆的时候我都会疯狂采购各种…

Ansible流程控制-条件语句_循环语句

文章目录 Ansible流程控制条件语句且、或、非、是模糊条件when指令的详细使用方法 循环语句如何使用使用item变量结合with_items或loop指令item变量有固定子元素&#xff1f; 实例-服务器安装基础环境优化需求部分实现换指定新仓库安装基础软件包 Ansible流程控制 一、 1. 条件…

文件服务器FastDFS 消息队列中间件RabbitMQ

新标签页 (chinaunix.net) FastDFS - Browse Files at SourceForge.net 一、FastDFS Tracker和Storage&#xff1a; tracker用来管理所有的storage&#xff0c;只是管理服务器&#xff0c;负责负载均衡。 storage是存储服务器&#xff0c;每一个storage服务器都是一个单独的个…

Cilium + ebpf 系列文章-什么是ebpf?(一)

前言&#xff1a; 这篇非常非常干&#xff0c;很有可能读不懂。 这里非常非常推荐&#xff0c;建议使用Cilium官网的lab来辅助学习&#xff01;&#xff01;&#xff01;Resources Library - IsovalentExplore Isovalents Resource Library, your one-stop destination for ins…

828华为云征文|华为云Flexus云服务器X实例部署Xnote笔记应用

828华为云征文&#xff5c;华为云Flexus云服务器X实例部署Xnote笔记应用 前言一、Flexus云服务器X实例介绍1.1 Flexus云服务器X实例简介1.2 Flexus云服务器X实例特点1.3 Flexus云服务器X实例使用场景 二、Note Mark 介绍2.1 Xnote简介2.2 Xnote特点2.3 主要使用场景 三、本次实…

浅谈剩余电流动作保护装置的功能和应用

【摘要】介绍了剩余电流动作保护装置的组成、类型及功能&#xff0c;并针对设计中存在的问题&#xff0c;提出了在工程应用中需要注意的事项&#xff0c;进而结合相应的规范、标准和应用实际&#xff0c;分析了剩余电流动作保护装置在不同应用场所、不同电气环境下应如何正确选…

数据结构实验二之线性表(中)

实验题3:实现双链表的各种基本运算的算法 题目描述 编写一个程序dlinklist.cpp,实现双链表的各种基本运算和整体建表算法(偏 双链表的元素类型ElemType为int),并在此基础上设计一个程序exp2-3.cpp完成以 功能。 (1)初始化双链表h。 (2)依次采用尾插法插入元素a、b、c、d、e。 …

springboot itextpdf 形式导出pdf

先看效果(这里只设置了软件版本和 完成情况的勾选框) 导入pom依赖 <dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version> </dependency> <!--itextpdf--> <d…

C++之初识STL(概念)

STL&#xff08;标准模板库&#xff09; STL广义分类为&#xff1a;容器&#xff0c;算法&#xff0c;迭代器 * **容器**和**算法**之间通过**迭代器**进行无缝连接 意义&#xff1a;C的**面向对象**和**泛型编程**思想&#xff0c;目的就是**复用性的提升** STL六大组件 1. 容…

Flink 本地启动的多种方式

Flink 本地启动的多种方式 Application模式通过代码提交到Yarn上启动 //设置Yarn客户端 YarnClient yarnClient ; Configuration configuration new Configuration(); if (customConfiguration ! null) {configuration.addAll(customConfiguration); } configuration.set(Jo…

PostgreSQL的学习心得和知识总结(一百五十一)|[performance] PostgreSQL列对齐

目录结构 注&#xff1a;提前言明 本文借鉴了以下博主、书籍或网站的内容&#xff0c;其列表如下&#xff1a; 1、参考书籍&#xff1a;《PostgreSQL数据库内核分析》 2、参考书籍&#xff1a;《数据库事务处理的艺术&#xff1a;事务管理与并发控制》 3、PostgreSQL数据库仓库…

九泰智库 | 医械周刊- Vol.59

⚖️ 法规动态 国家药监局&#xff1a;截至目前已批准296个创新医疗器械上市 近日&#xff0c;国家药监局在“推动高质量发展”系列主题新闻发布会介绍。截至目前&#xff0c;国家药监局已批准296个创新医疗器械上市&#xff0c;这些创新医疗器械主要集中在植介入类设备、高端…

孤独伤感视频素材哪里找?分享热门伤感短视频素材资源网站

你是不是也经常在抖音上刷到很火的伤感视频&#xff0c;那么伤感视频素材都在哪里可以下载呢&#xff1f;作为一名从业多年的视频剪辑师&#xff0c;今天就跟大家聊聊那些可以下载伤感素材高清无水印的网站&#xff0c;如果你也在苦苦找寻伤感素材&#xff0c;快来看看吧&#…

基于Es和智普AI实现的语义检索

1、什么是语义检索 语义检索是一种利用自然语言处理&#xff08;NLP&#xff09;和人工智能&#xff08;AI&#xff09;技术来理解搜索查询的语义&#xff0c;以提供更准确和相关搜索结果的搜索技术&#xff0c;语义检索是一项突破性的技术&#xff0c;旨在通过深入理解单词和…