Last active
March 23, 2017 15:41
-
-
Save shahrulnizam/6238735 to your computer and use it in GitHub Desktop.
PIC Lesson: Modbus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Project: Modbus RTU Lesson | |
Programmer: Shahrulnizam Mat Rejab | |
PIC (Crystal Frequency): | |
PIC16F877A (20MHz), PIC16F887 (20MHz), PIC16F1939 (Internal 32MHz) | |
PIC18F4520 (10MHz), PIC18F4550 (10MHz), PIC18F46K22 (Internal 16MHz) | |
Compiler: HI-TECH C for PIC10/12/16 V9.83, PIC18 V9.80 | |
Last Modified: 1 January 2014 | |
Website: http://shahrulnizam.com | |
*/ | |
#define TIMER0 | |
#define ADC | |
#define UART | |
#define HD44780 | |
#define SHAHRULNIZAM | |
#define SLAVE_ID 1 | |
#include "LibraryHardware.h" | |
#include "LibraryPeripheral.h" | |
#include "LibraryUniversalDisplay.h" | |
#include "LibraryModbus.h" | |
char rep,z,request[40],response[40],bitt[20]; | |
unsigned int address[40],counter; | |
static void interrupt isr(void) | |
{char i; | |
if(RCIF==1){ | |
counter=0; | |
request[z]=uart1_read(); | |
if(z<39) z++;} | |
if(TMR0IF==1){ | |
TMR0IF=0; | |
counter++; | |
if(counter==1000){ | |
rep=1; | |
z=0;} | |
if(counter==20000){ | |
for(i=0;i<=9;i++) request[i]=0; | |
for(i=0;i<=9;i++) response[i]=0;}} | |
} | |
void setup() | |
{ | |
initialize(); | |
TRISA=0b00000001; | |
TRISB=0b00000000; | |
TRISC=0b10000000; | |
TRISD=0b00000000; | |
TRISE=0b00000000; | |
timer0(0); | |
enable_tmr0(); | |
enable_rc1(); | |
BUZZER=1; | |
uart1_init(9600); | |
lcd_init(); | |
lcd_goto(0,0); | |
lcd_string(" MODBUS PROTOCOL "); | |
while(1) loop(); | |
} | |
void loop() | |
{int i; | |
address[0]=adc_read(0); | |
lcd_goto(0,1); | |
for(i=0;i<=6;i++){ | |
lcd_number(request[i],HEX,2); | |
if(i!=6) lcd_write(' ');} | |
lcd_goto(0,2); | |
for(i=7;i<=13;i++){ | |
lcd_number(request[i],HEX,2); | |
if(i!=13) lcd_write(' ');} | |
lcd_goto(0,3); | |
lcd_string("ADD0="); | |
lcd_number(address[0],10,4); | |
lcd_string(" ADD1="); | |
lcd_number(address[1],10,5); | |
if(rep==1){ | |
rep=0; | |
modbus_slave(address,bitt,request,response);} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment