我使用的是带MSP430的16x2液晶屏,不知道如何从我的AtoD打印值。我的液晶屏使用ST7066U驱动程序,液晶屏的设置代码如下:
> #include <msp430.h>
> #include <delays.h>
>
> #define CMD 0
> #define DATA 1
>
> #define LCD_OUT P2OUT
> #define LCD_DIR P2DIR
> #define D4 BIT4
> #define D5 BIT5
> #define D6 BIT6
> #define D7 BIT7
> #define RS BIT2
> #define EN BIT3
>
> // Function to pulse EN pin after data is written to accept it void
> pulseEN(void) { LCD_OUT |= EN; delay_us(100); LCD_OUT &= ~EN;
> delay_us(100); }
>
> //Function to write data/command to LCD void lcd_write(int value,int
> mode) { if(mode == CMD) LCD_OUT &= ~RS;
> // Set RS -> LOW for Command mode else LCD_OUT |= RS;
> // Set RS -> HIGH for Data mode
>
> // Data has to be sent to LCD in 2 lots of 4 bits, as it is running in 4 bit mode LCD_OUT = ((LCD_OUT & 0x0F) | (value &
> 0xF0)); // Write top 4 bits pulseEN(); delay_us(10);
>
> LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0)); // Write
> lower 4 bits pulseEN(); delay_us(10); }
>
> // Function to print a string on LCD void lcd_print(char *s) {
> while(*s) { lcd_write(*s, DATA); s++; }
> delay_ms(10); }
>
> // Function to move cursor to desired position on LCD void
> lcd_setCursor(int row, int col) { const row_offsets[] = { 0x00,
> 0x40}; lcd_write(0x80 | (col + row_offsets[row]), CMD); delay_ms(1);
> }
>
> // Function to clear the LCD void lcd_clr() { lcd_write(0x01, CMD);
> // Clear screen delay_ms(5); }
>
> // Initialize LCD void lcd_init() { P2SEL &= ~(0x60); LCD_DIR |=
> (D4+D5+D6+D7+RS+EN); LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
>
> delay_ms(15); // Wait for
> power up ( 15ms ) lcd_write(0x33, CMD);
> // Initialization Sequence 1 delay_ms(5); lcd_write(0x32, CMD);
> // Initialization Sequence 2 delay_ms(1);
>
> // All subsequent commands take 40 us to execute, except clear &
> cursor return (1.64 ms), may as well set the lot to 5ms just to be
> safe
>
> lcd_write(0x28, CMD); // 4 bit
> mode, 2 line delay_ms(5);
>
> lcd_write(0x0C, CMD); // Display
> ON, Cursor OFF, Blink OFF delay_ms(5);
>
> lcd_write(0x01, CMD); // Clear
> screen delay_ms(5);
>
> lcd_write(0x06, CMD); // Auto
> Increment Cursor delay_ms(5); }在我的主代码中,我读取了一个AtoD值,但我不太确定如何显示它。AtoD运行良好,我可以在Crossworks中使用调试模式,并看到AtoD值是我所期望的。我可以在LCD上显示文本,如下所示:
lcd_setCursor(0,0);
lcd_print("AtoD Test");
lcd_setCursor(1,0);
lcd_print("AtoD OK");这很好。它将显示我写的字符。但是我如何让它显示我从某处读取的值呢?
我是一个完全的新手在这方面,用于设置液晶的代码取自HERE和稍微编辑,以适应我的MSP和设置。从本质上讲,我只是想做一些像这样的事情
lcd_print("ADC_Result");但在执行此操作时,它只是逐字打印这些单词,并且在尝试时
lcd_print("some text %*",ADC_Result);我得到一个错误,说太多的参数给lcd_print。我假设这意味着在我的设置中,我需要更改lcd_print的工作方式,但不确定如何做到这一点。有没有人可以让我知道如何编辑这篇文章,这样我就可以做一些类似于
lcd_print ("Some text %c", some_data);然后让它打印一些东西?
发布于 2021-10-27 15:45:00
您在借用的代码中使用的lcd_print()函数只接受一个参数,即指向以null结尾的字符串的指针。请参见该代码的第51行。但是您希望使用类似printf()的style输出到LCD,这是一个接受格式字符串和任意数量的变量的可变函数。这就是来自编译器的“说参数太多的错误”问题。
要解决这个问题,有两种通用的解决方案:重写lcd_print()以便可以像printf()一样使用它,或者在调用lcd_print()之前执行字符串转换。
下面是一个使用sprintf()函数的解决方案:
// Create a buffer for sprintf to work with, use largest size you can afford
char sbuffer[BUFSIZE];
// Convert result to a string
sprintf(sbuffer, "ADC_Result = %d", some_data);
// Output string to LCD
lcd_print(sbuffer);https://stackoverflow.com/questions/66746298
复制相似问题