Forum: Mikrocontroller und Digitale Elektronik SPRINTF funktioniert nicht!


von Andre P. (stanleyku)


Lesenswert?

Hallo Zusammen,

ich habe ein wahrscheinlich ziemlich triviales Problem. Ich möchte eine 
Zahl in eine Zeichenkette anhängen und auf meine 4x20 LC Display 
anzeigen. Die Zahlen funktionieren nur rein gar nicht. Ich weiß nicht 
mehr woran es noch liegen kann. Ich programmiere in der Umgebung von 
MicroC Pro und habe einen PIC18F66K80. Hier mein Code:
1
/*******************************************************************************
2
     MCU:             PIC18F66K80
3
     Oscillator:      HS, 18.0000 MHz
4
 ******************************************************************************/
5
 #define             IP_adress            PORTF
6
7
8
char lcd_line1[20], lcd_line2[20], lcd_line3[20], lcd_line4[20]   ;
9
unsigned char IDmodule=0;
10
11
12
// Lcd module connections
13
sbit LCD_RS at LATD4_bit;
14
sbit LCD_EN at LATD6_bit;
15
sbit LCD_D4 at LATD0_bit;
16
sbit LCD_D5 at LATD1_bit;
17
sbit LCD_D6 at LATD2_bit;
18
sbit LCD_D7 at LATD3_bit;
19
20
sbit LCD_RS_Direction at TRISD4_bit;
21
sbit LCD_EN_Direction at TRISD6_bit;
22
sbit LCD_D4_Direction at TRISD0_bit;
23
sbit LCD_D5_Direction at TRISD1_bit;
24
sbit LCD_D6_Direction at TRISD2_bit;
25
sbit LCD_D7_Direction at TRISD3_bit;
26
// End Lcd module connections
27
28
//------------------------------------------------------------------------------
29
//                          Startup Configuration
30
//------------------------------------------------------------------------------
31
void configure_mcu(void)
32
{
33
34
  
35
    ANCON0 = 0x00;
36
    ANCON1 = 0x00;                     // No analog inputs
37
38
    TRISA = 0x00;
39
    LATA = 0x00;
40
41
    TRISC = 0;                                                // set PORTC as output
42
    PORTC = 0;                                                // clear PORTC
43
44
    TRISD = 0;
45
    TRISE = 16;                                             // RE4 as input, RE5 as output
46
    WPUB = 0x00;                                            // Disable all PORTB pull-up resistors
47
    TRISF = 0xFF;                                                // set PORTF as input
48
}
49
50
void main() {
51
configure_mcu();
52
Lcd_Init();                        // Initialize Lcd
53
     IDmodule = 1; 
54
     
55
    Lcd_Cmd(_LCD_CLEAR);               // Clear display
56
    Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
57
    Delay_ms(100);
58
59
    sprintf(lcd_line1, "Modul ID = %d", IDmodule);
60
    Lcd_Out(1,1, lcd_line1);
61
    Delay_ms(100);
62
63
    sprintf(lcd_line2, "Length = %d", IDmodule);
64
    Lcd_Out(2,1, lcd_line2);         // Write text
65
    Delay_ms(20);
66
    
67
    sprintf(lcd_line3, "Rx_ID = %d", IDmodule);
68
    Lcd_Out(3,1, lcd_line3);         // Write text
69
70
    sprintf(lcd_line4, "Payload = %d", IDmodule);
71
    Lcd_Out(4,1, lcd_line4);         // Write text
72
}

Auf dem Display erscheint gerade:

Zeile1: [Modul ID = 3585]
Zeile2: [Length =  = 3041]
Zeile3: [Rx_ID = 3329]
Zeile4: [Payload = 3073]

Die Zahlen ändern sich teilweise wenn der uC neu bespielt wird. Aber 
warum sind die überhaupt unterschiedlich obwohl überall der gleiche Wert 
1 stehen müsste?! Übertragen wird das .hex-file mit einem PicKit3.

Kann mir jemand weiter helfen?

VG und Danke!!!

von Peter D. (peda)


Lesenswert?

"%d" erwartet ein int (16bit) als Argument.
1
printf("%d", (int)IDmodule);
Es gibt aber auch oft einen Formatstring für 8Bit-Zahlen, mußt mal in 
Deinem Compilermanual nachschauen.

von Andre P. (stanleyku)


Lesenswert?

EY DANKE!!!! War zu einfach.

von Peter II (Gast)


Lesenswert?

Peter D. schrieb:
> "%d" erwartet ein int (16bit) als Argument.printf("%d", (int)IDmodule);
> Es gibt aber auch oft einen Formatstring für 8Bit-Zahlen, mußt mal in
> Deinem Compilermanual nachschauen.

wird nicht jeder Parameter von einer variablen Argumentenliste 
mindestens auf int erweitert?

von Eric B. (beric)


Lesenswert?

Andre P. schrieb:
> char lcd_line1[20], lcd_line2[20], lcd_line3[20], lcd_line4[20]   ;

1) Pass auf, die sind alle 1 char zu "kurz". Du kannst 20 Zeichen auf 
dem Display darstellen und brauchst dann noch einen NUL ('\0') als 
String-Ende.

2) Warum das nicht als 'char lcd_lines[4][21]' declarieren?

von Andre P. (stanleyku)


Lesenswert?

Aus Mangel an Erfahrung... Habs umgesetzt. Vielen Dank!

von Rolf M. (rmagnus)


Lesenswert?

Peter D. schrieb:
> "%d" erwartet ein int (16bit) als Argument.printf("%d", (int)IDmodule);
> Es gibt aber auch oft einen Formatstring für 8Bit-Zahlen, mußt mal in
> Deinem Compilermanual nachschauen.

Hmm, dann ist dieses printf (bzw. der Compiler) aber nicht 
standardkonform, denn eigentlich müsste bei varargs alles, was kleiner 
als int ist, erst auf int erweitert werden.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.