Close
Login to Your Account
Faadooengineers




Results 1 to 2 of 2

Thread: Microcontroller based automatic room light controller

  1. #1
    Fuchcha FaaDoO Engineer
    Join Date
    Aug 2012
    Posts
    3

    Gender: : Male

    Branch: : Electronics Engineering

    City : Jamnagar

    Send Private Message
    Send Friend Request

    Database 32 Microcontroller based automatic room light controller

    INCLUDE REG_52.PDF ; ***LCD CONTROL*** LCD_RS EQU P0.0 ;LCD REGISTER SELECT LINE LCD_E EQU P0.1 ;LCD ENABLE LINE LCD_DB4 EQU P0.2 ;PORT 1 IS USED FOR DATA LCD_DB5 EQU P0.3 ;USED FOR DATA LCD_DB6 EQU P0.4 ;FOR DATA LCD_DB7 EQU P0.5 ;FOR DATA ; ***CURSOR CONTROL INSTRUCTIONS*** OFFCUR EQU 0CH BLINKCUR EQU 0DH ; ***DISPLAY CONTROL INSTRUCTIONS*** CLRDSP EQU 01H ONDSP EQU 0CH ; ***SYSTEM INSTRUCTIONS*** CONFIG EQU 28H ; 4-BIT DATA,2 LINES,5X7 MATRIX LCD ENTRYMODE EQU 6 ; INCREMENT CURSOR DON'T SHIFT DISPLAY RELAY EQU P0.7 LED EQU P3.6 PLUS EQU P1.0 MINUSS EQU P1.1 PLUS1 EQU P1.2 MINUS1 EQU P1.3 SDA1 EQU P2.7 ;SDA=PIN5 SCL1 EQU P2.6 ;SCL=PIN6 WTCMD EQU 10100000B ;WRITE DATA COMMAND Note 3 RDCMD EQU 10100001B ;READ DATA COMMAND Note 3 SENSOR1 EQU P3.2 RB0 EQU 000H ; Select Register Bank 0 RB1 EQU 008H ; Select Register Bank 1 ...poke to PSW to use DSEG ; This is internal data memory ORG 20H ; Bit adressable memory ORG 20H ; Bit adressable memory SENP: DS 1 SENP1 BIT SENP.0 MINUS BIT SENP.1 MIN1 BIT SENP.2 MIN2 BIT SENP.3 TEMP1: DS 1 TEMP1D: DS 1 RAM: DS 9 TEP1: DS 1 TEP2: DS 1 READING: DS 2 STACK: DS 1 CSEG AT 0 ; RESET VECTOR ;---------==========----------==========---------=========--------- ; PROCESSOR INTERRUPT AND RESET VECTORS ;---------==========----------==========---------=========--------- ORG 00H ; Reset MOV PSW,#RB0 ; Select register bank 0 MOV SP,#STACK CLR RELAY CALL RESETLCD4 CALL TITLES SETB PLUS SETB MINUSS SETB PLUS1 SETB MINUS1 CLR MIN2 SETB LED ; CALL DELAYS CALL DELAYS ;################################################## ; READ AND RESTORE THE VALUES FROM EEPROM ;################################################## MOV R1,#READING ;GET DATA IN BYTES(RAM) MOV R4,#10H ;DATA ADDRESS IN EEPROM MOV R6,#2 ;NUMBER OF BYTES CALL READ_EEPROM CALL DELAY MOV A,READING CJNE A,#0FFH,NO_RESET CALL RESET_VALUE ; CALL DATA_RESTORED CALL DELAYS NO_RESET: MOV R1,#READING ;GET DATA IN BYTES(RAM) MOV R4,#10H ;DATA ADDRESS IN EEPROM MOV R6,#2 ;NUMBER OF BYTES CALL READ_EEPROM ;################################################## UPP: CALL READ_TEMP1 CALL DISPLAY1 JNB SENP1,NOT_OK CALL DISPLAY2 CALL DELAYS AJMP UPP NOT_OK: MOV A,TEMP1 ;DISPLAY HIGHER DIGITS FOR SENSOR 1 ANL A,#0F0H SWAP A ADD A,#30H MOV R4,A CALL WRLCDDATA CALL MDELAY MOV A,TEMP1 ANL A,#0FH ADD A,#30H MOV R4,A CALL WRLCDDATA CALL MDELAY MOV R4,#"." CALL WRLCDDATA CALL MDELAY MOV A,TEMP1D ;DISPLAY DECIMAL DIGITS FOR SENSOR 1 ANL A,#0F0H SWAP A ADD A,#30H MOV R4,A CALL WRLCDDATA CALL MDELAY CALL DISPLAY3 MOV A,READING ;DISPLAY HIGHER DIGITS FOR SENSOR 1 ANL A,#0F0H SWAP A ADD A,#30H MOV R4,A CALL WRLCDDATA CALL MDELAY MOV A,READING ANL A,#0FH ADD A,#30H MOV R4,A CALL WRLCDDATA CALL MDELAY CALL DISPLAY4 MOV A,READING+1 ;DISPLAY HIGHER DIGITS FOR SENSOR 1 ANL A,#0F0H SWAP A ADD A,#30H MOV R4,A CALL WRLCDDATA CALL MDELAY MOV A,READING+1 ANL A,#0FH ADD A,#30H MOV R4,A CALL WRLCDDATA CALL MDELAY MOV R0,#05H RAPA3: MOV R1,#0FFH RAPA2: MOV R2,#0FFH RAPA1: JNB PLUS,DCF1 JNB MINUSS,DCF2 JNB PLUS1,DCF3S JNB MINUS1,DCF4S DJNZ R2,RAPA1 DJNZ R1,RAPA2 DJNZ R0,RAPA3 JNB MIN2,UPA MOV R1,#READING ;store READING MOV R4,#10H ;Starting Address IN EEPROM MOV R6,#2 ;STORE 2 BYTES CALL STORE_EEPROM CALL DELAY CALL DATA_STORED CALL DELAYS CLR MIN2 UPA: MOV A,TEMP1 CLR C SUBB A,READING JNC DCW1 CLR RELAY SETB LED DCW1: MOV A,TEMP1 CLR C SUBB A,READING+1 JC DCW2 SETB RELAY CLR LED DCW2: AJMP UPP DCF4S: AJMP DCF4 DCF3S: AJMP DCF3 ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ DCF1: MOV A,READING CJNE A,#99H,DCFF1 JNB PLUS,$ CALL DELAY AJMP UPP DCFF1:CJNE A,READING+1,DCFF22 JNB PLUS,$ CALL DELAY AJMP UPP DCFF22: ADD A,#01H DA A MOV READING,A JNB PLUS,$ CALL DELAY SETB MIN2 AJMP UPP DCF2: MOV A,READING CJNE A,#00H,DCFF2 JNB MINUSS,$ CALL DELAY AJMP UPP DCFF2: DEC A MOV R1,A ANL A,#0FH CJNE A,#0FH,SDX1 CLR C MOV A,R1 SUBB A,#06 MOV R1,A SDX1: MOV READING,R1 JNB MINUSS,$ SETB MIN2 CALL DELAY AJMP UPP ;************************************************** ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ DCF3: MOV A,READING+1 CJNE A,#99H,DCFF3 JNB PLUS1,$ CALL DELAY AJMP UPP DCFF3:ADD A,#01H DA A MOV READING+1,A JNB PLUS1,$ SETB MIN2 CALL DELAY AJMP UPP DCF4: MOV A,READING+1 CJNE A,#00H,DCFF4 JNB MINUS1,$ CALL DELAY AJMP UPP DCFF4:CJNE A,READING,DCFF44 JNB MINUS1,$ CALL DELAY AJMP UPP DCFF44: DEC A MOV R1,A ANL A,#0FH CJNE A,#0FH,SDX2 CLR C MOV A,R1 SUBB A,#06 MOV R1,A SDX2: MOV READING+1,R1 JNB MINUS1,$ SETB MIN2 CALL DELAY AJMP UPP ;************************************************** ;########################################################## ; DISPLAY ROUTINES ;########################################################## TITLES: MOV DPTR,#MSAG CALL LCD_MSG RET MSAG: DB 1H,82H,'Temperature',0C3H,'Controller',00H ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DISPLAY1: MOV DPTR,#MSAG1 CALL LCD_MSG RET MSAG1: DB 1H,80H,'Temperature:',00H ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DISPLAY2: MOV DPTR,#MSAG2 CALL LCD_MSG RET MSAG2: DB 1H,80H,'** NO SENSOR **',00H ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DISPLAY3: MOV DPTR,#MSAG3 CALL LCD_MSG RET MSAG3: DB 0C0H,'Low:',00H ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DISPLAY4: MOV DPTR,#MSAG4 CALL LCD_MSG RET MSAG4: DB 0C9H,'High:',00H ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DATA_STORED: MOV DPTR,#MSAG5 CALL LCD_MSG RET MSAG5: DB 1H,82H,'Data Stored..',00H ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DELAYS: ;One second delay routine MOV R6, #00H ;put 0 in register R6 (R6 = 0) MOV R5, #004H ;put 5 in register R5 (R5 = 4) LOOPB: INC R6 ;increase R6 by one (R6 = R6 +1) ACALL DELAYMS ;call the routine above. It will run and return to here. MOV A, R6 ;move value in R6 to A JNZ LOOPB ;if A is not 0, go to LOOPB DEC R5 ;decrease R5 by one. (R5 = R5 -1) MOV A, R5 ;move value in R5 to A JNZ LOOPB ;if A is not 0 then go to LOOPB. RET ;************************************************************************** DELAYMS: ;millisecond delay routine ; ; MOV R7,#00H ;put value of 0 in register R7 LOOPA: INC R7 ;increase R7 by one (R7 = R7 +1) MOV A,R7 ;move value in R7 to Accumlator (also known as A) CJNE A,#0FFH,LOOPA ;compare A to FF hex (256). If not equal go to LOOPA RET ;return to the point that this routine was called from ;************************************************************************** ;********************************************************** DELAY: MOV R1,#0FFH REPA2: MOV R2,#0FFH REPA1: NOP DJNZ R2,REPA1 DJNZ R1,REPA2 RET ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ;********************************************************** ; INITIALIZE THE LCD 4-BIT MODE ;********************************************************** INITLCD4: CLR LCD_RS ; LCD REGISTER SELECT LINE CLR LCD_E ; ENABLE LINE MOV R4, #CONFIG; FUNCTION SET - DATA BITS, ; LINES, FONTS CALL WRLCDCOM4 MOV R4, #ONDSP ; DISPLAY ON CALL WRLCDCOM4 MOV R4, #ENTRYMODE ; SET ENTRY MODE CALL WRLCDCOM4 ; INCREMENT CURSOR RIGHT, NO SHIFT MOV R4, #CLRDSP; CLEAR DISPLAY, HOME CURSOR CALL WRLCDCOM4 RET ; ********************************************************** ; SOFTWARE VERSION OF THE POWER ON RESET ; ********************************************************** RESETLCD4: CLR LCD_RS ; LCD REGISTER SELECT LINE CLR LCD_E ; ENABLE LINE CLR LCD_DB7 ; SET BIT PATTERN FOR... CLR LCD_DB6 ; ... POWER-ON-RESET SETB LCD_DB5 SETB LCD_DB4 SETB LCD_E ; START ENABLE PULSE CLR LCD_E ; END ENABLE PULSE MOV A, #4 ; DELAY 4 MILLISECONDS CALL MDELAY SETB LCD_E ; START ENABLE PULSE CLR LCD_E ; END ENABLE PULSE MOV A, #1 ; DELAY 1 MILLISECOND CALL MDELAY SETB LCD_E ; START ENABLE PULSE CLR LCD_E ; END ENABLE PULSE MOV A, #1 ; DELAY 1 MILLISECOND CALL MDELAY CLR LCD_DB4 ; SPECIFY 4-BIT OPERATION SETB LCD_E ; START ENABLE PULSE CLR LCD_E ; END ENABLE PULSE MOV A, #1 ; DELAY 1 MILLISECOND CALL MDELAY MOV R4, #CONFIG; FUNCTION SET CALL WRLCDCOM4 MOV R4, #08H ; DISPLAY OFF CALL WRLCDCOM4 MOV R4, #1 ; CLEAR DISPLAY, HOME CURSOR CALL WRLCDCOM4 MOV R4,#ENTRYMODE ; SET ENTRY MODE ACALL WRLCDCOM4 JMP INITLCD4 ; ********************************************************** ; SUB RECEIVES A COMMAND WORD TO THE LCD ; COMMAND MUST BE PLACED IN R4 BY CALLING PROGRAM ; ********************************************************** WRLCDCOM4: CLR LCD_E CLR LCD_RS ; SELECT READ COMMAND PUSH ACC ; SAVE ACCUMULATOR MOV A, R4 ; PUT DATA BYTE IN ACC MOV C, ACC.4 ; LOAD HIGH NIBBLE ON DATA BUS MOV LCD_DB4, C ; ONE BIT AT A TIME USING... MOV C, ACC.5 ; BIT MOVE OPERATOINS MOV LCD_DB5, C MOV C, ACC.6 MOV LCD_DB6, C MOV C, ACC.7 MOV LCD_DB7, C SETB LCD_E ; PULSE THE ENABLE LINE CLR LCD_E MOV C, ACC.0 ; SIMILARLY, LOAD LOW NIBBLE MOV LCD_DB4, C MOV C, ACC.1 MOV LCD_DB5, C MOV C, ACC.2 MOV LCD_DB6, C MOV C, ACC.3 MOV LCD_DB7, C CLR LCD_E SETB LCD_E ; PULSE THE ENABLE LINE CLR LCD_E CALL MADELAY POP ACC RET ; ********************************************************** ; SUB TO RECEIVE A DATA WORD TO THE LCD ; DATA MUST BE PLACED IN R4 BY CALLING PROGRAM ; ********************************************************** WRLCDDATA: CLR LCD_E SETB LCD_RS ; SELECT READ DATA PUSH ACC ; SAVE ACCUMULATOR MOV A, R4 ; PUT DATA BYTE IN ACC MOV C, ACC.4 ; LOAD HIGH NIBBLE ON DATA BUS MOV LCD_DB4, C ; ONE BIT AT A TIME USING... MOV C, ACC.5 ; BIT MOVE OPERATOINS MOV LCD_DB5, C MOV C, ACC.6 MOV LCD_DB6, C MOV C, ACC.7 MOV LCD_DB7, C SETB LCD_E ; PULSE THE ENABLE LINE CLR LCD_E MOV C, ACC.0 ; SIMILARLY, LOAD LOW NIBBLE MOV LCD_DB4, C MOV C, ACC.1 MOV LCD_DB5, C MOV C, ACC.2 MOV LCD_DB6, C MOV C, ACC.3 MOV LCD_DB7, C CLR LCD_E SETB LCD_E ; PULSE THE ENABLE LINE CLR LCD_E NOP NOP POP ACC RET ; ********************************************************** ; SUB TAKES THE STRING IMMEDIATELY FOLLOWING THE CALL AND ; DISPLAYS ON THE LCD. STRING MUST BE TERMINATED WITH A ; NULL (0). ; ********************************************************** LCD_MSG: CLR A ; Clear Index MOVC A,@A+DPTR ; Get byte pointed by Dptr INC DPTR ; Point to the next byte JZ LCD_Msg9 ; Return if found the zero (end of stringz) CJNE A,#01H,Lcd_Msg1 ; Check if is a Clear Command MOV R4,A CALL WRLCDCOM4 ;If yes, RECEIVE it as command to LCD JMP LCD_MSG ;Go get next byte from stringz Lcd_Msg1: CJNE A,#0FFH,FLL ;Check for displaying full character MOV R4,A CALL WRLCDDATA JMP LCD_MSG FLL: CJNE A,#080h,$+3 ; Data or Address? If => 80h then is address. JC Lcd_Msg_Data ; Carry will be set if A < 80h (Data) MOV R4,A CALL WRLCDCOM4 ; Carry not set if A=>80, it is address JMP Lcd_Msg ; Go get next byte from stringz Lcd_Msg_Data: ; MOV R4,A CALL WRLCDDATA ; It was data, RECEIVE it to Lcd JMP Lcd_Msg ; Go get next byte from stringz Lcd_Msg9: RET ; Return to Caller ; ********************************************************** ; 1 MILLISECOND DELAY ROUTINE ; ********************************************************** MDELAY: PUSH ACC MOV A,#0A6H MD_OLP: INC A NOP NOP NOP NOP NOP NOP NOP NOP JNZ MD_OLP NOP POP ACC RET MADELAY: PUSH ACC MOV A,#036H MAD_OLP: INC A NOP NOP NOP NOP NOP NOP NOP NOP JNZ MAD_OLP NOP POP ACC RET ;*********************************************************************** ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ; SENSOR 1 ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ READ_TEMP1: CLR SENP1 ;ASSUME ALL SENSORS OK MOV A,#0CCH ;SKIP ROM CALL TEWB MOV A,#044H ;READ CONVERT CALL TEWB CALL TERES CALL TECHK JC ASX1 ;JMP IF sensor PRESENT MOV TEMP1,#0FFH MOV TEMP1D,#0FFH SETB SENP1 ;SET FOR NO SENSOR JMP DCFG1 ASX1: MOV R2,#0 DJNZ R2,$ CALL TERES CALL TEPRE MOV R2,#0 DJNZ R2,$ MOV A,#0CCH ;SKIP ROM CALL TEWB MOV A,#0BEH ;READ SCRATCHPAD CALL TEWB MOV R6,#9 MOV R0,#RAM TCOTEM2: CALL TERB ;Read Temperature from SCRATCHPAD MOV @R0,A INC R0 DJNZ R6,TCOTEM2 CALL TERES ;RESET CALL TEPRE CALL ds1820crc_ok JNC DCFG1 ;jump if CRC Check failure CALL CALCULATE MOV A,TEP1 CJNE A,#85H,REP1 JMP READ_TEMP1 REP1: MOV TEMP1,TEP1 MOV TEMP1D,TEP2 CLR MIN1 ;ASSUME +VE MOV A,RAM+1 JNB ACC.0,DCFG1 SETB MIN1 ;minus temp DCFG1: RET ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; CALCULATE TEMP FOR HIGH RESOLUTION ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CALCULATE: MOV A,RAM+1 ;MSB restore RRC A MOV MINUS,C JNB MINUS,DLC4 MOV A,RAM CPL A INC A MOV RAM,A DLC4: MOV A,RAM ANL A,#01H JZ DLC2 DEC RAM DLC2: MOV A,RAM MOV B,#50 MUL AB ADD A,#25 MOV R6,A MOV A,B ADDC A,#00H MOV R7,A ;LSB=R11 ,MSB=R12 MOV A,RAM+7 CLR C SUBB A,RAM+6 MOV B,#100 MUL AB ANL A,#0F0H SWAP A MOV TEP1,A MOV A,B ANL A,#0FH SWAP A ORL A,TEP1 ADD A,R6 MOV R6,A MOV A,R7 ADDC A,#00H MOV R7,A MOV A,R6 MOV R2,A ;CONVERT HEX VALUE TO BCD MOV A,R7 MOV R1,A MOV R3,#00D MOV R4,#00D MOV R5,#00D MOV R6,#00D CALL HEX2BCD MOV A,R4 SWAP A ORL A,R3 MOV TEP2,A MOV A,R6 SWAP A ORL A,R5 MOV TEP1,A RET ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DS1820CRC_OK: ; EXTERN BIT DS1820CRC_OK(VOID); MOV R5, #9 ; 8+1 BYTE CRC MOV R7, #0 ; CLEAR CRC REGISTER MOV DPTR, #DOWCRC_TABLE MOV R0, #RAM CRC: MOV A, @R0 ; SCRATCHAD VALUE XRL A, R7 MOVC A, @A+DPTR MOV R7, A INC R0 DJNZ R5, CRC JZ OK CLR CY RET OK: SETB CY RET ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% dowcrc_table: DB 0, 94, 188, 226, 97, 63, 221, 131 DB 194, 156, 126, 32, 163, 253, 31, 65 DB 157, 195, 33, 127, 252, 162, 64, 30 DB 95, 1, 227, 189, 62, 96, 130, 220 DB 35, 125, 159, 193, 66, 28, 254, 160 DB 225, 191, 93, 3, 128, 222, 60, 98 DB 190, 224, 2, 92, 223, 129, 99, 61 DB 124, 34, 192, 158, 29, 67, 161, 255 DB 70, 24, 250, 164, 39, 121, 155, 197 DB 132, 218, 56, 102, 229, 187, 89, 7 DB 219, 133, 103, 57, 186, 228, 6, 88 DB 25, 71, 165, 251, 120, 38, 196, 154 DB 101, 59, 217, 135, 4, 90, 184, 230 DB 167, 249, 27, 69, 198, 152, 122, 36 DB 248, 166, 68, 26, 153, 199, 37, 123 DB 58, 100, 134, 216, 91, 5, 231, 185 DB 140, 210, 48, 110, 237, 179, 81, 15 DB 78, 16, 242, 172, 47, 113, 147, 205 DB 17, 79, 173, 243, 112, 46, 204, 146 DB 211, 141, 111, 49, 178, 236, 14, 80 DB 175, 241, 19, 77, 206, 144, 114, 44 DB 109, 51, 209, 143, 12, 82, 176, 238 DB 50, 108, 142, 208, 83, 13, 239, 177 DB 240, 174, 76, 18, 145, 207, 45, 115 DB 202, 148, 118, 40, 171, 245, 23, 73 DB 8, 86, 180, 234, 105, 55, 213, 139 DB 87, 9, 235, 181, 54, 104, 138, 212 DB 149, 203, 41, 119, 244, 170, 72, 22 DB 233, 183, 85, 11, 136, 214, 52, 106 DB 43, 117, 151, 201, 74, 20, 246, 168 DB 116, 42, 200, 150, 21, 75, 169, 247 DB 182, 232, 10, 84, 215, 137, 107, 53 ;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ; SENSOR 1 ;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ; ********** TEPRE SUB ********** ; TEMP MEMORY PRESENCE ; REG = R2 TEPRE: JB SENSOR1,$ ;PRESENCE (RX) JNB SENSOR1,$ MOV R2,#8 DJNZ R2,$ RET ; ********** TERES SUB ********** ; TEMP MEMORY RESET ; REG = R2 TERES: CLR SENSOR1 ;RESET (TX) MOV R2,#200 ;APP. 900 uS TERES1: NOP NOP DJNZ R2,TERES1 SETB SENSOR1 MOV R2,#8 DJNZ R2,$ RET ; ********** TECHK SUB ********** ; TEMP MEMORY CHECK PRESENCE AT FIRST ; OUT = CY 0=NO 1=OK ; REG = R2,R3 TECHK: MOV R2,#8 TECHK1: MOV R3,#0 TECHK2: JNB SENSOR1,TECHK5 ;PRESENCE (RX) DJNZ R3,TECHK2 DJNZ R2,TECHK1 CLR C RET TECHK5: JNB SENSOR1,$ MOV R2,#8 DJNZ R2,$ SETB C RET ; ******************************************************** ; TEMP MEMORY WRITE BYTE ; IN = A ; REG = A,R2,R3 ; ******************************************************** TEWB: MOV R3,#8 ;WRITE BYTE TEWB1: RRC A JC TEWB2 CALL TEW0 SJMP TEWB3 TEWB2: CALL TEW1 TEWB3: DJNZ R3,TEWB1 RET TEW0: CLR SENSOR1 ;WRITE 0 MOV R2,#15 ;65 uS TEW01: NOP NOP DJNZ R2,TEW01 SETB SENSOR1 NOP ;4 uS NOP NOP NOP RET TEW1: CLR SENSOR1 ;WRITE 1 NOP ;4 uS NOP NOP NOP SETB SENSOR1 MOV R2,#15 ;65 uS TEW11: NOP NOP DJNZ R2,TEW11 RET ; ********** TERB SUB ********** ; TEMP MEMORY READ BYTE ; OUT = A ; REG = A,R2,R3 TERB: MOV R3,#8 ;READ BYTE TERB1: CALL TEBIT RRC A DJNZ R3,TERB1 RET TEBIT: CLR SENSOR1 ;READ BIT NOP NOP SETB SENSOR1 NOP NOP NOP NOP MOV C,SENSOR1 MOV R2,#15 ;65 uS TEBIT2: NOP NOP DJNZ R2,TEBIT2 RET ;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ;************************************************** HEX2BCD: MOV B,#10D MOV A,R2 DIV AB MOV R3,B ; MOV B,#10 ; R7,R6,R5,R4,R3 DIV AB MOV R4,B MOV R5,A CJNE R1,#0H,HIGH_BYTE ; CHECK FOR HIGH BYTE SJMP ENDD HIGH_BYTE: MOV A,#6 ADD A,R3 MOV B,#10 DIV AB MOV R3,B ADD A,#5 ADD A,R4 MOV B,#10 DIV AB MOV R4,B ADD A,#2 ADD A,R5 MOV B,#10 DIV AB MOV R5,B CJNE R6,#00D,ADD_IT SJMP CONTINUE ADD_IT: ADD A,R6 CONTINUE: MOV R6,A DJNZ R1,HIGH_BYTE MOV B, #10D MOV A,R6 DIV AB MOV R6,B MOV R7,A ENDD: RET ;*************************************************************************** ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ; READ DATA FROM EEPROM ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% READ_EEPROM: MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS CALL OUTS ;SEND IT MOV A,R4 ;GET LOW BYTE ADDRESS CALL OUT ;SEND IT MOV A,#RDCMD ;LOAD READ COMMAND CALL OUTS ;SEND IT BRDLP: CALL IN ;READ DATA MOV @R1,a ;STORE DATA INC R1 ;INCREMENT DATA POINTER DJNZ R6,AKLP ;DECREMENT LOOP COUNTER CALL STOP ;IF DONE, ISSUE STOP CONDITION RET ;DONE, EXIT ROUTINE AKLP: CLR SDA1 ;NOT DONE, ISSUE ACK SETB SCL1 NOP ;NOTE 1 NOP NOP NOP ;NOTE 2 NOP CLR SCL1 JMP BRDLP ;CONTINUE WITH READS ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ; STORE DATA IN EEPROM ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STORE_EEPROM: MOV A,#WTCMD ;LOAD WRITE COMMAND CALL OUTS ;SEND IT MOV A,R4 ;GET LOW BYTE ADDRESS CALL OUT ;SEND IT BTLP: MOV A,@R1 ;GET DATA CALL OUT ;SEND IT INC R1 ;INCREMENT DATA POINTER DJNZ R6,BTLP ;LOOP TILL DONE CALL STOP ;SEND STOP CONDITION RET ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ;*********************************************************************** ; THIS ROUTINE SENDS OUT CONTENTS OF THE ACCUMULATOR ; to the EEPROM and includes START condition. Refer to the data sheets ; for discussion of START and STOP conditions. ;*********************************************************************** OUTS: MOV R2,#8 ;LOOP COUNT -- EQUAL TO BIT COUNT SETB SDA1 ;INSURE DATA IS HI SETB SCL1 ;INSURE CLOCK IS HI NOP ;NOTE 1 NOP NOP CLR SDA1 ;START CONDITION -- DATA = 0 NOP ;NOTE 1 NOP NOP CLR SCL1 ;CLOCK = 0 OTSLP: RLC A ;SHIFT BIT JNC BITLS SETB SDA1 ;DATA = 1 JMP OTSL1 ;CONTINUE BITLS: CLR SDA1 ;DATA = 0 OTSL1: SETB SCL1 ;CLOCK HI NOP ;NOTE 1 NOP NOP CLR SCL1 ;CLOCK LOW DJNZ R2,OTSLP ;DECREMENT COUNTER SETB SDA1 ;TURN PIN INTO INPUT NOP ;NOTE 1 SETB SCL1 ;CLOCK ACK NOP ;NOTE 1 NOP NOP CLR SCL1 RET ;********************************************************************** ; THIS ROUTINE SENDS OUT CONTENTS OF ACCUMLATOR TO EEPROM ; without sending a START condition. ;********************************************************************** OUT: MOV R2,#8 ;LOOP COUNT -- EQUAL TO BIT COUNT OTLP: RLC A ;SHIFT BIT JNC BITL SETB SDA1 ;DATA = 1 JMP OTL1 ;CONTINUE BITL: CLR SDA1 ;DATA = 0 OTL1: SETB SCL1 ;CLOCK HI NOP ;NOTE 1 NOP NOP CLR SCL1 ;CLOCK LOW DJNZ R2,OTLP ;DECREMENT COUNTER SETB SDA1 ;TURN PIN INTO INPUT NOP ;NOTE 1 SETB SCL1 ;CLOCK ACK NOP ;NOTE 1 NOP NOP CLR SCL1 RET STOP: CLR SDA1 ;STOP CONDITION SET DATA LOW NOP ;NOTE 1 NOP NOP SETB SCL1 ;SET CLOCK HI NOP ;NOTE 1 NOP NOP SETB SDA1 ;SET DATA HIGH RET ;******************************************************************* ; THIS ROUTINE READS A BYTE OF DATA FROM EEPROM ; From EEPROM current address pointer. ; Returns the data byte in R1 ;******************************************************************* CREAD: MOV A,#RDCMD ;LOAD READ COMMAND CALL OUTS ;SEND IT CALL IN ;READ DATA MOV R1,A ;STORE DATA CALL STOP ;SEND STOP CONDITION RET ;********************************************************************** ; THIS ROUTINE READS IN A BYTE FROM THE EEPROM ; and stores it in the accumulator ;********************************************************************** IN: MOV R2,#8 ;LOOP COUNT SETB SDA1 ;SET DATA BIT HIGH FOR INPUT INLP: CLR SCL1 ;CLOCK LOW NOP ;NOTE 1 NOP NOP NOP SETB SCL1 ;CLOCK HIGH CLR C ;CLEAR CARRY JNB SDA1,INL1 ;JUMP IF DATA = 0 CPL C ;SET CARRY IF DATA = 1 INL1: RLC A ;ROTATE DATA INTO ACCUMULATOR DJNZ R2,INLP ;DECREMENT COUNTER CLR SCL1 ;CLOCK LOW RET ;********************************************************************* ; This routine test for WRITE DONE condition ; by testing for an ACK. ; This routine can be run as soon as a STOP condition ; has been generated after the last data byte has been sent ; to the EEPROM. The routine loops until an ACK is received from ; the EEPROM. No ACK will be received until the EEPROM is done with ; the write operation. ;********************************************************************* ACKTST: MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS MOV R2,#8 ;LOOP COUNT -- EQUAL TO BIT COUNT CLR SDA1 ;START CONDITION -- DATA = 0 NOP ;NOTE 1 NOP NOP CLR SCL1 ;CLOCK = 0 AKTLP: RLC A ;SHIFT BIT JNC AKTLS SETB SDA1 ;DATA = 1 JMP AKTL1 ;CONTINUE AKTLS: CLR SDA1 ;DATA = 0 AKTL1: SETB SCL1 ;CLOCK HI NOP ;NOTE 1 NOP NOP CLR SCL1 ;CLOCK LOW DJNZ R2,AKTLP ;DECREMENT COUNTER SETB SDA1 ;TURN PIN INTO INPUT NOP ;NOTE 1 SETB SCL1 ;CLOCK ACK NOP ;NOTE 1 NOP NOP JNB SDA1,EXIT ;EXIT IF ACK (WRITE DONE) JMP ACKTST ;START OVER EXIT: CLR SCL1 ;CLOCK LOW CLR SDA1 ;DATA LOW NOP ;NOTE 1 NOP NOP SETB SCL1 ;CLOCK HIGH NOP NOP SETB SDA1 ;STOP CONDITION RET ;********************************************************************* RESET_VALUE: MOV READING,#30H MOV READING+1,#40H MOV R1,#READING ;store READING MOV R4,#10H ;Starting Address IN EEPROM MOV R6,#2 ;STORE 2 BYTES CALL STORE_EEPROM CALL DELAY CALL DELAY RET END



  2. #2
    Fuchcha FaaDoO Engineer
    Join Date
    Sep 2012
    Posts
    1

    Gender: : Male

    Branch: : Electronics Engineering

    City : Hisar

    Send Private Message
    Send Friend Request

    Re: Microcontroller based automatic room light controller

    very useful contents.Acurate