summaryrefslogtreecommitdiffstats
path: root/src/rs232.c
blob: e06075911dc73d0aba1f372effff36f5389db096 (plain)
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
#include "rs232.h"
#include <xc.h>

void eusart1_init(void)
{
    // set Async and 8 bits frame
    TXSTA1bits.SYNC = 0;
    TXSTA1bits.TX9 = 0;
    
    // baud prescaler
    RCSTA1bits.SPEN = 1;
    SPBRG1 = 31;
    SPBRGH1 = 0;
    TXSTA1bits.BRGH = 0;
    BAUDCON1bits.BRG16 = 0;
    
    // set up TX / RX pins
    TRISCbits.TRISC7 = 1;
    TRISCbits.TRISC6 = 1;
    RCSTA1bits.CREN = 1; // enable continuous reception
    TXSTA1bits.TXEN = 1;
}

void eusart2_init(void)
{
    // TODO
}

void putch(char c)
{
    while (!TX1IF);
    TX1REG = c;
}

char getch(void)
{
    while (!RC1IF);
    return RC1REG;
}

char getche(void)
{
    char c = getch();
    putch(c); // echo
    
    return c;
}