PDA

View Full Version : 18f4550 a/d



starwick
- 26th November 2013, 10:50
Hi all.
I have done A/d to with success with 16f877 and16f887 now I have a problem with 18f4550 the value is fluctuating so much even in the high rail I still get values between 1023 and 1018 and in the low rail some times go up to 8. I think it's a TAD problem but I let you guys help me out.
Here is the code(nothing fancy just read and display):


==========================MCU SETUP============================================
DEFINE OSC 48
Include "modedefs.bas"
'***************************LCD DEFINE*******************************
DEFINE LCD_DREG PORTD 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4
DEFINE LCD_RSREG PORTD 'LCD register select port
DEFINE LCD_RSBIT 5 'LCD register select bit , RS=PORTE.2
DEFINE LCD_EREG PORTD 'LCD enable port
DEFINE LCD_EBIT 7 'LCD enable bit E=portE.0
DEFINE LCD_RWREG PORTD 'LCD read/write port
DEFINE LCD_RWBIT 6 'LCD read/write bit R/W=POTRD.1
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 4 'Number lines on LCD
DEFINE LCD_COMMANDUS 2000 'Command delay time in us
DEFINE LCD_DATAUS 50
'************************************************* *****************
DEFINE ADC_BITS 10 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 3 ' Work around that sets Vref bits in ADCON1
DEFINE ADC_SAMPLEUS 50 ' ADC sampling time (uSec)


'==========================I/O SETUP============================================



TRISD=0
TRISE=0
TRISC=%10000000
TRISA=%11111111
TRISB=0

ADCON1=14
ADCON2=%10000000


'================================variables======== ==============================
adcval var word
TX VAR PORTC.6
RX VAR PORTC.7

'==============================BAUD SETUP=======================================
N96 con 16416 ' Inverted 19200 baud for the lcd rs232
T192 con 32 '19200 TTL
T96 CON 84'9600 TTL
T38 CON 6'38K
'==========================I/O SETUP============================================
'======================LCD LINES SETUP==========================================
L1 CON 1 'LINE1
L2 CON $C0'LINE2
L3 CON $94'LINE3
L4 CON $D4'LINE4

CLEAR

'================================================= ============================


PAUSE 500
loop:
LCDOUT $FE,l1
PAUSE 50
LCDOUT "a/d test"


MAIN:

gosub adc0
lcdout $fe,l2
lcdout "pot:" ,dec adcval," "
pause 600

GOTO MAIN


'=================================ADC0============ ==============================
ADC0:
ADCin 0, adcval
pause 50

RETURN
'================================================= =============================

END




can you please help have a 0.1uf between the input and ground of AN0 the power supply is solid do not change.
ground wire from PS to MCU and to analog input.
anything I'm missing.
tried to change
DEFINE ADC_CLOCK 3 to 0 then 2 then 6 with no help.

Demon
- 26th November 2013, 13:23
I don't see many CONFIGs (the cause of many problems when migrating from 16F to 18F.

Robert

Charlie
- 27th November 2013, 14:05
Clock speed, sample times, power rail noise, board layout - lots of potential issues. But do you even have an issue? The readings you describe have less than than 1% error, and likely only at the rails. If you really need to get better than that there are techniques for averaging and even for deriving further bits if you do a search on the forum. Microchip also has application notes on the subject.

Archangel
- 27th November 2013, 19:17
Question: I see you have set ALL of PortA to be inputs, are they ALL PROPERLY terminated to either VCC or VSS or are they left floating?

starwick
- 28th November 2013, 08:44
Hi all and thanks for the replies,

Demon:
The Config are saved in the .INC file and are as follow:
CONFIG FOSC = HSPLL_HS, PLLDIV = 5 , CPUDIV = OSC1_PLL2
CONFIG FCMEN = ON , PWRT = OFF, BOR = ON, VREGEN = ON , WDT = OFF,PBADEN = OFF ,MCLRE = ON
CONFIG CCP2MX = OFF, LPT1OSC = OFF , LVP=OFF, ICPRT = OFF,STVREN = OFF , IESO = OFF

Charlie:
I'm using a 20mhz crystal with the about settings.
sample time: DEFINE ADC_SAMPLEUS 50 I think that's 50us.
The power supply: I'm using a high end with no noise and constant 5vdc.
the board I'm using has a ground plane but I still soldered wires from the PS directly the MCU and the analog input.
You are correct about the error % but those numbers are only at rails when I'm in middle it can jump 25 max(ie: from 350 to 325) but not always sometime it does stay consistant within 1 which i'll be happy with.I'll be reading a temp sensor so variation of 20 can results in a false temp reading or big variation.


Archangel:
Good catch I changed porta to output except AN0 to input. But did not help


I added a 10uf 50v cap to the input seems to help a bit but still once in a while not all the time I get those unexpected drop or rise in value as much as 20.

I've done similar with 16F887 that was dead on i had problems at first but added cap and ground wires and worked perfect.

Now what i don't understand is the DEFINE OSC 48 in the 18F4450 is it too fast should i just run everything internal OSC(4mhz) and ADC clock internal(RC) would that help? I don't want to go through that until i ask you guys speed is not an issue here(probably I should've run it with internal clock to start with and save a crystal HAHAHAHAH).Or is it the TAd the sampling time the clock speed of the ADC(DEFINE ADC_CLOCK 3)

Thank you all for your help.

Happy Thanksgiving,

Joe

Demon
- 28th November 2013, 15:14
I never used a serial LCD before. LCDOUT will use parallel on Port D the way you defined it.

I always use the internal oscillators on 18F PICs, never had a problem.

Don't have enough skill to comment on ADC stuff.

Robert

Archangel
- 28th November 2013, 19:14
Now what i don't understand is the DEFINE OSC 48 . . .
The thing to remember is this: The define explains to PBP what OSC speed you are using, it does not SET the OSC to that speed. It is supposed to match so your timing is correct for all the bit / bang operations to work properly, and pause timing to be accurate. It is possible to lie to PBP and compensate in code.