-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial.c
104 lines (85 loc) · 3.16 KB
/
serial.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
/* ===========================================================================
serial.c - serial com port I/O routines for Axiom CML12SDP256 board
Version: 1.0
Author: Dusty Nidey, Axiom Manufacturing (www.axman.com)
Compiler: GNU for 68HC11 & 68HC12 (www.gnu-m68hc11.org)
This is freeware - use as you like
==============================================================================
------------------------------------------------------------------------------
*/
#include "ports_d256.h"
#include "serial.h"
// RAM Variables
// -------------
unsigned char ioport; // 0 = use SC0 for standard i/o. 1 = use SC1
//---------------------------------------------------------------------------
// Initialize serial port
void InitSerial(unsigned char baudrate){
SC0BDL = baudrate; // set baud rate register
SC0BDH = 0x00;
SC0CR1 = 0; // configure SCI0 control registers
SC0CR2 = 0x0C; // enable transmit and receive
}
/*----------------------------------------------------------------------------
ReadyToSend
-----------
Wait for the serial port Status register to indicate the transmiter is
ready to receive another byte.
INPUT: ioport 0= SCI0, 1= SCI1
OUTPUT: returns 0 when ready. NO TIMEOUT is currently implemented
----------------------------------------------------------------------------*/
char ReadyToSend(char portnum){
unsigned char Status;
do{
if(portnum == 0) Status = SC0SR1;
else Status = SC1SR1;
}while((Status & 0x80) == 0);
return 0;
}
/*----------------------------------------------------------------------------
putchar
-------
Wait for the serial port transmiter to return ready then write a single byte
to the transmit register.
INPUT: SByte Value to write
ioport 0= SCI0, 1= SCI1
OUTPUT: returns 0 when finished. NO TIMEOUT is currently implemented
----------------------------------------------------------------------------*/
char putchar(unsigned char scbyte){
if(ReadyToSend(ioport)) return 1; // wait for ready to send
if(ioport == 0) SC0DRL = scbyte;
else SC1DRL = scbyte;
return 0;
}
/*----------------------------------------------------------------------------
putch
-----
Send a single byte out the serial port.
If this is the '\n' character then 2 bytes are sent which are the carriage
return followed by line feed characters.
INPUT: ch Value to write
ioport 0= SCI0, 1= SCI1
----------------------------------------------------------------------------*/
void putch(unsigned char ch){
if (ch == '\n'){
putchar(CR);
putchar(LF);
}
else{
putchar(ch);
}
}
/*----------------------------------------------------------------------------
puts
----
Send a string of characters to the serial port. The string must end in a
NULL character (0x00).
INPUT: sptr pointer to the string
ioport 0= SCI0, 1= SCI1
----------------------------------------------------------------------------*/
void puts(char *sptr){
while(*sptr){
putch(*sptr);
++sptr;
}
}