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.

Adc Pic16f877a

Status
Not open for further replies.
hi guys, may i know what is the step to enable the sleep mode for PIC to allow my ADC accuracy to be precise?

thanks in advance
 
Without sleep mode your ADC should still be accurate to 1 bit. If you have noise problems it is not because you haven't used sleep mode. You should post a description of what you are trying to do and your ADC code.

Mike.
 
hey you go pommie my codings:


list p=pic16f877A, f=inhx8m
include <P16F877A.INC>
__config _CP_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF & _BODEN_OFF & _LVP_OFF

ERRORLEVEL -302 ;Suppress bank warning




cblock h'20'
delaytime
endc


org 0x000
goto start

int org 0x004
goto int


start
call initial
again
call delay
call startconv
call check
call result
goto again

initial
BCF STATUS,RP1
BSF STATUS,RP0
MOVLW b'00000001'
MOVWF TRISA
MOVLW b'00000000'
MOVWF TRISB
MOVLW b'00000000'
MOVWF TRISC
MOVLW b'00000000'
MOVWF TRISD
MOVLW b'10001110'
MOVWF ADCON1
BCF STATUS,RP1
BCF STATUS,RP0
MOVLW b'01000001'
MOVWF ADCON0
RETURN

delay
MOVLW h'07'
MOVWF delaytime
run
DECFSZ delaytime,f ; 21micro second
goto run

startconv
BSF ADCON0,GO
RETURN


check
BTFSS PIR1,ADIF
goto check
; MOVLW b'00000001'
; MOVWF PORTC
BCF PIR1,ADIF
return

result
BCF STATUS,RP1
BSF STATUS,RP0
MOVF ADRESL,W
BCF STATUS,RP0
MOVWF PORTB
BCF STATUS,RP1
BCF STATUS,RP0
MOVF ADRESH,W
MOVWF PORTD
RETURN


END

basically just a simple A/D converter using the PIC with a DC voltage apply to the analogue pin which i assigned so that i display the converter results on the leds. Before that i used a 1Kohm potential resistor to vary my voltages to the analogue pin but the values shown in the leds (10 leds= 10bit results) are incorrect.

so perhaps the circuitry before connecting the supply voltages (from 0v- 5v) to the analogue pins of the PIC are incorrect.that's my dillema...
 
It's much easier to read code if you use the code tags. However, I had a quick look and it appears that your delay routine is missing a return instruction and is therefore executing the startconv routine twice. I don't know if setting the go bit twice would cause a problem but it might.

When you say,
Before that i used a 1Kohm potential resistor to vary my voltages to the analogue pin but the values shown in the leds (10 leds= 10bit results) are incorrect.
Are you saying it didn't work correctly with the potentiometer?

Mike.
 
oops there should be a return there sorry.. i accidentally miss-copy that part so the GO bit is being activated once only..

Yea using the potentiometer of 1kohm didn't work but i seen a schematic diagram of one of the boards from microchip, PICDEM 2 Plus which has a schematic diagram of how they build that part of the circuit to vary the voltages to the PIC pin.

they used a 5kohm resistor connected in series to the 470ohm before connecting to the AN0 pin of the PIC. i haven't try it yet btw.

Is there a chance whereby i can convert a analogue voltage of about (0v-25V) using PIC into 10-bit digital result.. i'm aware that a circuitry has to be placed before as a regulator to reduce this analogue voltage (0v-25v)
 
derrick826 said:
Is there a chance whereby i can convert a analogue voltage of about (0v-25V) using PIC into 10-bit digital result.. i'm aware that a circuitry has to be placed before as a regulator to reduce this analogue voltage (0v-25v)

You might try reading my tutorials?, the analogue tutorial includes an attenuator to give 0-10.23V range using a 2.5V external reference. It only needs a simple resistor change to alter the range. For a 25V range I would suggest you actually set it for a 102.3V range, as there's no maths required then, and it still gives you 0.1V accuracy, PLUS (most importantly?) the values increment in single digits.
 
It sounds like you have more code than above, often the error is in the code that is working correctly. May I suggest that you go back to the potentiometer and get a working file that takes 0-5V in and lights the LEDs correctly.

Mike.
 
hi derrick,

Retested your second program [added the RETURN] it works OK in the Simulator, what problems are you actually having with the program.?

Regarding the SLEEP mode versus ADC performance, all I can see in the datasheet
is the reference to digital noise being absent while in the Sleep mode.

As Pommie points out you will still get 1 bit lsb resolution, when not in the sleep mode.

I cant see in your application, how it would be viable to keep going into and out of sleep mode
while trying to sample 'ac' type waveforms???

Can you be more precise in telling us what you are planning to do with sampled 'ac' waveform data.

The front end attenuator is simple enough to design.
 
hi eric,

currently i am using the code which i posted just to convert a dc voltage of 0-5v into 10-bit digital form to tell wheather or not the converted value is accurate or not by displaying on the 10 LEDs connected to the port B. due to the 'not accurate' conversion , i tought that sleeping mode might be best cause it might give me a good accuracy in the conversion, (based on some website i read)

i'm using a POT variable 5kohm with a supply voltage of 5v connected in series with 470ohm into AN0 and as i turn the POT variable of 5kohm, the converted results are 1 bit more than the calculated result from the equation i mentioned. (Here's my question regarding on my situation above: is there any more accurate method which i can do to get a more precise converted value?)

after trying out the dc voltage of 0-5v even tough the conversion results are a bit out, i tought of converting a sine wave, using it AC voltage instead of DC voltage to check it out.. that's why i'm asking about the analogue circuitry in the previous post.

that's all i'm asking for now.. most of my questions have been answered by you guys.Don't worry about the sine wave question anymore, i think i had it figured out as well as the high dc voltage of 25v.
 
Last edited:
derrick826 said:
hi eric,

currently i am using the code which i posted just to convert a dc voltage of 0-5v into 10-bit digital form to tell wheather or not the converted value is accurate or not by displaying on the 10 LEDs connected to the port B. due to the 'not accurate' conversion , i tought that sleeping mode might be best cause it might give me a good accuracy in the conversion, (based on some website i read)

i'm using a POT variable 5kohm with a supply voltage of 5v connected in series with 470ohm into AN0 and as i turn the POT variable of 5kohm, the converted results are 1 bit more than the calculated result from the equation i mentioned. (Here's my question regarding on my situation above: is there any more accurate method which i can do to get a more precise converted value?)

One bit error is only 0.1% error, is your 5V supply the same as that feeding the PIC as you're only using the 5V supply for reference - you should use a precision reference for better accuracy, as my tutorials do. Then how do you know it's in error?, is your multi-meter better than 0.1% accurate?.

You should also bear in mind that any analogue to digital conversion is +/- one bit!.
 
yea.. i should try the precision reference cause basically when i turn the pot resistor to the max, it doen't acutally supply 5v exactly to the AN0, instead the highest voltage it gives is 4.84v. hmm..i should do what you tell me nigel! thanks for your suggestion
 
derrick826 said:
i'm using a POT variable 5kohm with a supply voltage of 5v connected in series with 470ohm into AN0 and as i turn the POT variable of 5kohm, the converted results are 1 bit more than the calculated result from the equation i mentioned. (Here's my question regarding on my situation above: is there any more accurate method which i can do to get a more precise converted value?)
.

Can you explain why you have 470Ω resistor anywhere? Just connect your pot to +5V-AN0-Gnd. And, why are you worrying about 1 bit, if your idea works then you can switch to a 42 bit ADC and all is well. Whatever your idea is, pretend you only have an 8 bit ADC - drop the bottom 2 bits and use that value.:mad:

Mike.
 
well i'm just following up the connection according to one of the board in this website. (look up on the PICDEM 2 Plus Schematics (latest version: RoHS update) )

**broken link removed**

there's this problem which came up afterthat, when i turn my pot to max to allow higher voltage, it is measured to be 4.85v and all the LEDs are lighted up. when i left it for about 2 seconds and continue to minimize the voltage using the pot, the leds does not seem to change, they still stay all lighted up. i have to reset the circuit and vary the pot again to enable the AD conversion again
 
derrick826 said:
there's this problem which came up afterthat, when i turn my pot to max to allow higher voltage, it is measured to be 4.85v and all the LEDs are lighted up. when i left it for about 2 seconds and continue to minimize the voltage using the pot, the leds does not seem to change, they still stay all lighted up. i have to reset the circuit and vary the pot again to enable the AD conversion again

Many people, including me, have built this circuit and written this software. It works. You choose to post bits of code and bits of descriptions of what you are trying to do. There are many, many things that may be wrong. Why don't you post your (complete) code and circuit diagram? I promise not to steel it.

Mike.
 
I have had another look at your code and you appear to have the Tad time wrong. This assumes you are using a 20MHz crystal.
Code:
	MOVLW b'[COLOR="Red"]10[/COLOR]000001'
	MOVWF ADCON0

You had it this way in your original code but as 01 in your latest posting.

I notice on your circuit that you don't have LEDs on portD and so you will get a binary pattern that repeats 4 times. Is this what you are seeing.

HTH

Mike.
 
derrick826 said:
org 0x000
goto start

int org 0x004
goto int


start
call initial
again
call startconv
call check
call result
goto again

initial
BCF STATUS,RP1
BSF STATUS,RP0
MOVLW b'00000001'
MOVWF TRISA
MOVLW b'00000000'
MOVWF TRISB
MOVLW b'00000000'
MOVWF TRISC
MOVLW b'00000000'
MOVWF TRISD
MOVLW b'10001110'
MOVWF ADCON1
BCF STATUS,RP1
BCF STATUS,RP0
MOVLW b'10000001'
MOVWF ADCON0
RETURN

startconv
BSF ADCON0,GO
RETURN


check
BTFSS PIR1,ADIF
goto check
MOVLW b'00000001'
MOVWF PORTC
BCF PIR1,ADIF
return

result
BCF STATUS,RP1
BSF STATUS,RP0
MOVFW ADRESL
MOVWF PORTB
BCF STATUS,RP1
BCF STATUS,RP0
MOVFW ADRESH
MOVWF PORTD
RETURN

END

hey dude,
I may suggest you to use a compiler rather than coding in assembly.Mikrobasic is a very good one.Its very easy to use and learn .
for example your program would be equivalent to the code below in mikrobasic:
program ADc
dim temp_res as word //register to store ADC result
main:
TRISA = %111111 // designate PORTA as input
TRISB = $3F // designate RB7, RB6 pins as outputs
TRISD = $0 // designate PORTD as output
ADCON1 = %10000000 // configure analog inputs and Vref
while true //endless loop
temp_res = ADC_read (2)// read analog value on channel 2 and store
//result in temp_res
Delay_ms(1000) //1 sec delay
PORTD = temp_res //send lower 8 bits to PORTD
PORTB = word(temp_res >> 2) ///send two most significant bits to ///PORTB, pins RB7,RB6
wend
end.

All the problems you had wont exist as the built-in routine ADC_read(channel) takes cares of everything(acquisition time,when to start conversion and handle result when finished.Mikrobasic also give a copy of the code in assembly when it has compiled it.
 
Hey guys, i think i found out the problem. So just you know that the codes are now being corrected and i have used the proper XT_osc 4mhz with the proper setings in ADCON0.

As i mentioned that i used a POT resistor of about 5kohm connected in series with a 470ohm resistor before connecting to the AN0 pin of the PIC. I found out that using the 5v from the power supply,(directly from health kit) which connects to the POT resistor and to Vdd of the PIC may have caused the inaccurate problem.

What i did was i used 2 health kits, one of them supply the Vdd 5v to the PIC and the other health kit connected to the 5kohm POT. This time i'm able to get close values displayed on the 10-bit leds.

Vinke: thanks for ur suggestion on the mikrobasic, i might have a look
Pommie : thanks for your patience for helping me out, i think i got it right this time

yea i think i have solve this part right now. thank you all for helping!
 
hi guys,

i have been trying to input the DC voltage into the AN0 pin of my PIC so that i am able to get the 10-bit digital data. This time i'm planning on input a sinewave into my AN0 pin so that the i'm able to get the 10-bit digital data that is being converted , use these datas to plot the original sinewave data that i input in the pc.

I'm using a 9600 baudrate, 4Mhz XT on my PIC16F877A. Now i input a sinewave with a frequency of 50hz in to AN0 (sinewave voltage range from 0-5v) and i transmit the converted 10-bit data via the rs232 to the hyper terminal. the results i get on the hyper terminal :


0058

0082

0204

0374

0574

0754

0887

0952

0921

0808

0629

0432

0247

0099

0057

AS u can see above, the data is increasing and decreasing after that, which shows that it is a sinewave which i input, but the data on the hyperterminal is very little and there's a large gap in between the numbers. example, from 0058 it jumps to 0082. which i cannot really plot a nice sinewave graph.

However when i use a lower frequency of sinewave .. say 2 hertz my results are:

1014

1002

0984

0963

0940

0912

0876

0843

0803

0758

0718

0664

0614

0562

0511

0458

0408

0358

0310

0263

0224

0186

0144

0118

0081

0059

which is preety nice cause there's less gap, and more datas in between so i can plot a nice sinewave graph.

1) My problem now is how do i get as many datas for a higher frequency sinewave just like the low frequency sinewave above?

2) When i use the low frequency sinewave like above and trasmit the converted 10-bit data into the hyperterminal, i notice that it doesn't reach to 1023. i have double checked the sinewave and it is approximately to the range of 0-5v. i used the the phase shifter consisting of the capsitor and 2 resistor to shift a sinewave of Vpp = 5v , V+= 2.5v , v- = -2.5v

3) Eric has taught me about the consideration of using a higher baud rate so that more samples can be send per cycle. Example:

9600 baudrate/10 = 960 character/sec
960 character/sec divide by (4 data charcter + 2 for crlf) = 160 ADCvlues/sec
160 ADCvalues/sec divide by (50 which is the sinewave frequency) =
3.2 samples/cycle

is my calculation above correct?

4) for PIC, may i know if the SPBRG can be set so that i can push it to a higer baud rate?

that's all , i hope you can help me with my problems
thanks in advance
 
Hi,
The sampling rate of the PIC may not be high enough for the high input frequency. How many Hz do you want?
Even you have higher baudrate, the sampling rate is still an issue for higher frequency.
SPBRG can be changed for different baudrate. The values recommended are in the datasheet for different Fosc.
 
bananasiong said:
Hi,
The sampling rate of the PIC may not be high enough for the high input frequency. How many Hz do you want?
Even you have higher baudrate, the sampling rate is still an issue for higher frequency.
SPBRG can be changed for different baudrate. The values recommended are in the datasheet for different Fosc.

hi
Thanks for replying,
I would like to measure 50hz sinewave.

there are only 2 buadrate recommended in the data sheet, 9600 and 19200.

1) Is there a way which i can do to get higher sampling rate for my PIC for a higher frequency?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top