Simple pic project but having problems...

Discussion in 'Embedded Systems and Microcontrollers' started by bluebrakes, Aug 30, 2010.

  1. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    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.


    Code ( (Unknown Language)):
    1.    LIST    p=16F628        ; TELLS THE PROGRAMMER WHAT CHIP WE'RE USING.
    2.     include "P16F628.inc"    ; INCLUDES DEFAULTS FOR THE CHIP.
    3.     ERRORLEVEL    0, -302        ;suppress bank selection messages
    4.     __config 0x3D18            ;sets the configuration settings (oscillator type etc.)
    5. ;__config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_OFF
    6.  
    7. cblock    0x20            ;start of general purpose registers
    8.             count            ;used in looping routines
    9.             count1            ;used in delay routine
    10.             counta            ;used in delay routine
    11.             countb            ;used in delay routine
    12. endc
    13.  
    14. ; **************************************
    15. ; LEDs and Switches
    16.  
    17.     LEDPORT    Equ    PORTB            ;set constant LEDPORT = 'PORTB'
    18.     SWPORT    Equ    PORTA            ;set constant SWPORT = 'PORTB'
    19.     LEDTRIS    Equ    TRISB
    20.     SWTRIS     Equ TRISA    
    21.    
    22.     SW1        Equ    0x04             ; Identifies Switch One on Schematic diagram
    23.     SW2        Equ    0x05             ; Identifies Switch Two on Schematic diagram
    24.     SW3        Equ 0x06             ; Identifies Switch Three on Schematic diagram
    25.     SW4        Equ 0x07             ; Identifies Switch Four on Schematic diagram
    26.    
    27.     LED1    Equ 0x00             ; Identifies LED One on the Schematic diagram
    28.     LED2    Equ 0x01             ; Identifies LED Two on the Schematic diagram
    29.     LED3     Equ 0x02             ; Identifies LED Three on the Schematic diagram
    30.     LED4     Equ 0x03             ; Identifies LED Four on the Schematic diagram
    31.    
    32. SWDel    Set    Delay50            ;set the de-bounce delay (has to use 'Set' and not 'Equ')
    33.  
    34.  
    35. ; **************************************
    36. ; end of LEDs and switches
    37.  
    38. org    0x0000                ;org sets the origin, 0x0000 for the 16F628
    39.  
    40.         movlw    0x07
    41.         movwf    CMCON            ;turn comparators off (make it like a 16F84)
    42.        
    43.        
    44.  
    45. SetPorts    bsf     STATUS,        RP0        ;select bank 1
    46.         movlw    0x00    ; make all pins outputs
    47.         ;bcf    STATUS,        RP0        ;select bank 0
    48.        
    49.         movlw    b'00001111'
    50.         ;movlw    0x00    ; make all pins outputs
    51.         movwf     LEDTRIS
    52.         bcf    STATUS,        RP0    ;select bank 0
    53.        
    54.         clrf    LEDPORT            ;set all outputs low
    55.         ;clrf    SWPORT        
    56.  
    57. ; Loop waits for a button press.
    58.             Loop1 btfss    SWPORT,    SW1 ; Increase time button
    59.                     call Switch1 ; Call sub routine when SW1 is pressed
    60.                   btfss    SWPORT,    SW2 ; Reset time button
    61.                       call Switch2 ; Call sub routine when SW2 is pressed
    62.                   btfss    SWPORT,    SW3 ; Start Timer button
    63.                       call Switch3
    64.             Goto Loop1    
    65.  
    66.  
    67. Switch1 call SWDel
    68. bsf    LEDPORT, LED1    ;turn LED1 on
    69. call Delay255
    70. bcf LEDPORT,LED1
    71. goto Loop1
    72.  
    73. Switch2 call SWDel
    74. bsf    LEDPORT, LED2    ;turn LED1 on
    75. call Delay255
    76. bcf LEDPORT, LED2
    77. goto Loop1
    78.  
    79. Switch3 Call SWDel
    80. bsf    LEDPORT, LED3    ;turn LED1 on
    81. call Delay255
    82. bcf LEDPORT,LED3
    83. goto Loop1
    84.  
    85.  
    86.  
    87.  
    88.  
    89. ; ***************************************
    90. ; DELAY ROUTINES
    91.  
    92.  
    93. Delay255    movlw    0xff        ;delay 255 mS (0xff in hexadecimal)
    94.         goto    d0
    95. Delay100    movlw    d'100'        ;delay 100mS
    96.         goto    d0
    97. Delay50        movlw    d'50'        ;delay 50mS
    98.         goto    d0
    99. Delay20        movlw    d'20'        ;delay 20mS
    100.         goto    d0
    101. Delay5        movlw    0x05        ;delay 5.000 ms (4 MHz clock)
    102. d0        movwf    count1
    103. d1        movlw    0xC7            ;delay 1mS
    104.         movwf    counta
    105.         movlw    0x01
    106.         movwf    countb
    107. Delay_0
    108.         decfsz    counta, f
    109.         goto    $+2
    110.         decfsz    countb, f
    111.         goto    Delay_0
    112.  
    113.         decfsz    count1    ,f
    114.         goto    d1
    115.         retlw    0x00
    116.        
    117.  
    118. ; END OF DELAY ROUTINES
    119.  
    120. ; ***************************************
    121.  
    122. end
    I do get alot of warnings but not errors when I try to build this project... Something about OpCode.

    Thanks! :)
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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.
     
  3. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    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.
     
  4. wannaBinventor

    Member

    Apr 8, 2010
    179
    4
    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.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  6. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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.
     
  7. eblc1388

    Senior Member

    Nov 28, 2008
    1,542
    102
    I agree with wannaBinventor. You have made RB0~RB3 an input.

    Perhaps you have been working with AVRs for too long a time. :)
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I made my self this rule for the TRIS register 0=0utput, or 0=o. Easy to picture
     
Loading...