VERY basic PIC problem

Thread Starter

CoximusPrime

Joined Jun 21, 2009
7
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

Rich (BB code):
;========================================================================== 
; 
;       Configuration Bits 
; 
;========================================================================== 
 
_BODEN_ON               EQU     3FFFh 
_BODEN_OFF              EQU     3FBFh 
_CP_ALL                 EQU     03FFh 
_CP_75                  EQU     17FFh 
_CP_50                  EQU     2BFFh 
_CP_OFF                 EQU     3FFFh 
_DATA_CP_ON             EQU     3EFFh 
_DATA_CP_OFF            EQU     3FFFh 
_PWRTE_OFF              EQU     3FFFh 
_PWRTE_ON               EQU     3FF7h 
_WDT_ON                 EQU     3FFFh 
_WDT_OFF                EQU     3FFBh 
_LVP_ON                 EQU     3FFFh 
_LVP_OFF                EQU     3F7Fh 
_MCLRE_ON               EQU     3FFFh 
_MCLRE_OFF              EQU     3FDFh 
_ER_OSC_CLKOUT          EQU     3FFFh 
_ER_OSC_NOCLKOUT        EQU     3FFEh 
_INTRC_OSC_CLKOUT       EQU     3FFDh 
_INTRC_OSC_NOCLKOUT     EQU     3FFCh 
_EXTCLK_OSC             EQU     3FEFh 
_LP_OSC                 EQU     3FECh 
_XT_OSC                 EQU     3FEDh 
_HS_OSC            EQU     3FEEh 
 
    __CONFIG        _BODEN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_ON & _INTRC_OSC_NOCLKOUT 
 
;========================================================================== 
; 
;       Set Up Constants 
; 
;========================================================================== 
 
STATUS          EQU    03h    ; Address of the status register 
TRISA           EQU    85h    ; Address of the TRISA register 
TRISB           EQU    86h    ; Address of the TRISB register 
PORTA           EQU    05h    ; Address of the Port A 
PORTB           EQU    06h    ; Address of the Port B 
COUNT1          EQU    20h    ; Address of the first general purpose register 
COUNT2          EQU    21h    ; Address of the second genral purpose register 
 
;========================================================================== 
; 
;       Start Of Program 
; 
;========================================================================== 
 
;*****Set Up the port*****; 
 
          BSF       STATUS, 5    ; Set bit 5 of the status register - move to bank 1 
          MOVLW     b'00001111'  ; Move 00001111 into the W register 
          MOVWF     TRISA        ; Move the value is the W register to TRISA register RA0-RA3 as inputs
          MOVLW     b'00000000'  ; Move 00000000 into the W register 
          MOVWF     TRISB        ; Move the value is the W register to TRISA register RB0-RB7 outputs 
          BCF       STATUS, 5    ; Clear bit 5 of the status register - move back to bank 0 
 
;*****Turn On The LED*****; 
 
Start     CALL      LED_On        ; Call LED on subroutine
          CALL      Delay         ; Call delay subroutine 
          CALL      LED_Off       ; Call LED Off Subroutine 
          CALL      Delay         ; Call delay subroutine
 
;****Now go back to the start of the program****  
 
          GOTO    Start        ;go back to Start and turn LED on again 

;****Subroutines**** 

;-----Subroutine To Turn LED On-----
LED_On    MOVLW    b'00000001'    ; Turn the LED on by first putting
          BTFSC    PORTA,0        ; If RA0  is set, skip next line
          MOVLW    b'00000011'    ; Turn the LED on by first putting 
          MOVWF   PORTB           ; it into the w register and then on the port

          return
;-----End Of Subroutine-----

;-----Subroutine To Turn LED OFF-----
LED_Off    MOVLW    b'00000000'    ; Turn the LED off by first putting     
          MOVWF    PORTB        ; it into the w register and then on the port 

          return
;-----End Of Subroutine-----

;-----Subroutine To Turn Incur Delay-----
Delay    MOVLW    b'11111111'    ; Moves 11111111 into W register
          MOVWF    COUNT1        ; Moves the value of W into COUNT1
          MOVWF    COUNT2        ; Moves the value of W into COUNT2
 
Loop    DECFSZ    COUNT1,1
          GOTO    Loop
          DECFSZ    COUNT2,1
          GOTO    Loop

          return
;-----End Of Subroutine----- 
 
;****End of the subroutines**** 

;****End of the program****  
 
          end            ;Needed by some compilers, and also just in case we miss the goto instruction.
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
 
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.
 

AlexR

Joined Jan 16, 2008
732
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.
Rich (BB code):
MOVLW 0x07 ;Turn comparators off and
MOVWF CMCON ;enable pins for I/O
 

Thread Starter

CoximusPrime

Joined Jun 21, 2009
7
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.
Rich (BB code):
MOVLW 0x07 ;Turn comparators off and
MOVWF CMCON ;enable pins for I/O

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
 
Top