summaryrefslogtreecommitdiffstats
path: root/src/rs232.c
blob: ab8991f431122b3e49e30bc18df30cae4c0e1698 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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;
    // enable continuous reception
    RCSTA1bits.CREN = 1;
    TXSTA1bits.TXEN = 1;
}

void eusart2_init(void)
{
    // set Async and 8 bits frame
    TXSTA2bits.SYNC = 0;
    TXSTA2bits.TX9 = 0;
    
    // baud prescaler
    RCSTA2bits.SPEN = 1;
    SPBRG2 = 31;			// 31250 bps
    //SPBRG2 = 103;			// 9600 bps
    SPBRGH2 = 0;
    TXSTA2bits.BRGH = 0;
    BAUDCON2bits.BRG16 = 0;
    
    // set up TX / RX pins
    TRISDbits.TRISD7 = 1;
    TRISDbits.TRISD6 = 1;
    // enable continuous reception
    RCSTA2bits.CREN = 1;
    TXSTA2bits.TXEN = 1;
}

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

void eusart2_putch(char c)
{
    while (!TX2IF);
    TX2REG = c;
}

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

char eusart1_getche(void)
{
    char c = eusart1_getch();
    eusart1_putch(c); // echo
    
    return c;
}