You are on page 1of 6

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 sour ce 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/4 096 DIM MAXWHIGH AS WORD DIM MAXREAD AS WORD DIM Voltage as float DIM MAXHIGH AS BYTE DIM MAXLOW AS BYTE DIM WADDRESS AS BYTE DIM RADDRESS AS BYTE DIM WCONTROL AS BYTE DIM CH[8] AS BYTE DIM CHNUM AS BYTE WADDRESS=%01010000 RADDRESS=%01010001 WCONTROL=%10000000 OWN=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 t emperature on LCD Dim command as Dim i as Dim temp as Symbol DQ = Symbol DQ_DIR = on pin byte byte word PORTC.0 TRISC.0 ' Storage for command ' Storage for loop counter ' Storage for temperature ' Alias DS1820 data pin ' Alias DS1820 data directi 'will hold Voltage result 'READ ADDRESS 'WRITE ADDRESS 'CONTROL BYTE 'FIXED DEVICEID=0101 ADDRESS=0 WRITE=0 'FIXED DEVICEID=0101 ADDRESS=0 READ=1 'START=1 CHANNEL=0 RANGE=0 BIPOLAR=0 POWERD

'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 o f 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 modu le FOR CHNUM = 0 TO 0 ' If you want to scan further 'channels, ad just 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 .5V ALARMFLAG = 1 ENDIF DELAYMS 500 "

' setting the Alarm threshold at 2

' inserting the temp mainloop Gosub init1820 command = $cc Gosub write1820 command = $44 Gosub write1820 Delayms 2000 Gosub init1820 command = $cc ' Init the DS1820 ' Issue Skip ROM command

-=================

' Start temperature conversion ' Wait 2 seconds for conversion to complete ' Do another init ' Issue Skip ROM command

Gosub write1820 command = $be Gosub write1820 Gosub read1820

' Read the temperature

' 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 e is over 29C ALARMFLAG =1 rature (in degrees C) = temp/2 PRINT AT 4,1, "Over Temp!" 59; ENDIF ' then Temperature in C = 59/2 = 29.5C ' e.g.: temp = ' Tempe ' If temperatur

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 DQ_DIR = 1 Return ' Write a 1 bit to the DS1820 write1: Low DQ @ nop DQ_DIR = 1 Delayus 60 Return

' Low for > 60us for 0 ' Release data pin (set to input for high)

' ' ' '

Low for < 15us for 1 Delay 1us at 4MHz Release data pin (set to input for high) Use up rest of time slot

' 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 Low DQ @ nop DQ_DIR = 1 If DQ = 0 Then temp.15 = 0 Endif Delayus 60 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 COMM AND BSTOP ' SEND A STOP CONDIT ION

' ' ' '

Preset read bit to 1 Start the time slot Delay 1us at 4MHz Release data pin (set to input for high)

' Set bit to 0 ' Wait out rest of time slot

'READ THE CONVERSION RESULT BSTART ONDITION

' SEND A START C

BUSIN RADDRESS,[MAXHIGH,MAXLOW]' READ CONVERSION HIGH & LOW BYTES BSTOP ' SEND A STOP CONDIT ION '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 SM S message ENDIF ' if it encounters errors more after 10 retried, then it ' will start from the beginni ng. '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ 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 rem ove 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 mes sage '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 = m inute 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 id ea ' to have an END point in case of errors in the code. ' This provides some safety against unpredictable operation and ' makes debugging easier (sometimes).

You might also like