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.

Validating Code Without PIC

Status
Not open for further replies.

burg

New Member
Hey,

I have a program that needs to be tested, it's an LED tachometer which runs 5 LED's depending on the input signal (0-5V) and it also tests for Oil pressure and Water temperature. If those two values are below or above a certain value, an LED each will come on.

I need to test the program output, but I don't know how to do this without the PIC (16F88) to test it on. Is there any program that can take an input and give me the programs output?

Also, when using the A/D converts, with a 4Mhz clock, setting the A/D to the interal clock of the A/D will be ok?

Here is my code if anyone wants to look over it. Thanks a lot!

Code:
;***************************************************************
;	Title:	"PIC Dash Controller"
;	Date;	 April 7th, 2007
;***************************************************************
	
;REVIEW A/D CONVERSION CLOCK SETTING

		list				p=16F88
		include				"p16F88.inc"
		errorlevel			-302
		__CONFIG    		_CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO

;***************************************************************
;	Initializations
;***************************************************************


Word		equ			0x30		;general memory	


;****************************************************************
;	System initialization
;****************************************************************

			org			0x0000
			goto		SYSTEMINIT
			nop
			nop
			nop

SYSTEMINIT	clrf		PORTB		;clear contents of PORTB, making it 0
			bsf			STATUS,RP0	;select bank 1
			
			bsf			OSCCON,6	;Set oscilator to 4Mhz 
			bsf			OSCCON,5
			movlw		0x07
			
			movwf		CMCON		;Turn off Comparators
			
			movlw		b'00000000'	;set PortB all outputs
			movwf		TRISB
			movlw		b'11111111'	;set PortA all inputs
			movwf		TRISA		
			movlw		b'00000111'	;sets RA0-RA2 to A/D inputs
			movwf		ANSEL		;
			bcf			STATUS,RP0	;select bank 0

;Set ADCON1
			bsf			STATUS,RP0
    		movlw   	b'10000000' ;right justified, no clock divide, Vdd and Vcc as reference
    		movwf   	ADCON1
    		bcf			STATUS,RP0

;***************************************************************
;	Main Program
;***************************************************************

START   	call 		INIT_AN0	;initialize AN0
			call		READ		;read the input of AN0
			
			goto		TACHRESULT	;find out tach result
			
TESTOIL		call		DELAY
			
			call		INIT_AN1	;initialize AN1
			call		READ		;read the input of AN1
			goto		OILRESULT	;find out oil pressure result
			
TESTWATER	call		DELAY
			
			call 		INIT_AN2	;initialize AN2
			call		READ		;read the input of AN2
			goto		WATERRESULT	;find out water temperature result
	
LAST		call		DELAY
			goto		START
		

;****************************************************************
;	A/D Initializations
;****************************************************************


INIT_AN0	movlw		b'11000001'	;setting AN0 for tachometer sensor
			movwf		ADCON0		;move into ADCON0
			nop
			nop
			return
	
INIT_AN1	movlw		b'11001001'	;setting AN1 for water temperature
			movwf		ADCON0		;move into ADCON0
			nop
			nop
			return
	
INIT_AN2	movlw		b'11010001'	;setting AN2 for oil pressure sensor
			movwf		ADCON0		;move into ADCON0	
			nop
			nop
			return

;****************************************************************
;	Read in the analog input and convert to digital
;****************************************************************

READ   		call 		DELAY       	;small delay for A/D aquisition time.
    		bsf			ADCON0,GO_DONE	;initiate conversion
ADCDONE		btfsc   	ADCON0,GO_DONE
    		goto    	ADCDONE			;wait for ADC to finish
            
			bsf			STATUS,RP0		;select bank 1
			movf    	ADRESL,W        ;move results from ADRESL register to W
    		movwf   	Word            ;move value from W to memory named Word
    		bcf			STATUS,RP0      ;switch to bank 0
    		movf    	ADRESH,W        ;move results from ADRESH register to W
    		movwf   	Word+1          ;move value from W to memory named Word and add 1
			nop
    		nop
    		nop
    		return

;****************************************************************
;	Results (Volts)
;	Tach 	- X < 1 - 0 LED			
;		- 1 < X < 1.8 - 1 LED		- RB0
;		- 1.8 < X < 2.5 - 2 LED's	- RB0, RB1
;		- 2.5 < x < 3.3 - 3 LED's	- RB0, RB1, RB2
;		- 3.3 < x < 4.1 - 4 LED's	- RB0, RB1, RB2, RB3
;		- x > 4.1	- 5 LED's		- RB0, RB1, RB2, RB3, RB4
;	
;	Oil	- 0.9 < x < 3.9 - LED on 	- RB5
;
;	Water	- x < 1 - LED on		- RB6
;****************************************************************

;Testing for x < 1

TACHRESULT	movlw		low(0x00cc)     ; load W with value of low 0x00cc
	        subwf		Word,W          ; subtract value in file Word, with value in W
        	movlw		high(0x00cc)    ; load W with value of 0x00cc
          	btfss		STATUS,C        ; check to see if bit "C", in Status is set.
         	movlw		high(0x00cc)+1  ; load W with value high 0x00cc and add 1
        	subwf		Word+1,W        ; subtract value in file Word, with value in W
           	btfss		STATUS,C        ; carry clear if word is less than 0x00cc
	        goto		TESTOIL			; if below 1V, no LEDs on, check oil

;Testing for 1 < x < 1.8
         	
TESTRB0		movlw		low(0x0170)     ; load W with value of low 0x0170
	        subwf		Word,W          ; subtract value in file Word, with value in W
        	movlw		high(0x0170)    ; load W with value of 0x0170
          	btfss		STATUS,C        ; check to see if bit "C", in Status is set.
         	movlw		high(0x0170)+1  ; load W with value high 0x0170 and add 1
        	subwf		Word+1,W        ; subtract value in file Word, with value in W
           	btfss		STATUS,C        ; carry clear if word is less than 0x0170
	        goto		TESTRB0H
			goto		TESTRB1
	
TESTRB0H	movlw		low(0x0170)		;subtract low of 1.8 from Word
			subwf		Word,F			;put result into Word
			movlw		high(0x0170)	;subtract high of 1.8 from Word + 1
			subwf		Word+1,F		;put result into Word+1	

			movlw		low(0x00cd)
        	subwf		Word,W
        	movlw		high(0x00cd)
         	btfss		STATUS,C
         	movlw		high(0x00cd)+1
	        subwf		Word+1,W
        	btfsc		STATUS,C        ; carry set if word is greater or equal to 0x00cd
	        goto		RB0_ON

;Testing for 1.8 < x < 2.5

TESTRB1		movlw		low(0x01ff)     ; load W with value of low 0x01ff
	        subwf		Word,W          ; subtract value in file Word, with value in W
        	movlw		high(0x01ff)    ; load W with value of 0x01ff
          	btfss		STATUS,C        ; check to see if bit "C", in Status is set.
         	movlw		high(0x01ff)+1  ; load W with value high 0x01ff and add 1
        	subwf		Word+1,W        ; subtract value in file Word, with value in W
           	btfss		STATUS,C        ; carry not set if word is less than 0x01ff
	        goto		TESTRB1H
			goto		TESTRB2       	

TESTRB1H	movlw		low(0x01ff)		;subtract low of 1.8 from Word
			subwf		Word,F			;put result into Word
			movlw		high(0x01ff)	;subtract high of 1.8 from Word + 1
			subwf		Word+1,F		;put result into Word+1		
			
			movlw		low(0x0171)
        	subwf		Word,W
        	movlw		high(0x0171)
         	btfss		STATUS,C
         	movlw		high(0x0171)+1
	        subwf		Word+1,W
        	btfsc		STATUS,C        ; carry set if word is greater or equal to 0x0171
	        goto		RB1_ON

;Testing for 2.5 < x < 3.3
		
TESTRB2		movlw		low(0x02a3)     ; load W with value of low 0x02a3
	        subwf		Word,W          ; subtract value in file Word, with value in W
        	movlw		high(0x02a3)    ; load W with value of 0x02a3
          	btfss		STATUS,C        ; check to see if bit "C", in Status is set.
         	movlw		high(0x02a3)+1  ; load W with value high 0x02a3 and add 1
        	subwf		Word+1,W        ; subtract value in file Word, with value in W
           	btfss		STATUS,C        ; carry clear if word is less than 0x02a3
	        goto		TESTRB2H
			goto		TESTRB3

TESTRB2H	movlw		low(0x02a3)		;subtract low of 1.8 from Word
			subwf		Word,F			;put result into Word
			movlw		high(0x02a3)	;subtract high of 1.8 from Word + 1
			subwf		Word+1,F		;put result into Word+1	
         	
			movlw		low(0x0200)
        	subwf		Word,W
        	movlw		high(0x0200)
         	btfss		STATUS,C
         	movlw		high(0x0200)+1
	        subwf		Word+1,W
        	btfsc		STATUS,C        ; carry set if word is greater or equal to 0x0200
	        goto		RB2_ON

;Testing for 3.3 < x < 4.1

TESTRB3		movlw		low(0x0346)     ; load W with value of low 0x0346
	        subwf		Word,W          ; subtract value in file Word, with value in W
        	movlw		high(0x0346)    ; load W with value of 0x0346
          	btfss		STATUS,C        ; check to see if bit "C", in Status is set.
         	movlw		high(0x0346)+1  ; load W with value high 0x0346 and add 1
        	subwf		Word+1,W        ; subtract value in file Word, with value in W
           	btfss		STATUS,C        ; carry clear if word is less than 0x0346
	        goto		TESTRB3H
			goto		TESTRB4
         	
TESTRB3H	movlw		low(0x0346)		;subtract low of 1.8 from Word
			subwf		Word,F			;put result into Word
			movlw		high(0x0346)	;subtract high of 1.8 from Word + 1
			subwf		Word+1,F		;put result into Word+1	

			movlw		low(0x02a4)
        	subwf		Word,W
        	movlw		high(0x02a4)
         	btfss		STATUS,C
         	movlw		high(0x02a4)+1
	        subwf		Word+1,W
        	btfsc		STATUS,C        ; carry set if word is greater or equal to 0x02a4
	        goto		RB3_ON

;Testing for x > 4.1

		
TESTRB4		movlw		low(0x0347)
        	subwf		Word,W
        	movlw		high(0x0347)
         	btfss		STATUS,C
         	movlw		high(0x0347)+1
	        subwf		Word+1,W
        	btfsc		STATUS,C        ; carry set if word is greater or equal to 0x0347
	        goto		RB4_ON
		
			goto		TESTOIL			;go back to main program, tach test is done

;Testing Oil for 0.9 > x and x > 3.9

OILRESULT	movlw		low(0x00b8)     ; load W with value of low 0x00b8
	        subwf		Word,W          ; subtract value in file Word, with value in W
        	movlw		high(0x00b8)    ; load W with value of 0x00b8
          	btfss		STATUS,C        ; check to see if bit "C", in Status is set.
         	movlw		high(0x00b8)+1  ; load W with value high 0x00b8 and add 1
        	subwf		Word+1,W        ; subtract value in file Word, with value in W
           	btfss		STATUS,C        ; carry clear if word is less than 0x00b8
	        goto		OIL_ON
         	
			movlw		low(0x031d)
        	subwf		Word,W
        	movlw		high(0x031d)
         	btfss		STATUS,C
         	movlw		high(0x031d)+1
	        subwf		Word+1,W
        	btfsc		STATUS,C        ; carry set if word is greater or equal to 0x031d
	        goto		OIL_ON

			goto		TESTWATER		;go back to main program, oil testing is done
	
;Testing for x < 1

WATERRESULT	movlw		low(0x00cc)     ; load W with value of low 0x00cc
	        subwf		Word,W          ; subtract value in file Word, with value in W
        	movlw		high(0x00cc)    ; load W with value of 0x00cc
          	btfss		STATUS,C        ; check to see if bit "C", in Status is set.
         	movlw		high(0x00cc)+1  ; load W with value high 0x00cc and add 1
        	subwf		Word+1,W        ; subtract value in file Word, with value in W
           	btfss		STATUS,C        ; carry clear if word is less than 0x00cc
	        goto		WATER_ON		;if below 1V, water LED on
		
			goto		LAST			;if it is above 1V, return to main program
		
;****************************************************************
;	Subroutines
;****************************************************************	


RB0_ON		movf		PORTB,W				;move contents of PORTB into W
			andlw		b'11100001'			;AND literal with W, store in W	
			movlw		PORTB
			goto 		TESTOIL

RB1_ON		movf		PORTB,W				;move contents of PORTB into W
			andlw		b'11100011'			;AND literal with W, store in W	
			movlw		PORTB
			goto 		TESTOIL

RB2_ON		movf		PORTB,W				;move contents of PORTB into W
			andlw		b'11100111'			;AND literal with W, store in W	
			movlw		PORTB				;move the result from W into PORTB
			goto 		TESTOIL

RB3_ON		movf		PORTB,W				;move contents of PORTB into W
			andlw		b'11101111'			;AND literal with W, store in W	
			movlw		PORTB				;move the result from W into PORTB
			goto 		TESTOIL

RB4_ON		movf		PORTB,W				;move contents of PORTB into W
			andlw		b'11111111'			;AND literal with W, store in W	
			movlw		PORTB				;move the result from W into PORTB
			goto 		TESTOIL

OIL_ON		movf		PORTB,W				;move contents of PORTB into W
			andlw		b'10111111'			;AND literal with W, store in W	
			movlw		PORTB				;move the result from W into PORTB
			goto 		TESTWATER

WATER_ON	movf		PORTB,W				;move contents of PORTB into W
			andlw		b'11011111'			;AND literal with W, store in W	
			movlw		PORTB				;move the result from W into PORTB
			goto		START				

DELAY		movlw		0x05               	;will delay W*8 cycles including the call/return

DELAYW		addlw		0xff                ;add -1 to W			
	    	btfsc		STATUS,Z            ;=zero?				
	    	goto		DONEDELAY           ;if zero, finish delay		
			nop                         	;delay a bit			
	    	goto		$+1		
	    	goto		DELAYW              ;go around again		
DONEDELAY	return	                    	

			end
 
You can use the simulator from MPLAB. Under "Debugger", select MPLAB SIM.

It is good for testing how the code works but not how the ADC converts, I don't know if there's any way to simulate tach pulses either. So you probably won't be able to tell if you've properly configured the ADC to do what you want.

Typically what you'd do is insert test code that puts data in the ADC output register and write ADIF=1 to trigger your ISR. You can pause the code and do it manually or just assemble a test version with assembly that does these writes anytime after it's done with configuring things.

Write a value that isn't supposed to light the LED and check the LATx bit for the LED. Write a value that should light the LED and check the LATx bit. That's pretty much what you can do.
 
hi burg,
You can also use the www.oshonsoft.com simulator, it has ADC input voltage
simulation, also LED modules.

There is a trial version available.
 
hi burg,
A quick first run thru my sim, shows a problem in the 'READ' subr, check which BANK where the 'WORD' variable is supposed to be???

The coding is very well commented and layed out and very easy to follow, nice one.

The only minor suggestion I would make is dont use the 'WORD' as variable name.

Lets know what you find.

Regards

EDIT: the 'testing for X' subroutines are not working
Added a compare listing from the web, it may help.
 
Last edited:
Thanks a lot ericgibbs, that programs amazing actually.

I've only had a few minutes to play with it, but it seems that the testing blocks don't work at all like you said. I'll see if I can fix these up a bit, if not I'll try switching to the compares you mentioned.

Thanks again!
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top