Sie sind auf Seite 1von 5

UART

Contoh:

$regfile = "m88def.dat"
$crystal = 8000000
$baud = 19200
Dim Akey As Byte 'Here we declare a byte variable
Print
Print "Hello, hit any alphanumerical key..."
Akey = Waitkey() 'Waitket waits untill a char is received from
the UART
Print Akey
Wait 1
Print
Print "Thanks!, as you could see the controller prints a number"
Print "but not the key you pressed."
Wait 1
Print
Print "Now try the enter key..."
Akey = Waitkey()
Akey = Waitkey()
Print Akey
Print
Print "The number you see is the ASCII value of the key you pressed."
Print "We need to convert the number back to the key..."
Print 'Notice what this line does
Print "Please try an alphanumerical key again..."
Akey = Waitkey()
Print Chr(akey) 'Notice what this does
Print "That's fine!"
Wait 1
Print
Print "For a lot of functions, just one key is not enough..."
Print "Now type your name and hit enter to confirm"

Dim Inputstring As String * 12 'Declare a string variable here


Do
Akey = Waitkey()
If Akey = 13 Then Goto Thanks 'On enter key goto thanks
Inputstring = Inputstring + Chr(akey) 'Assign the string
Loop

Thanks:
Print "Thank you " ; Inputstring ; " !" 'Notice what ; does
Wait 1
Print
Print "Take a look at the program code and try to understand"
Print "how this program works. Also press F1 at the statements"
Print
Print "If you understand everything continue to the next experiment"
End
BUFFERING SERIAL DATA
Untuk mengirim atau menerima data pada kecepatan tinggi diperlukan buffer untuk
serial input atau serial output. Fasilitas ini tersedia pada BASCOM-AVR dan hanya
dapat digunakan untuk hardware UART.

Config Serialout = Buffered , Size = 20


Atau
Config Serialin = Buffered , Size = 20

Contoh:
--------------------------------------------------------------
' RS232BUFFER.BAS
' (c) 2000-2003, MCS Electronics
' This example shows the difference between normal and buffered
' serial INPUT
'--------------------------------------------------------------
$crystal = 4000000
$baud = 9600

'first compile and run this program with the line below remarked
Config Serialin = Buffered , Size = 20

'dim a variable
Dim Name As String * 10

'the enabling of interrupts is not needed for the normal serial mode
'So the line below must be remarked to for the first test
Enable Interrupts

Print "Start"
Do
'get a char from the UART
Name = Inkey()

If Err = 0 Then 'was there a char?


Print Name 'print it
End If

Wait 1 'wait 1 second


Loop

'You will see that when you slowly enter characters in the terminal emulator
'they will be received/displayed.
'When you enter them fast you will see that you loose some chars
'NOW remove the remarks from line 11 and 18
'and compile and program and run again
'This time the chars are received by an interrupt routine and are
'stored in a buffer. This way you will not loose characters providing that
'you empty the buffer
'So when you fast type abcdefg, they will be printed after each other with the
'1 second delay

'Using the CONFIG SERIAL=BUFFERED, SIZE = 10 for example will


'use some SRAM memory
'The following internal variables will be generated :
'_Rs_head_ptr0 BYTE , a pointer to the location of the start of the buffer
'_Rs_tail_ptr0 BYTE , a pointer to the location of tail of the buffer
'_RS232INBUF0 BYTE ARRAY , the actual buffer with the size of SIZE

SOFTWARE UART
Dapat digunakan jika AVR tidak memiliki fasilitas UART Hardware.

Contoh:
$regfile = "m88def.dat"
$crystal = 8000000
$baud = 19200
Dim B As Byte
Waitms 100

'Open a TRANSMIT channel for output


Open "comc.1:19200,8,n,1" For Output As #1
Print #1 , "serial output"

'Now open a RECEIVE channel for input


Open "comc.2:19200,8,n,1" For Input As #2

'Since there is no relation between the input and output pin


'there is NO ECHO while keys are typed

Print #1 , "Press any alpha numerical key"


'With INKEY() we can check if there is data available
'To use it with the software UART you must provide the channel
Do
'Store in byte
B = Inkey(#2)
'When the value > 0 we got something
If B > 0 Then
Print #1 , Chr(B) 'Print the character
End If
Loop

Close #2 'Close the channels


Close #1
End

Alternatif:
'--------------------------------------------------------------------
' SERINT.BAS
' (c) 1999-2003 MCS Electronics
' serial interrupt example for AVR
' also look at CONFIG SERIALIN for buffered input routines
'--------------------------------------------------------------------
$regfile = "8535def.dat"
Const Cmaxchar = 20 'number of characters

Dim B As Bit 'a flag for signalling a received character


Dim Bc As Byte 'byte counter
Dim Buf As String * Cmaxchar 'serial buffer
Dim D As Byte

'Buf = Space(20)
'unremark line above for the MID() function in the ISR
'we need to fill the buffer with spaces otherwise it will contain garbage

Print "Start"

On Urxc Rec_isr 'define serial receive ISR


Enable Urxc 'enable receive isr

Enable Interrupts 'enable interrupts to occur

Do
If B = 1 Then 'we received something
Disable Serial Disables URXC, UDRE and UTXC
Print Buf 'print buffer
Print Bc 'print character counter

'now check for buffer full


If Bc = Cmaxchar Then 'buffer full
Buf = "" 'clear
Bc = 0 'rest character counter
End If

Reset B 'reset receive flag


Enable Serial
End If
Loop

Rec_isr:
Print "*"
If Bc < Cmaxchar Then 'does it fit into the buffer?
Incr Bc 'increase buffer counter

If Udr = 13 Then 'return?


Buf = Buf + Chr(0)
Bc = Cmaxchar
Else
Buf = Buf + Chr(udr) 'add to buffer
End If

' Mid(buf , Bc , 1) = Udr


'unremark line above and remark the line with Chr() to place
'the character into a certain position
'B = 1 'set flag
End If
B=1 'set flag
Return