0% found this document useful (0 votes)
4 views3 pages

Pic Modbus

The document outlines a simple RS485 communication program using PIC16F628A microcontroller and MAX485 transceiver. It includes both master and slave configurations, detailing the initialization of UART, handling of interrupts, and the logic for sending and receiving data. The master sends specific commands based on input states, while the slave responds by controlling output pins accordingly.

Uploaded by

ZHONG LI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Pic Modbus

The document outlines a simple RS485 communication program using PIC16F628A microcontroller and MAX485 transceiver. It includes both master and slave configurations, detailing the initialization of UART, handling of interrupts, and the logic for sending and receiving data. The master sends specific commands based on input states, while the slave responds by controlling output pins accordingly.

Uploaded by

ZHONG LI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

MASTER - RS485 COMMUNICATION


SIMPLE RS485 PROGRAM USING
1. PIC16F628A - 4MHZ internal oscillator
2. MAX485
*/
char dat[9]; // buffer for receving/sending messages
sbit rs485_rxtx_pin at RB0_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISB0_bit; // set transcieve pin direction

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

void main(){
CMCON |= 0x07; // Disable comparators
TRISA = 0xff; //all porta input
TRISB = 0x07;
PORTA = 0;
PORTB = 0;
/*
PORTA.0 = INPUT 1
PORTA.1 = INPUT 2
PORTA.2 = INPUT 3
PORTA.3 = INPUT 4
PORTA.4 = INPUT 5
*/

/*
PORTB.0 = RX/TCX
PORTB.1 = RX
PORTB.2 = TX
PORTB.3 = OUTPUT 1
PORTB.4 = OUTPUT 2
PORTB.5 = OUTPUT 3
PORTB.6 = OUTPUT 4
PORTB.7 = OUTPUT 5
*/

UART1_Init(9600); // initialize UART1 module


Delay_ms(100);

RS485Master_Init(); // initialize MCU as Master


dat[0] = 0;
dat[1] = 0x00;
dat[2] = 0x00;
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,100);

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){
//1
while (porta.f4==1 && porta.f3==1 && porta.f2==1 && porta.f1==1 &&
porta.f0==0){
delay_ms(10);
dat[0]= 1;
RS485Master_Send(dat,1,100);
}
//2
while (porta.f4==1 && porta.f3==1 && porta.f2==1 && porta.f1==0 && porta.f==1)
{
delay_ms(10);
dat[0]= 2;
RS485Master_Send(dat,1,100);
}
//3
while (porta.f4==1 && porta.f3==1 && porta.f2==1 && porta.f1==0 && porta.f==0)
{
delay_ms(10);
dat[0]= 3;
RS485Master_Send(dat,1,100);
}
dat[0]= 0; //send 0 if no input
RS485Master_Send(dat,1,100);
}
}
//////
/*
SLAVE - RS485 COMMUNICATION
1. PIC16F628A - 4MHZ internal oscillator
2. MAX485
*/
char dat[9]; // buffer for receving/sending messages

sbit rs485_rxtx_pin at Rb0_bit; // set transcieve pin


sbit rs485_rxtx_pin_direction at TRISb0_bit; // set transcieve pin direction

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

void main() {
CMCON |= 0x07; // Disable comparators
TRISA = 0x1F; //INITIALIZE PORTA
TRISB = 0x07; //INITIALIZE PORTB
PORTA = 0;
PORTB = 0;
/*
PORTA.0 = INPUT 1
PORTA.1 = INPUT 2
PORTA.2 = INPUT 3
PORTA.3 = INPUT 4
PORTA.4 = INPUT 5
*/

/*
PORTB.0 = RX/TCX
PORTB.1 = RX
PORTB.2 = TX
PORTB.3 = OUTPUT 1
PORTB.4 = OUTPUT 2
PORTB.5 = OUTPUT 3
PORTB.6 = OUTPUT 4
PORTB.7 = OUTPUT 5
*/

UART1_Init(9600); // initialize UART1 module


Delay_ms(100);
RS485Slave_Init(100); // Intialize MCU as slave, address 100

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

//ALL MASTER INPUTS ARE OFF


if (dat[0]==0) {
portb=0x00 ;
}
//1
else if (dat[0]==1) {
portb=0x08 ; //TURN ON PORTB.F3 OF SLAVE

}
//2
else if(dat[0]==2){
portb=0x10; //TURN ON PORTB.F4 OF SLAVE

}
//3
else if(dat[0]==3){
portb=0x18; //TURN ON PORTB.F3 && PORTB.F4 OF SLAVE

You might also like