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.

Reading analogue value of A/D convertor with PIC 16F877A. Need Help!!

Status
Not open for further replies.
It looks like you are corrupting the data in ADRESH with the subwf ADRESH,1 instruction which, for clarity, should be written as subwf ADRESH,F because you are storing the result of the subtraction in ADRESH. But you really should do this instead: subwf ADRESH,W because you want to preserve the data in ADRESH. This is also bad; movf Peak. I'm sure you intended this; movf Peak, W

Code:
Wait              btfsc   ADCON0,GO       ;Wait for conversion to complete
                          goto    Wait	
      
		movfw	Peak		;get Peak value '0' to w
		[COLOR="Red"]subwf	ADRESH,1[/COLOR]	;do ADC-Peak
		btfss	STATUS,C	;C set if ADC>Peak
		goto	DoneMax		;not higher
		movfw	ADRESH		;is higher
		movwf	Peak		;so copy to peak
		goto	Main			;do it again!

DoneMax
		[COLOR="RoyalBlue"]movf   Peak[/COLOR]      ;Write **AMPLITUDE** result to PORTC
                         movwf  PORTC           ;LEDs
		goto   loop_abc

loop_abc 
		clrwdt
		goto	loop_abc
 
Last edited:
Thank you so much Kchriste and Gayan for your valuable suggestions. :)

I really really appreciate that!

I don't know what you exactly going to do?

Well basically I have a 1kHz 1V-pk AC signal [Halfwave rectified] going into the PIC. I want to write a code which can detect the amplitude of the input signal [1V-pk in this case]. So if I fluctuate the input to 2V, the Output [LEDs on PORTC] now should display 2V accordingly.

To do that, I have already explained my code but my problem is that the sampling does not start at zero all the time.

For detecting the peak value of the signal, I will compare the last sample taken to the current one.
Take ADRESH - save to a memory location, wait 20 microseconds, take a new sample, compare to last sample, if the new sample is larger, throw out the older sample and save the newer one in its place, and so on ... until the newer sample is smaller than the older one. Thus detecting the peak = amplitude. This works fine when sampling starts and the signal is at zero and then each sample values increases. But the code fails when sampling starts and the signal has already crossed its max peak and the sample values are decreasing.

So that's why I suggested to have a zero crossing code so that the sampling can always begin when signal is at zero

Hope this makes sense now.

Ps: Could you please provide any example code or tutorial where I can see the zero crossing routine?

Thanks again.
 
If you use external interrupt pin to detect zero cross it will go to the ISR routine on every rising edge (if you have enable the edge selecting bit to rising edge).

Suddenly you start a timer & wait 500uS (for1khz) when it overflows take the sample & update your display.
 
Thanks so much again Gayan.
:)

If you use external interrupt pin to detect zero cross it will go to the ISR routine on every rising edge (if you have enable the edge selecting bit to rising edge).

Suddenly you start a timer & wait 500uS (for1khz) when it overflows take the sample & update your display.


Sounds so difficult. :confused:

I am a bit puzzled about the rising edge? I am using sinewave not square wave. Will it make any difference??

Could you advise me how to do that? Where in datasheet?

Many thanks.
 
The edge selecting bit is in the option reg & the RB0 external interrupt enable bits & respective flags in the intcon register.

You can use a resister from output AC to RB0 pin.The value must calculate according to your voltage

Do you get 1KHz frequency all the time or do you change the frequency as well?
 
Last edited:
Do you get 1KHz frequency all the time or do you change the frequency as well?

Basically I will be getting 1kHz frequency signal from a mixer. [It gets low pass filtered before coming to the PIC]

The value must calculate according to your voltage

Well I am using a 1V signal for testing only. The final program should detect the peak of an unknown signal. That is, I will be having a signal where I have to find the unknown peak voltage .


Many thanks.
 
Then that will be do in another way I thought you knowing the frequency thats why i calculate that 500uS as the amplitude mark for 1Khz.

If its an unknown frequency the coding will be very advanced.Must measure the time between two zero crosses & must act according to it.For that you need to invert the edge selecting bit for negative side of the wave form as well.

I have tested for 12VAC signal & the resister i used was 22K.

Don't know the accuracy of that mixer signal.I have dealing with 230VAC waves.
 
Hi Gayan. I might have misinterpreted it!


Gayan Soyza said:
Then that will be do in another way I thought you knowing the frequency thats why i calculate that 500uS as the amplitude mark for 1Khz.

The frequency is the same at 1kHz but the magnitude of the signal will be unknown.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top