แนะนำมือใหม่กับการใช้ Printf จากหลายจุดบน XC8

Started by ph_sarawut, June 29, 2015, 01:33:10 PM

Previous topic - Next topic

ph_sarawut

        สำหรับมือใหม่ที่ยังไม่ทราบ พอดีไปเจอมาครับ ตามลิ้งค์อ้างอิงด้านล่าง โดยเวลาเราจะสั่ง Print ออกพอร์ต UART หรือ LCD เราจะต้องแอดไลบรารี่ #include <stdio.h> แล้วเราสร้างฟังชั่น void putch(char c) ขึ้นมา (printf ต้องการฟังชั่น putch() มาใช้ โดยประกาศอยู่ใน stdio.h อยู่แล้ว) ทีนี้ถ้าเรามีจุดที่จะ printf หลายจุด เราก็ใช้วิธีหลักการ พอยเตอร์ของฟังชั่นมาช่วย ลองดูตามโค้ดตัวอย่างครับ

ไฟล์ตัวอย่างซิมบน Proteus 8 : http://www.zabzaa.com/upload/download.php?file=456XC8_TestPrintf_18F8722.zip

อ้างอิง : http://www.electro-tech-online.com/threads/is-it-possible-if-so-how-to-use-scanf-printf-on-your-own-input-output-routine.135005/

ปล. : ห้องนี้เงียบจังครับ หรือ หันไปเล่น Arduino กันหมด

#define _XTAL_FREQ 40000000UL

#include <xc.h>
#include "Config.h" // #pragma config
#include <stdio.h>

void delay_ms(unsigned int i)
{
    for(;i;i--)
    {
        __delay_ms(1) ;
    }
}

void uart_init1(unsigned int baudrate){
   
    TXSTA1 = 0;           // Reset USART registers to POR state
    RCSTA1 = 0;

    // BRG_val = ((FOSC/Desired Baud Rate)/64) - 1
    unsigned int BRG_val = ((_XTAL_FREQ/baudrate)/(64))-1;
    SPBRGH1 = BRG_val>>8;
    SPBRG1 = BRG_val;

    TXSTA1bits.BRGH = 0; // 0 = low speed mode ,1= high speed mode
    TXSTA1bits.SYNC = 0 ;
    BAUDCON1bits.BRG16 = 0;
    TRISCbits.TRISC6 = 0;
    TRISCbits.TRISC7 = 1;

    TXSTA1bits.TX9  = 0 ;
    TXSTA1bits.TXEN = 1;
    RCSTA1bits.SPEN = 1;
}

void uart_init2(unsigned int baudrate){
   
    TXSTA2 = 0;           // Reset USART registers to POR state
    RCSTA2 = 0;

    // BRG_val = ((FOSC/Desired Baud Rate)/64) - 1
    unsigned int BRG_val = ((_XTAL_FREQ/baudrate)/(64))-1;
    SPBRGH2 = BRG_val>>8;
    SPBRG2 = BRG_val;

    TXSTA2bits.BRGH = 0; // 0 = low speed mode ,1= high speed mode
    TXSTA2bits.SYNC = 0 ;
    BAUDCON2bits.BRG16 = 0;
    TRISGbits.TRISG1 = 0;
    TRISGbits.TRISG2 = 1;

    TXSTA2bits.TX9  = 0 ;
    TXSTA2bits.TXEN = 1;
    RCSTA2bits.SPEN = 1;
}

void uart_putc1(unsigned char c){
    while(!TXSTA1bits.TRMT);
    TXREG1 = c;
}

void uart_putc2(unsigned char c){
    while(!TXSTA2bits.TRMT);
    TXREG2 = c;
}

void (*putchFunc)(char c) = uart_putc1 ; // UART1 is putch defualt
void putch(char c)
{
    putchFunc(c);   // call whatever function we've set the pointer to point to
}

void main(void)
{
    uart_init1(4800) ;
    uart_init2(9600) ;
   
    printf("Test printf UART1 on PIC18F8722.\r") ; // Print UART1
    delay_ms(500) ;
   
    putchFunc = uart_putc2 ;
    printf("Test printf UART2 on PIC18F8722.\r") ; // Print UART2
    delay_ms(500);
   
    putchFunc = uart_putc1 ;
    printf("Test printf UART1 agian.\r") ; // Print UART1 agian.
    printf("End.") ;
   
    while(1);
}


ผลรันจะได้


fin

ขอบคุณครับ
ปล. ผม PIC ไม่ยอมเปลี่ยนแปลง ส่งงานลูกค้าแต่ PIC แต่ Arduino กำลังมา เดี๋ยวคุยกับวัยรุ่นไม่รู้เรื่อง ต้องรู้ไว้ครับ คิดเหมือนผมไหม :) :) :)

ph_sarawut

Quote from: fin on July 05, 2015, 12:57:14 AM
ขอบคุณครับ
ปล. ผม PIC ไม่ยอมเปลี่ยนแปลง ส่งงานลูกค้าแต่ PIC แต่ Arduino กำลังมา เดี๋ยวคุยกับวัยรุ่นไม่รู้เรื่อง ต้องรู้ไว้ครับ คิดเหมือนผมไหม :) :) :)

ผมเองก็มือใหม่ไม่นานนี้เอง ชอบ PIC มีให้เลือกเอาไปใช้ให้เหมาะกับงานเยอะดีครับ