Menu

[r4]: / lowlevel / usart.h  Maximize  Restore  History

Download this file

311 lines (267 with data), 7.3 kB

  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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
' USART routines for Great Cow BASIC
' Copyright (C) 2009 - 2010 Hugh Considine
' This library is free software; you can redistribute it and/or
' modify it under the terms of the GNU Lesser General Public
' License as published by the Free Software Foundation; either
' version 2.1 of the License, or (at your option) any later version.
' This library is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
' Lesser General Public License for more details.
' You should have received a copy of the GNU Lesser General Public
' License along with this library; if not, write to the Free Software
' Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
'********************************************************************************
'IMPORTANT:
'THIS FILE IS ESSENTIAL FOR SOME OF THE COMMANDS IN GCBASIC. DO NOT ALTER THIS FILE
'UNLESS YOU KNOW WHAT YOU ARE DOING. CHANGING THIS FILE COULD RENDER SOME GCBASIC
'COMMANDS UNUSABLE!
'********************************************************************************
'Changes:
' 12/12/2009: Corrected TXIF checking
' 8/2/2010: Added delay after each byte in print
'For compatibility with USART routines in Contributors forum, add this line:
'#define USART_BLOCKING
'This will cause send and receive to wait until data can be sent or received
'Note that the HSerReceive routine is implemented differently. In this
'library, it is a subroutine, as with SerReceive in rs232.h. This is purely
'a design decision, and has been made to keep included routines consistent
'with each other.
'To slow down print, set this delay:
#define USART_DELAY 1 ms
'Some wrappers for compatibility with Contributors USART routines
#define HserPrintByte HSerPrint
Sub HserPrintByteCRLF(In PrintValue)
HSerPrint(PrintValue)
HSerPrintCRLF
End Sub
Sub HserPrintCRLF
HSerSend(13)
Wait USART_DELAY
HSerSend(10)
End Sub
'Script to calculate baud rate generator values
'Also sets constants to check if byte received
#script
If PIC Then
USARTHasData = "RCIF = On"
If USART_BAUD_RATE Then
'Find best baud rate
If Bit(BRG16) Then
'Try 16 bit /4 (H = 1, 16 = 1)
SPBRG_TEMP = ChipMHz / USART_BAUD_RATE / 4 * 1000000 - 1
BRGH_TEMP = 1
BRG16_TEMP = 1
If SPBRG_TEMP > 65535 Then
'If too high, try 16 bit /16 (H = 0, 16 = 1)
SPBRG_TEMP = ChipMHz / USART_BAUD_RATE / 16 * 1000000 - 1
BRGH_TEMP = 0
BRG16_TEMP = 1
If SPBRG_TEMP < 256 Then
'If low enough, use 8 bit /16 (H = 1, 16 = 0)
BRGH_TEMP = 1
BRG16_TEMP = 0
End If
'If value is too high, baud rate is too low
If SPBRG_TEMP > 65535 Then
Error Msg(UsartBaudTooLow)
End If
End If
End If
If NoBit(BRG16) Then
'Try /16
SPBRG_TEMP = ChipMHz / USART_BAUD_RATE / 16 * 1000000 - 1
BRGH_TEMP = 1
'If too high, try /64
If SPBRG_TEMP > 255 Then
SPBRG_TEMP = ChipMHz / USART_BAUD_RATE / 64 * 1000000 - 1
BRGH_TEMP = 0
'If still too high, error
If SPBRG_TEMP > 255 Then
Error Msg(UsartBaudTooLow)
End If
End If
End If
'Get high and low bytes
SPBRGL_TEMP = Int(SPBRG_TEMP) And 255
SPBRGH_TEMP = Int(SPBRG_TEMP / 256)
End If
End If
If AVR Then
USARTHasData = "RXC = On"
If USART_BAUD_RATE Then
UBRR_TEMP = ChipMHz * 1000000 / (16 * USART_BAUD_RATE) - 1
U2X0_TEMP = 0
'If using a high speed, use double speed mode
If UBRR_TEMP < 16384 Then
UBRR_TEMP = ChipMHz * 1000000 / (8 * USART_BAUD_RATE) - 1
U2X0_TEMP = 1
End If
'Check that rate will work
If UBRR_TEMP > 65535 Then
Error Msg(UsartBaudTooLow)
End If
'Get high and low bytes
UBRRL_TEMP = Int(UBRR_TEMP) And 255
UBRRH_TEMP = Int(UBRR_TEMP / 256)
End If
End If
#endscript
#startup InitUSART
Sub InitUSART
#ifdef PIC
'Set baud rate
SPBRG = SPBRGL_TEMP
#ifdef Bit(BRG16)
SPBRGH = SPBRGH_TEMP
BRG16 = BRG16_TEMP
#endif
BRGH = BRGH_TEMP
'Enable async mode
Set SYNC Off
Set SPEN On
'Enable TX and RX
Set CREN On
Set TXEN On
#endif
#ifdef AVR
'Set baud rate
U2X = U2X0_TEMP
UBRRL = UBRRL_TEMP
UBRRH = UBRRH_TEMP
'Enable TX and RX
RXEN = On
TXEN = On
#endif
End Sub
sub HSerSend(In SerData)
#ifdef PIC
#ifdef USART_BLOCKING
Wait While TXIF = Off
#endif
TXREG = SerData
#endif
#ifdef AVR
#ifdef USART_BLOCKING
Wait While UDRE = Off
#endif
UDR = SerData
#endif
end sub
Function HSerReceive
HSerReceive(SerData)
HSerReceive = SerData
End Function
Sub HSerReceive(Out SerData)
'If set up to block, wait for data
#ifdef USART_BLOCKING
Wait Until USARTHasData
#endif
#ifdef PIC
'Get a bytes from FIFO
If USARTHasData Then
SerData = RCREG
End if
'Clear error
If OERR Then
Set CREN Off
Set CREN On
End If
#endif
#ifdef AVR
If USARTHasData Then
SerData = UDR
End If
#endif
End Sub
sub HSerPrint (PrintData As String)
'PrintLen = LEN(PrintData$)
PrintLen = PrintData(0)
If PrintLen <> 0 then
'Write Data
for SysPrintTemp = 1 to PrintLen
HSerSend(PrintData(SysPrintTemp))
Wait USART_DELAY
next
End If
'CR
#IFDEF SerPrintCR
HSerSend(13)
Wait USART_DELAY
#ENDIF
#IFDEF SerPrintLF
HSerSend(10)
Wait USART_DELAY
#ENDIF
end sub
sub HSerPrint (In SerPrintVal)
OutValueTemp = 0
IF SerPrintVal >= 100 Then
OutValueTemp = SerPrintVal / 100
SerPrintVal = SysCalcTempX
HSerSend(OutValueTemp + 48)
Wait USART_DELAY
End If
If OutValueTemp > 0 Or SerPrintVal >= 10 Then
OutValueTemp = SerPrintVal / 10
SerPrintVal = SysCalcTempX
HSerSend(OutValueTemp + 48)
Wait USART_DELAY
End If
HSerSend(SerPrintVal + 48)
Wait USART_DELAY
'CR
#IFDEF SerPrintCR
HSerSend(13)
Wait USART_DELAY
#ENDIF
#IFDEF SerPrintLF
HSerSend(10)
Wait USART_DELAY
#ENDIF
end sub
Sub HSerPrint (In SerPrintVal As Word)
Dim SysCalcTempX As Word
OutValueTemp = 0
If SerPrintVal >= 10000 then
OutValueTemp = SerPrintVal / 10000 [word]
SerPrintVal = SysCalcTempX
HSerSend(OutValueTemp + 48)
Wait USART_DELAY
Goto HSerPrintWord1000
End If
If SerPrintVal >= 1000 then
HSerPrintWord1000:
OutValueTemp = SerPrintVal / 1000 [word]
SerPrintVal = SysCalcTempX
HSerSend(OutValueTemp + 48)
Wait USART_DELAY
Goto HSerPrintWord100
End If
If SerPrintVal >= 100 then
HSerPrintWord100:
OutValueTemp = SerPrintVal / 100 [word]
SerPrintVal = SysCalcTempX
HSerSend(OutValueTemp + 48)
Wait USART_DELAY
Goto HSerPrintWord10
End If
If SerPrintVal >= 10 then
HSerPrintWord10:
OutValueTemp = SerPrintVal / 10 [word]
SerPrintVal = SysCalcTempX
HSerSend(OutValueTemp + 48)
Wait USART_DELAY
End If
HSerSend(SerPrintVal + 48)
Wait USART_DELAY
'CR
#IFDEF SerPrintCR
HSerSend(13)
Wait USART_DELAY
#ENDIF
#IFDEF SerPrintLF
HSerSend(10)
Wait USART_DELAY
#ENDIF
End Sub
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.