Escolar Documentos
Profissional Documentos
Cultura Documentos
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"
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.
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()
'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
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
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
'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"
Do
If B = 1 Then 'we received something
Disable Serial Disables URXC, UDRE and UTXC
Print Buf 'print buffer
Print Bc 'print character counter
Rec_isr:
Print "*"
If Bc < Cmaxchar Then 'does it fit into the buffer?
Incr Bc 'increase buffer counter