Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 13
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
Vaxian (17)


Next birthdays
05/21 Dalus (34)
05/21 Kizmo (37)
05/22 Skynet (32)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

pic ADC problems

Move Thread LAN_403
Turkey9
Mon Apr 26 2010, 09:34PM Print
Turkey9 Registered Member #1451 Joined: Wed Apr 23 2008, 03:48AM
Location: Boulder, Co
Posts: 661
I've set up the PIC to monitor a voltage on four different capacitors and then stop charging when when the input goes to a certain level. Basically a comparator function. It was working great on low power tests, but when I changed the charging level to stop, nothing changed!!! Even though it should wait until the ADC gets to 04fh to turn off, it turns of at something around 0fh. This is with any value greater than 0fh. Did I set up the ADC wrong? Also, when I tried this code on my development board, I set it to turn on an LED when it reaches the value, and then turn it off when it goes bellow the value. Now, the LED comes on at about 3 different voltage levels! It never stays on like it should.

Here's the code... Not commented very well sorry
#include <p16F690.inc>
      errorlevel -302 
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
	 org 0000h

;---------------------------------;
;        ALFA markVI Control      ;
;                                 ;
;        Jesse Kane Ellison       ;
;		                          ;
;	    	April, 2010           ;
;                                 ;
;RC1=Charge Enable_2              ;
;RC5=Ionization Signal 20kHz      ;
;RC7=Valve Actuate                ;
;RC6=Drive Coil Fire              ;
;                                 ;
;RB4=Charge Enable_1              ;
;RB6=Charge Enable_3              ;
;RB7=Charge Enable_4              ;
;                                 ;
;RA0=Cap ADC1                     ;
;RA1=Cap ADC2                     ;
;RA2=Cap ADC3                     ;
;RA4=Cap ADC4                     ;
;RA5=Sequence Trigger             ;
;                                 ;
;---------------------------------;
DELAY		equ	20h               ;
DELAY2		equ	21h               ;
DELAY3		equ	22h               ;
                                  ;
BEGIN:                            ;
                                  ;
	bsf		 STATUS,RP0           ;     
	movlw	 031h                 ; 
	movwf	 PR2                  ;
	movlw	 00h                  ;
	movwf	 TRISC                ;
	movlw	 00h                  ;
	movwf	 TRISB                ;
	movlw	 03fh                 ;
	movwf	 TRISA                ;
	movlw	 07fh                 ;
	movwf    OPTION_REG           ;
	bsf	     ADCON1,4             ;
	bcf		 STATUS,RP0           ;                    
    bsf		 STATUS,RP1           ;
	movlw	 0fh                  ;
	movwf	 ANSEL                ;
	clrf	 ANSELH               ;
	bcf 	 STATUS,RP1           ;
	bsf		 STATUS,RP0           ;
	bsf 	 TRISC,5              ;
	bcf	     STATUS,RP0           ;
	movlw 	 03ch                 ;
	movwf	 CCP1CON              ;
	movlw	 018h                 ;
	movwf	 CCPR1L               ;
	bcf		 PIR1,TMR2IF          ;     
	bsf		 STATUS,RP0           ;
	bcf		 TRISC,5              ;
	bcf	     STATUS,RP0           ;
	movlw 	 01h                  ;
	movwf	 ADCON0               ;
	clrf	 PORTB                ;
	clrf	 PORTC                ;
	bcf	 	 T2CON,TMR2ON         ;
                                  ;
START:                            ;
	btfsc	 PORTA,5              ;
	goto	 START                ;
	call	 WAIT3                ;
	bsf	     PORTC,0              ;
                                  ;
CHARGE:                           ;
	call	 CHARGE1              ;
                                  ;
	call     CHARGE2              ;
                                  ;
	call	 CHARGE3              ;
                                  ;
	call	 CHARGE4              ; 
 goto	 START                                 ;
FIRE:                             ;
	clrf 	 PORTC                ;
                                  ;
	bsf	 	 PORTC,7              ; 
                                  ;
	bsf		 T2CON,TMR2ON         ;
                                  ;
	call	 WAIT                 ;
                                  ;
	call     WAIT                 ;
                                  ;
	;call 	 WAIT                 ;
                                  ;
	call 	 WAIT                 ;
                                  ;
	bcf		 PORTC,7              ;
	                              ;
	call 	 WAIT2                ;
	                              ;
	bsf		 PORTC,6              ;
                                  ;
	call 	 WAIT                 ;
	                              ;
	bcf	 	 PORTC,6	          ;
                                  ; 
	bcf	     T2CON,TMR2ON         ;
	clrf	 CCP1CON              ;
    movlw	 00h                  ;
	movwf	 PORTC                ;
	call 	 WAIT3                ;
					              ;
                                  ;
	goto	 BEGIN                ;
;---------------------------------;
WAIT:                             ;
	movlw	 0efh                 ;
	movwf	 DELAY                ;
	                              ;
	movlw	 14h                  ;
	movwf	 DELAY2               ;
	                              ;
loop:                             ;
	                              ;
	decfsz	 DELAY,1              ;
	goto	 loop                 ;
                                  ;
	decfsz	 DELAY2,1             ;
	goto	 loop                 ;
	RETURN                        ;
                                  ;
WAIT2:                            ;
	movlw	 02h                  ;
	movwf	 DELAY2               ;
                                  ;
loop2:                            ;
	                              ;
	decfsz	 DELAY2,1             ;
	goto	 loop2                ;
                                  ;
	RETURN                        ;
                                  ;
WAIT3:                            ;
	movlw	 0ffh                 ;
	movwf	 DELAY                ;
                                  ;
	movlw	 0ffh                 ;
	movwf	 DELAY2               ;
	                              ;
	movlw	 01eh                 ;
	movwf	 DELAY3               ;
                                  ;
loop3:                            ;
	decfsz	 DELAY,1              ;
	goto	 loop3                ;
                                  ;
	decfsz	 DELAY2,1             ;
	goto	 loop3                ;
                                  ;
	decfsz	 DELAY3,1             ;
	goto	 loop3                ;
                                  ;
	RETURN                        ;
;---------------------------------;
;---------------------------------;
CHARGE1:                          ;
	                              ;
	bsf	     PORTB,4	          ;
    bcf		 ADCON0,1             ;
	call	 WAIT2 
	call	 WAIT2                ;
ADC1:                             ;
    clrw                              ;
	bsf	     ADCON0,1             ;
	btfsc	 ADCON0,1             ;
                                  ;
	goto	 $-1                  ;
                                  ;
	movlw	 04fh                 ;
    subwf	 ADRESH,0             ;
                                  ;
	btfss	 STATUS,1             ;
                                  ;
	goto	 ADC1                 ;
                                  ;
	bcf	     PORTB,4              ;
	                              ;
	RETURN                        ;
                                  ;
CHARGE2:                          ;
                                  ;
	bsf		 PORTC,1              ;                    
    bsf	 	 ADCON0,2                              ; 
	call	 WAIT2
	call	 WAIT2                ;
ADC2:	  	                      ;
    clrw                              ;
	bsf	     ADCON0,1             ;
	btfsc	 ADCON0,1             ;
                                  ;
	goto	 $-1                  ;
                                  ;
	movlw	 04fh                 ;
    subwf	 ADRESH,0             ;
                                  ;
	btfss	 STATUS,1             ;
	                              ;
	goto	 ADC2                 ;
                                  ;
    bcf	     PORTC,1              ;
                                  ;
	RETURN                        ;
                                  ;
CHARGE3:                          ;
                                  ;
	bsf	     PORTB,6              ;
	bcf		 ADCON0,2
	bsf		 ADCON0,3           ;
	call	 WAIT2
	call	 WAIT2                ;
ADC3:  	                          ;
    clrw  	                          ;
    bsf		 ADCON0,1             ;
	btfsc	 ADCON0,1             ;
                                  ;
	goto	 $-1                  ;
                                  ;
	movlw	 04fh                 ;
    subwf	 ADRESH,0             ;
                                  ;
	btfss	 STATUS,1             ;
                                  ;
	goto	 ADC3                 ;
                                  ;
	bcf	     PORTB,6              ;
                                  ;
	RETURN                        ;
                                  ;
CHARGE4:                          ;
		                          ;
	bsf	     PORTB,7              ;
	bsf		 ADCON0,2&3	          ;
	call	 WAIT2
	call	 WAIT2                ;
ADC4:                             ;
    clrw                              ;
	bsf		 ADCON0,1             ;
	btfsc	 ADCON0,1             ;
                                  ;
	goto	 $-1                  ;
                                  ;
	movlw	 04fh                 ;
    subwf	 ADRESH,0             ;
                                  ;
	btfss	 STATUS,1             ;
	                              ;
	goto	 ADC4                 ;
                                  ;
	bcf	     PORTB,7              ;
                                  ;
	RETURN                        ;
;---------------------------------;
	end                           ;
;---------------------------------;

Thanks!
Back to top
Bjørn
Tue Apr 27 2010, 03:19AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
Start removing code until you are left with only the code that contains the prolem, then it becomes much easier to find the fault.
Back to top
Turkey9
Tue Apr 27 2010, 04:04AM
Turkey9 Registered Member #1451 Joined: Wed Apr 23 2008, 03:48AM
Location: Boulder, Co
Posts: 661
I did have a bunch of stuff commented out but removed it to post it. I think the issue is that it is partially damaged. I accidentally sent 12v into a pin and it never worked, but the rest of the device seemed to work fine. Could a value have been locked into the ADC results register by this?
Back to top
Turkey9
Wed Apr 28 2010, 03:15AM
Turkey9 Registered Member #1451 Joined: Wed Apr 23 2008, 03:48AM
Location: Boulder, Co
Posts: 661
ok new development...

Got new parts and still have the exact same problem...

I'm comparing the adc output to a constant. What is happening is that for the first 4 bits (1 through f) the program works as expected. But when I go into 8 bits, something strange happens! it seems that the 4 most significant bits are ignored! An example...

08h gives a voltage cutoff of 25v. 0fh gives one of 45v.

18h gives 25v! f8h gives 25v no matter what the farthest left digit is, only the right one is used!

The adc is a 10 bit one but i'm using it as an 8 bit (only using 8 most significant bits). Left justified i believe.

I'm using the constant in the following way... in the beginning of the code i load it into a register. Then i subtract the contents of the register with the adc output.

movfw VOLT
subwf ADRESH,0

then check the carry bit to see which is bigger. Is this the right way?
Back to top
Bjørn
Wed Apr 28 2010, 06:57AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
That will work fine for comparisons. I think you need to double check the alignment of the data from the A/D. It looks a bit like you are only using 4 bits.

I suggest you use errorlevel -305 and define W to be 0.
then you write subwf ADRESH,W when you want the result in W.
and subwf ADRESH when you want the result in the file.

It is easier to read that way and is more in line with what was the idea when the assembler was designed.
Back to top
Steve Conner
Wed Apr 28 2010, 09:12AM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
Good point, check that the left/right justify thing in the ADC setup register is set properly.

If you're sensing multiple channels, there's a settling time for the multiplexer that you have to watch out for.

Also don't forget to clear the carry bit before you use it. bcf STATUS, C and so on. Some arithmetic instructions only set the carry and zero bits, they don't clear them if they were previously set. I can never remember which ones, so I always just clear the thing anyway.

Well, nowadays I use C and let the compiler worry about all of this smile
Back to top
Turkey9
Wed Apr 28 2010, 10:37PM
Turkey9 Registered Member #1451 Joined: Wed Apr 23 2008, 03:48AM
Location: Boulder, Co
Posts: 661
Figured it out!!!!

When I was saying test STATUS,1 i was testing the digit carry bit! In subwf the DC bit only reflects the results of the first four bits of what you're comparing!

Thanks for your suggestions, i wouldn't have figured it out without looking at the status reg.
Back to top
Steve Conner
Thu Apr 29 2010, 09:19AM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
Glad to hear it :)

In future, it's better to use the mnemonics: you can write STATUS, Z or STATUS, C or whatever, and the assembler will figure it out for you.
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.