Professional Documents
Culture Documents
Home Automation System Sir
Home Automation System Sir
Home Automation System Sir
Mplab code
#include <p18f4520.inc>
; Configuration bits
CONFIG FOSC = HS ; High-Speed External oscillator (HS)
CONFIG WDTE = OFF ; Watchdog Timer disabled
CONFIG LVP = OFF ; Low-Voltage Programming disabled
; Variables
TEMP_TEMP EQU 0x20
TEMP_RESULT EQU 0x21
TEMP_TEMP1 EQU 0x22
TEMP_TEMP2 EQU 0x23
; Reset vector
ORG 0000
GOTO main
; Interrupt vector
ORG 0008
RETFIE 1
main:
; Initialize I/O ports
BANKSEL TRISB
CLRF TRISB
BANKSEL TRISC
CLRF TRISC
; Initialize ADC
CALL initializeADC
loop:
; Read temperature from sensor
CALL readTemperature
; Control smart thermostat based on temperature
CALL controlThermostat
controlLighting:
; Add code here for controlling lighting based on motion and door status
RETURN
; Delay
delayLoop1:
MOVLW 0xFF
MOVWF TEMP_TEMP1
delayLoop2:
MOVLW 0xFF
MOVWF TEMP_TEMP2
delayLoop3:
DECFSZ TEMP_TEMP2, F
GOTO delayLoop3
DECFSZ TEMP_TEMP1, F
GOTO delayLoop2
initializeADC:
BANKSEL ADCON1
MOVLW 0x0F
MOVWF ADCON1
MOVLW 0x00
MOVWF ADCON0
BANKSEL ADCON0
BSF ADCON0, ADON
RETURN
readTemperature:
BANKSEL ADCON0
MOVLW TEMP_SENSOR_CHANNEL
MOVWF ADCON0
BSF ADCON0, GO_DONE
readLoop:
BTFSC ADCON0, GO_DONE
GOTO readLoop
MOVF ADRESH, W
MOVWF TEMP_TEMP
MOVLW 0x00
MOVWF TEMP_RESULT
MOVLW 0x01
SUBWF ADRESL, W
BTFSC STATUS, C
INCF TEMP_RESULT, F
RETURN
controlThermostat:
BANKSEL TEMP_TEMP
MOVF TEMP_TEMP, W
SUBWF TEMP_RESULT, W
BTFSS STATUS, Z
GOTO tempHigh
BCF THERMOSTAT_PIN
GOTO tempDone
tempHigh:
BSF THERMOSTAT_PIN
tempDone:
RETURN
END
ERRORS