Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 4th August 2006, 02:12 PM   #1
Default pic18f452 ADC

i try to use pic18f452 as analog to digital i programme it and i get successful programming by IC- prog but when i t try

to convert analog to digital i do not get any thing in the PORTC i use RC oscillator

and pin#2(AN0) is analog input and Vref+,Vref- are the same Vdd&Vss...

this is the code:

List p=18f452,f=inhx32
#include <p18f452.inc>
org 0x0000
clrf PORTC
movlw 0x00
movwf TRISC

movlw B'11000000'
movwf ADCON1

movlw B'11000001'
movwf ADCON0

again:

bsf ADCON0, GO

Wait:
btfsc ADCON0,GO ;Wait for conversion to complete
goto Wait

movf ADRESH,W ;Write A/D result to PORTC
movwf PORTC ;LEDs

goto again

end

why i do not get the result of converting analog to digital in the portC
and how we can get it... is the problem from code or circuit
strong-storm2006 is offline  
Old 4th August 2006, 04:10 PM   #2
Default

Ok, if you load your code into MPLAB (www.microchip.com) and run it, you will see the following error generated....

"ADC-W0010: A Minimum of 2 TADs are required before another conversion should be started."

In other words, you need to leave a delay before starting another conversion. Depending how fast you are clocking this device, will depend on how long you need to wait.

So something like......

clrf 0x30

wait_2tad:
decfsz 0x30,f
goto wait_2tad ;wait 256 clks

This will wait 256 clks, but this may be too short/long, check it out

Also a.....

movlw 0x7f
movwf TRISA

After (or before) your TRISC wouldn't go amis, I know it's supposed to power-up as inputs, but it's good practice to included it.

Just another problem waiting to get you is that this processor has 2 inturupt vectors at 0x04 and 0x08, if you put code into these areas and don't disable all the int's then fun things will start to happen!

Just go over the data sheet again, pages 181->187, it takes you step by step through the ADC.

I may have also missed the setting of another register, just got that feeling PIC's have a habit of not playing nice, and the data sheets don't always spell it out (well only after you know what went wrong, and you find it!).

Anyone else got any input (i.e. tell me what I have missed!)?

List p=18f452,f=inhx32
#include <p18f452.inc>
org 0x0000
clrf PORTC
movlw 0x00
movwf TRISC

movlw 0x7f
movwf TRISA

movlw B'11000000'
movwf ADCON1

movlw B'11000001'
movwf ADCON0

again:

bsf ADCON0, GO

Wait:
btfsc ADCON0,GO ;Wait for conversion to complete
goto Wait

movf ADRESH,W ;Write A/D result to PORTC
movwf PORTC ;LEDs

clrf 0x30

wait_2tad:
decfsz 0x30,f
goto wait_2tad ;wait 256 clks

goto again

end


Hope this helps
__________________
Regards


MATT!
Matt(Pic progger) is offline  
Old 4th August 2006, 04:55 PM   #3
Default

In addition to the wait that Matt added, there should be one wait routine right after the point where you turn the A/D on. The sample and hold capacitor needs time to charge when first turned on and after every sample.

Another thing you are doing is moving your the high bite of your acquisition to PORTC when you are set up to right justify your conversion. Change ADCON1 to B'01000000' to make it left justified. That way the all of the MSBs will reside in ADRESH.
phalanx is offline  
Old 4th August 2006, 05:16 PM   #4
Default

See, I KNEW I had forgotton something Thanks phalanx
__________________
Regards


MATT!
Matt(Pic progger) is offline  
Old 4th August 2006, 08:06 PM   #5
Default

Quote:
Originally Posted by Matt(Pic progger)
See, I KNEW I had forgotton something Thanks phalanx
Like 80% of the people who ask why they can't make the ADC work missed these two points. It's a really easy mistake to make.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline  
Old 30th November 2008, 08:14 AM   #6
Default Need help with project

I am new to actually programming the pic, but my question is I am trying to build a project where I input an analog from an audio jack to the Pic18452 and then use the AD in the pic to get a Digital signal, which will be transferred to nrf24L01+, which should transfer it to another nrf24L01+ (should be set as a receiver) and use the Pic18452 to output the digital as analog to the audio jack or whatever source.

Right now I am trying to compile the code and I am getting an error in the line


List p=18f452,f=inhx32

can someone provide me information on what does that mean or direct me where to read, I am willing to learn, and please if anyone can provide me any tutorials to a project similar to this.
soulz9 is offline  
Reply

Tags
adc, pic

Thread Tools
Display Modes




All times are GMT. The time now is 07:34 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker