Simple pic project but having problems...

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
I'm using a pic 16f628a.

It's been a while since i've done pic programming and it's taking time to get back into it....

Basically the circuit has four LEDs and four press buttons.

RA4,RA5,RA6,RA7 are press buttons
RB0,RB1,RB2,RB3 are LEDs

All I want to do is have the corresponding LED to come on for 255 mSeconds when the button is pressed and then switch off.

So for example, if RA4 is pressed, I would like RB0 LED to come on, RA5 switches on RB1, RA6 to switch on RB2 and finally RA7 to bring on RB3.


Rich (BB code):
   LIST    p=16F628        ; TELLS THE PROGRAMMER WHAT CHIP WE'RE USING.
    include "P16F628.inc"    ; INCLUDES DEFAULTS FOR THE CHIP.
    ERRORLEVEL    0, -302        ;suppress bank selection messages
    __config 0x3D18            ;sets the configuration settings (oscillator type etc.)
;__config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_OFF

cblock    0x20            ;start of general purpose registers
            count            ;used in looping routines
            count1            ;used in delay routine
            counta            ;used in delay routine
            countb            ;used in delay routine
endc

; **************************************
; LEDs and Switches

    LEDPORT    Equ    PORTB            ;set constant LEDPORT = 'PORTB'
    SWPORT    Equ    PORTA            ;set constant SWPORT = 'PORTB'
    LEDTRIS    Equ    TRISB
    SWTRIS     Equ TRISA    
    
    SW1        Equ    0x04             ; Identifies Switch One on Schematic diagram
    SW2        Equ    0x05             ; Identifies Switch Two on Schematic diagram
    SW3        Equ 0x06             ; Identifies Switch Three on Schematic diagram
    SW4        Equ 0x07             ; Identifies Switch Four on Schematic diagram
    
    LED1    Equ 0x00             ; Identifies LED One on the Schematic diagram
    LED2    Equ 0x01             ; Identifies LED Two on the Schematic diagram
    LED3     Equ 0x02             ; Identifies LED Three on the Schematic diagram
    LED4     Equ 0x03             ; Identifies LED Four on the Schematic diagram
    
SWDel    Set    Delay50            ;set the de-bounce delay (has to use 'Set' and not 'Equ')


; **************************************
; end of LEDs and switches 

org    0x0000                ;org sets the origin, 0x0000 for the 16F628

        movlw    0x07
        movwf    CMCON            ;turn comparators off (make it like a 16F84)
        
        

SetPorts    bsf     STATUS,        RP0        ;select bank 1
        movlw    0x00    ; make all pins outputs
        ;bcf    STATUS,        RP0        ;select bank 0
        
        movlw    b'00001111'
        ;movlw    0x00    ; make all pins outputs
        movwf     LEDTRIS
        bcf    STATUS,        RP0    ;select bank 0
        
        clrf    LEDPORT            ;set all outputs low
        ;clrf    SWPORT        

; Loop waits for a button press.
            Loop1 btfss    SWPORT,    SW1 ; Increase time button
                    call Switch1 ; Call sub routine when SW1 is pressed
                  btfss    SWPORT,    SW2 ; Reset time button
                      call Switch2 ; Call sub routine when SW2 is pressed
                  btfss    SWPORT,    SW3 ; Start Timer button
                      call Switch3
            Goto Loop1    


Switch1 call SWDel 
bsf    LEDPORT, LED1    ;turn LED1 on
call Delay255
bcf LEDPORT,LED1
goto Loop1

Switch2 call SWDel
bsf    LEDPORT, LED2    ;turn LED1 on
call Delay255
bcf LEDPORT, LED2
goto Loop1

Switch3 Call SWDel
bsf    LEDPORT, LED3    ;turn LED1 on
call Delay255
bcf LEDPORT,LED3
goto Loop1





; ***************************************
; DELAY ROUTINES


Delay255    movlw    0xff        ;delay 255 mS (0xff in hexadecimal)
        goto    d0
Delay100    movlw    d'100'        ;delay 100mS
        goto    d0
Delay50        movlw    d'50'        ;delay 50mS
        goto    d0
Delay20        movlw    d'20'        ;delay 20mS
        goto    d0
Delay5        movlw    0x05        ;delay 5.000 ms (4 MHz clock)
d0        movwf    count1
d1        movlw    0xC7            ;delay 1mS
        movwf    counta
        movlw    0x01
        movwf    countb
Delay_0
        decfsz    counta, f
        goto    $+2
        decfsz    countb, f
        goto    Delay_0

        decfsz    count1    ,f
        goto    d1
        retlw    0x00
        

; END OF DELAY ROUTINES

; ***************************************

end
I do get alot of warnings but not errors when I try to build this project... Something about OpCode.

Thanks! :)
 

Markd77

Joined Sep 7, 2009
2,806
I can see there should be returns in the switch1-3 functions instead of gotos. I'm assuming that MCLR is dealt with in the config. What seems to be the main problem?
Warnings aren't always bad.
 

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
thanks for the quick reply...

It just doesn't seem to do anything. the project is dead. If I program the pic with some example code it all works... so definately something within the code.

I was hoping it would be something really obvious that i was missing.
 
I'm not much of an expert but I'll give it a crack.

To be honest, I'm not really understanding the flow of your code. I'm not seeing the org 0 then goto start that I normally have in my assembly code. I'm fairly new to this, so maybe it's merely one of the many ways to skin a cat that I learned. Your count user files are also done wierdly to me, but again, the cat thing...

I'm not seeing a configuration for the internal oscillator. Is it 4mhz by default?

Is the program working at all? I'm guessing it's not based on this:

SetPorts bsf STATUS, RP0 ;select bank 1
movlw 0x00 ; make all pins outputs
;bcf STATUS, RP0 ;select bank 0

movlw b'00001111'
;movlw 0x00 ; make all pins outputs
movwf LEDTRIS


Here, I don't understand the RP0.... I just use "0" to select bit 0 (cat reference again). The big problem I see is that you are moving 0x00 to w, then moving b'00001111' to w and then moving it to the portb tristate which would make ports 0-3 on PORTB inputs, with 4-7 as the outputs.
 

t06afre

Joined May 11, 2009
5,934
Have you tried to single step the program using MPLAB SIM. It might give you a hint about what is wrong. I see you have configured the CMCON register first in you code:) So all analog functions should be turned off. A good start, in this project.
 

Markd77

Joined Sep 7, 2009
2,806
Apart from the missing returns, I've just spotted that the wrong half of PORTB is set as outputs.
PORTA will be inputs by default.
 
Top