본문 바로가기

PIC/XC8

[XC8]18F452 UART 시리얼 통신

18F452에서 XC8 컴파일러를 사용하여 UART를 구현했습니다.

통신 프로그램에서 받은 값을 그대로 다시 출력하는 프로그램입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* 
 * File:   main.c
 * Author: JangSeongJin
 *
 * Created on 2014년 1월 2일 (목), 오후 5:49
 */
 
#include <xc.h> //XC 기본으로 추가
#include <pic18F452.h>
 
#pragma config OSCS = OFF
#pragma config OSC = HS
#pragma config WDT = OFF
 
typedef unsigned char BYTE;
typedef unsigned int  WORD;
 
 
void Initial_UART(void) {
    TXSTAbits.TXEN = 1; //Transmit Enable bit
    TXSTAbits.BRGH = 1; //High Baud Rate Select bit
    RCSTAbits.SPEN = 1; //Serial Port Enable bit
    RCSTAbits.CREN = 1; //Continuous Receive Enable bit
    SPBRG = 129; //9600 데이터 시트 확
}
 
void Initial_Interrupts(void) {
    RCONbits.IPEN = 1; //Interrupt Priority Enable bit
 
    INTCONbits.GIE = 1; //Global Interrupt Enable bit
    INTCONbits.PEIE = 1; //Peripheral Interrupt Enable bit
 
    PIE1bits.RCIE = 1; //USART Receive Interrupt Enable
    IPR1bits.RCIP = 1; //USART Receive Interrupt Priority bit
}
 
void Initial_PORT(void) {
    TRISD = 0x00; //PORTD 전부 OUTPUT으로 설정
    PORTD = 0x00; //PORTD 전부 0 출력
}
 
void putch(BYTE data) {
    while(!TXIF) continue;
    TXREG = data;
}
 
BYTE getch(void) {
    while(!RCIF) continue;
    
    return RCREG; 
}
 
void interrupt isr(void) {
    if(RCIE && RCIF)
        putch(getch());
}
 
 
void main(void) {
 
    Initial_PORT();
    Initial_UART();
    Initial_Interrupts();
 
    while (1) {
   }
}

 

'PIC > XC8' 카테고리의 다른 글

[XC8]18F452에서 IR 리모콘 구현  (0) 2014.01.03
[XC8]18F452에서 PWM 사용하기  (0) 2014.01.03
[XC8]18F452 PORT Control  (0) 2014.01.02