Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - ph_sarawut

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

ผมเองก็มือใหม่ไม่นานนี้เอง ชอบ PIC มีให้เลือกเอาไปใช้ให้เหมาะกับงานเยอะดีครับ
#10
        สำหรับมือใหม่ที่ยังไม่ทราบ พอดีไปเจอมาครับ ตามลิ้งค์อ้างอิงด้านล่าง โดยเวลาเราจะสั่ง 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);
}


ผลรันจะได้

#11
เข้าไปกดในลิ้งค์นี้ครับ http://filepi.com/i/6hAGVtX

#12
Quote from: mcusysdev on June 08, 2015, 03:12:21 PM
จัดไปนะครับ ผมแนบเป็น text นะครับ

ขอบคุณครับ
#13
ขออีกครั้งได้ไหมครับ
ตัวโปรแกรมโหลดได้ แต่ใช้พาสเวิร์ดที่ให้มาคลายซิปไม่ได้
ส่วนแครกไปที่ภาษาอาหรับ อ่านไม่ออกเลยครับ ฮ่าๆๆ
#14
C Programming for Arduino 

เล่มนี้มีให้โหลดยังครับ พอดีไปเจอมาครับ กดที่ลิ้งค์มันจะโหลดมาเลยครับ



http://www.lakos.fs.uni-lj.si/images/Predmeti/MK/2014/C%20Programming%20for%20Arduino.pdf
#15
Quote from: p_chusak on December 09, 2014, 01:49:44 PM
     การเขียน mcu ขนาดเล็ก หากไม่สามารถตั้งค่า reg เองได้ง่าย ผมว่ามันไม่ถึงแก่น และจำกัดการใช้งานมากเกินไป เหตุนี้เอง ผมจึงไม่ค่อยหลงไหลใน arduino สักเท่าไร     เพราะว่า compiler และ DOC ของค่าย Microchip เน้น อธิบาย register ของ ตัวเอง และยังมี header file ที่ทำให้เราสามารถเข้าถึงระดับ bit ของ register โดยง่าย โดยเฉพาะ XC ชื่อ register ใน header file กับใน datasheet ตรงกันเกือบ 100%

ผมไม่ชอบ arduino เหมื่อนกันครับ แต่ตรง IDE ของมัน

Quote from: pa_ul on December 09, 2014, 10:09:08 PM
ไอซีสองเบอร์นี้มีการทำงานเกี่ยวกับ Interrupt On Change ที่ไม่เหมือนกัน

จริงด้วยครับ ผมว่ามันคงทำงานต่างกัน
#16
Quote from: p_chusak on December 09, 2014, 08:25:13 AM
   ถ้าใช้ xc8 ต้อง clear flag เองครับ ccs ก็คงเป็นแบบนั้น แต่ บางครั้งมัน CCS ก็ให้ความสดวกเราโดยมันเคลียรืให้
   ผมจึงเลิกใช้ CCS ไปเลยเพราะมันไม่แน่นอน

 

7.4 Clearing Interrupt Flags
The individual status flags, (IOCBFx bits), can be
cleared by resetting them to zero. If another edge is
detected during this clearing operation, the associated
status flag will be set at the end of the sequence,
regardless of the value actually being written.
In order to ensure that no detected edge is lost while
clearing flags, only AND operations masking out known
changed bits should be performed. The following
sequence is an example of what should be performed.
ตามที่ท่าน p_chusak บอก
บางเบอร์มัน clear flag ให้ แต่บางเบอร์มันคงลื่ม clear flag ให้ด้วย
ไม่แน่นอนอย่างนี้ เขียนไปไม่รู้ว่าบักจากเราหรือคอมไพเลอร์ ฮ่าๆๆ  ;D ;D ;D ;D