Capacitance meter using pic16f88 help please

Thread Starter

Supernova

Joined Nov 19, 2012
2
I really need help on an collage assignment that I have to do, but there are a few things that are not working right, but I do not know what is wrong with it. Can somebody just look over my code and give any advice. Thanx in advance

sorry that there is no comments

Rich (BB code):
	List p=PIC16F88
	#INCLUDE <P16F88.INC>

	__CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
  	   __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF
;------------------------------------------------------------------------------------------------------------------------------------------------------------  	
current_state 		 equ 0x20   
cap_current_state 	 equ 0x25
charge_time		 equ 0x26
WORK_REG_PORTA   	 equ 0x22
WORK_REG_PORTB 	 equ 0x23
;------------------------------------------------------------------------------------------------------------------------------------------------------------
digit1_state 				 equ	0
digit2_state 			 	 equ	1
digit3_state 				 equ 	2
digit4_state 				 equ	3
cap_wait_state 			            equ 	0
cap_reset_state 			 equ 	1
cap_test_state 			            equ 	2
cap_tested_state			 equ	3

;------------------------------------------------------------------------------------------------------------------------------------------------------------
W_TEMP        			 equ  0x7D  ; w register for context saving (ACCESS)
STATUS_TEMP   		 equ  0x7E  ; status used for context saving (ACCESS)
PCLATH_TEMP    		 equ  0x7F  ; variable used for context saving
;------------------------------------------------------------------------------------------------------------------------------------------------------------
number_thousands		equ 0x27
number_hundreds		equ 0x28
number_tens 			equ 0x29
number_units			equ 0x30
	
;------------------------------------------------------------------------------------------------------------------------------------------------------------
START      
	ORG 0x0000
       goto initialize
;------------------------------------------------------------------------------------------------------------------------------------------------------------     
       ORG 0x0004
ISR: 
;******************************
	movwf   W_TEMP            ; save off current W register contents
       movf    STATUS,W          ; move status register into W register
       movwf   STATUS_TEMP       ; save off contents of STATUS register
       movf    PCLATH,W          ; move pclath register into W register
       movwf   PCLATH_TEMP       ; save off contents of PCLATH register
;******************************

 	bcf INTCON, TMR0IF
 	 	
	call STATE_CAP_TEST
	call NUM2DIGIT
	call STATE_DIGITS



;******************************
	movf    PCLATH_TEMP,W     ; retrieve copy of PCLATH register
       movwf   PCLATH            ; restore pre-isr PCLATH register contents
       movf    STATUS_TEMP,W     ; retrieve copy of STATUS register
       movwf   STATUS            ; restore pre-isr STATUS register contents
       swapf   W_TEMP,F	 ; restore pre-isr W register contents
       swapf   W_TEMP,W   
;******************************
	retfie     
;------------------------------------------------------------------------------------------------------------------------------------------------------------     
initialize:
	
 	 banksel TRISB
        movlw b'00000000'
        movwf TRISB
	 movlw b'01000000'
	 movwf TRISA
	 
	 movlw b'00000000'
	 movwf OSCCON
	 movlw b'00000111'
 	 movwf OPTION_REG
	 banksel PORTA
	 clrf PORTA
	 clrf PORTB
	 clrf current_state
	 clrf cap_current_state
	 banksel INTCON	 
	 bcf INTCON,TMR0IF
	 bsf INTCON,TMR0IE
	 bsf INTCON,GIE
	 
	init_loop:
	goto init_loop
		 
;***********************************************************************************************************************	 
STATE_DIGITS:

	clrz 

	movf	digit1_state,w
	xorwf	current_state, w
	btfsc	STATUS, Z
	goto 	digit1
	
	clrz 
	movf	digit2_state,w
	xorwf	current_state, w
	btfsc	STATUS, Z
	goto 	digit2
	
	clrz 
	movf	digit3_state,w
	xorwf	current_state, w
	btfsc	STATUS, Z
	goto 	digit3
	
	goto 	digit4
	
;=========================================

digit1:	
	
	banksel PORTB
	bcf	PORTB, RB5
	movlw	high	LED_DISP_LUT		
	movwf	PCLATH
	call NUM2DIGIT
	movf number_thousands,w	
	call 	 LED_DISP_LUT
	call  LED_OUTPUT
	movf    digit2_state,w
	movwf  current_state   
	bsf 	PORTB, RB2    
	goto  state_end
	
digit2:
	banksel PORTB
	bcf PORTB, RB2
	movlw	high	LED_DISP_LUT		
	movwf	PCLATH
	call NUM2DIGIT
	movf number_hundreds,w
	call LED_DISP_LUT
	call LED_OUTPUT
	movf      digit3_state,w
	movwf      current_state  
	bsf PORTB, RB3 
	goto state_end
	
digit3:
	banksel PORTB
	bcf PORTB, RB3
	movlw	high	LED_DISP_LUT		
	movwf	PCLATH
	call NUM2DIGIT
	movf number_tens,w
	call LED_DISP_LUT
	call LED_OUTPUT
	movf      digit4_state,w
	movwf      current_state  
	bsf PORTB, RB4
	goto state_end
	
digit4
	banksel PORTB
	bcf PORTB, RB4
	movlw	high	LED_DISP_LUT		
	movwf	PCLATH
	call NUM2DIGIT
	movf number_units,w
	call LED_DISP_LUT
	call LED_OUTPUT
	movf     digit1_state,w
	movwf      current_state  
	bsf PORTB, RB5
	goto state_end

state_end:
	return
	 
;***********************************************************************************************************************	 
STATE_CAP_TEST:
     
      clrz 
     	movf	cap_wait_state,w
	xorwf	cap_current_state, w
	btfsc	STATUS, Z
	goto 	wait_cap_read
	
	clrz 
	movf	cap_reset_state,w
	xorwf	cap_current_state, w
	btfsc	STATUS, Z
	goto 	cap_reset
	
	clrz 
	movf	cap_test_state,w
	xorwf	cap_current_state, w
	btfsc	STATUS, Z
	goto 	cap_test
	
	goto 	cap_tested
	
;=========================================
	
	
	
	
	
wait_cap_read:
	banksel PORTA
	bsf PORTA,RA7
	nop	
	bcf PORTA,RA7
	btfss PORTA, RA6
	goto capstate_end
	movf   cap_reset_state,w 
	movwf   cap_current_state
	goto capstate_end 
    
cap_reset:
	banksel PORTA
       bsf PORTA,RA7 
       movf    cap_test_state,w 
	movwf      cap_current_state
	goto capstate_end
	
cap_test:
	banksel T1CON
	btfsc PORTA, RA6
       goto capstate_end
       bcf PORTA,RA7 
       bsf T1CON, TMR1ON
       movf cap_tested_state,w
       movwf cap_current_state
       goto capstate_end
	
cap_tested:
	banksel T1CON
	btfss PORTA, RA6
	goto capstate_end
	bcf T1CON, TMR1ON
	movf cap_wait_state,w
	movwf      cap_current_state
	;clrf TMR1L
	;clrf TMR1H
	goto capstate_end
	
capstate_end 
        return
;***********************************************************************************************************************    
	; Bit Number      6 10 43210
	;   PORTB,6 =>  0 00 00000 <=PORTA
	;    	           ^^^ < = PORTB        
	
LED_OUTPUT
	movwf WORK_REG_PORTA
	movwf WORK_REG_PORTB
	banksel PORTA
;=============================	
	; Bit Number      6 10 43210
	;  PORTB,6 =>  0 00 00000 <=PORTA
	;    		  ^ < = PORTB       
	
	btfsc WORK_REG_PORTB, 7
	bsf PORTB, RB6	    	
	movlw b'01100000'
	andwf WORK_REG_PORTB,f
	
	rrf WORK_REG_PORTB,f
	rrf WORK_REG_PORTB,f
	rrf WORK_REG_PORTB,f
	rrf WORK_REG_PORTB,f
	rrf WORK_REG_PORTB,f
	
	movf WORK_REG_PORTB,w
	movlw PORTB
;=============================	
	; Bit Number      6 10 43210
	;  PORTB,6 =>  0 00 00000 <=PORTA
	;    				  ^ < = PORTB       
	
	movlw b'00011111'
	andwf WORK_REG_PORTA,f
	movf WORK_REG_PORTA,w
	movlw PORTA

	return
	
;***********************************************************************************************************************  
	
NUM2DIGIT:

thousands_loop
	clrc
	banksel TMR1L
	movlw 1000
	subwf TMR1L,f
	btfss STATUS,C
	goto thousands_loop
	addwf TMR1L
	movlw .1
	addwf number_thousands
		
hundreds_loop:
	clrc
	movlw .100
	subwf TMR1L,f
	btfss STATUS,C
	goto hundreds_loop
	addwf TMR1L
	movlw .1
	addwf number_hundreds
	
tens_loop:
	clrc
	movlw .10
	subwf TMR1L,f
	btfss STATUS,C
	goto tens_loop
	addwf TMR1L
	movlw .1
	addwf number_hundreds

units_loop:
	clrc
	movf TMR1L,w
	movlw number_units

	return
;***********************************************************************************************************************  	
LED_DISP_LUT:
	; Bit Number      6 10 43210
	;  PORTB,6 =>  0 00 00000 <=PORTA
	;    				  ^ < = PORTB           
	addwf	PCL, f
	retlw	b'00111111' ;0         
	retlw	b'00000110' ;1
	retlw	b'01011011' ;2 
	retlw	b'01001111' ;3
	retlw	b'01100110' ;4
	retlw	b'01101101' ;5
	retlw	b'01111101' ;6
	retlw	b'00000111' ;7
	retlw	b'01111111' ;8
	retlw	b'01101111' ;9
	retlw     b'10000000' ; .
	
end_loop:
	goto end_loop
	
	end
 

ErnieM

Joined Apr 24, 2011
8,377
No comments, also no description of the issue. And no schematic either.

<accessing mind reading and other divination techniques>

Nope, got nothing for you.

Break your project down to smaller pieces. You make a reading, convert that to decimal, then output the results. Try each piece in turn by forcing known values. I'd work this backwards, get the routine that outputs the results to work from a known number you hard code in. Once that works, give the reading conversion routine a whack. Finally check the measurement portion .

If you have a PICkit you may want to try some in circuit debugging so you can at least see what number is coming back from your reading. You can also use it to force register values without rebuilding the program over and over, though that is what I frequently do when debugging & testing code.
 

tshuck

Joined Oct 18, 2012
3,534
I agree with ErnieM, you can't just throw code out there without any sort of reference, comments, schematic, justification for doing something, etc. and expect help. We need help to be able to help you.

How are you trying to measure capacitance? Why do you think this will work? What effort have you put it, since, as far as we know, you just copied this code from a classmate...

You can step through your program and see everything that is happening with a pickit(with the right devices...), it is a good investment...
 

MrChips

Joined Oct 2, 2009
30,807
Lesson #1 - Writing code is the last thing you do.

Step #1 - Start off by defining the problem - i.e. your goals
Step #2 - Draw a block diagram
Step #3 - Draw a flow chart

Do these steps before writing code and then you will have a chance that someone will help you.
 

thatoneguy

Joined Feb 19, 2009
6,359
A Schematic that shows which pins are connected to the ports, as well as commented code would help.

A bigger help would be going into detail about "some problems with it", as stated many times, the more information you provide, the more assistance you'll receive.

Combine this info with the rest of the replies above.
 

Thread Starter

Supernova

Joined Nov 19, 2012
2
Hello there thanx for all the helpful advice, It is my first time doing this so i didnt know where to start. And NO tshuck i did not copy it from a classmate that was my attempt at writing a code. I will try and solve my problems using all the advice I received. If I have a problem ill ask it properly. Thanx again
 
You need to discharge the capacitor to start with.
After it is discharged start charging it up and measure how much time it takes to reach a threshold. Have a timeout on this in case the cap doesn't charge up.
 

tshuck

Joined Oct 18, 2012
3,534
Hello there thanx for all the helpful advice, It is my first time doing this so i didnt know where to start. And NO tshuck i did not copy it from a classmate that was my attempt at writing a code. I will try and solve my problems using all the advice I received. If I have a problem ill ask it properly. Thanx again
I did not accuse you of copying it. All I said was that we don't know one way or the other, so why would we help when you could have put no effort in already?
 
Top