16F877a & Bluetooth

Introduction: 16F877a & Bluetooth

This is my first play with the HC-05 Bluetooth device (and my first instructable !)

I programmed the PIC to receive and send data @ 9600 baud as this was the default settings of the HC-05.

Basically I connect to the bluetooth device and send data from my Android phone.

Sent text gets displayed on the LCD panel. Sending '#' then numbers 1 to 8 operates the associated relay.

When the relay is activated, the PIC sends either the number of the intended relay activated (1 to 8) or a letter, A to H (A = relay 1, B= relay 2 etc.)

This returned value is used to change the colour of the text on the button from red to green or green to red depending on the state of the relay.

Because there is no 'handshaking' I send each character with a 250ms delay to avoid overrun.

The PIC programme was written with the aid of Oshonsoft simulator (invaluable and inexpensive software) and the Android App was written using MIT App Inventor 2 - a completely different way of coding, but an excellent piece of software - and it's FREE !

Share

    Recommendations

    • Fix It! Contest

      Fix It! Contest
    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    12 Discussions

    Here is the PIC code. Its a tad messy as its based around an LCD test prog from China.

    Not too much in the way of comments either :-(

    ===================================================

    ;Control LCD & Relays using Bluetooth . Servo code for future - code untested.

    ;Based on LCD test code - some code is not used!

    #INCLUDE<c:\P16F877a.INC>

    __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

    dexRLF 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

    7 replies

    Sir, even if you gave the original file of this project . . Its still unable for me to run that source code . . I didnt even know which part that i need to erase . . I just wanna play around the code with protues software .however i was unable to assemble the file into hex file . . Sorry sir im disturbing you too much

    Hi
    The protues software looks interesting !!

    It has been a while since I programmed the PIC chips.
    The code has to be compiled using Microchip MPASM and then uploaded to the PIC !

    Programming PIC chips using assembly code can be challenging and at times, frustrating.

    I changed to Arduino mainly due to the amount of free knowledge and examples there are on the Internet!
    Programming, debugging and testing are so much easier using the Arduino - and they are so cheap too!

    I am not too sure what your goals are, what are you trying to achieve please? Is it for learning or a particular project?

    kind regards
    Terry

    Hi samsungite . . Can u send me the actual source code asm of pic simulator ide . . Tq in advance . . Plss . . Kekekep93@gmail.com

    I will try and add the original file to the project

    Terry :-)

    Thank you sir . . After this project working . . I wanted to play with arduino soon . . Huhu

    Hi
    If you are programming pic chips, this software is invaluable !!

    Terry

    http://www.oshonsoft.com/pic.html

    Sir ,currently im using this oshon pic simulator ide and ive tried to assemble and load above coding, unfortunately theres a lot off error . . T_T

    Hi

    Thank you & I'm really sorry for the dreadfully long delay in responding.

    It was more a proof of concept! I've done a few projects with a PIC but I have fallen in love with the Arduino!!

    My first Arduino project also uses Bluetooth, so the knowledge gained from using a PIC was invaluable.

    I will post the project over the Christmas period, its a time lapse trigger for a Sony camera using IR !

    I'm also working on my Internet of Things, first of these will be a milk monitor !!

    Terry

    1 reply

    Hi there,

    Excellent project there. Would you be willing to share the coding?

    Thank you.

    Cool first Instructable! It looks like you've mostly heard crickets to this point, so consider using the video as a supplement to a step by step project rather than the sole piece of media that you share. Was this a proof of concept or do you have grander designs for this project?