'**************************************************************** '* Name : UNTITLED.BAS * '* Author : [select VIEW...EDITOR OPTIONS] * '* Notice : Copyright (c) 2007 [select VIEW...EDITOR OPTIONS] * '* : All Rights Reserved * '* Date : 18.12.2007 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** Include "PROTON_4a.inc" ' Author: Ashfaq Juna of Maxim Dallas www.maxim-ic.com. ' Email: ashfaq_juna@maximhq.com ; ' This code and project is provided for evaluation/trial purposes only and must not be used ' in safety critical applications. It should not be relied upon as the sole source of alerts. '==================== Device 16F877 '====================== ' ---------- Variable/Constants used for the MAX127 ----- Declare SDA_PIN PORTB.1 Declare SCL_PIN PORTB.0 Dim ALARMFLAG As Bit ' this is to be used as a flag for sending the SMS out Symbol PART 0.001220703 'value to multiple A/D result by .. 5v/4096 Dim MAXWHIGH As Word Dim MAXREAD As Word Dim Voltage As Float 'will hold Voltage result Dim MAXHIGH As Byte Dim MAXLOW As Byte Dim WADDRESS As Byte 'READ ADDRESS Dim RADDRESS As Byte 'WRITE ADDRESS Dim WCONTROL As Byte 'CONTROL BYTE Dim CH[8] As Byte Dim CHNUM As Byte WADDRESS=%01010000 'FIXED DEVICEID=0101 ADDRESS=0 WRITE=0 RADDRESS=%01010001 'FIXED DEVICEID=0101 ADDRESS=0 READ=1 WCONTROL=%10000000 'START=1 CHANNEL=0 RANGE=0 BIPOLAR=0 POWERDOWN=0 CH[0]=%10000000 CH[1]=%10010000 CH[2]=%10100000 CH[3]=%10110000 CH[4]=%11000000 CH[5]=%11010000 CH[6]=%11100000 CH[7]=%11110000 '-------------------MAX127 '-----------Variables/Constants for DS18S20 ------------ ' Program to read DS1820 1-wire temperature sensor and display temperature on LCD Dim command As Byte ' Storage for command Dim i As Byte ' Storage for loop counter Dim temp As Word ' Storage for temperature Symbol DQ = PORTC.0 ' Alias DS1820 data pin Symbol DQ_DIR = TRISC.0 ' Alias DS1820 data direction pin 'Print $fe, 1, "Temp in degrees C" ' Display sign-on message '=======================VARIABLES FOR SMS ========== Dim SMS_counter As Byte ' this will log the number of retries ' in the event of SMS errors Dim SMSDELAY As Byte ' keeping track of the number of tries '=============================================================================== '-------------------------- MAIN SECTION OF THE CODE ------------------------- '=============================================================================== MAIN: Clear ALARMFLAG ' clear the Alarm Flag Clear SMS_counter ' reset the sms error counter Clear SMSDELAY ' reset SMS delay counter, this is incremented every minute Cls ' clear LCD screen; using a 4 lines x 20 character LCD module For CHNUM = 0 To 0 ' If you want to scan further 'channels, adjust the end point ' eg. sampling channel 0 - 4 : FOR CHNUM = 0 TO 4 ' you will also need to store the results for each channel seprately GoSub MAX127IN DelayMS 1000 Next CHNUM Print At 1,1, "Analogue i/p = " ,@MAXREAD," " DelayMS 500 If MAXREAD > 2047 Then ' setting the Alarm threshold at 2.5V ALARMFLAG = 1 EndIf DelayMS 500 ' inserting the temp mainloop -================= GoSub init1820 ' Init the DS1820 command = $cc ' Issue Skip ROM command GoSub write1820 command = $44 ' Start temperature conversion GoSub write1820 DelayMS 2000 ' Wait 2 seconds for conversion to complete GoSub init1820 ' Do another init command = $cc ' Issue Skip ROM command GoSub write1820 command = $be ' Read the temperature GoSub write1820 GoSub read1820 ' Display the decimal temperature Print At 2, 1, Dec (temp >> 1), ".", Dec (temp.0 * 5), " degrees C" Print At 3,1, Dec temp ' SET THE TEMPERATURE TRIP THRESOLD HERE If temp > 58 Then ' If temperature is over 29C ALARMFLAG =1 ' Temperature (in degrees C) = temp/2 Print At 4,1, "Over Temp!" ' e.g.: temp = 59; EndIf ' then Temperature in C = 59/2 = 29.5C If ALARMFLAG = 1 Then Print At 4,1, " Send SMS please!" DelayMS 300 EndIf '----------------------- If ALARMFLAG <> 0 Then ' setting the threshold at 2.5V GoSub SMS EndIf DelayMS 100 GoTo MAIN '==--== temperature============================================= ' Subroutine to read the temperature and display on LCD ' Initialize DS1820 and check for presence init1820: Low DQ ' Set the data pin low to init DelayUS 500 ' Wait > 480us DQ_DIR = 1 ' Release data pin (set to input for high) DelayUS 100 ' Wait > 60us If DQ = 1 Then Print $fe, 1, "DS1820 not present" DelayMS 500 ' Goto mainloop ' Try again EndIf DelayUS 400 ' Wait for end of presence pulse Return ' Write "command" byte to the DS1820 write1820: For i = 1 To 8 ' 8 bits to a byte If command.0 = 0 Then GoSub write0 ' Write a 0 bit Else GoSub write1 ' Write a 1 bit EndIf command = command >> 1 ' Shift to next bit Next i Return ' Write a 0 bit to the DS1820 write0: Low DQ DelayUS 60 ' Low for > 60us for 0 DQ_DIR = 1 ' Release data pin (set to input for high) Return ' Write a 1 bit to the DS1820 write1: Low DQ ' Low for < 15us for 1 @ nop ' Delay 1us at 4MHz DQ_DIR = 1 ' Release data pin (set to input for high) DelayUS 60 ' Use up rest of time slot Return ' Read temperature from the DS1820 read1820: For i = 1 To 16 ' 16 bits to a word temp = temp >> 1 ' Shift down bits GoSub readbit ' Get the bit to the top of temp Next i Return ' Read a bit from the DS1820 readbit: temp.15 = 1 ' Preset read bit to 1 Low DQ ' Start the time slot @ nop ' Delay 1us at 4MHz DQ_DIR = 1 ' Release data pin (set to input for high) If DQ = 0 Then temp.15 = 0 ' Set bit to 0 EndIf DelayUS 60 ' Wait out rest of time slot Return ' End '----------------------[[[[ temperature---------------- MAX127IN:'START A CONVERSION ON CHANNEL 0 : 5V RANGE :UNIPOLAR BStart ' SEND A START CONDITION BusOut WADDRESS,[CH[CHNUM]] ' SEND SLAVE ADDRESS AND WRITE COMMAND BStop ' SEND A STOP CONDITION 'READ THE CONVERSION RESULT BStart ' SEND A START CONDITION BusIn RADDRESS,[MAXHIGH,MAXLOW]' READ CONVERSION HIGH & LOW BYTES BStop ' SEND A STOP CONDITION 'REORGANIZE THE BIT POSITIONS TO FORM 12 BIT READING MAXLOW=MAXLOW/16 MAXWHIGH=MAXHIGH*16 MAXREAD = MAXWHIGH + MAXLOW Return '================== END Temperature ========================= '$$$$$$$$$$$$$$DO SMS Messaging Here $$$$$$$$$$$$$$$$$$$$ SMS: ' in this subroutine the SMS message will be sent. Inc SMS_counter If SMS_counter >10 Then ' after sampling the temperature and voltage from the LDR, GoTo MAIN ' the unit will attempt to send an SMS message EndIf ' if it encounters errors more after 10 retried, then it ' will start from the beginning. '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ DelayMS 100 HSerOut ["AT" ,13] 'Sending "AT" and Carraige return to GSM module ' If its ok, the it will send a response back : "OK" HSerIn 5000, SMS,[Wait("OK")] ' wait for response ok for 5 seconds ' if it times out, it will go back to SMS and retry 'If GSM Module is OK, will send response "OK" ' Other wise it will send "ERROR" or no response ' If "OK" is not received withing 5 seconds ' The code will report and error to HOST PC by going to ' MSG_TIMEOUT subroutine. A string "MSGER" to the HOST PC HSerOut ["AT+CMGF=1" ,13] HSerIn 5000, SMS,[Wait("OK")] ' wait for response ok for 5 seconds ' if it times out, it will go back to SMS and retry 'Here the GSM module is being entered in TEXT MODE. 'This is necessary to do in order for it to ne in TEXT MODE rather than PDU MODE 'PDU mode is Packet Data Mode. We need TEXT MODE to be able to send SMS messages. HSerOut ["AT+CMGS=+447---------"] 'Here the GSM module is told the desitnation phone number. 'This is done by sending string "AT+CMGS= desitnation mobile number" 'ENTER THE DESISTATION MOBILE NUMBER provided by TynTec in the gap above and remove all the Dashes 'Enter in 'international' format, eg. +447789...... HSerOut [13] HSerIn 5000 , SMS, [Wait(">")] HSerOut [ "Temperature is ", Dec(temp >> 1), ".", Dec (temp.0 * 5), " Degrees C ; "] '=-=-=-converting the ADC reading to volts Voltage=PART * MAXREAD 'do calculation '0-0-0-0-0 HSerOut ["Voltage from the LDR is ",DEC2 Voltage," Volts"] 'result HSerOut [26] ' this is ASCII for Ctrl+Z HSerIn 15000 , SMS,[Wait("+CMG")] 'Here we are waiting for a string CMG. If that is received it means that the message 'has been transmitted successfuly! Print At 4,1, "MSG SENT" Repeat DelayMS 60000 ' wait for 1 minute seconds Inc SMSDELAY Until SMSDELAY = 10 ' this value determines the number of 1 minute delays ' before it samples the temperature and analogue input ' Change it to your requirements. 1 = minute delay, ' 10 = 10 minute delay and so on... Return ' return from subroutine '=========================================================================================== End ' This is where the code will stop to execute. ' This program should not reach this point as the last subroutine above ' will return to the point after from where it was called. ' So this 'END' instruction is not really need. BUT it is a very good idea ' to have an END point in case of errors in the code. ' This provides some safety against unpredictable operation and ' makes debugging easier (sometimes).