PDA

View Full Version : 16F876A ADC write and read?



clem
- 18th April 2007, 19:24
I've never used an ADC function on a PIC, and am fairly inexperienced with PIC's compared to you guys,
My question is, does anyone have a sample of code that defines and configures an ADC input, does a read, writes that variable to a place in memory, and is read some time later?

Thank you in advance
Jim-

skimask
- 18th April 2007, 19:28
I've never used an ADC function on a PIC, and am fairly inexperienced with PIC's compared to you guys,
My question is, does anyone have a sample of code that defines and configures an ADC input, does a read, writes that variable to a place in memory, and is read some time later?

Thank you in advance
Jim-

ADC Input configuring and reading - fine example in the little green book
EEPROM writing - little green book
EEPROM reading - little green book

So, what I am eluding to here, is that there is a lot of good information in the little green book.
So, read the book, write some code and try to make it work the way you want it to.
If and when that code doesn't work, we here in the forums will be more than happy to help you figure out why it doesn't work.

clem
- 18th April 2007, 19:37
I read the little green book and it is'nt very intuitive.
here is the code... I keep getting a output must be a variable error message
at ADCIN PORTA.0, 0


PULSE VAR PORTC.2
RL1 VAR PORTC.1
RL2 VAR PORTC.0
HEATIN VAR PORTA.1
SENSIN VAR PORTA.0
ADCON1 = 0011 'Declare A ports 0 & 1 as Analog to dig converters
ADCON1.7 = 1 'Analog to Digital variables declared right justified
TRISA = 255
DEFINE ADC_BITS 10
DEFINE ADC_SAMPLEUS 5



loop_delay CON 500 ' adjust to tweak timing
ms_count VAR WORD


loop:

FOR ms_count = 0 TO 999 ' 0 to 999mS

IF ms_count < 14 THEN ' PULSE low 0 to 13 mS
low PULSE
ELSE ' PULSE and RL1 high at 14
high PULSE
high RL1
high RL2
ENDIF

IF MS_count > 8 THEN ' RL1 low at 9 mS
high RL2
ENDIF

IF ms_count > 995 THEN ' RL2 low at 995 mS, high at 0
low RL1
ENDIF

IF ms_count = 997 THEN ' Read PORTA.0 ADC VALUE put in mem loc 0
ADCIN PORTA.0, 0
ENDIF

PAUSEUS loop_delay ' fill in time for 1mS loop

NEXT ms_count

goto loop
END

What do you think?

skimask
- 18th April 2007, 19:48
Read the ADCIN section again, 5.2.
ADCIN PORTA.0, 0
Where's the variable?

clem
- 18th April 2007, 20:24
Do I have to set aside specific memory locations?
Do I have to declare specific variables?
The book states:
ADCIN channel, Var

I'm still lost

skimask
- 18th April 2007, 20:27
Do I have to set aside specific memory locations?
Do I have to declare specific variables?
The book states:
ADCIN channel, Var

I'm still lost

Did you read section 5.2?
You are using the command ADCIN, which reads the specific ADC channel, which is connected to a certain pin. This value which is read is put into a specifica variable. You aren't using a variable in your code, you haven't specified a place to put it. You are using variables in other spots...so why not here?

How much PIC programming have you done so far? LED blinky? Anything like that?

clem
- 18th April 2007, 20:30
Sorry this is my first time.
But I did figure it out,
thank you for the help

Jim-

skimask
- 18th April 2007, 20:32
Sorry this is my first time.
But I did figure it out,
thank you for the help

Jim-

If you're starting off with ADCIN type commands, you might want to back up a bit and play around some with some LEDs and some buttons first. Get the basics down. Dig in too deep and too hard right off the get go, and you're setting yourself up for headaches in the future.
Just my thoughts on the process...

clem
- 18th April 2007, 20:35
Thanks for the advice...much appreciated

Jim-