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 library code does not work properly

Status
Not open for further replies.

mamech

Full Member level 3
Joined
Nov 9, 2010
Messages
176
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
3,135
Hello

I intend to make a master / slave rs485 communication between 2 pic micros. I found a example about how to make this in mikroE compiler help, then I edited few lines in it to suit my application. The problem that the micros do not perform the supposed logic of the code.

Master code:

char dat[10]; // 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() {
RS485Master_Receive(dat);
}

void main(){
long cnt = 0;

PORTB = 0;
PORTD = 0;
PORTE = 0;
TRISB = 0;
TRISD = 0;
TRISE = 0;


UART1_Init(9600); // initialize UART1 module
Delay_ms(100);

RS485Master_Init(); // initialize MCU as Master
dat[0] = 0x02;
dat[1] = 0x0;
dat[2] = 0x0;
dat[4] = 0; // ensure that message received flag is 0
dat[5] = 0; // ensure that error flag is 0
dat[6] = 0;

RS485Master_Send(dat,1,160);


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){
// upon completed valid message receiving
// data[4] is set to 255
cnt++;
if (dat[5]) { // if an error detected, signal it
PORTD = 0xff; // by setting portd to 0xAA
}
if (dat[4]) { // if message received successfully
cnt = 0;
dat[4] = 0; // clear message received flag
if(dat[0]=='k')
{
PORTE = ~ PORTE;
Delay_ms(5000);
dat[0] = 0x02; // send back to master
Delay_ms(1);
RS485Master_Send(dat,1,160);
Delay_ms(100);
}
}
if (cnt > 100000) {
PORTD ++;
cnt = 0;
RS485Master_Send(dat,1,160);
if (PORTD > 10) // if sending failed 10 times
RS485Master_Send(dat,1,50); // send message on broadcast address
}
}

}

Slave Code:

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() {


PORTB = 0;
PORTD = 0;
PORTE= 0;
TRISB = 0;
TRISD = 0;
TRISE = 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[5]) { // if an error detected, signal it by
PORTD = 0xAA; // setting portd to 0xAA
dat[5] = 0;
}
if (dat[4]) { // upon completed valid message receive
dat[4] = 0; // data[4] is set to 0xFF
if(dat[0]==0x02)
{
PORTE = ~ PORTE;
Delay_ms(5000);
dat[0] = 'k'; // send back to master
Delay_ms(1);
RS485Slave_Send(dat,1);
Delay_ms(100);
}

}
}
}


Can anyone tell me what is wrong in this code? It is almost the same as rs485 example of microE but all what it does is the slave sets portA then nothing after this step.

Did anyone here used rs485 library of mikroE? Is it good one or it is buggy?
 

mamech

I may be wrong, but at firmware sight, RS-485 and RS-232 behaves the same way.
The only change is at phisical layer - signal level ( see : RS485, RS232, RS422, RS423, Quick Reference Guide ).

So, it is possible to you rearch for other implementations on Web not restricted to RS-485 library but including RS-232.
I found bellow code at this forum but are many other posts :


Other points :
1 ) You did not mentioned what uC you´re using ( PIC16 / PIC18 ).
2 ) Pehaps you wish refer to MikroC ( not MikroE ).

+++
 

I use pic16f877a
 

You should use PIC 18F serie, 18F458 for example. Take a look in to mikroBasic forum for RS485 example.

Regards
 

I think that I found the source of the problem. I tried even a simple uart program having if statments , and it did not completed the required sequence. If what I did with uart did not succeed , then the case for rs485 will be the same.

What is wrong in the following code ?

char uart_rd;

void main() {

PORTA = 0;
TRISA = 0;
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize

UART1_Write_Text("Start");
UART1_Write(10);
UART1_Write(13);

while (1) { // Endless loop
if (UART1_Data_Ready()) { // If data is received,
uart_rd = UART1_Read(); // read the received data,




}

if(uart_rd=='d')
{
PORTA=~PORTA;
uart_rd = 0;
}
}
}


The strange thing that when I send 'd' using a terminal program, then led connected to portA lights, then it stays lighting even if I send more 'd' charachters using the terminal program.

Why this program work only in the begining then fails?


Thanks
 

mammech

I sugest you post codes formated by
Code:
 expression nex time.
However seems that are not missing anything.
( hope you don´t mind to me post again your code to analyse easyer in this format )

+++

[CODE]char uart_rd;

void main() {

PORTA = 0;
TRISA = 0;
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize

UART1_Write_Text("Start");
UART1_Write(10);
UART1_Write(13);

while (1) { // Endless loop
if (UART1_Data_Ready()) { // If data is received,
uart_rd = UART1_Read(); // read the received data,
}

if(uart_rd=='d')
{
PORTA=~PORTA;
uart_rd = 0;
}
}
}
 

Sorry for this . I will post codes in using
Code:
 next time 

I think that the code is straight forward, so I do not know what I am missing.But there must be a cause for this weird performance.
 

Exacly

I compared you code with others posted in this forum but the same main simple commands were used at both.
I can sugest you try others to certify if some detail not covered by us could interfere the proper working.

+++
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top