Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] pic16f688 pull-up problem

Status
Not open for further replies.

creativefla

Newbie level 4
Joined
Sep 3, 2011
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,338
hi today i tried to build a simple program that turns on LED when a button is push, using weak pull-up feature

Code:
 list       F=inhx8m, P=16F688, R=hex, N=0

#include P16F688.INC

	__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CPD_OFF & _BOD_OFF & _IESO_ON & _FCMEN_ON

;definitions ---------------------------

bank0	macro
		bcf	status, rp1
		bcf status, rp0
		endm
bank1	macro
		bcf	status,	rp1
		bsf	status,	rp0
		endm
bank2	macro
		bsf	status, rp1
		bcf status, rp0
		endm
bank3	macro
		bsf status, rp1
		bsf status, rp0
		endm

;#Define ButtonInput	PORTA,0;
#Define Switch1	PORTA,0
#Define Switch2 PORTA,1
#Define Switch3 PORTA,2
#Define Output1 PORTC,0
#Define Output2 PORTC,1
#Define Output3 PORTC,2
; RAM preserved ----------------------------------
	cblock 0x20
	endc

;program memory ------------------------------
	org		0x00
	goto	Init

;Inuterrupt service routine -------------------
	org		0x04


;initialisation ----------------------------
Init

				bank1
				movlw	b'01100001'
				movwf	OSCCON  ;set oscillator
				clrf	ansel
				movlw	b'00000111'
				movwf	CMCON0
				bank0

;main prog ---------------------------

start
		bank1
		movlw b'00000111'
		movwf TRISA

		movlw b'11111000'
		movwf TRISC
		bank0


		bank1
		bcf	OPTION_REG, NOT_RAPU ;enable pull up resistor for PORTA,
		movlw b'00110111'
		movwf WPUA
		bank0

mainloop

		btfss	switch3
		goto	switch_3_is_0
		bcf		output3
		goto	check_switch_2

switch_3_is_0

		bsf		output3

check_switch_2

		btfss	switch2
		goto	switch_2_is_0
		bcf		output2
		goto	check_switch_1

switch_2_is_0

		bsf		output2

check_switch_1

		btfss	switch1
		goto	switch_1_is_0
		bcf		output1
		goto	mainloop

switch_1_is_0

		bsf		output1
		goto	mainloop

fins

	end

my program is working under Real PIC simulator but not working on board. my program only works for pin RC2, not RC1 and RC0. for both RC1 and RC0, they always give high value even though i dont press any button. ive skimmed through datasheet so many times yet i couldnt find any answer.
 
Last edited:

hi guys, finally i today i managed to get it working. it seem to be the order of initialisation? i dont know.

Code:
Init

				bank1
				movlw	b'01100001'
				movwf	OSCCON  ;set oscillator
				clrf	ansel
				movlw	b'00000111'
				movwf	CMCON0
				bank0

;main prog ---------------------------

start
		bank1
		movlw b'00000111'
		movwf TRISA

		movlw b'11111000'
		movwf TRISC
		bank0

i changed them to

Code:
		BANKSEL PORTA ;
		CLRF PORTA ;Init PORTA
		MOVLW 07h ;
		MOVWF CMCON0 ;
		BANKSEL ANSEL ;
		CLRF ANSEL ;digital I/O
		MOVLW b'00000111' ;
		MOVWF TRISA ;

		BANKSEL PORTC
		CLRF	PORTC
		BANKSEL	TRISC
		movlw b'11111000'
		movwf TRISC
		bank0

is the order of initialisation that important or do i really have to clear PORTX everytime i want to configure TRISX? thanks






edit: nvm, ive just realised that CMCON0 is in different register bank from ANSEL. silly me. sorry =((
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top