PIC-Microcontroller

Elektronik u. Mikrocontroller => Compiler Software => Thema gestartet von: pic18 in 19.04.2025, 14:06:57 CEST

Titel: printf() in C
Beitrag von: pic18 in 19.04.2025, 14:06:57 CEST
Hallo, ich bin gerade dabei die Funktion printf() auf meine LCD-Anzeige oder auch andere Ausgabe-Schnittstelle umzuleiten. Das hat den Vorteil, dass ich float Zahlen formatiert ausgeben kann. Ich habe mir folgenden Code geschrieben, muss aber noch testen, ob dieser funktioniert. Könnte es so gehen, oder habt ihr Verbesserungsvorschläge?
#include "configuration.h"
#include "usb.h"
#include "main.h"
#include <stdio.h>

#if UIP_LOGGING == 1

void uip_log(const rom char *data) {
    printOut( "%S\r\n", data );
}

#endif

#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "net/TcpApps/telnetd.h"
#include "commandParser.h"
#include "hardware.h"
#include "lcd/lcd.h"

typedef void (*put_t)(const uint8_t);
static int _print(put_t put, const rom char *f, va_list ap);

stdout= _H_USER; //damit Aufruf in main.c
int _user_putc(char c){  // für printf
    put_t fct;

#if TELNETD_SUPPORT == 1 && USB_SUPPORT == 1
    fct = (stdOut == source_usb) ? &transmitUSB : &telnetSendChar;
#elif TELNETD_SUPPORT == 1
    fct = &telnetSendChar;
#else
    fct = &transmitUSB;
#endif
    if (stdOut == source_lcd) fct = &writeLCDRomData;//&writeLCDData
    fct( c );   
}

extern source_t stdOut, stdOut_mr; // für print_test

void print_test(void){
    lcd_neu= 0; LCD_ausw();// Anzeige (Hauptmenu) auswählen
    writeLCDInitCmd(LCD_crcr_blink); //zum Testen des blinkens
    writeLCDInitCmd(LCD_CLEARDISPLAY);
    stdOut_mr = stdOut; //vorsichtshalber sichern
    stdOut = source_lcd;    //damit auf LCD Anzeige
    //#define _user_putc(c) writeLCDData(c) in main.h definiert siehe putc.c
    printf("%d\n",123);
    printf("%3.2f\n",3.14);
    printf("%f\n",3.14);
    stdOut = stdOut_mr;
}