• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

address problem rs485 microc

Status
Not open for further replies.

maryem

Member
i used the library rs485 of microc to configure pic16f1508 1master 1slave in master code i send a data to a different address of slave but i get it in my slave .i didn't know what's the problem , pleassssssse help
RS485Master_Send(dat,3,150) ;
and i configure my slave with address 160
master code
char Dummy ;
char dat[10];
char buf[10];
int i,j ;
sbit rs485_rxtx_pin at RB6_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISB6_bit;
bit etat ;
bit etat_1;
bit etat_2;
int Device_ID ;
bit cnt ;
bit oldstate;
unsigned inputvalue ;
bit Jeton_0 ;
bit Jeton_1 ;
bit Jeton_2 ;
bit Jeton_3 ;
bit Jeton_4 ;


void interrupt (void)
{if (INTCON.IOCIF==1)
{ if ((PORTA.f2==1 ) && (IOCAF.IOCAF2 ==1))
{if ( PORTA.F0==1)
{ PORTC.f7= 0;
PORTC.f6= 0;
etat_2=1;
Dummy=PORTA ;}
else if ( PORTA.F1==1)
{ PORTC.f7= 0;
PORTC.f6= 0;
etat_2=0;
Dummy=PORTA ;
} }
if ((PORTA.f3==1 ) && (IOCAF.IOCAF3 ==1))
{if ( PORTA.F0==1)
{ PORTC.f7= 1;
PORTC.f6= 0;
etat_1=1;
Dummy=PORTA ;}
else if ( PORTA.F1==1)
{ PORTC.f7= 1;
PORTC.f6= 0;
etat_1=0;
Dummy=PORTA ;
}
IOCAF.IOCAf3=0 ;}
if ((PORTA.f0==1 ) && (IOCAF.IOCAF0 ==1))
{PORTC.f3= 1;
PORTC.f4= 0;
Dummy=PORTA ;
IOCAF.IOCAf0=0 ;}
if ((PORTA.f1==1 ) && (IOCAF.IOCAF1 ==1))
{PORTC.f3= 0;
PORTC.f4= 1;
Dummy=PORTA ;
IOCAF.IOCAf1=0 ;}
if ((PORTB.f4==1 ) && (IOCBF.IOCBF4 ==1))
{ if ( PORTA.F0==1)
{ PORTC.f6= 1;
PORTC.f7= 0;
etat=1;
Dummy=PORTB ;}
else if ( PORTA.F1==1)
{PORTC.F6= 1;
PORTC.f7= 0;
etat=0;
Dummy=PORTB ;
}

IOCBF.IOCBf4=0 ;}
INTCON.IOCIF=0;}

if ( ( PIR1.RCIF==1) &&(PORTA.F1==1) )
{RS485Slave_Receive(dat);
for ( i=0;i<8;i++)
{ buf=RCREG ; }
if (buf[5])
{dat[5]=0;}
if (buf[4])
{ dat[4]=0;
if (buf[2]==0xF0)
{ LATC.F6=1;
LATC.F7=0;
}
if (buf[2]==0xFF)
{LATC.F6=0;
LATC.F7=1;
}
if (buf[2]==0x0F )
{ LATC.F6=1;
LATC.F7=0;
}
buf[2]==0x00;
PIR1.RCIF=0;} } }
void main() {
ANSELA=ANSELB=ANSELC=0;
CM1CON0.C1ON= CM2CON0.C2ON=0;
TRISC=0b00100111;
TRISA=0b00000111;
TRISB=0b00010000;

INTCON.GIE=1;
INTCON.IOCIE=1;
INTCON.IOCIF=0;
IOCAP=0b00101111 ;
IOCAP.IOCAP1=1;
IOCBP=0b00010000 ;


PORTC.F6=0;
PORTC.F7=0;/* TMR0 = 0;INTCON.TMR0IE=1;INTCON.TMR0IF=0;OPTION_REG = 00000100 ;OPTION_REG.TMR0CS=0;OPTION_REG.PSA=0;*/



if ((PORTC.F0==1)&&(PORTC.F1==0)&&(Portc.F2==0))
{if (PORTA.F1==1)
{PORTC.F3=0;
PORTC.F4=1;
UART1_Init(9600); // initialize UART1 module
RS485Slave_Init(150);}
if (PORTA.F0==1)
{PORTC.F3=1;
PORTC.F4=0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(5);
Device_ID=1 ;
RS485Master_Init();}}

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;
RCIF_bit = 0;

do {

if ((Device_ID==0 ))
{Jeton_0=1;}
if ((Device_ID==1 ))
{Jeton_1=1;}
if ((Device_ID==2 ))
{Jeton_2=1;}
if ((Device_ID==3 ))
{Jeton_3=1;}


if (Button(&PORTB, 4, 1, 1)) { // Detect logical one
oldstate = 1; }
if (oldstate && Button(&PORTB, 4, 1, 0)&&(PORTA.F0==1) ) // Detect one-to-zero transition
{ oldstate = 0;
dat[0] = 0xA0;
dat[1] = 0;
dat[2] = 0xF0;
dat[3]=0x03 ;
dat[4] = 0; // ensure that message received flag is 0
dat[5] = 0; // ensure that error flag is 0





if ( ( Jeton_1==1) )
{RS485Master_Send(dat,3,150);
}
}

}while(1) ;}
slave code
char Dummy ;
char dat[10];
char buf[10];
int i,j ;
sbit rs485_rxtx_pin at RB6_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISB6_bit;
bit etat ;
bit etat_1;
bit etat_2;
int Device_ID ;
bit cnt ;
bit oldstate;
unsigned inputvalue ;
bit Jeton_0 ;
bit Jeton_1 ;
bit Jeton_2 ;
bit Jeton_3 ;
bit Jeton_4 ;


void interrupt (void)
{if (INTCON.IOCIF==1)
{ if ((PORTA.f2==1 ) && (IOCAF.IOCAF2 ==1))
{if ( PORTA.F0==1)
{ PORTC.f7= 0;
PORTC.f6= 0;
etat_2=1;
Dummy=PORTA ;}
else if ( PORTA.F1==1)
{ PORTC.f7= 0;
PORTC.f6= 0;
etat_2=0;
Dummy=PORTA ;
} }
if ((PORTA.f3==1 ) && (IOCAF.IOCAF3 ==1))
{if ( PORTA.F0==1)
{ PORTC.f7= 1;
PORTC.f6= 0;
etat_1=1;
Dummy=PORTA ;}
else if ( PORTA.F1==1)
{ PORTC.f7= 1;
PORTC.f6= 0;
etat_1=0;
Dummy=PORTA ;
}
IOCAF.IOCAf3=0 ;}
if ((PORTA.f0==1 ) && (IOCAF.IOCAF0 ==1))
{PORTC.f3= 1;
PORTC.f4= 0;
Dummy=PORTA ;
IOCAF.IOCAf0=0 ;}
if ((PORTA.f1==1 ) && (IOCAF.IOCAF1 ==1))
{PORTC.f3= 0;
PORTC.f4= 1;
Dummy=PORTA ;
IOCAF.IOCAf1=0 ;}
if ((PORTB.f4==1 ) && (IOCBF.IOCBF4 ==1))
{ if ( PORTA.F0==1)
{ PORTC.f6= 1;
PORTC.f7= 0;
etat=1;
Dummy=PORTB ;}
else if ( PORTA.F1==1)
{PORTC.F6= 1;
PORTC.f7= 0;
etat=0;
Dummy=PORTB ;
}

IOCBF.IOCBf4=0 ;}
INTCON.IOCIF=0;}

if ( ( PIR1.RCIF==1) &&(PORTA.F1==1) )
{RS485Slave_Receive(dat);
for ( i=0;i<8;i++)
{ buf=RCREG ; }
if (buf[5])
{dat[5]=0;}
if (buf[4])
{ dat[4]=0;
if (buf[2]==0xF0)
{ LATC.F6=1;
LATC.F7=0;
}
if (buf[2]==0xFF)
{LATC.F6=0;
LATC.F7=1;
}
if (buf[2]==0x0F )
{ LATC.F6=1;
LATC.F7=0;
}
buf[2]==0x00;
PIR1.RCIF=0;} } }
void main() {
ANSELA=ANSELB=ANSELC=0;
CM1CON0.C1ON= CM2CON0.C2ON=0;
TRISC=0b00100111;
TRISA=0b00000111;
TRISB=0b00010000;

INTCON.GIE=1;
INTCON.IOCIE=1;
INTCON.IOCIF=0;
IOCAP=0b00101111 ;
IOCAP.IOCAP1=1;
IOCBP=0b00010000 ;

PORTC.F6=0;
PORTC.F7=0;/* TMR0 = 0;INTCON.TMR0IE=1;INTCON.TMR0IF=0;OPTION_REG = 00000100 ;OPTION_REG.TMR0CS=0;OPTION_REG.PSA=0;*/
if ((PORTC.F0==0)&&(PORTC.F1==0)&&(Portc.F2==0))





{if (PORTA.F1==1)
{PORTC.F3=0;
PORTC.F4=1;
UART1_Init(9600); // initialize UART1 module
RS485Slave_Init(160);}
if (PORTA.F0==1)
{PORTC.F3=1;
PORTC.F4=0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(5);
Device_ID=0 ;
RS485Master_Init();}}



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;
RCIF_bit = 0;

do {

if ((Device_ID==0 ))
{Jeton_0=1;}
if ((Device_ID==1 ))
{Jeton_1=1;}
if ((Device_ID==2 ))
{Jeton_2=1;}


if (Button(&PORTB, 4, 1, 1)) { // Detect logical one
oldstate = 1; }
if (oldstate && Button(&PORTB, 4, 1, 0)&&(PORTA.F0==1) ) // Detect one-to-zero transition
{ oldstate = 0;
dat[0] = 0xA0;
dat[1] = 0;
dat[2] = 0xF0;
dat[3]=0x03 ;
dat[4] = 0; // ensure that message received flag is 0
dat[5] = 0; // ensure that error flag is 0




if ( (Jeton_0==1))
{ RS485Master_Send(dat,3,160);


}
}

}while(1) ;}
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top