+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Junior Member level 3
    Points: 2,076, Level: 10

    Join Date
    Nov 2001
    Posts
    26
    Helped
    0 / 0
    Points
    2,076
    Level
    10

    How to use printf with Hi-Tech PIC C and 16F877?

    I am using Keil C to program 8051. I can use printf to send data to serial I/O. Can i use it with HI-TECH PIC C and 16F877 ? And how ?

    Thanks

    •   Alt19th January 2003, 21:05

      advertising

        
       

  2. #2
    Member level 3
    Points: 2,732, Level: 12

    Join Date
    Feb 2002
    Posts
    54
    Helped
    2 / 2
    Points
    2,732
    Level
    12
    Yes ,You can but you must define primitive printf function "putch" or "putchar".



    •   Alt19th January 2003, 22:24

      advertising

        
       

  3. #3
    Member level 5
    Points: 3,237, Level: 13

    Join Date
    Jun 2001
    Posts
    80
    Helped
    1 / 1
    Points
    3,237
    Level
    13
    Or simply use a sprintf function and copy the string to UART.
    Actually, this way can be better, because you only have one function for "print" a string to some device and you can choose (with one additional parameter) where will go the output (UART, LCD, SPI, I2C etc.)


    1 members found this post helpful.

    •   Alt20th January 2003, 14:28

      advertising

        
       

  4. #4
    Full Member level 5
    Points: 3,376, Level: 13

    Join Date
    May 2001
    Posts
    251
    Helped
    14 / 14
    Points
    3,376
    Level
    13

    hi ...

    hi .. i tried this with avr .. i found that on elektroda ;0)

    int putchar(int c)
    {
    if (c == '\n')
    putchar('\r');
    while(!(USR&32)); //wait while tx buffer empty
    UDR = (char)c; //send char to uart register
    return c;
    }

    after you overload original putchar with this all printf features would work.

    regards

    cancel



  5. #5
    Full Member level 2
    Points: 2,628, Level: 11
    Gomez's Avatar
    Join Date
    Feb 2002
    Posts
    125
    Helped
    10 / 10
    Points
    2,628
    Level
    11
    Quote Originally Posted by CADDevil
    Or simply use a sprintf function and copy the string to UART.
    Actually, this way can be better, because you only have one function for "print" a string to some device and you can choose (with one additional parameter) where will go the output (UART, LCD, SPI, I2C etc.)
    And printf need the new() function.
    Better is sprintf with a static array and is possible a "const" format string (into flash).

    Gomez



  6. #6
    Advanced Member level 5
    Points: 18,113, Level: 32

    Join Date
    Jul 2001
    Location
    Oberon
    Posts
    1,888
    Helped
    420 / 420
    Points
    18,113
    Level
    32
    use this code

    #include <pic.h>
    #include <stdio.h>
    #include <string.h>

    //------ init uart

    void InitUART(void)
    {
    SPBRG = 12; /* 19200bPS @ 4MHz */
    BRGH = 1; /* High Speed Mode */
    TXEN = 1; /* Enable Transmit */
    SPEN = 1; /* Enable Serial Port */
    }

    //------ write char to uart

    void putch(unsigned char byte)
    {
    while(!TRMT); /* Wait for TX Buffer Empty */
    TXREG = byte;
    }

    and then just use normal printf.



+ Post New Thread
Please login