Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

will this code work [PIC16F628]?

Status
Not open for further replies.

Fletcher

New Member
im trying to design a program for a pic 16f628 which will read five switches from portB (RB0 - RB4) and then output the binary equivalent to 3 leds on portb (RB5 - RB7). Eg, if switch 5 was high then the output would be 101.

I have written this program but have no equipment to test it at the moment. Im Just wondering if anyone can look it over and see whether im on the right track. Thanks.

Code:
;*************************************************************
;SETUP PROCEDURE
ORG 0X000

BANK1
MOVLW 0XE0
MOVWF TRISB			;SET PORTB I/O PINS TO 5 INPUTS (RB0 - RB4) AND 3 OUTPUTS (RB5 - RB7)
BANK0

;************************************************************
;MAINLINE PROCEDURE
MAIN
MOVF PORTB,0        ;GET DATA ON PORTB
ANDLW 0X1F          ;MASK LAST 3 BITS OF PORTB (RB5 - RB7)
CALL TABLE          ;CALL TABLE AND GET BINARY VALUE
MOVWF PORTB         ;MOVE VALE RETURNED FROM TABLE TO PORTB
GOTO MAIN           ;START AGAIN

;*************************************************************
;LOOK UP TABLE PROCEDURE
TABLE
ADDWF PCL			;ADD VALUE FROM PORTB TO PROGRAM COUNTER
RETLW 0XFF
RETLW 0X20			;VALUE FOR 001 
RETLW 0X40			;VALUE FOR 010
RETLW 0XFF
RETLW 0X60			;VALUE FOR 011
RETLW 0XFF
RETLW 0XFF
RETLW 0XFF
RETLW 0X80			;VALUE FOR 100
RETLW 0XFF
RETLW 0XFF
RETLW 0XFF
RETLW 0XFF
RETLW 0XFF
RETLW 0XFF
RETLW 0XFF
RETLW 0XA0			;VALUE FOR 101

END
 
Since the input uses 5 bits, there are 32 possible values. The computed goto table has only 17 entries. This means for input values greater than 16 (e.g. 10001), the program will end up beyond the jump table.

If no code exists beyond it, the PIC will execute the ANDLW 0xFF instruction (0x3FFF). The program counter will eventually overflow and restart at 0 and nothing harmless will result.

However, if other code exists beyond it, ...... :?: :!: :?
 
ok thanks for that, ill take your advice into account.
Does the rest look ok? Its just that im not too sure about using one port for inputs and outputs.
 
Oops, a 1 in the TRIS register sets up the pin as input while a zero is output. For correct operation, your code should read as:


Code:
    MOVLW  b'00011111'  ;  It was 0xE0.  It should be 0x1F
    MOWF   TRISB
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top