' -----[ Title ]----------------------------------------------------------- ' File...... DIOJR.PBP for microEngineering Labs PicBasic Pro compiler v2.12+ ' Purpose... DIO-Link clone module ' Author.... Michel Clavette ' E-mail.... Clavette.Michel@videotron.ca ' URL....... http://pages.infinit.net/clavette/hcs.html ' Started... 29 APR 1998 ' Updated... ' License... Copyright (C) 2001 Michel Clavette ' ' This program is free software; you can redistribute it and/or ' modify it under the terms of the GNU General Public License ' as published by the Free Software Foundation; either version 2 ' of the License, or (at your option) any later version. ' ' This program is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ' GNU General Public License for more details. ' ' You should have received a copy of the GNU General Public ' License along with this program; If not, write to the Free Software ' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA ' 02111-1307, USA. ' ' -----[ Program Description ]--------------------------------------------- ' This program will act as a DIO-Link designed to interface to the Circuit Cellar Inc HCS II ' home automation system using an RS-485 network. ' Connections: ' PIC16C84 ' -----\_/----- ' HEARTBEAT -| RA2 RA1 |- RS-485 SERIAL I/O (pin 1 and 4 of 75176 chip) ' STROBE -| RA3 RA0 |- RS-485 TX ENABLE (pin 2 and 3 of 75176 chip) ' Mod. reset -| RA4 OSC1 |- CRYSTAL ' +5V via R -| /MCL OSC2 |- CRYSTAL ' GND -| Vss Vdd |- +5V ' I/O bit 0 -| RB0 RB7 |- I/O bit 7 ' I/O bit 1 -| RB1 RB6 |- I/O bit 6 ' I/O bit 2 -| RB2 RB5 |- I/O bit 5 ' I/O bit 3 -| RB3 RB4 |- I/O bit 4 ' ------------- ' -----[ Revision History ]------------------------------------------------ ' -----[ Includes ]-------------------------------------------------------- ' -----[ Constants ]------------------------------------------------------- SIO CON 8 'PORTA.0: serial I/O TX CON 9 'PORTA.1: TX enable: sets pin 1 to 1 for transmit and 0 for receive HrtBt CON 10 'PORTA.2: Heartbeat LED Strobe CON 11 'PORTA.3: Strobe line used to signal new data available on PORTB ModRes CON 12 'PORTA.4: If high, restore module address in EEPROM to default value "0" ' -----[ Variables ]------------------------------------------------------- address VAR BYTE[5] 'Address of the DIO module (0-7) saved into byte 0 of the EEPROM packet VAR BYTE[13]'Buffer used for command input checksum VAR BYTE 'Packet checksum i VAR BYTE 'Temporary value variables j VAR BYTE ' -----[ EEPROM Data ]----------------------------------------------------- Data @0,"DIO0" 'Default DIO module address stored in EEPROM location 0 ' -----[ Initialization ]-------------------------------------------------- Reset: 'Module initialization routine, execute at startup and on reset Clear 'Clear all variables OPTION_REG = $7f For i = 0 TO 3 Read i,address[i] 'Read module address (0-7) from EEPROM locations 0 to 3 Next i Low TX High Strobe ' -----[ Main Code ]------------------------------------------------------- ReceivePacket: 'Wait here for network packet starting with the module address (DIO0-DIO7) Low HrtBt SerIn2 SIO,84,[WAIT("#"),HEX 2 checksum,skip 1, STR packet/10/13] 'Input command line High HrtBt ValidateNodeID: 'Check to see if incomming packet is for this node For i = 0 to 3 IF packet[i] != address[i] Then ReceivePacket Next i ValidateChecksum: 'Check if received packet is valid --------> add code here! ProcessCommand: IF packet[5] = "A" Then DIOAddress IF packet[5] = "Q" Then DIOQuery IF packet[5] = "R" Then Reset IF packet[5] = "S" Then DIOSet GoTo ReceivePacket ' Go back to serial input SendReply: 'Send reply packet High TX: Pause 50 SerOut2 SIO,84,["$",HEX 2 checksum,address," ",i,13] Low TX GoTo ReceivePacket End ' -----[ Subroutines ]----------------------------------------------------- ' Command routines DIOAddress: 'Action: Receive 4 hexadecimal characters node ID and save to EEPROM 'Reply: Blink heartbeat LED For i = 0 TO 3 Write i,packet[i + 8] 'Save received address in EEPROM byte 0 to 3 Next i For i = 1 TO 10 'Blink heartbeat LED to confim the change Toggle Pulse Pause 250 Next i GoTo Reset 'Reinitialize the module DIOQuery: 'Action: Get I/O data as per configuration 'Reply: Return as shown on AM manual page 14 i = PORTB 'Read the pin states of PORTB ' IF i = %00000001 THEN ' LOW STROBE ' PAUSE 1000 ' HIGH STROBE ' ENDIF GoTo SendReply DIOSet: 'Action: Receive 1 hex byte and set the digital outputs to requested value 'Reply: None Toggle Pulse GoTo ReceivePacket ' Checksum routines Checksum: 'Packet checksum routine h0 = 416 + b0 + b1 + Address 'Add-up all the ASCII values of the packet to be sent, h0 = -h0 'Then take the two's complement Convert: 'HEX conversion routine H1 = (H0 & %00001111) + 48 'Mask off the last four bits of H0, add 48 to bring us 'into the ASCII digits "0-9" range and save it to H1 H0 = (H0 >> 4) + 48 'Shift the content of H0 four bits to the right '(incoming bits are 0), add 48 to bring us into 'the ASCII digits "0-9" range and save back into H0 IF H1 > 57 Then 'If value is greater than "9" (in ASCII) H1 = H1 + 7 'Then add 7 to bring us into the "A-F" range EndIF IF H0 > 57 Then 'Ditto for H0 H0 = H0 + 7 EndIF Return