-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsci.c
114 lines (94 loc) · 1.91 KB
/
sci.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <htc.h>
#include "sci.h"
/* Routines for initialisation and use of the SCI
* for the PIC processor.
*/
/* other options:
* frame errors
*/
unsigned char
sci_Init(unsigned long int baud, unsigned char ninebits)
{
int X;
unsigned long tmp;
/* calculate and set baud rate register */
/* for asynchronous mode */
tmp = 16UL * baud;
X = (int)(FOSC/tmp) - 1;
if((X>255) || (X<0))
{
tmp = 64UL * baud;
X = (int)(FOSC/tmp) - 1;
if((X>255) || (X<0))
{
return 1; /* panic - baud rate unobtainable */
}
else
BRGH = 0; /* low baud rate */
}
else
BRGH = 1; /* high baud rate */
SPBRG = X; /* set the baud rate */
SYNC = 0; /* asynchronous */
SPEN = 1; /* enable serial port pins */
CREN = 1; /* enable reception */
SREN = 0; /* no effect */
TXIE = 0; /* disable tx interrupts */
RCIE = 0; /* disable rx interrupts */
TX9 = ninebits?1:0; /* 8- or 9-bit transmission */
RX9 = ninebits?1:0; /* 8- or 9-bit reception */
TXEN = 1; /* enable the transmitter */
return 0;
}
void
sci_PutByte(unsigned char byte)
{
while(!TXIF) /* set when register is empty */
continue;
TXREG = byte;
return;
}
unsigned char sci_GetByte(void)
{
char timer = 0;
while(timer<76){ //while register is empty try to read char for 1 second, if nothing then return 1
if(T0IF){
timer++;
T0IF = 0;
}
if(RCIF){
return RCREG;
}
}
return 1;
}
unsigned char
sci_CheckOERR(void)
{
char a;
if(OERR) /* re-enable after overrun error */
{
CREN = 0;
a=RCREG;
a=RCREG;
a=RCREG;
CREN = 1;
return 1;
}
return 0;
}
unsigned char
sci_GetFERR(void)
{
char timer = 0;
while(timer<76){ /* while register is empty */
if(T0IF){
timer++;
T0IF = 0;
}
if(RCIF){
return FERR; /* RXD9 and FERR are gone now */
}
}
return 0; /* RCIF is not cleared until RCREG is read */
}