Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RS232, Pic16f84 and GSM

Status
Not open for further replies.
Mr Nigel Goodwin

Its good that you are here! I am doing an alarm system project based on a pic16f84 microcontroller controlling a GSM mobile phone Nokia 5110/6110. I have written a code to do the control. Here it is. Can you correct me.


TXPIN EQU 0x03
RXPIN EQU 0x02
ALARMPIN EQU 0x00

DELAYCNT EQU 0x0C
BITCNT EQU 0x0D
BYTE EQU 0x0E
MSGPTR EQU 0x10
W_SCRATCH EQU 0x11

START ORG 0x00
GOTO MAIN
TXBYTE MOVWF W_SCRATCH
BCF PORTA,0x03 ;SEND START BIT
CALL DELAY104
MOVF W_SCRATCH,W
MOVWF BYTE
MOVLW 0x08
MOVWF BITCNT

TXLOOP RRF BYTE,F
BTFSC STATUS, C
CALL TXHIBIT
BTFSS STATUS,C
CALL TXLOBIT
DECFSZ BITCNT
GOTO TXLOOP
BSF PORTA,0x03 ;SEND STOP BIT
CALL DELAY104
RETURN

TXHIBIT BSF PORTA,0x03
CALL DELAY104
RETURN

TXLOBIT BCF PORTA,0x03
CALL DELAY104
RETURN


DELAY104 MOVLW 0x22 ;??????
MOVWF DELAYCNT
DELAYLOOP DECFSZ DELAYCNT
GOTO DELAYLOOP
RETURN


OUTMSG MOVLW 0x00
MOVWF MSGPTR ; put 'W' into message pointer
MSGLOOP MOVF MSGPTR, W ; put the offset in 'W'
CALL MSGTABLE ; returns ASCII character in 'W'
XORLW 0x00 ; sets the zero flag if W = 0, here we are testing to see if at end of table
BTFSC STATUS, Z ; skip if zero bit not set
RETURN ; finished if W = 0
CALL TXBYTE ; output the character
INCF MSGPTR, f ; point at next
GOTO MSGLOOP ; more characters

CHKALM BTFSS PORTB,0x00 ;loop to check for alarm
CALL OUTMSG
GOTO CHKALM
RETURN

MAIN CLRF PORTB
BSF STATUS,RP0
MOVLW 0x02 ;SET RA2 AS I/P, RA3 AS O/P
MOVWF TRISA
MOVLW 0x01
MOVWF TRISB ;SET RB0 AS INPUT
BCF STATUS,RP0
GOTO CHKALM

MSGTABLE ADDWF PCL, f ; offset added to PCL
RETLW 0x48 ; 'H'
RETLW 0x65 ; 'e'
RETLW 0x6C ; 'l'
RETLW 0x6C ; 'l'
RETLW 0x6F ; 'o'
RETLW 0x20 ; ' '
RETLW 0x57 ; 'W'
RETLW 0x6F ; 'o'
RETLW 0x72 ; 'r'
RETLW 0x6C ; 'l'
RETLW 0x64 ; 'd'
RETLW 0x21 ; '!'
RETLW 0x00 ; indicates end
END
 
emufambirwi said:
Mr Nigel Goodwin

Its good that you are here! I am doing an alarm system project based on a pic16f84 microcontroller controlling a GSM mobile phone Nokia 5110/6110. I have written a code to do the control. Here it is. Can you correct me.

It would help if you use the Code button to surround your code, to keep the formatting correct.

But for a start, what happens when you run it to your phone?, and what happens if you feed it to a PC serial port and view it with Hyperterminal (or similar)?.
 
I have read your code and added some comments. Hopefully you'll get lots more comments from our Nigel with a better formatted code.

Code:
;============================================================================
;
; Program to communicate with a Nokia phone using 16F84
;
; Using bitbang technique
;
;============================================================================

        TXPIN           EQU 0x03
        RXPIN           EQU 0x02
        ALARMPIN        EQU 0x00

        DELAYCNT        EQU 0x0C
        BITCNT          EQU 0x0D
        BYTE            EQU 0x0E
        MSGPTR          EQU 0x10
        W_SCRATCH       EQU 0x11

        ORG 0x00
START 
        GOTO MAIN

TXBYTE 
        MOVWF W_SCRATCH         ;save a copy first
        BCF PORTA,TXPIN         ;SEND START BIT
        CALL DELAY104           ;delay 104 instruction cycles
                                ;    assuming 4MHz clock, that's 1us per instruction cycle
                                ;    so delaying 104us
        MOVF W_SCRATCH,W        ;get back data
        MOVWF BYTE              ;save to BYTE
        MOVLW 0x08
        MOVWF BITCNT            ;do eight times
TXLOOP 
        RRF BYTE,F              ;move BYTE bit0 into carry      
        BTFSC STATUS, C         ;a HIGH ?
        CALL TXHIBIT            ;yes, send it
        BTFSS STATUS,C          ;a LOW ?
        CALL TXLOBIT            ;yes, send it
        ;
        ;------------------------------------------------------------
        ; the above is not a safe method to do things 
        ; as the Carry flag might got changed in the subroutine(s)
        ; you've just called. Changing one instruction and adding 
        ; one extra instruction solves the problem, or try method 2
        ;------------------------------------------------------------
        ;Method 1: Test again
        ;
        ;RRF BYTE,W <<<<        ;move BYTE bit0 into carry, but do not change value in BYTE     
        ;BTFSC STATUS, C        ;a HIGH ?
        ;CALL TXHIBIT           ;yes, send it
        ;RRF BYTE,F <<<<        ;move BYTE bit0 into carry
        ;BTFSS STATUS,C         ;a LOW ?
        ;CALL TXLOBIT           ;yes, send it
        ;
        ;
        ;Method 2: Test only once and acts accordingly
        ;
        ;       RRF BYTE,F      ;move BYTE bit0 into carry      
        ;       BTFSS STATUS, C 
        ;       GOTO CARRY_IS_0 
        ;       CALL TXHIBIT
        ;       GOTO NEXT_STEP
        ;       ;
        ;CARRY_IS_0
        ;       CALL TXLOBIT
        ;       ;
        ;NEXT_STEP      
        ;
        ;------------------------------------------------------------
        ;
        DECFSZ BITCNT           ;
        GOTO TXLOOP             ;loop for 7 more times
        ;
        BSF PORTA,TXPIN         ;SEND STOP BIT
        CALL DELAY104
        RETURN

TXHIBIT 
        BSF PORTA,TXPIN
        CALL DELAY104
        RETURN

TXLOBIT 
        BCF PORTA,TXPIN
        CALL DELAY104
        RETURN

        ;---------------------------------------------
        ; SETUP DELAYCNT = 2 cycles
        ; DELAYLOOP (DELAYCNT) * 3 - 1 = 98 cycles  
        ; Call/return    = 4 cycles
        ; Total = 104 cycles, DELAYCNT=0x21, not 0x22
        ;---------------------------------------------
        ;
DELAY104
        MOVLW 0x21      ;one cycle,HEX 21 = Decimal 33
        MOVWF DELAYCNT  ;one cycle
        ;
DELAYLOOP 
        DECFSZ DELAYCNT ;one cycle if DELAYCNT not zero, two if zero
        GOTO DELAYLOOP  ;two cycles
        RETURN          ;
        ;
OUTMSG 
        MOVLW 0x00
        MOVWF MSGPTR ; put 'W' into message pointer

        ;CLRF MSGPTR    ;this will do instead of the above two instructions
        
MSGLOOP 
        MOVF MSGPTR, W  ; put the offset in 'W'
        CALL MSGTABLE   ; returns ASCII character in 'W'
        XORLW 0x00      ; sets the zero flag if W = 0, here we are testing to see if at end of table
        BTFSC STATUS, Z ; skip if zero bit not set
        RETURN          ; finished if W = 0 and return to CHKALM
        CALL TXBYTE     ; output the character
        INCF MSGPTR, f  ; increase pointer value to point at next
        GOTO MSGLOOP    ; more characters

CHKALM 
        BTFSS PORTB,0   ;loop to check for alarm
        CALL OUTMSG
        GOTO CHKALM     ;loop again
        RETURN          ;this instruction not needed    
;
;===========================================================
;       M A I N
;===========================================================

MAIN 
        CLRF PORTB
        BSF STATUS,RP0  ;select register bank1
        MOVLW 0x02      ;SET RA2 AS I/P, RA3 AS O/P
        MOVWF TRISA
        MOVLW 0x01      ;set RB0 as input
        MOVWF TRISB     ;
        BCF STATUS,RP0  ;select bank0
        ;
        BSF PORTA,TXPIN ;THIS IS REQUIRED TO SET THE INITIAL LEVEL OF TX PIN
        CALL DELAY104
        ;
        GOTO CHKALM     

MSGTABLE 
        ADDWF PCL, f ; offset added to PCL
        RETLW 0x48 ; 'H'
        RETLW 0x65 ; 'e'
        RETLW 0x6C ; 'l'
        RETLW 0x6C ; 'l'
        RETLW 0x6F ; 'o'
        RETLW 0x20 ; ' '
        RETLW 0x57 ; 'W'
        RETLW 0x6F ; 'o'
        RETLW 0x72 ; 'r'
        RETLW 0x6C ; 'l'
        RETLW 0x64 ; 'd'
        RETLW 0x21 ; '!'
        RETLW 0x00 ; indicates end of string
END
EDITED: Code added in MAIN to set TX pin level to HIGH at program start.
 
My main comment is that the TXBYTE routine seems rather over complicated?, the routine in my tutorial is somewhat simpler and easier to follow.

Apart from that, as before, what actually happens with the program?.
 
Thanks Mr Goodwin and eblc1388.

The program is supposed to check for a change of state on one of the porta pins(alarm). If an alarm condition exists, then the program will branch to a section which initiates sending of an sms from a gsm phone to another gsm phone number which is stored in pic memory. take a look at my proposed hardware solution.
 

Attachments

  • gsm-picalarm.doc
    29.5 KB · Views: 462
  • gsm-picalarm.doc
    29.5 KB · Views: 616
emufambirwi said:
Thanks Mr Goodwin and eblc1388.

The program is supposed to check for a change of state on one of the porta pins(alarm). If an alarm condition exists, then the program will branch to a section which initiates sending of an sms from a gsm phone to another gsm phone number which is stored in pic memory. take a look at my proposed hardware solution.

Yes, I know what it's supposed to do, but what actually happens?.
 
Nigel Goodwin said:
emufambirwi said:
Thanks Mr Goodwin and eblc1388.

The program is supposed to check for a change of state on one of the porta pins(alarm). If an alarm condition exists, then the program will branch to a section which initiates sending of an sms from a gsm phone to another gsm phone number which is stored in pic memory. take a look at my proposed hardware solution.

Yes, I know what it's supposed to do, but what actually happens?.

I left yesterday without answering your question, sorry about that! The thing is I have only constructed the data cable, and have the PIC. But I haven't integrated the two yet, the reason being scarcity of resourses in this part of the world. So one wouldn't want to take too much risk by implementing something that can damage the PIC. (You rarely find one here)

So I wanted to get your opinion prior to the actual implementation.
 
emufambirwi said:
Nigel Goodwin said:
emufambirwi said:
Thanks Mr Goodwin and eblc1388.

The program is supposed to check for a change of state on one of the porta pins(alarm). If an alarm condition exists, then the program will branch to a section which initiates sending of an sms from a gsm phone to another gsm phone number which is stored in pic memory. take a look at my proposed hardware solution.

Yes, I know what it's supposed to do, but what actually happens?.

I left yesterday without answering your question, sorry about that! The thing is I have only constructed the data cable, and have the PIC. But I haven't integrated the two yet, the reason being scarcity of resourses in this part of the world. So one wouldn't want to take too much risk by implementing something that can damage the PIC. (You rarely find one here)

So I wanted to get your opinion prior to the actual implementation.

For a start try connecting it to your PC (adding a MAX232 if required) running HyperTerminal, and see if it displays the correct string on the screen.
 
Status
Not open for further replies.

Latest threads

Back
Top