Mikroelektronika MIKROE-742 데이터 시트

다운로드
페이지 532
Library Example
This is a simple demonstration of RS485 Library routines usage.
Master sends message to Slave with address 160 and waits for a response. The
Slave accepts data, increments it and sends it back to the Master. Master then does
the same and sends incremented data back to Slave, etc.
Master displays received data on PORTB, while error on receive (0xAA) and number of
consecutive unsuccessful retries are displayed on PORTC. Slave displays received data
on PORTB, while error on receive (0xAA) is displayed on PORTC. Hardware configura-
tions in this example are made for the EasyAVR5A board and ATmega16.
RS485 Master code:
program RS485_Master_Example;
uses __Lib_RS485;
var dat : array[10] of byte ; // buffer for receving/sending messages
i, j : byte;
cnt : longint;
var rs485_rxtx_pin  : sbit at PORTD.B2;        // set transcieve pin
rs485_rxtx_pin_direction : 
sbit at DDRD.B2;    // set transcieve
pin direction
// Interrupt routine
procedure interrupt(); org 0x16;
begin
RS485Master_Receive(dat);
end;
begin
cnt := 0;
PORTA  := 0;                          // clear PORTA
PORTB  := 0;                          // clear PORTB
PORTC  := 0;                          // clear PORTC
DDRA   := 0xFF;                       // set PORTA as output
DDRB   := 0xFF;                       // set PORTB as output
DDRC   := 0xFF;                       // set PORTB as output
// Pass pointers to UART functions of used UART module
UART_Wr_Ptr:= @UART1_Write;
UART_Rd_Ptr := @UART1_Read;
UART_Rdy_Ptr := @UART1_Data_Ready;
345
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroPASCAL PRO for AVR
CHAPTER 6