c - 使用 avr micro 和 Avrstudio 在 LCD 中显示 float

标签 c avr lcd

我编写了一个 Avrstudio V6 程序(c 代码),用于在 lcd 中显示 Pt100 温度,因此我正确地从 ACDW 读取值,但是当我对此值进行一些数学运算时,我无法在 lcd 上显示它的浮点值。

简单的程序在LCD中显示一个浮点值,我使用的lcd库来自LCD Library

主要代码为:

#include <avr/io.h>
#include <avr/interrupt.h>
 #include <stdlib.h>
//#define F_CPU 16000000UL
#define F_CPU 8000000
#include <util/delay.h>
#define ADC_VREF_TYPE 0x40
//#include "adc_new.h"
#define D4 eS_PORTC4
#define D5 eS_PORTC5
#define D6 eS_PORTC6
#define D7 eS_PORTC7
#define RS eS_PORTC2
#define EN eS_PORTC3
#include "lcd.h" //Can be download from the bottom of this article
//  Constants and variables
//*****************************************************************************
// Input: Square wave on ICP PIN
// This program determines the pulse width of a square wave and if the pulse width is greater than 40 us
//than PD4 goes higher ,if its smaller than PD4 is low.

//---------------------------------------------------------//
void LCD_0(void)
{
    /*
' Lcd module connections
dim LCD_RS as sbit at PORTc2_bit
dim LCD_EN as sbit at PORTc3_bit
dim LCD_D4 as sbit at PORTc4_bit
dim LCD_D5 as sbit at PORTc5_bit
dim LCD_D6 as sbit at PORTc6_bit
dim LCD_D7 as sbit at PORTc7_bit

dim LCD_RS_Direction as sbit at DDc2_bit
dim LCD_EN_Direction as sbit at DDc3_bit
dim LCD_D4_Direction as sbit at DDc4_bit
dim LCD_D5_Direction as sbit at DDc5_bit
dim LCD_D6_Direction as sbit at DDc6_bit
dim LCD_D7_Direction as sbit at DDc7_bit

*/

DDRC = 0xFF;


Lcd4_Init();
Lcd4_Set_Cursor(1,1);
Lcd4_Write_String("Elasa.ir Test"); 
}   

//http://www.geeksforgeeks.org/convert-floating-point-number-string/
// Converts a floating point number to string.
void ftoa(float n, char *res, int afterpoint)
{
    // Extract integer part
    int ipart = (int)n;

    // Extract floating part
    float fpart = n - (float)ipart;

    // convert integer part to string
    int i = itoa(ipart, res, 0);

    // check for display option after point
    if (afterpoint != 0)
    {
        res[i] = '.';  // add dot

        // Get the value of fraction part upto given no.
        // of points after dot. The third parameter is needed
        // to handle cases like 233.007
        fpart = fpart * pow(10, afterpoint);

        itoa((int)fpart, res + i + 1, afterpoint);
    }
}


int main(void) {
DDRD = (0<<PD4);     // put PortB bit 5 as input
//PORTD = 0<<PD4;       // Enable PE4 pull-up resistor
DDRC = 0xFF;
//enable overflow and input capture interrupts

TIMSK=0x24;

/*Noise canceller, without prescaler, rising edge*/

TCCR1B=0xC1;



// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: Int., cap. on AREF
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;_delay_ms(30);


Lcd4_Init();

Lcd4_Set_Cursor(1,1);   
Lcd4_Write_String("Pulse width measuring:");
//Lcd4_Set_Cursor(2,1); 

//itoa(pulse_width2, str3, 10);

_delay_ms(3);



    while(1)
    {
        //http://www.edaboard.com/thread63677.html
        char buffer22[24];
        float x = 1.5;

        sprintf(buffer22, "Flo %f", x);
        Lcd4_Clear();
        Lcd4_Set_Cursor(1,1);   
        Lcd4_Write_String("Temp_eda:");
        Lcd4_Set_Cursor(2,1);
        Lcd4_Write_String(buffer22);
        _delay_ms(300);


        char res[20];
        float n = 233.007;
        ftoa(n, res, 4);


        //ftoa3(myFloatStr, myFloat, 10);
        //Lcd4_Write_String(myFloatStr);
        Lcd4_Clear();
        Lcd4_Set_Cursor(1,1);   
        Lcd4_Write_String("Temp_PT100:");
        Lcd4_Set_Cursor(2,1);
        Lcd4_Write_String(res);
        _delay_ms(300);

    }
}

所以 proteus 的输出在这里: pt100

正如你看到的字符串“?”在lcd中显示的是“1.5”,那么你能在我的代码中找到错误的部分吗,你可以在这里看到avrstudio和proteus代码: C codes

google Drive C code

非常感谢。

最佳答案

默认情况下,avr-libc库不支持带有 float 的sprintf,因为它们在不使用时需要大量内存,因此当您执行时

 sprintf(buffer22, "Flo %f", x);

它实际上只显示“Flo ?”正如 proteus 向您展示的那样...

要启用 float 支持,您需要 enable at the linker settings: add -lprintf_flt .

关于c - 使用 avr micro 和 Avrstudio 在 LCD 中显示 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41085067/

相关文章:

c - 局部变量在栈上的地址

c - libcurl 出现编译错误

c - 为什么我们需要功能测试宏?

c - 在 vector、SEI 和 intflags 仍然设置后中断停止执行。 (AtMega4809)

c - 设置 ILI9325 TFT 显示器的方向

python - LCD 脉冲消息

c - 带有管道的简单主函数中的错误文件号

c - 8 位 MCU 中的 32 位变量移位

c - 尝试对 TPIC6C595 移位寄存器进行位操作但没有输出

c - 将 rs232 编程为 lcd (Linux)