Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RS485 : PC to PIC16F877A master and slave communication

Status
Not open for further replies.

sohailimdad

Newbie level 2
Joined
Mar 18, 2010
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Pakistan
Activity points
1,288
Project : controlling 8 PIC16F877A through PC via RS485 sending and receiving through PC Rs232.
Problem:
On RS485 data transfer between master and slave controllers are perfect but now i want to send data from terminal via RS232 comport to master and try to pass on this code to slaves one, and result send back on to master then to terminal(PC), but still not successful. please check this code and help me to resolve this.
issue: no data received from terminal on master or any slave via RS232.
Code:
program RS485_Master
‘Microcontroller : PIC16F877A , 8.00Mhz
'board Easy PIC6
'mikrobasic 
'connected to slave via 485, and to PC via comport Rs232 checked.
'Output
symbol motor = PORTA.0
symbol light = PORTA.1
'input
symbol Sen_DN = PORTD.0
symbol Sen_UP = PORTD.1
symbol Hit_Sen = PORTD.2
'Status output
symbol MSG_Ok = PORTB.0
Symbol MSG_Fault = PORTB.1
'---------------------------declarations
dim uart_rd as byte
dim inputvalue as byte
dim uart_rw as byte
dim var_light as byte
dim uptime as byte
dim Dntime as byte
dim Hit as byte
dim dat as byte[20] ' buffer for receving/sending messages
i, j as byte
cnt as longint

dim rs485_rxtx_pin as sbit at RC2_bit ' set transceive pin
dim rs485_rxtx_pin_direction as sbit at TRISC2_bit ' set transceive pin direction

' Interrupt routine
sub procedure interrupt()
RS485Master_Receive(dat)
end sub

main:
cnt = 0
main:
'ANSEL = 0 ' Configure AN pins as digital I/O
'ANSELH = 0
'C1ON_bit = 0 ' Disable comparators
'C2ON_bit = 0
'-----------------------------------------------------
PORTA = 0 'output for motor, Light,
PORTB = 0 'output for status Display
PORTD = 0 'input sensors
'--------------------------------------------------------
TRISA = 0x00 'initialize for output
TRISB = 0x00 'initialize for output
TRISD = 0xFF 'initialize for input
'--------------------------------------------------------
UART1_Init(9600) ' initialize UART1 module
Delay_ms(100)
RS485Master_Init() ' initialize MCU as Master
'------------------------------------------------------------
dat[0] = 0
dat[1] = 0
dat[2] = 0
dat[4] = 0 ' ensure that message received flag is 0
dat[5] = 0 ' ensure that message received flag is 0
dat[6] = 0 ' ensure that error flag is 0

'RS485Master_Send(dat,1,160)

PIE1.RCIE = 1 ' enable interrupt on UART1 receive
PIE1.TXIE = 1 ' disable interrupt on UART1 transmit
INTCON.PEIE = 1 ' enable peripheral interrupts
INTCON.GIE = 1 ' enable all interrupts

while TRUE
if (UART1_Data_Ready() <> 0) then ' If data is received,
uart_rd = UART1_Read() ' read the received data,
inputvalue = uart_rd
end if
select case inputvalue 'check value of input in from comport
case 0 'when receive 0 character
motor = 1
delay_ms(1000)
motor = 0
dat[0] = 0
RS485Master_Send(dat,1,160)
case 1
light = 1
delay_ms(1000)
light = 0
dat[0] = 1
RS485Master_Send(dat,1,160)
end select

Inc(cnt)
'-----------------Error detected --------------------
if (dat[5] <> 0) then ' if an error detected, signal it by
msg_fault = 1 ' display fault on PORTB.1
delay_ms(1000)
msg_fault = 0
end if
'-----------------Message completed--------------
if (dat[4] <> 0) then ' upon completed valid message receive
dat[4] = 0 ' data[4] is set to 0xFF
msg_ok = 1 ' display fault on PORTB
delay_ms(1000)
msg_ok = 0

end if
wend
end.
____________________________________________________________
Slave program 
' MCU: PIC16F877A

' Oscillator: HS, 8.0000 MHz
' Ext. Modules: RS485 on PORTC
' SW: mikroBasic PRO for PIC
'salve connected via 485 on port C
' *

program RS485_Slave
'Output
symbol motor = PORTA.0
symbol light = PORTA.1
'input
symbol Sen_DN = PORTD.0
symbol Sen_UP = PORTD.1
symbol Hit_Sen = PORTD.2
'Status output
symbol MSG_Ok = PORTB.0
Symbol MSG_Fault = PORTB.1

dim dat as byte[20] ' buffer for receving/sending messages
i, j as byte

dim uart_rd as byte
dim inputvalue as byte
dim uart_rw as byte
dim var_light as byte
dim uptime as byte
dim Dntime as byte
dim Hit as byte

dim rs485_rxtx_pin as sbit at RC2_bit ' set transcieve pin
rs485_rxtx_pin_direction as sbit at TRISC2_bit ' set transcieve pin direction

' Interrupt routine
sub procedure interrupt()
RS485Slave_Receive(dat)
end sub

main:
'ANSEL = 0 ' Configure AN pins as digital I/O
'ANSELH = 0
'C1ON_bit = 0 ' Disable comparators
'C2ON_bit = 0
'-----------------------------------------------------
PORTA = 0 'output for motor, Light,
PORTB = 0 'output for status Display
PORTD = 0 'input sensors
'--------------------------------------------------------
TRISA = 0x00 'initialize for output
TRISB = 0x00 'initialize for output
TRISD = 0xFF 'initialize for input
'--------------------------------------------------------
UART1_Init(9600) ' initialize UART1 module
Delay_ms(100)
RS485Slave_Init(160) ' Initialize MCU as slave, address 160
'------------------------------------------------------------
dat[4] = 0 ' ensure that message received flag is 0
dat[5] = 0 ' ensure that message received flag is 0
dat[6] = 0 ' ensure that error flag is 0
'-----------------------------------------------------------
PIE1.RCIE = 1 ' enable interrupt on UART1 receive
PIE1.TXIE = 0 ' disable interrupt on UART1 transmit
INTCON.PEIE = 1 ' enable peripheral interrupts
INTCON.GIE = 1 ' enable all interrupts
'--------------------------------------------------------------
while TRUE
'-----------------error detected --------------------
if (dat[5] <> 0) then ' if an error detected, signal it by
'light = 1 ' display fault on PORTB.1
'delay_ms(1000)
'light = 0
end if
'-----------------message completed--------------
if (dat[4] <> 0) then ' upon completed valid message receive
dat[4] = 0 ' data[4] is set to 0xFF
portA.2 = 1 ' display fault on PORTB
delay_ms(1000)
portA.2 = 0
inputvalue = dat[0]
select case inputvalue 'check value of input in from comport
case 0 'when receive 0 character

motor = 1
delay_ms(1000)
motor = 0
case 1
light = 1
delay_ms(1000)
light = 0
end select
end if
wend
end.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top