使用PIC16F1824浮动为字符串

如何解决使用PIC16F1824浮动为字符串

使用PIC16F1824时遇到一些麻烦。 我想转换从ADC转换器获取的值并将其传输到UART。 我已使用MCC生成代码。 当我逐步调试代码时,电压值类似于我在示波器上获得的电压值。 但是,当我想在串行接口上​​打印此值时,printf中的参数%f(或%.2f)出现错误。

C:\ Program 文件\ Microchip \ xc8 \ v2.20 \ pic \ sources \ c99 \ common \ nf_fputc.c:16 :: 警告:(1498)表达式中的指针(未知)可能没有目标 C:\程序 文件\ Microchip \ xc8 \ v2.20 \ pic \ sources \ c99 \ common \ doprnt.c:66 ::错误: (1250)找不到变量_dbuf(908)退出的空间(80个字节) 状态= 1

这是我的代码

#include "mcc_generated_files/mcc.h"

void main(void) {
    SYSTEM_Initialize();

    uint16_t convertedValue,convertedValue2;
    float voltage,voltage2;
    float temp = 0;
    volatile float sensorRH = 0;
    char buffer[5];
    
    while (1) {
        //                 ADC_SelectChannel(channel_AN0);
        //        ADC_StartConversion();
        //        while (!ADC_IsConversionDone());
        //        convertedValue = ADC_GetConversionResult();
        //        voltage = convertedValue * 0.0048875;
        //        temp = (voltage2 - 0.424) / 0.00625; // LM60

        ADC_SelectChannel(channel_AN2);
        ADC_StartConversion();
        while (!ADC_IsConversionDone());
        convertedValue2 = ADC_GetConversionResult();
        voltage2 = convertedValue2 * 0.0048875; // Tension 5V - Résolution 10 bits (5/1023)   
        sensorRH = (voltage2 - 0.852) / 0.031483; // HIH-4000
        
        printf("ADC converted value = %.2f\n",sensorRH);               
        sprintf(buffer,"%f",sensorRH);
        
    }
}

我也尝试过:

  • include stdio lib:#include <stdio.h>
  • 将浮点值更改为volatile float sensorRH;
  • 在主函数之外声明我的浮点数。这给了我另一个错误:

C:\ Program 文件\ Microchip \ xc8 \ v2.20 \ pic \ sources \ c99 \ common \ nf_fputc.c:16 :: 警告:(1498)表达式中的指针(未知)可能没有目标 mcc_generation_files / eusart.c:64 ::错误:(1250)找不到空间 _eusartTxBuffer(908)退出状态= 1时为8个字节

  • 尝试使用不同大小的缓冲区(与第一个相同的错误)使用sprintf函数。

我正在将PIC16F1824与PIC4kit调试器和XC8编译器一起使用。 预先感谢

解决方法

所收到的错误意味着您没有足够的RAM或闪存,或者换句话说,您使用了过多的空间。减少空间的一种非常简单的方法是启用优化器。减少更多空间的一种方法是编写不需要太多空间的代码。 printf()系列还占用了大量的空间和浮点数。如果您不能使用printf()sprintf()并用整数运算代替浮点运算,则应该可以,但这可能会给您带来不太精确的计算,这并不重要,因为您的ADC并非如此。首先非常精确。

,

要解决来自编译器的诊断消息:

C:\Program Files\Microchip\xc8\v2.20\pic\sources\c99\common\nf_fputc.c:16:: warning: (1498) pointer (unknown) in expression may have no targets 
 
C:\Program Files\Microchip\xc8\v2.20\pic\sources\c99\common\doprnt.c:66:: error: (1250) could not find space (80 bytes) for variable _dbuf (908) exit status = 1

第一个“警告:(1498)”之所以生成,是因为XC8编译器在处理有时称为不透明指针的时候是愚蠢的。但是,由于这是参考Microchip库代码,因此您不应更改该代码,而实际上是正确的,只需忽略此警告即可。

第二个“错误:(1250)”是您的代码中的错误。您声明的对象超过了可用RAM的数量。

这是我刚刚为您创建的仅使用整数数学的代码:

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
#define VREF_VALUE (500)   // ADC VREF voltage in 1/100 of a volt
    uint32_t convertedValue2;
    uint16_t voltage2;
    
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts,you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        // Add your application code
        ADC_SelectChannel(channel_AN2);
        __delay_ms(1000);
        ADC_StartConversion();
        while (!ADC_IsConversionDone());
        convertedValue2 = ADC_GetConversionResult();
        voltage2 = (uint16_t)( (convertedValue2 * VREF_VALUE + 0x8000ul) >> 16 );
        printf("ADC voltage = %1u.%02u\r\n",voltage2/100,voltage2%100);
    }
}

由于您尚未提供所用传感器的任何详细信息,因此我无法显示整数数学来进行特定于传感器的缩放和偏移。

完整的MPLABX v5.40项目可在我的git hub存储库here中找到。

,

非常感谢您的回答。 我想我了解建议,将值转换为整数,然后将其除以在串行接口中获得浮点值。 我对这条线有疑问

voltage2 = (uint16_t)( (convertedValue2 * VREF_VALUE + 0x8000ul) >> 16 );

它给了我一些错误 enter image description here

我正在使用传感器

LM60 HIH-4000

我已使用此代码进行了转换

voltage = convertedValue * 0.0048875; // VREF : 5V / 10 bits ADC    
temp = (voltage - 0.424) / 0.00625; // LM60   
sensorRH = (voltage - 0.852) / 0.031483; // HIH-4000

感谢您的帮助

,

最后,我以毫伏为单位通过UART发送ADC值,并在PC软件上进行转换。 它不是真的很干净,但是可以完成工作... 谢谢

 int i = 0;
 int j = 0;
 while (1) {
        ADC_SelectChannel(channel_AN2);
        ADC_StartConversion();
        while (!ADC_IsConversionDone());
        convertedValue = ADC_GetConversionResult();
        voltage = convertedValue * 0.0048875;
        i = voltage * 100;
       
        __delay_ms(500);                    
        
        ADC_SelectChannel(channel_AN3);     
        ADC_StartConversion();
        while (!ADC_IsConversionDone());
        convertedValue2 = ADC_GetConversionResult();
        voltage2 = convertedValue2 * 0.0048875; 
        j = voltage2 * 100;
            
        printf("ADC voltage = %1u - %1u\r\n",i,j);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-