【问题标题】:Displaying AtoD value on 16x2 LCD using MSP430使用 MSP430 在 16x2 LCD 上显示 AtoD 值
【发布时间】:2023-04-06 17:03:01
【问题描述】:

我正在使用带有 MSP430 的 16x2 LCD,但无法弄清楚如何从我的 AtoD 打印值。我的 LCD 使用的是 ST7066U 驱动,我的 LCD 的设置代码是这样的:

> #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");

这很好。它会显示我写的字符。但是如何让它显示我从某处读取的值?

我完全是新手,设置 LCD 的代码取自 HERE,并稍作编辑以适合我的 MSP 和设置。本质上,我只是想做类似的事情

lcd_print("ADC_Result");

但是在这样做的时候,它只是在字面上打印这些单词,并且在尝试时

lcd_print("some text %*",ADC_Result);

我收到一个错误,提示 lcd_print 的参数太多。我认为这意味着在我的设置中,我需要更改 lcd_print 的工作方式,但不确定如何执行此操作。有人可以让我知道如何编辑它,以便我可以按照以下方式做一些事情

lcd_print ("Some text %c", some_data);

让它打印一些东西?

【问题讨论】:

    标签: msp430 lcd


    【解决方案1】:

    您从借用的代码中使用的 lcd_print() 函数只接受一个参数,即指向以空字符结尾的字符串的指针。请参阅该代码的第 51 行。但是您想使用类似 printf() 的样式输出到 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多