我有以下問題: 開關應被推動,並且經由端口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;
}
你'字符*文本[20];'是二十初始化*指針* – wildplasser
注意數組:不要使用AVR或類似微控制器的流功能。他們太笨拙和膨脹。編寫你自己的小型轉換函數。 – Olaf
你在混淆數組和指針。 – Olaf