Mikroelektronika MIKROE-350 Datenbogen

Seite von 526
Library Example
This example demonstrates simple data exchange via software UART. If MCU is
connected to the PC, you can test the example from the mikroBasic PRO for AVR
USART Terminal Tool.
program Soft_UART
' Soft UART connections
dim Soft_UART_Rx_Pin as sbit at PIND.B0
Soft_UART_Tx_Pin 
as sbit at PORTD.B1
Soft_UART_Rx_Pin_Direction 
as sbit at DDRD.B0
Soft_UART_Tx_Pin_Direction 
as sbit at DDRD.B1
' End Soft UART connections
dim error_, counter, byte_read as byte   ' Auxiliary variables
main:
DDRB = 0xFF       ' Set PORTB as output (error signalization)
PORTB = 0                              ' No error
error_ = Soft_UART_Init(9600, 0) ' Initialize Soft UART at 9600 bps
if (error_ > 0) then
PORTB = error_                       ' Signalize Init error
while TRUE
nop                              ' Stop program
wend
end if
Delay_ms(100)
for counter = "z" to "A" step -1  ' Send bytes from 'z' downto 'A'
Soft_UART_Write(counter)
Delay_ms(100)
next counter
while TRUE                             ' Endless loop
byte_read = Soft_UART_Read(error_) ' Read byte, then test error flag
if (error_ <> 0) then
' If error was detected
PORTB = error_                     '   signal it on PORTB
else
Soft_UART_Write(byte_read)         ' If error was not detect-
ed, return byte read
end if
wend
end.
356
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroBasic PRO for AVR
CHAPTER 6