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 pic18f4550 not working

Status
Not open for further replies.

arafatronics

Newbie level 3
Joined
Apr 10, 2013
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,323
Hi
I'm Programming 2 pic18f4550 -Master and slave- for the RS485 communication using MAX485
when i tried it in protus it worked very nice but in hardware there is nothing work at all
the slave code is :

char dat[9]; // buffer for receving/sending messages
char i,j;

sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction

// Interrupt routine
void interrupt() {
RS485Slave_Receive(dat);
}

void main() {
ADCON1 |= 0x0F; // Configure all ports with analog function as digital
CMCON |= 7; // Disable comparators

PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISC6_bit = 0;
TRISC7_bit = 1;
TRISC1_bit = 0;
TRISD = 0;


UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize 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

RCIE_bit = 1; // enable interrupt on UART1 receive
TXIE_bit = 0; // disable interrupt on UART1 transmit
PEIE_bit = 1; // enable peripheral interrupts
GIE_bit = 1; // enable all interrupts

while (1) {
if(dat[0] == 0x01){
RD3_bit ^= 1;
Delay_ms(1000);
dat[0] = 0;
}
if(dat[0] == 0x02){
RD2_bit ^= 1;
Delay_ms(1000);
dat[0] = 0;
}
if(!RC1_bit){
Delay_ms(50);
if(!RC1_bit){
RD3_bit ^= 1;
}
}


}
}

I suppose that the problem is in the slave code
i tried to connect the A and B to VCC and GND by resistors but no action happens
could please help me
thanks a lot
 

Can you give the function definition too:

UART1_Init(9600); // initialize UART1 module
RS485Slave_Init(160); // Intialize MCU as slave, address 160
RS485Slave_Receive(dat);

Also try to make TRISC6_bit = 1;
 

I had declared the problem

the data is sent and received but the data received is not the desired data sent actually
then the receiver cannot recognize it

so how to overcome that problem?
thanks a lot
 

Normally max 485 in receive mode DE/RE = 0;
When you want to transmit the data you will make max485 into transmit mode know. that is DE/RE = 1;
After that put a delay of about 15ms then you start transmission.
After finishing the data transmission before changing the mode back to receive mode DE/RE = 0. put a delay of 15ms. then make DE/RE = 0.

It may help;
 

@murugesh_89

Can you tell me why 15ms delay is needed while switching between tx and rx?
 

See if you have a code like this...
TXREG = VALUE;
DE/RE = 0;

Here you are immediately switching to receive mode after written into TXREG register. So before the data is completely shift out from TSR register you changing back to receive mode and hence the receiver device may receive wrong data. Even some times the receiver device gets no data as it may not receive stop bit.

So i mentioned to put delay before changing the mode. But the value of 15ms is depends on baud rate. How larger the baud you can reduce this delay. I just told this value for baud of even 1200.

There is another way to do this also. You may also poll the TSR empty pin and change the rs485 mode once the register is empty. Since no interrupt logic available for this bit i simply used delay.
 

checking the BF flag will do this ..
if Transmitting after loading val to TXREG .. while(!BF) ; if recieving after eneabling RCEN .. while(BF); .
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top