Microchip Technology DV164139 データシート

ページ / 54
© 2009 Microchip Technology Inc.
DS41356B-page 35
EXAMPLE 2-10: 
INITIALIZEUSART() CODE
The reader is encouraged to review the data sheet for the PIC18F14K50 EUSART 
section for more information on the specifics of the configuration code.
Next, the application specific code will be added that will implement the flowchart 
shown in Figure 2-6.
10. Scroll down to the ProcessIO()and copy and paste the contents of 
Example 2-11 into the section labeled:
/*****************************************************
ADD CODE TO CHECK IF RS232 HAS
BEEN SENT ALONG USB
AND THE CODE TO CHECK IF 
ANY NEW RS232 TRANSMISSION 
HAS BEEN RECEIVED AND STORED
******************************************************/
As per the flowchart in Figure 2-6, this code will ensure that the buffer containing the 
data transmitted from the RS-232 has been sent to the USB firmware. If so, the code 
then checks for any new RS-232 data that has been stored in the buffer.
    
#if defined(__18CXX)
unsigned char c;
        #if defined(__18F14K50)
ANSELHbits.ANS11 = 0;
// Make RB5 digital so USART can
//use pin for Rx
        #endif
        UART_TRISRx=1;
// RX
        UART_TRISTx=0;
// TX
        TXSTA = 0x24;       
// TX enable BRGH=1
        RCSTA = 0x90;       
// Single Character RX
        SPBRG = 0x70;
        SPBRGH = 0x02;      
// 0x0271 for 48MHz -> 19200 baud
        BAUDCON = 0x08;     
// BRG16 = 1
        c = RCREG;
// read 
    #endif