如何解决使用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 );
我正在使用传感器
我已使用此代码进行了转换
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 举报,一经查实,本站将立刻删除。