Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
Bahman Mohammadhoseini
bobx

posted on 2011/11/11 11:42:43 AM CET

Communication > CAN / LIN / RS485

RS485 and 18F452???

Hello.I wrote a program using RS485 [ PIC18F452/XTAL=20MHZ/mikroC for PIC compiler v5.1.0 ] to transfer the following arrays;

unsigned long int LOWD[]={1000,1000,1000,1000,1000,1000,500,500,500,100,4,44,40,7,5,5,5,400,500000,300000};

unsigned long int DHIGH[]={25000,25000,25000,25000,25000,25000,1200,1200,1200,290,11,60,280,1,33,33,33,3190,30000000,30000000};


from U2=PIC18F452 as a master to U1=PIC18F452 as a slave just at the begining of my main
and for one time and also to transfer the above arrays from U1=PIC18F452 slave for each
5 seconds to a PC.U2 is about 800 meters far from U1 and PC is 650 meters far from U2 and U2.

So,I wrote the following code for my PIC Master U2;

/*
*** Using RS485***

MCU: PIC18F452.
Oscillator: External, 20.000 MHz.
Ext. Modules: RS485 on PORTC.
SW: mikroC v5.1.
*/


char datx[39],dat[39];//Buffer for receving/sending messages.
char i,k, Iflag;


unsigned long int LOWD[]={1000,1000,1000,1000,1000,1000,500,500,500,100,4,44,40,7,5,5,5,400,500000,300000};

unsigned long int DHIGH[]={25000,25000,25000,25000,25000,25000,1200,1200,1200,290,11,60,280,1,33,33,33,3190,30000000,30000000};


//Function declration;

void MRSX485();


void MRSX485(){

UART1_Init(9600);//initialize uart module.
Delay_ms(100);

RS485Master_Init();//intialize mcu as master.

PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISD = 0;

for(i=0;i<20;i++){
datx[i]=LOWD[i];
}

for(i=20;i<40;i++){
datx[i]=DHIGH[i];
}


k=0;
MX1:
dat[0] = datx[k];
dat[1] = datx[k+1];
dat[2] = datx[k+2];

dat[3] = 0;
dat[4] = 0; //*****What I should assign to dat[3] thru dat[6]?*****
dat[5] = 0;
dat[6] = 0;

Iflag = 0;

RS485Master_Send(dat,1,160);

PORTB = 0;
PORTD = 0;

dat[4] = 0;//ensure that message received flag is 0.
dat[5] = 0;//ensure that error flag is 0.

PIE1.RCIE = 1;//Enable interrupts.
INTCON.PEIE = 1;//On byte received.
PIE2.TXIE = 0;//Via UART (RS485).
INTCON.GIE = 1;


//**? **upon completed valid message receiving.
//**? **data[4] is set to 255.


if(dat[5]){ //if error detected, signal it by.
PORTD.F0 = 1;//setting PORTD.F0 to 1.
goto MX1;//Repeat sending that error messages again untill to be sent successfully.
}

if(dat[4]){ //if message received successfully.
dat[4] = 0;//clear message received flag.
PORTD.F1=1;//setting PORTD.F1 to 1.
Delay_ms(1);
K=K+3;
goto MX1; //Go for sending the remains.
}

}//~


// Interrupt routine
void interrupt() {
RS485Master_Receive(dat);
}//~

sbit rs485_rxtx_pin at RC6_bit;//set transcieve pin.
sbit rs485_rxtx_pin_direction at TRISC6_bit;//set transcieve pin directio.

void main() {

PORTA=0;//Clear portA.
TRISA=0XFF;//PortA is input.
PORTD=0;//Clear portD.
TRISD=0;//PortD is output.
PORTC=0;//Clear portC.
TRISC=0;//portC is output.
PORTB=0;//Clear portB.
TRISB=0XFF;//portB is input.
PORTE=0;//Clear portE.
TRISE=0;//PortE is Output. zzzz
TRISC = PORTC = 0;

// Only for PIC18F452 //

RCON.IPEN =1;//Enable priority levels on interrupts.
INTCON.INT0IE =1;//Enables the INT0 external interrupt.
INTCON2.INTEDG0 =0;//Interrupt on falling edge.
INTCON.RBIE =1;//Enables the RB Port change interrupt.

Delay_ms(400);

MRSX485();

}//~!

But that is not worked correctly.Would you please tell me what is wrong with my code?
Also,how can I sure that my arrays transferred correctly into U1 in a effective,fast and simply?
And how to write the Slave[U1] for that,to receive arrays send byU2?

Thanks,