;Control LCD & Relays using Bluetooth . Servo code for future - code untested. ;Based on LCD test code - some code is not used! #INCLUDE __CONFIG _DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_ON&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC ;THE configure of MCU,watchdog OFF,electrify delay OPEN,power down check OFF, ;LOW power programme OFF,encrypt,4M crystal HS surge. Z EQU 2 ;define Z status's address RP0 EQU 5 ;define RP0's address ;#DEFINE RS PORTC,0 ;#DEFINE RW PORTC,1 ;#DEFINE E PORTC,4 #DEFINE RS PORTA,1 #DEFINE RW PORTA,2 #DEFINE E PORTA,3 ;********************* ;RS EQU 1 ; ;RW EQU 2 ; ;E EQU 3 ; COUNT2 EQU 27H ;define count reg address TMP1 EQU 28H ;define temp count reg. COUNT3 EQU 34H ;define count reg address TMP3 EQU 35H ;define temp count reg. VAL_US EQU .249 ;short delay parameter VAL_MS EQU .200 ;long delay parameter COUNT EQU 25H COUNT_MS EQU 26H ORG 00H NOP NOP NOP GOTO MAIN ORG 04H ;interrupt entrance address BTFSS PIR1,RCIF ;judge if COMM receive interrupt GOTO nextint ;no, jump to interrupt end program MOVF RCREG,0 ;read data of receive register goto after ;;;;;;;;;;;;;;;;;;;;;;;;; ; nextint btfss INTCON,2 goto EXIT call serv goto EXIT after ;;;;;;;;;;;;;;;;;;;;;;;;; movwf 2Ah ;temp store for rx buffer isittilde ; ~ stop servo control movf 02Ah,0 XORLW 07eH ;~ BTFSS STATUS,Z goto isitcaret clrf 02bh ; clr the last ctrl BCF INTCON,5h ; timer0 int DISable goto EXIT isitcaret ; will be used control servo motor movf 02Ah,0 XORLW 05eH ;^ BTFSS STATUS,Z goto testcarat ;isithash incf 030h BSF INTCON,5h ; timer0 int enable movf 02ah,0 ; tmp store of last control character movwf 02bh movlw 02h movwf 02eh goto EXIT testcarat movf 02bh,0 ;was prec cont char ^ XORLW 05eh BTFSS STATUS,Z goto isithash ;clrit ; was clrit movf 02ah,0 ;delay value for servo delay movwf 03fh goto EXIT isithash movf 02Ah,0 XORLW 023H ;# BTFSS STATUS,Z goto testhash movf 02ah,0 movwf 02bh movlw 02h movwf 02eh ;call Line2 goto EXIT testhash D3 movf PORTB,0 movwf 03ah ; temp copy of portb ;set bit on port D if prevoius char was a # movf 02bh,0 XORLW 023h BTFSS STATUS,Z goto clrit movf 02Ah,0 ANDLW 0fh MOVwf 02ch movf 02ch,1 movlw 01h movwf 02dh dex RLF 02dh, 1 DECFSZ 02ch, 1 goto dex RRF 02dh, 1 movf 02dh,0 XORWF PORTB,1 ; update relays ;;===================transmit relay info ======================= movf 02Ah,0 ANDLW 0fh movwf 03dh b0 movf 03dh,0 XORLW 01h BTFSS STATUS,Z goto b1 movf 02Ah,0 btfss PORTB,0 ADDLW 10h call txit b1 movf 03dh,0 XORLW 02h BTFSS STATUS,Z goto b2 movf 02Ah,0 btfss PORTB,1 ADDLW 10h call txit b2 movf 03dh,0 XORLW 03h BTFSS STATUS,Z goto b3 movf 02Ah,0 btfss PORTB,2 ADDLW 10h call txit b3 movf 03dh,0 XORLW 04h BTFSS STATUS,Z goto b4 movf 02Ah,0 btfss PORTB,3 ADDLW 10h call txit b4 movf 03dh,0 XORLW 05h BTFSS STATUS,Z goto b5 movf 02Ah,0 btfss PORTB,4 ADDLW 10h call txit b5 movf 03dh,0 XORLW 06h BTFSS STATUS,Z goto b6 movf 02Ah,0 btfss PORTB,5 ADDLW 10h call txit b6 movf 03dh,0 XORLW 07h BTFSS STATUS,Z goto b7 movf 02Ah,0 btfss PORTB,6 ADDLW 10h call txit b7 movf 03dh,0 XORLW 08h BTFSS STATUS,Z goto b8 movf 02Ah,0 btfss PORTB,7 ADDLW 10h call txit b8 goto EXIT clrit movfw 02Ah XORLW 01H ; BTFSS STATUS,Z goto Homeit MOVLW 01H MOVWF PORTD ;clr screen CALL ENABLE CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY CALL DELAY clrf 02fh ; clr char counter goto EXIT Homeit movfw 02Ah XORLW 02H ; BTFSS STATUS,Z goto DISP MOVLW 02H MOVWF PORTD ;home screen CALL ENABLE CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY clrf 02fh ; clr char counter goto EXIT DISP ;it was not FF or FE so we can Display the data movfw 02Ah MOVWF PORTD CALL WRITE3 ;MOVWF TXREG ;keep in send register and send EXIT RETFIE infobak ADDWF PCL,1 ;address offset add the current pc. RETLW 20h RETLW 41H ;A RETLW 42H ;B RETLW 43H ;C RETLW 44H ;D RETLW 45H ;E RETLW 46H ;F RETLW 47H ;G RETLW 48h ;H RETLW 00H TABLE ;display version ADDWF PCL,1 ;address offset add the current pc. RETLW 20H ;space RETLW 56H ;V RETLW 65H ;e RETLW 72H ;r RETLW 73H ;s RETLW 69H ;i RETLW 6fH ;o RETLW 6eH ;n RETLW 20H ;space RETLW 33H ;3 RETLW 2eH ;. RETLW 31H ;1 RETLW 30H ;0 RETLW 30H ;0 RETLW 30H ;0 RETLW 61H ;0 RETLW 00H ;-------------------- ---------------------------------- TABLE1 ;display terry reynolds ADDWF PCL,1 ;address offset add the current pc. RETLW 20H ;space RETLW 54H ;T RETLW 65H ;e RETLW 72H ;r RETLW 72H ;r RETLW 79H ;y RETLW 20H ;space RETLW 52H ;R RETLW 65H ;e RETLW 79H ;y RETLW 6eH ;n RETLW 6fH ;o RETLW 6cH ;l RETLW 64H ;d RETLW 73H ;s RETLW 00H ;-------------------- ---------------------------------- TABLE3 ADDWF PCL,1 ;address offset add the current pc. RETLW 20H ;space RETLW 54H ;T RETLW 65H ;e RETLW 72H ;r RETLW 72H ;r RETLW 79H ;y RETLW 00H ;-------------------- ---------------------------------- MAIN USART BANKSEL TRISC ;select BANK1 MOVLW 0FFH ;set RA5 OUTPUT,the other bits INPUT ;MOVWF TRISB BSF TRISC,7 ;disconnect RC and USART TX BSF TRISC,6 ;disconnect RC and USART RX BcF TRISC,0 BcF TRISC,1 BcF TRISC,2 ; bits 0,1,2 & 3 of portc out put to control relay BcF TRISC,3 ; BcF TRISC,4 MOVLW 0CH ;load band rate=9600 0c=19200 brgh high MOVWF SPBRG MOVLW 24H ;enable USART,set band rate is high MOVWF TXSTA ;movlw 090h ;movwf RCSTA BSF PIE1,RCIE ;enable receive interrupt MOVLW 00h MOVWF TRISB ;BANKSEL TRISD MOVLW 00h MOVWF TRISD CLRF STATUS ;select BANK0 movlw 090h ;090h =cont rx movwf RCSTA movlw 0ffh ; set all portB o/p high as relay is active low movwf PORTB CLRF PORTA BSF STATUS, RP0 MOVLW 0x06 MOVWF ADCON1 MOVLW 0f0h MOVWF TRISA movlw 06h movwf OPTION_REG CLRF STATUS ;select BANK0 movlw 0ffh movwf 03fh MOVLW 0f0h ;; timer0 movwf TMR0 CALL DELAY MOVLW 90H MOVWF RCSTA ;enable USART continue receive BSF INTCON,GIE ;enable general interrupt BSF INTCON,PEIE ;enable outside interrupt ;BSF INTCON,5h ; timer0 int enable ; start up value for servo movlw 0xC7 ; value from user after ^ : 1ms centre = C7 1.5ms right = B7 2.0ms left =8f movwf 03fh ;;;;;;;;;;;;;;;;;; ;the following 16 instructions to set lcd display mode. CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY ;delay for lcd is slower than mcu. MOVLW 30H MOVWF PORTD CALL ENABLE ;write command. MOVLW 30H MOVWF PORTD CALL ENABLE ;write command. MOVLW 30H MOVWF PORTD CALL ENABLE ;write command. MOVLW 01H MOVWF PORTD ;clr screen CALL ENABLE ;write command. MOVLW 38H MOVWF PORTD ;8 bits 2 lines 5*7 mode. CALL ENABLE MOVLW 0EH ;display on,cursor on,blink on. MOVWF PORTD CALL ENABLE MOVLW 06H ;character not move,cursor rotate right. MOVWF PORTD CALL ENABLE MOVLW 080H ;80 = top line c0 = bottom line MOVWF PORTD ;the 1st line address. CALL ENABLE ;MOVLW 54H ;;MOVWF PORTD CALL WRITE1 ;. ;;;;;;;;;;;;;;;;;; USART_1 CALL DELAY_MS ;call two delay program ; CALL DELAY_MS ;CALL DELAY_MS btfss PORTE,0 GOTO num2 movlw 08fh movwf 03fh BSF INTCON,5h ; timer0 int enable num2 btfss PORTE,1 GOTO num3 movlw 0b7fh movwf 03fh BSF INTCON,5h ; timer0 int enable num3 btfss PORTE,1 GOTO errbit movlw 0c7fh movwf 03fh BSF INTCON,5h ; timer0 int enable errbit btfss RCSTA,2 goto erbit2 bsf PORTB,7 erbit2 btfss RCSTA,1 goto USART_1 bsf PORTB,6 GOTO USART_1 ; ;***** set to line 2. Line2 ;CALL DELAY ;delay for lcd is slower than mcu. ;CALL DELAY ;delay for lcd is slower than mcu. MOVLW 0C0H ;80 = top line c0 = bottom line MOVWF PORTD ;the 1st line address. CALL ENABLE CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY ;delay for lcd is slower than mcu. Return ;***** set to line 1 . Line1 ;CALL DELAY ;delay for lcd is slower than mcu. ;CALL DELAY ;delay for lcd is slower than mcu. MOVLW 080H ;80 = top line c0 = bottom line MOVWF PORTD ;the 1st line address. CALL ENABLE CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY ;delay for lcd is slower than mcu. Return ;************************200ms delay program****************** DELAY_MS MOVLW VAL_MS MOVWF COUNT_MS LOOP_MS CALL DELAY_US DECFSZ COUNT_MS,1 GOTO LOOP_MS RETURN ;************************100us delay program************** DELAY_US MOVLW VAL_US MOVWF COUNT LOOP_US NOP DECFSZ COUNT,1 GOTO LOOP_US RETURN DELAY_S MOVLW 0FFH MOVWF 50H CALL DELAY_MS DECFSZ 50H,1 GOTO $-2 RETURN ;******************send data to lcd.************ WRITE3 BSF RS MOVWF PORTD BCF RW BCF E CALL DELAY BSF E incf 02fh ;check for end of line1 - character counter 02fh movlw 010h xorwf 02fh,0 char16 BTFSS STATUS,Z goto char32 MOVLW 0c0H ;80 = top line c0 = bottom line MOVWF PORTD ;the 1st line address. CALL ENABLE call DELAY char32 ;check for end of line2 movlw 020h xorwf 02fh,0 BTFSS STATUS,Z goto here MOVLW 01H ;80 = top line c0 = bottom line -01 =cls 02= home MOVWF PORTD ;the 1st line address. CALL ENABLE call DELAY clrf 02fh ; reset character counter here ;incf 02fh ;check for end of line1 - character counter 02fh RETURN WRITE4 BSF RS MOVWF PORTD BCF RW BCF E CALL DELAY CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY ; CALL DELAY ;delay for lcd is slower than mcu. ;CALL DELAY BSF E Return ;****************send command to lcd.************** ENABLE BCF RS ; BCF RW BCF E CALL DELAY CALL DELAY ;delay for lcd is slower than mcu. CALL DELAY ; CALL DELAY ;delay for lcd is slower than mcu. ;CALL DELAY BSF E RETURN ;*********************delay subroutine****************** DELAY ;subroutine name and also is the entry. MOVLW 0FFH ; MOVWF 20H ;send external loop count 0ffh to 20h LP0 MOVLW 01H ; MOVWF 21H ;send internal loop count 0ffh to 21h LP1 DECFSZ 21H,1 ;decrease 21h,if 0,then jump GOTO LP1 ;continue loop at LP1 DECFSZ 20H,1 ;decrease 20h,if 0,then jump GOTO LP0 ;continue loop at LP0 RETURN ; ;===================================================== Transwt bsf STATUS,RP0 ; RAM Page 1 WtHere btfss TXSTA,TRMT ; (1) transmission is complete if hi goto WtHere bcf STATUS,RP0 ; RAM Page 0 return ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;****************** send the 1st data program*************** WRITE1 CLRF COUNT3 ;clear offset WRITE_A MOVFW COUNT3 CALL TABLE ;search to display www.pic16.com MOVWF TMP3 ; INCF COUNT3,1 ;offset added MOVFW TMP3 XORLW 00H ;check if finished.(the last byte is 00h.) BTFSC STATUS,Z GOTO $+4 ;all display finished. MOVFW TMP3 CALL WRITE3 ;no, send to LCD GOTO WRITE_A ; call WRITE2 RETURN ; ;****************** send the 2nd data program********* WRITE2 CLRF COUNT3 ;clear offset WRITE_2A MOVFW COUNT3 CALL TABLE1 ;search to display www.pic16.com MOVWF TMP3 ; INCF COUNT3,1 ;offset added MOVFW TMP3 XORLW 00H ;check if finished.(the last byte is 00h.) BTFSC STATUS,Z GOTO $+4 ;all display finished. MOVFW TMP3 CALL WRITE3 ;no, send to LCD GOTO WRITE_2A ; ;incf 02fh RETURN ; serv incf 041h bcf INTCON,2 bsf PORTC,1 call servtime bcf PORTC,1 movlw 0fah movwf 03fh retfie servtime ;movlw 0xB7 ; value from user after ^ : 1ms centre = C7 1.5ms right = B7 2.0ms left =8f movwf 03fh movlw 0x0C movwf 040h Delay_0 decfsz 03fh, f goto $+2 decfsz 040h, f goto Delay_0 return txit movwf TXREG ; echo data back to sender call Transwt ; wait until finished sending clrf 02bh decfsz 02eh,1 return END