102 lines
2.3 KiB
Plaintext
102 lines
2.3 KiB
Plaintext
' ########### start ###########
|
|
' pic:PIC16F886
|
|
' ########### end ###########
|
|
|
|
; FILE: Hardware Serial Serial Buffer - 16F886.gcb
|
|
; DATE: 31.01.2015
|
|
; VERSION: 1.0a
|
|
; AUTHOR: Evan R. Venn
|
|
;
|
|
; Description.
|
|
' This example code implements a serial buffer ring.
|
|
' This means the incoming serial port data is placed into a buffer that can be read at anytime.
|
|
' This example uses an interrupt and a buffer.
|
|
|
|
;
|
|
;
|
|
|
|
; This file is part of the Great Cow Basic compiler.
|
|
;
|
|
; This demonstration code 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 General Public License for more details.
|
|
;
|
|
; ----- Configuration
|
|
|
|
#chip 16F886, 16
|
|
|
|
; ----- Define Hardware settings
|
|
' Pot port
|
|
DIR PORTA.0 IN
|
|
|
|
; ----- Constants
|
|
' Define the USART port
|
|
#define USART_BAUD_RATE 9600
|
|
#define USART_BLOCKING true
|
|
' [[change to your config] Ensure these port addresses are correct
|
|
#define SerInPort PORTc.7
|
|
#define SerOutPort PORTc.6
|
|
'Set pin directions
|
|
Dir SerOutPort Out
|
|
Dir SerInPort In
|
|
|
|
; Constants etc required for Buffer Ring
|
|
#define BUFFER_SIZE 8
|
|
#define bkbhit (next_in <> next_out)
|
|
; Variables for Buffer Ring
|
|
Dim buffer(BUFFER_SIZE)
|
|
Dim next_in as byte: next_in = 1
|
|
Dim next_out as byte: next_out = 1
|
|
|
|
|
|
; ----- Main body of program commences here.
|
|
'Interrupt Handlers
|
|
On Interrupt UsartRX1Ready Call readUSART
|
|
|
|
HSerPrint "Buffer Ring"
|
|
HSerPrintCRLF 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Do
|
|
|
|
' wait for incoming data
|
|
do while bkbhit
|
|
|
|
' send back the last data byte received!!
|
|
HSerSend chr(bgetc)
|
|
|
|
Loop
|
|
|
|
Loop
|
|
|
|
; ----- Support methods. Subroutines and Functions
|
|
|
|
Sub readUSART
|
|
|
|
buffer(next_in) = HSerReceive
|
|
temppnt = next_in
|
|
next_in = ( next_in + 1 ) % BUFFER_SIZE
|
|
if ( next_in = next_out ) then ' buffer is full!!
|
|
next_in = temppnt
|
|
end if
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
function bgetc
|
|
wait while !(bkbhit)
|
|
bgetc = buffer(next_out)
|
|
next_out=(next_out+1) % BUFFER_SIZE
|
|
end Function
|
|
|
|
|