VERY basic PIC problem

Discussion in 'Embedded Systems and Microcontrollers' started by CoximusPrime, Oct 23, 2009.

  1. CoximusPrime

    Thread Starter New Member

    Jun 21, 2009
    5
    0
    Hi Guys,

    I'm kind of new to the whole programming of embedded systems, and circuit design, but I'm trying :)

    I've started with the basic, make LEDs flash etc, all working fine, now I'm trying to check an input bit, but having problems. The Code is as follows

    Code ( (Unknown Language)):
    1.  
    2. ;==========================================================================
    3. ;
    4. ;       Configuration Bits
    5. ;
    6. ;==========================================================================
    7.  
    8. _BODEN_ON               EQU     3FFFh
    9. _BODEN_OFF              EQU     3FBFh
    10. _CP_ALL                 EQU     03FFh
    11. _CP_75                  EQU     17FFh
    12. _CP_50                  EQU     2BFFh
    13. _CP_OFF                 EQU     3FFFh
    14. _DATA_CP_ON             EQU     3EFFh
    15. _DATA_CP_OFF            EQU     3FFFh
    16. _PWRTE_OFF              EQU     3FFFh
    17. _PWRTE_ON               EQU     3FF7h
    18. _WDT_ON                 EQU     3FFFh
    19. _WDT_OFF                EQU     3FFBh
    20. _LVP_ON                 EQU     3FFFh
    21. _LVP_OFF                EQU     3F7Fh
    22. _MCLRE_ON               EQU     3FFFh
    23. _MCLRE_OFF              EQU     3FDFh
    24. _ER_OSC_CLKOUT          EQU     3FFFh
    25. _ER_OSC_NOCLKOUT        EQU     3FFEh
    26. _INTRC_OSC_CLKOUT       EQU     3FFDh
    27. _INTRC_OSC_NOCLKOUT     EQU     3FFCh
    28. _EXTCLK_OSC             EQU     3FEFh
    29. _LP_OSC                 EQU     3FECh
    30. _XT_OSC                 EQU     3FEDh
    31. _HS_OSC            EQU     3FEEh
    32.  
    33.     __CONFIG        _BODEN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_ON & _INTRC_OSC_NOCLKOUT
    34.  
    35. ;==========================================================================
    36. ;
    37. ;       Set Up Constants
    38. ;
    39. ;==========================================================================
    40.  
    41. STATUS          EQU    03h    ; Address of the status register
    42. TRISA           EQU    85h    ; Address of the TRISA register
    43. TRISB           EQU    86h    ; Address of the TRISB register
    44. PORTA           EQU    05h    ; Address of the Port A
    45. PORTB           EQU    06h    ; Address of the Port B
    46. COUNT1          EQU    20h    ; Address of the first general purpose register
    47. COUNT2          EQU    21h    ; Address of the second genral purpose register
    48.  
    49. ;==========================================================================
    50. ;
    51. ;       Start Of Program
    52. ;
    53. ;==========================================================================
    54.  
    55. ;*****Set Up the port*****;
    56.  
    57.           BSF       STATUS, 5    ; Set bit 5 of the status register - move to bank 1
    58.           MOVLW     b'00001111'  ; Move 00001111 into the W register
    59.           MOVWF     TRISA        ; Move the value is the W register to TRISA register RA0-RA3 as inputs
    60.           MOVLW     b'00000000'  ; Move 00000000 into the W register
    61.           MOVWF     TRISB        ; Move the value is the W register to TRISA register RB0-RB7 outputs
    62.           BCF       STATUS, 5    ; Clear bit 5 of the status register - move back to bank 0
    63.  
    64. ;*****Turn On The LED*****;
    65.  
    66. Start     CALL      LED_On        ; Call LED on subroutine
    67.           CALL      Delay         ; Call delay subroutine
    68.           CALL      LED_Off       ; Call LED Off Subroutine
    69.           CALL      Delay         ; Call delay subroutine
    70.  
    71. ;****Now go back to the start of the program****  
    72.  
    73.           GOTO    Start        ;go back to Start and turn LED on again
    74.  
    75. ;****Subroutines****
    76.  
    77. ;-----Subroutine To Turn LED On-----
    78. LED_On    MOVLW    b'00000001'    ; Turn the LED on by first putting
    79.           BTFSC    PORTA,0        ; If RA0  is set, skip next line
    80.           MOVLW    b'00000011'    ; Turn the LED on by first putting
    81.           MOVWF   PORTB           ; it into the w register and then on the port
    82.  
    83.           return
    84. ;-----End Of Subroutine-----
    85.  
    86. ;-----Subroutine To Turn LED OFF-----
    87. LED_Off    MOVLW    b'00000000'    ; Turn the LED off by first putting    
    88.           MOVWF    PORTB        ; it into the w register and then on the port
    89.  
    90.           return
    91. ;-----End Of Subroutine-----
    92.  
    93. ;-----Subroutine To Turn Incur Delay-----
    94. Delay    MOVLW    b'11111111'    ; Moves 11111111 into W register
    95.           MOVWF    COUNT1        ; Moves the value of W into COUNT1
    96.           MOVWF    COUNT2        ; Moves the value of W into COUNT2
    97.  
    98. Loop    DECFSZ    COUNT1,1
    99.           GOTO    Loop
    100.           DECFSZ    COUNT2,1
    101.           GOTO    Loop
    102.  
    103.           return
    104. ;-----End Of Subroutine-----
    105.  
    106. ;****End of the subroutines****
    107.  
    108. ;****End of the program****  
    109.  
    110.           end            ;Needed by some compilers, and also just in case we miss the goto instruction.
    111.  
    Basically, in the subroutine which turns the LED on, b'00000001 is loaded into the W register, then RA0 is checked,in this version to see if the bit is clear, if it is set, the next line b'00000011' is skipped .... Therefore, if it is clear I should get 2 leds, if it is set, only one.

    I'm using a PIC 16F627 on the Velleman VM111 experiment/programming board. On this board, the LEDS are wired to RB0-RB5, and four switches to RA0-RA3 ..... My problem is that I only get one LED flashing in this version. If I change the bit check command to BTFSS instead of BTFSC, I get 2 LEDs, but pressing buttons does nothing ..... I'm probably doing something stupid, but I can't sleep until it stops taunting me!!!

    Does anyone have any insight into where I'm going wrong?? ... Feel free to make fun of coding style, criticism is the only way I'll learn. I know I should have used the include file instead of my own hardcoded constants, I'll sort that soon :)

    Cheers everyone
     
  2. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    First learn to use an include file. Also MPLAB has a simulator that can help debug your program.
    I have a simple LED flash example in my Inchworm quick project poster on my site.
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    PICs with analog/digital ports (as portA is in the 16F627) default analog. You have to tell the PIC to make the ports digital by turning off the comparator function for the port in your port setup. e.g.
    Code ( (Unknown Language)):
    1. MOVLW 0x07 ;Turn comparators off and
    2. MOVWF CMCON ;enable pins for I/O
     
  4. CoximusPrime

    Thread Starter New Member

    Jun 21, 2009
    5
    0

    Superstar!!!! .... works fine. I'll have a good read of the datasheet now to try and understand the comparator function ..... And I've altered it to pull in constants from the include file now too :)

    Cheers guys
     
Loading...