PDA

View Full Version : ADC not working - I'm a new member



johnbond
- 16th June 2011, 10:37
I've migrated from the Parallax sx but I'm new to PICs and PicBasic Pro

How do I use ADCIN

The following program is meant to display the binary value of a pot on AN2 on 8 LEDs on port RC. It doesn't work!!!

================================================== =========

' Name : LearningADC.bas
LEDs var PORTC

' Allocate variables

PotVal Var Byte

ANSEL = %00000100 ' Set PORTA.1 analog, rest digital
ANSELH = %00000000

mainloop:
Adcin 2, x ' Read ADC value on AN2 (PORTA.2)
LEDs = potval
Pause 100 ' Do it about 10 times a second
Goto mainloop

End

================================================== =========

Thanks from Kwa Dukuza, lost somewhere in South Africa
John Bond

cncmachineguy
- 16th June 2011, 12:28
I think your problem is here:


Adcin 2, x ' Read ADC value on AN2 (PORTA.2)

you are reading the value of AN2, then storing in x
You should be storing in potval

And WELCOME to the forum!!

johnbond
- 16th June 2011, 13:52
Sorry, I spotted that after I posted but this is still not working...

I've added

=========================================

ADCON1 = %10000100 ' Set PORTA analog and RIGHT justify result
ADCON0 = %01000001 ' Configure and turn on A/D Module channel o Fosc 8

=========================================

and changed

=========================================

Adcin 0, PotVal

=========================================

I don't understand registers. I've read the PIC16F690 DATASHEET a couple of times. I seem to handle these issues best using the "Monkey see... monkey do" philosophy when trying to grasp complex problems. (Maybe the Monkey see, monkey do is part of my African heritage :confused:)

cncmachineguy
- 16th June 2011, 14:29
I don't have my book handy, but if you look at the ADCIN section of the PBP manual, there are several DEFINES you need in order to use the ADCIN command. Also to post code in the cool code box, you need to use tags. Really simple:
[code[ <----should be the other, but then you won't see the tag
All your code here
[/code[ <----Again the second bracket should be } without the shift key.

Maje sense?

johnbond
- 16th June 2011, 15:22
Thanks for your help Bert,

Bert said "[code[ <----should be the other, but then you won't see the tag
All your code here
[/code[ <----Again the second bracket should be } without the shift key."

Ja dit maak sense, dis cool! (Afrikaans response to Bert's last question - For English, replace "this" for "dit" and "dis"):)

I've tried the various DEFINE and REGISTER options but I really don't know what I am doing. Nothing happens. I haven't linked up a UART/USART yet so I can't debug and I am in the dark. Hardware RS232 is my next exercise and I think I'll need to understand REGISTERS for that.

Kind regards from Kwa Dukuza, lost somewhere in darkest Africa
John Bond

cncmachineguy
- 16th June 2011, 19:00
Well now we need the basic stuff - lol. What pic? what version PBP? MPLAB or MCS? PM or MPASM? Internal osc or external? Some of it we don't need for this problem, but its a good idea to let us know what you are working with.

What doesn't work? do any led's work? Can you make them blink? Is the value just wrong?

HankMcSpank
- 16th June 2011, 19:22
I've just glanced at an old 16f690 program, which was unfeasibly huge so I've hacked it down & hopefully not missed anything during the slimming process...



'-------------------------------------------------------------------------
DEFINE ADC_BITS 8 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 1 ' ADC clock source (Fosc/8)
DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)
ADCON1 = %0100000 ' FOSC/4
'
CM1CON0 = 0 'turn the pesky comparators off
CM1CON1 = 0 'turn the pesky comparators off
CM2CON0 = 0 'turn the pesky comparators off
CM2CON1 = 0 'turn the pesky comparators off
'
ANSEL = %0000010 'RA1 (pin 18) AN1 analogue
'
PotVal Var Byte
'

'********program starts......
'
adcin 1, PotVal
'

'[do cool stuff with contents of PotVal here, then have a cup of tea & a nap]



You don't need to worry about ADCON0 (since the ADCIN command selects the ADC channel for you) *unless* you start wanting to use 10 bits, then you need to modify ADCON0.7 to suit (LH or RH Justify)

johnbond
- 17th June 2011, 08:57
Thanks Bert and Hank

No time for tea or a nap but I'm sure this will solve my ADC problem. I now have a problem with the compiler. It crashes after a couple of downloads and only works again after reloading both PBP and MPLAB. I will make a new post on these issues

Hank - Thanks for that warning on 10 bit (LH or RH Justify) because I will be using 10 bit to measure piezo pressure sensor and I will discarding the upper 1 or 2 bits to reduce the span to 8 bits. I am looking for a 0.05 Bar (peak to peak pressure fluctuation) to determine the speed of an air motor running at between 5 bar and 6 bar. 8 bit, without reducing the span, makes it a bit tight!

Thanks again and kind regards

John

Heckler
- 17th June 2011, 21:14
Here is another example of a small test program that will read the AN2 a/d converter and send it to a debug window.

one thing to remember is that the a/d converter names do not always match the pin #. example... AN2 does match PortA.2 but AN3 is actually on PortA.4.

If you have not used the debug feature of the PICkit programmer, you program the chip, power it up and then goto the Tools Menu and choose UART Tool. This will open up another Terminal window where you choose 2400 baud in the upper left and then click on Connect.



' 16F690
'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2007 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 12/4/2007 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
' 10-bit A/D conversion
' Connect analog input to channel-2 (RA2)
' -----[ I/O Definitions ]-------------------------------------------------
DEFINE DEBUG_REGG PORTA 'set debug port to porta
DEFINE DEBUG_BIT 0 'use pin a0 of porta for debug
DEFINE DEBUG_BAUD 2400 'set baud rate to 2400
DEFINE DEBUG_MODE 0 'communicate in true mode

' Define ADCIN parameters
' Set number of bits in result
DEFINE ADC_BITS 10
' Set clock source (3=rc)
DEFINE ADC_CLOCK 3
' Set sampling time in microseconds
DEFINE ADC_SAMPLEUS 50
adcVar VAR WORD ' Create variable to store result
' Set PORTA to all input
'TRISA = %11111111
' Set up AD
ANSEL = %00000100 'enable AN2, ALL THE REST DIGITAL
ANSELH = %00000000 'AN8-11 DIGITAL
ADCON0 = %10001000 ' RIGHT JUSTIFIED
ADCON1 = %00010000 'fOSC/8
Pause 500 ' Wait .5 second
main:
ADCIN 2, adcVar ' Read channel 0
' do something with adVar here
debug DEC adcvar," ",BIN adcVar,$0D,$0A
Pause 100 ' Wait.01 second
GoTo main ' Do it forever


Good Luck

johnbond
- 19th June 2011, 12:59
Thanks Dwight - that will sort me out once I've got my Microchip PICKit 2 dongle to start working again. The fault with the dongle seems like it may be electronic, not software.

When I started this thread, I didn't have a MAX232 rigged up but I do now so from now on I can use the debug

Kind regards from Kwa Dukuza

John Bond

mackrackit
- 19th June 2011, 13:52
Check out the PBP manual under DEBUG.
Then look at
DEFINE DEBUG_MODE X 'X being 0 or 1 - TRUE or INVERTED

johnbond
- 19th June 2011, 15:25
Thanks for that "Heads-Up" Dave. I am scared of PC Com Ports after I got a 100V shock from one (half our 220V phase). I prefer using a MAX232 to a fried PCB. We tend to get Kong Fu PCs here and you never know what fascinating undocumented features are included. Maybe it's just Chinese revenge :)

Regards form the dark continent - Africa
John Bond

johnbond
- 20th June 2011, 19:13
OK Dwight, so you're a Gentleman! Thanks for that neat code. I works like clockwork

I now understand both ADC and DEBUG!

Regards from Kwa Dukuza on the dark continent of Africa
John Bond