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.

PLS HELP!!!! Need working program asap..thank you!!

Status
Not open for further replies.

silverferro

New Member
Im having problems with reading two analog voltages from two different pins.I am supposed to read e first incoming voltage from pin 1, blink an led based on that value, and unless that value is within the desired range it will continue to loop.
Heres the problem, the first voltage read out works fine, however when i switch to the next pin(pin3) and read its incoming voltage, it tells me its not within the desired value i had already set. Im quite sure abt the values im checking the voltages with.If i read only either voltage in seperate programs, they work fine.Only when combined as a single program does the second checking go haywire.I think the new a/d value at tml2 is not being stored as the new adresh. How do i clear adresh from e first value and store the new value from pin 3, to do the second checking? Any assistnce would be greatly appreciated.

This is a sample of e program i wrote for the pic12f675.





PS CON 2
LED2 CON 5

Vin VAR BYTE
Vin2 VAR BYTE
FxVin VAR BYTE
FxVin2 VAR BYTE
K VAR BYTE
L VAR BYTE
M VAR BYTE

ANSEL = %00111010
TRISIO = %010010
FxVin = %01001101
FxVin2 = %00011001
GPIO = %000000
k = 0
L = 0


STEP1: FOR M = 1 TO 5 ; BATT CHECK
HIGH LED2
PAUSE 100
LOW LED2
PAUSE 100
NEXT M
GOTO check


CHECK: ADCON0 = %00000101
HIGH PS ; send a high to pin
ADCON0.1 = 1
Vin = ADRESH


Compare: ; COMPARING VOLTAGES

IF Vin > FxVin THEN

GOTO TML2 ;Enough Light

ELSE

GOTO TLL ; Too Little Light

ENDIF

TML2: ADCON0.3 = 1 ;check analog voltage on pin 3
ADCON0.1 = 1
Vin2 = ADRESH


Compare2:

IF Vin2 < FxVin2 THEN ; compare with new adresh value

GOTO allgd

ELSE

GOTO TLD ; too little distance

ENDIF

TLL: gosub a
low ps
Pause 2000
GOTO CHECK

TLD: gosub b
low ps
pause 2000
GOTO check


ALLGD: HIGH LED2
PAUSE 250
LOW LED2
PAUSE 250

GOTO check


A: FREQOUT GPIO.0,500,311
PAUSE 10
FREQOUT GPIO.0,250,233
PAUSE 10
RETURN

B: FREQOUT GPIO.0,500,311
PAUSE 20
FREQOUT GPIO.0,500,311
PAUSE 20
FREQOUT GPIO.0,500,311
PAUSE 20
RETURN

END
 
I don't know C code, but I write in MPASM with ease. By the way, I didn't write in 12Fxxx, but I think it's the same to 16F this case.

Code:
ADSTART		MOVWF	ADCON0
			MOVLW	D'5'
			MOVWF	DLY
					
DELAY12		DECFSZ	DLY,		F					; 3 x (DLY12-1) x 1 us + 1us + 2us
			GOTO	DELAY12							; 3 x (5-1) x 1 + 1 + 2 = 15us
			CLRF	ADRES
			BSF		ADCON0,		2
GODONE		BTFSC	ADCON0,		2
			GOTO	GODONE							; Cho den khi convert xong
			MOVF	ADRES,		W					; adcon0,2 = 0

			RETURN

Code:
AD_5		MOVLW	B'11100001'
			CALL	ADSTART
			MOVWF	REGAD5
 
Thank for replying

Thank you very much for replying falleafd. But I'm not using C either. Using 12F675 Pic Micro Controller. I'm using Pic Basic Pro to programme the chip. I'm afraid your 16F is of different language.

i belive the problem lies with the input pin initialisation at the beginning. Once it has been initialised then when I change it to another input pin later on in the programe to take the input voltage let say from pin 1 to pin 3 , it does not change. Either that or the ADRESH which contains pin 1's value cannot be erased and the new value from pin 3 to be stored causing the 2nd part of the programme to go haywire. If anyone is kind enough to point out the mistakes which I could have made in the coding above when changing the input pin or storing the value in the Adresh in the 2nd part or showing a sample code to correct the mistakes will be most helpful .Thank you.
 
Re: Thank for replying

silverferro said:
Thank you very much for replying falleafd. But I'm not using C either. Using 12F675 Pic Micro Controller. I'm using Pic Basic Pro to programme the chip. I'm afraid your 16F is of different language.

The 12F675 uses a similar 14 bit core to the 16F series, so the assembler code should work on either processor.

One point I would make, after you have switched analogue inputs add a small delay (50-100mS, something like that) to give the sample and hold in the chip time to charge. The charging time depends on the source impedance of the circuitry feeding it, and if you don't wait long enough for it to charge you get a low reading.

I spent ages once chasing round trying to fond why it didn't work properly, and that's all it was!.
 
Oh Nigel, I only need to wait about 10 - 15us, and it works good.

sorry silver, I write C with CCS C, I don't know your code :). You are using C18? or Hitech PICC? in CCS C, everything seems to be oki because I check its ADC function.

Once, you write in C, you may output ASM list to see how it work.

A clever way to debug your code is that:

Write a printf(ADC_result); inline, you can use hyperterminal to view the results.

goodluck.
 
The input needs time to charge its capacitance. The time required to do so is clearly laid out in the spec sheet, including variations for temp and the resistance of the analog signal. However, it is nowhere near 50-100mS, the typical number for Tacq is 12.6uS.

The same time limit applies whether or not you are changing channels. At least that's the way I interpret the spec sheet. Basically, you need to perform this delay after turning ADC "on", changing the channel, completing a previous conversion, or just changing the analog voltage. So if you've got an external analog switch going to the pin, you're going to need to wait Tacq after switching to a new signal in order to internally charge up to the new voltage.
 
Oznog said:
The input needs time to charge its capacitance. The time required to do so is clearly laid out in the spec sheet, including variations for temp and the resistance of the analog signal. However, it is nowhere near 50-100mS, the typical number for Tacq is 12.6uS.

But that's for a very low source impedance, if you're feeding the input via a resistor (which is good practice as it prevents the chance of too much current flowing in the protection diodes) it will be considerably slower.

It's not likely to get anywhere near as slow as 50mS, but I choose that value to make 110% sure that it had settled fully - based on the application, which only needs a very slow sample time anyway.

Obviously, if you are looking at fast sample rates, you would need to design the external circuit carefully for the fastest possible sample and hold time - the datasheet holds all the specifications.
 
Thanks Alot Guys!!

The Program is working fine now..its just like nigel goodwin said...a delay of 50 ms solved e problem. A 12.5us delay didnt really work for me. Thanks everyone for the quick responses and for all e help. I appreciate it alot. :D
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top