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.

PortB woes

Status
Not open for further replies.

dion

New Member
After running the following piece of code, PORTB has the following value '11000000', even though I specifically clear them. What am I doing wrong or not doing. I am running this prog under sim. No devices added. The pic is a 16F887. Thanks.

banksel ANSELH
clrf ANSELH
banksel ANSEL
clrf ANSEL
clrf CM1CON0
clrf CM2CON1

banksel TRISA
clrf TRISA ;Now need to set ports on PortA to output

;banksel TRISB
BSF STATUS, 5
movlw B'11111000'
movwf TRISB ;Now need to set ports on PortB to input/output

;movlw h'60'
;movwf OSCCON

BCF OPTION_R,7 ;Enable internal pull-ups on PortB

banksel PORTA ;Return to bank0
CLRF PORTA ;Clear bits in PortA

;banksel PORTB
BCF STATUS, 5
nop
CLRF PORTB
 
What makes you think that value is wrong? You have bits 6 and 7 as inputs and WPUs on. I'm surprised you don't get 11110000.

BTW, it's always a good idea to post complete code as the error often lies elsewhere.

Mike.
 
Please excuse my ignorance, but if bits 4 and 5 are also input, why does PORTB not reflect the following - '11111000'. I get the previous result, even with pull ups disabled. I am trying to follow the logic in a 3x4 keypad tutorial. All code is listed below. Thanks.


;Equates Section
;**********************************************************************
;STATUS EQU 3 ;Used to switch banks
;PORTA EQU 5 ;To comm with outside world
;PORTB EQU 6
;TRISA EQU 85H ;Used to set pins on port to input or output
;TRISB EQU 86H
OPTION_R EQU 81H ;This option handles the pull-ups

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

PROCESSOR 16f887 ; list directive to define processor
#include <p16f887.inc> ; processor specific variable definitions


; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V

;***** VARIABLE DEFINITIONS (examples)

Cblock 0x20 ; First free RAM location
endc ; No variables


;**********************************************************************
RESET_VECTOR CODE 0x0000 ; processor reset vector
nop
goto Start ; go to beginning of program

INT_VECTOR CODE 0x0004 ; interrupt vector location
pagesel Start
goto Start


;MAIN_PROG

;Config Section
Start
;bsf STATUS, 5 ;Turns to bank1. Note that bit 5 in the SFR Status is
;RP0. RP0 and RP1 together determine which bank is selected,
;eg RP1 RP0 Bank
; 0 0 0
; 0 1 1 etc.


banksel ANSELH
clrf ANSELH
banksel ANSEL
clrf ANSEL
clrf CM1CON0
clrf CM2CON1

banksel TRISA
clrf TRISA ;Now need to set ports on PortA to output

;banksel TRISB
bsf STATUS, 5
movlw B'11111000'
movwf TRISB ;Now need to set ports on PortB to input/output

;movlw h'60'
;movwf OSCCON

;bcf OPTION_R,7 ;Enable internal pull-ups on PortB

banksel PORTA ;Return to bank0
CLRF PORTA ;Clear bits in PortA

;banksel PORTB
bcf STATUS, 5
nop
CLRF PORTB ;Clear bits in PortB

;Program starts now
Column1
;{

clrf PORTB
bsf PORTB,0
bcf PORTB,1
bcf PORTB,2

;}

Check_Key1
;{

btfss PORTB,3 ;Check if bit 3 of PortB is clear
goto Check_Key4 ;Key 1 not pressed
movlw d'1' ;Key 1 was pressed
movwf PORTA ;Display a '1' on LEDs

;}

Check_Key4

;{
btfss PORTB,4 ;Check if bit 4 of PortB is high(=1)
goto Check_Key7 ;Key 4 not pressed
movlw d'4' ;Key 4 was pressed
movwf PORTA ;Display a '4' on LEDs

;}

Check_Key7

btfss PORTB,5 ;Check if bit 5 of PortB is high
goto Check_Key10 ;Key 7 not pressed
movlw d'7' ;Key 7 was pressed
movwf PORTA ;Display a '7' on LEDs

Check_Key10

btfss PORTB, 6 ;Check if bit 3 of PortB is clear
goto Column2 ;Key 10 not pressed
movlw d'10' ;Key 10 was pressed
movwf PORTA ;Display a '1' on LEDs

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

Column2

clrf PORTB
bcf PORTB, 0
bsf PORTB, 1
bcf PORTB, 2

CHECK_KEY2
btfss PORTB, 3 ;Check if bit 3 of PortB is clear
goto CHECK_KEY5 ;Key 2 not pressed
movlw d'2' ;Key 2 was pressed
movwf PORTA ;Display a '1' on LEDs

CHECK_KEY5
btfss PORTB, 4 ;Check if bit 3 of PortB is clear
goto CHECK_KEY8 ;Key 5 not pressed
movlw d'5' ;Key 5 was pressed
movwf PORTA ;Display a '1' on LEDs

CHECK_KEY8
btfss PORTB, 5 ;Check if bit 3 of PortB is clear
goto CHECK_KEY11 ;Key 8 not pressed
movlw d'8' ;Key 8 was pressed
movwf PORTA ;Display a '1' on LEDs

CHECK_KEY11
btfss PORTB, 6 ;Check if bit 3 of PortB is clear
goto COLUMN3 ;Key 10 not pressed
movlw d'11' ;Key 10 was pressed
movwf PORTA ;Display a '1' on LEDs
;************************************************************

COLUMN3

clrf PORTB
bcf PORTB, 0
bcf PORTB, 1
bsf PORTB, 2

CHECK_KEY3
btfss PORTB, 3 ;Check if bit 3 of PortB is clear
goto CHECK_KEY6 ;Key 3 not pressed
movlw d'3' ;Key 3 was pressed
movwf PORTA ;Display a '1' on LEDs

CHECK_KEY6
btfss PORTB, 4 ;Check if bit 3 of PortB is clear
goto CHECK_KEY9 ;Key 6 not pressed
movlw d'6' ;Key 6 was pressed
movwf PORTA ;Display a '1' on LEDs

CHECK_KEY9
btfss PORTB, 5 ;Check if bit 3 of PortB is clear
goto CHECK_KEY12 ;Key 9 not pressed
movlw d'9' ;Key 9 was pressed
movwf PORTA ;Display a '1' on LEDs

CHECK_KEY12
btfss PORTB, 6 ;Check if bit 3 of PortB is clear
goto Column1 ;Key 12 not pressed
movlw d'12' ;Key 12 was pressed
movwf PORTA ;Display a '1' on LEDs
goto Column1 ;Do this forever


END ; directive 'end of program'
 
I only got to the 5th instruction before I found the first error,
Code:
		banksel	ANSELH		;select bank 3
		clrf	ANSELH
		banksel	ANSEL		;select bank 3 again
		clrf	ANSEL
		clrf	CM1CON0		;this is in bank 2
		clrf	CM2CON1		;and this

I personally don't like the banksel macro as it wastes a lot of space and I prefer to keep track of banking myself. So I would write,
Code:
		bsf	STATUS,RP0	;bank 1
		bsf	STATUS,RP1	;bank 3
		clrf	ANSELH
		clrf	ANSEL
		bcf	STATUS,RP0	;bank 2
		clrf	CM1CON0		
		clrf	CM2CON1

Also, when posting code use the code tags. Type [code] before your code and [/code] after it.

Mike.
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top