2015-12-09 42 views
1

我有以下問題: 開關應被推動,並且經由端口A的輸入端子發送信號移1位到端口B(曾作爲計劃)。現在程序將計算按下的按鈕並將該文本打印到小型LCD顯示器上。複印文本使用的sprintf()插入char數組 - C /愛特梅爾工作室7

當我嘗試使用我的sprintf沒有得到寫入存儲位置來保存文本到一個數組。我究竟做錯了什麼?注意:我們應該使用sprintf作爲解決方案。

#include "avr/io.h" 
#include "stdint.h" 
#include "lcd.h" 
#include "stdio.h" 

int main(void) { 
    unsigned char mask, presetb; 
    char *text[20]; 
    unsigned char buttons; 

    //variable to save lcd infos 
    display myLCD; 

    //set pointer to DDRA & set to Input(0) 
    DDRA &= ~((1 << DDA2) | (1 << DDA3) | (1 << DDA4) | (1 << DDA5)); //sets bits 2 to 5 to 0 

    //set ptr to DDRB and set as output (1) 
    DDRB |= ((1 << DDB3) | (1 << DDB4) | (1 << DDB5) | (1 << DDB6)); //sets bits 3 to 6 to 1 

    //initialize LCD 
    lcd_init(&myLCD, &PORTD); 

    for (;;) { 
     //set pointer to PINA & read bits 2 to 5, save in 'mask' and shift number <<1 
     mask = PINA & ((1 << PIN2) | (1 << PIN3) | (1 << PIN4) | (1 << PIN5)); 
     mask = mask << 1; 

     //set ptr to PORTB and copy shifted number 
     presetb = (~((1 << PIN3) | (1 << PIN4) | (1 << PIN5) | (1 << PIN6))) & (PORTB); //save bitvalues of bits 0 - 2 and 7 
     PORTB = (mask | presetb); //copy bits 0-2 and 7 of 'presetb' and bits 3 -6 of 'mask' to PORTB 

     //number of buttons pressed 
     buttons = 0; 

     if (PINB3 == 1) 
      buttons += 1; 

     if (PINB4 == 1) 
      buttons += 1; 

     if (PINB5 == 1) 
      buttons += 1; 

     if (PINB6 == 1) 
      buttons += 1; 

     //set string 
     sprintf(*text, "Pushed Buttons %d", (unsigned char)buttons); 

     //print text "Pushed Buttons [Nbr]" 
     lcd_send_string(&myLCD, *text, 1, 1); 
    } 
    return 0; 
} 
+1

你'字符*文本[20];'是二十初始化*指針* – wildplasser

+1

注意數組:不要使用AVR或類似微控制器的流功能。他們太笨拙和膨脹。編寫你自己的小型轉換函數。 – Olaf

+0

你在混淆數組和指針。 – Olaf

回答

1
char *text[20]; 

是20個指針數組char。在線路

sprintf(*text, "Pushed Buttons %d", (unsigned char) buttons); 

你要使用sprintf把文本轉換爲*text,這是數組的第一個條目,字符指針。未初始化的指針。

你想要的是

char text[20]; 
sprintf(text, "Pushed Buttons %d", (unsigned char) buttons); 

的20個字符數組。

1

文本應是

char text[20]; 

取下*

+0

'的sprintf(*文字,...'應修改'的sprintf(文字,...'以及 – chqrlie

+0

良好的漁獲@chqrlie,而*需要從lcd_send_string(myLCD,*文字,1,1刪除); – nicomp

+0

謝謝你,我錯過了的是,現在的作品:) – Jordan181

相關問題