help me out please, my asm code is not working

Thread Starter

kent0198

Joined Nov 9, 2008
1
Hi, I am doing a doorbell using P16F84A chip.
I have programmed in asm in MPLAB 8.10,but the code is not working at all.
no any errors show up, but just not working.
please help me out.

#include P16F84A.inc

__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
errorlevel -302
org 0x0000
porta equ 05h
portb equ 06h
NoteC equ 0Ch
NoteD equ 0Dh
NoteE equ 0Eh
NoteF equ 0Fh
NoteG equ 11h
NoteA equ 12h
NoteB equ 13h
postC equ 14h
postD equ 15h
postE equ 16h
postF equ 17h
postG equ 18h
postB equ 19h
postA equ 1Ah
post30 equ 1Bh
post2 equ 1Ch

Start
;==============
Init clrf porta
clrf portb
movlw b'1111'
tris porta
movlw b'00000000'
tris portb

movlw d'30'
movwf post30

movlw d'127'
movwf NoteC

movlw d'113'
movwf NoteD

movlw d'101'
movwf NoteE

movlw d'95'
movwf NoteF

movlw d'85'
movwf NoteG

movlw d'75'
movwf NoteA

movlw d'67'
movwf NoteB

movlw d'2'
movlw post2

movlw d'131'
movlw postC

movlw d'147'
movlw postD

movlw d'165'
movlw postE

movlw d'175'
movlw postF

movlw d'196'
movlw postG

movlw d'220'
movlw postA

movlw d'297'
movlw postB

main bcf portb,0
btfsc porta,0
goto melody1
;btfsc porta,1
goto melody2
btfsc porta,2
goto melody3
btfsc porta,3
goto melody4
goto main

melody1 call CNote
call DNote
call ENote
call FNote
call GNote
call ANote
call BNote
goto main

melody2 call ENote
call ENote
call FNote
call GNote
call GNote
call FNote
call ENote
call DNote
call CNote
call CNote
call DNote
call ENote
call DNote
call CNote
call CNote
goto main

melody3 call BNote
call ANote
call GNote
call FNote
call ENote
call DNote
call CNote
goto main

melody4 call BNote
call FNote
call GNote
call DNote
call ENote
call CNote
call CNote
call ANote
call BNote
goto main



CNote decfsz NoteC,f
goto CNote
decfsz post30,f
goto CNote
movlw b'00000001'
xorwf portb,0
movlw d'127'
movwf NoteC
movlw d'30'
movwf post30
decfsz postC,f
goto CNote
decfsz post2,f
goto CNote
movlw d'2'
movwf post2
movlw d'131'
movlw postC
goto main
;return


DNote decfsz NoteD,f
goto DNote
decfsz post30,f
goto DNote
movlw b'00000001'
xorwf portb,0
movlw d'113'
movwf NoteD
movlw d'30'
movwf post30
decfsz postD,f
goto DNote
decfsz post2,f
goto DNote
movlw d'2'
movwf post2
movlw d'147'
movwf postD
return

ENote decfsz NoteE,f
goto ENote
movlw d'101'
movwf NoteE
decfsz post30,f
goto ENote
movlw d'30'
movwf post30
movlw b'00000001'
xorwf portb,0
movlw d'101'
movwf NoteE
movlw d'30'
movwf post30
decfsz postE,f
goto ENote
movlw d'165'
movwf postE
decfsz post2,f
goto ENote
movlw d'2'
movwf post2
movlw d'165'
movwf postE
return

FNote decfsz NoteF,f
goto FNote
decfsz post30,f
goto FNote
movlw b'00000001'
xorwf portb,0
movlw d'95'
movwf NoteF
movlw d'30'
movwf post30
decfsz postF,f
goto FNote
decfsz post2,f
goto FNote
movlw d'2'
movwf post2
movlw d'175'
movwf postF
return

GNote decfsz NoteG,f
goto GNote
decfsz post30,f
goto GNote
movlw b'00000001'
xorwf portb,0
movlw d'95'
movwf NoteG
movlw d'30'
movwf post30
decfsz postG,f
goto GNote
decfsz post2,f
goto GNote
movlw d'2'
movwf post2
movlw d'196'
movwf postG
return

ANote decfsz NoteA,f
goto ANote
decfsz post30,f
goto ANote
movlw b'00000001'
xorwf portb,0
movlw d'75'
movwf NoteA
movlw d'30'
movwf post30
decfsz postA,f
goto ANote
decfsz post2,f
goto ANote
movlw d'2'
movwf post2
movlw d'220'
movwf postA
return

BNote decfsz NoteB,f
goto BNote
decfsz post30,f
goto BNote
movlw b'00000001'
xorwf portb,0
movlw d'67'
movwf NoteB
movlw d'30'
movwf post30
decfsz postB,f
goto BNote
decfsz post2,f
goto BNote
movlw d'2'
movwf post2
movlw d'297'
movwf postB
return

;==============

END
 

SgtWookie

Joined Jul 17, 2007
22,230
Just added CODE tags to preserve your code formatting.
Rich (BB code):
#include    P16F84A.inc

    __CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
    errorlevel -302
    org        0x0000
porta    equ        05h
portb    equ        06h
NoteC    equ        0Ch        
NoteD    equ        0Dh
NoteE    equ        0Eh
NoteF    equ        0Fh
NoteG    equ        11h
NoteA    equ        12h
NoteB    equ        13h
postC    equ        14h
postD    equ        15h
postE    equ        16h
postF    equ        17h
postG    equ        18h
postB    equ        19h
postA    equ        1Ah
post30    equ        1Bh
post2    equ        1Ch

Start        
;==============
Init    clrf    porta
        clrf    portb
        movlw    b'1111'
        tris    porta
        movlw    b'00000000'
        tris    portb
    
        movlw    d'30'
        movwf    post30
    
        movlw    d'127'
        movwf    NoteC
    
        movlw    d'113'
         movwf    NoteD
    
        movlw    d'101'
        movwf    NoteE
    
        movlw    d'95'
        movwf    NoteF
        
        movlw    d'85'
        movwf    NoteG
    
        movlw    d'75'
        movwf    NoteA
        
        movlw    d'67'
        movwf    NoteB
        
        movlw    d'2'
        movlw    post2
    
        movlw    d'131'
        movlw    postC
    
        movlw    d'147'
        movlw    postD
    
        movlw    d'165'
        movlw    postE
    
        movlw    d'175'
        movlw    postF
    
        movlw    d'196'
        movlw    postG
    
        movlw    d'220'
        movlw    postA
    
        movlw    d'297'
        movlw    postB    

main    bcf        portb,0
        btfsc    porta,0
        goto    melody1
        ;btfsc    porta,1
        goto    melody2
        btfsc    porta,2
        goto    melody3
        btfsc    porta,3
        goto    melody4
        goto     main

melody1    call    CNote
        call    DNote
        call     ENote
        call    FNote
        call     GNote
        call     ANote
        call    BNote
        goto    main

melody2    call    ENote
        call    ENote
        call     FNote
        call    GNote
        call     GNote
        call     FNote
        call    ENote
        call    DNote
        call    CNote
        call     CNote
        call    DNote
        call     ENote
        call     DNote
        call    CNote
        call    CNote
        goto    main

melody3    call    BNote
        call    ANote
        call     GNote
        call    FNote
        call     ENote
        call     DNote
        call    CNote
        goto    main    

melody4    call    BNote
        call    FNote
        call     GNote
        call    DNote
        call     ENote
        call     CNote
        call    CNote
        call    ANote
        call    BNote
        goto    main



CNote    decfsz    NoteC,f
        goto    CNote
        decfsz    post30,f
        goto    CNote
        movlw    b'00000001'
        xorwf    portb,0
        movlw    d'127'
        movwf    NoteC
        movlw    d'30'
        movwf    post30
        decfsz  postC,f
        goto    CNote
        decfsz    post2,f
        goto    CNote
        movlw    d'2'
        movwf    post2
        movlw    d'131'
        movlw    postC
        goto    main
        ;return


DNote    decfsz    NoteD,f
        goto    DNote
        decfsz    post30,f
        goto    DNote
        movlw    b'00000001'
        xorwf    portb,0
        movlw    d'113'
        movwf    NoteD
        movlw    d'30'
        movwf    post30
        decfsz postD,f
        goto    DNote
        decfsz    post2,f
        goto    DNote
        movlw    d'2'
        movwf    post2
        movlw    d'147'
        movwf    postD
        return

ENote   decfsz    NoteE,f
        goto    ENote
        movlw    d'101'
        movwf    NoteE
        decfsz    post30,f
        goto    ENote
        movlw    d'30'
        movwf    post30
        movlw    b'00000001'
        xorwf    portb,0
        movlw    d'101'
        movwf    NoteE
        movlw    d'30'
        movwf    post30
        decfsz  postE,f
        goto    ENote
        movlw    d'165'
        movwf    postE
        decfsz    post2,f
        goto    ENote
        movlw    d'2'
        movwf    post2
        movlw    d'165'
        movwf    postE
        return

FNote    decfsz    NoteF,f
        goto    FNote
        decfsz    post30,f
        goto    FNote
        movlw    b'00000001'
        xorwf    portb,0
        movlw    d'95'
        movwf    NoteF
        movlw    d'30'
        movwf    post30
        decfsz postF,f
        goto    FNote
        decfsz    post2,f
        goto    FNote
        movlw    d'2'
        movwf    post2
        movlw    d'175'
        movwf    postF
        return

GNote    decfsz    NoteG,f
        goto    GNote
        decfsz    post30,f
        goto    GNote
        movlw    b'00000001'
        xorwf    portb,0
        movlw    d'95'
        movwf    NoteG
        movlw    d'30'
        movwf    post30
        decfsz postG,f
        goto    GNote
        decfsz    post2,f
        goto    GNote
        movlw    d'2'
        movwf    post2
        movlw    d'196'
        movwf    postG
        return

ANote    decfsz    NoteA,f
        goto    ANote
        decfsz    post30,f
        goto    ANote
        movlw    b'00000001'
        xorwf    portb,0
        movlw    d'75'
        movwf    NoteA
        movlw    d'30'
        movwf    post30
        decfsz postA,f
        goto    ANote
        decfsz    post2,f
        goto    ANote
        movlw    d'2'
        movwf    post2
        movlw    d'220'
        movwf    postA
        return

BNote    decfsz    NoteB,f
        goto    BNote
        decfsz    post30,f
        goto    BNote
        movlw    b'00000001'
        xorwf    portb,0
        movlw    d'67'
        movwf    NoteB
        movlw    d'30'
        movwf    post30
        decfsz postB,f
        goto    BNote
        decfsz    post2,f
        goto    BNote
        movlw    d'2'
        movwf    post2
        movlw    d'297'
        movwf    postB
        return

;==============

    END
 

markm

Joined Nov 11, 2008
16
One possible problem (depending on the hardware) is your use of

xorwf portb,0

to toggle the output bit on and off. This reads the whole port B byte, changes one bit, and writes it back. One "feature" of the PIC is that it doesn't always read back what you wrote last, but rather it reads the current logic level of the pins. It's pretty common to see a PIC output pin connected to an NPN transistor (for instance) in such a way that setting the pin high turns the transistor on, but doesn't raise the voltage of the pin high enough to read back as a high. Result, every time you execute that XOR, it reads back a 0 and changes it to a 1.

That sounds like the PIC is being overloaded, but it's designed to handle it. Sometimes it comes in useful - you can use this feature to implement bi-directional communications or detect shorted pins without having to keep switching the pin from input to output and back.

The simplest fix: store an image of port B in memory (RAM, which in PICs is called "general registers"). Change that image with the XOR command, then move the image to W and then to port B. If you don't have one whole byte of RAM left, it gets a little difficult, but you can alternatively store just one bit in RAM, and use a bit-test and goto's to select either a bit-set or bit-clr to port B. (Do remember that bit set and clear are read-modify-write instructions, so if there are other output pins in port B, they may mysteriously change also.)

Or there's probably a hardware fix. If it's driving an NPN transistor, a large enough value resistor in series between the pin and the transistor gate will limit the current so the pin actually does go high. This also limits your output (collector) current to (transistor beta) * (gate current), so better calculate whether that's enough to drive the speaker.
 
Top