PDA

View Full Version : A2D on PIC 16F685



Forkosh
- 15th October 2007, 02:15
Please i've tried everything!
I want this stupid program to work on my PIC 16F685. I programmed it using PIC BASIC and the U2 EPIC programmer. I configured the oscillator to RCIO with the EPIC programming software.

I've been trying to get this to work for hours!!!

Please include an explanation for changes in my code. None of the sample programs coming with the PIC BASIC compiler are working!

What should I configure my oscillator to in the EPIC programmer?

All i want is an A2D conversion to be displayed to my LCD connected to RB.7.

Here is the code I used

' 10-bit A/D conversion
' Connect analog input to channel-0 (RA0)

' Define ADCIN parameters
' Set number of bits in result
DEFINE ADC_BITS 10
' Set clock source (3=rc)
DEFINE ADC_CLOCK 4
' Set sampling time in microseconds
DEFINE ADC_SAMPLEUS 10

adcVar VAR WORD ' Create variable to store result

' Set PORTA to all input
TRISA = %11111111

' Set up ADCON1
ADCON1 = %10000010

Pause 500 ' Wait .5 second
HIGH PORTB.6 ' Turn on status LED
main:
ADCIN 0, adcVar ' Read channel 0
serout portb.7, 6, ["S.1 =", #ADCVAR]

Pause 10 ' Wait.01 second
GoTo main ' Do it forever

Bruce
- 15th October 2007, 13:49
What speed oscillator are you using?

Are you using an external crystal, resonator, the internal osc?

Do you have a pull-up resistor to /MCLR?

Have you managed to get an LED blinking yet?

Does it work sending anything to your serial LCD?

Little stuff like that makes it easier for people to help.

Forkosh
- 15th October 2007, 17:37
internal osc, mclear disabled. LCD works and blinking LED works. JUST A2D is messed up for soem reason.

What speed of the ADC_Clock do i set it to for my microcontroller. I have it set to 3 now, what should i change it to.

Bruce
- 15th October 2007, 19:09
Microchip likes to switch things around a bit from time to time. Some PICs have the A/D channel select & conversion clock select bits in ADCON0. Some newer versions have moved a few bits into ADCON1. Of course this totally screws up 3rd party compilers with nifty
commands like ADCIN for using the A/D modules...;o}

ADCIN expects the first version. Where channel select & conversion clock select bits are in ADCON0.

Until MeLabs changes the PBP library commands to catch up, you'll need to do it the old yuck fashioned way.

This should work. If not let me know. Unfortunately, I don't have a 16F685 to test it out on.


@ DEVICE PIC16F685,MCLR_OFF,WDT_OFF,INTRC_OSC_NOCLKOUT,PROT ECT_OFF

adcVar VAR WORD ' Create variable to store result

' Set PORTA to all input
TRISA = %11111111

' Set up ADCON1
ADCON0 = %10000001 ' right justify for 10-bit, select AN0 channel, A/D module enabled
ADCON1 = %00110000 ' A/D conversion clock = Frc

Pause 500 ' Wait .5 second
HIGH PORTB.6 ' Turn on status LED

main:
ADCON0.1=1 ' Set GO/DONE bit to start A/D conversion
WHILE ADCON0.1=1 ' Wait for it to complete
WEND
adcVar.HighByte = ADRESH ' Read high-byte of result
adcVar.LowByte = ADRESL ' Read low-byte of result

serout portb.7, 6, ["S.1 =", #adcVar]

Pause 10 ' Wait.01 second
GoTo main ' Do it forever

END

Forkosh
- 16th October 2007, 01:36
It worked!
But how would I change it to read AN0 and AN1

How do I change the program to read multiple channels?

Forkosh
- 16th October 2007, 13:51
which PICs work with the ADCIN command? WHich one that you used worked with that command?

sayzer
- 16th October 2007, 14:41
It worked!
But how would I change it to read AN0 and AN1

How do I change the program to read multiple channels?


which PICs work with the ADCIN command? WHich one that you used worked with that command?



ADCON0.1=1 ' Set GO/DONE bit to start A/D conversion
WHILE ADCON0.1=1 ' Wait for it to complete
WEND
adcVar.HighByte = ADRESH ' Read high-byte of result
adcVar.LowByte = ADRESL ' Read low-byte of result


Instead of the part above, you may try
ADCIN 0, AdcVar

and to continue with multiple channels, you may try
ADCIN 1, AdcVar1
ADCIN 2, AdcVar2

etc.

ADCIN command will take care of setting ADC registers etc.


Edit:

Here is another version of reading as many ADC channels as available on your PIC.



NumOfCh CON 7 ' Number of Analog Channels we will read.
' Change this number as desired.

AdcValue VAR BYTE[NumOfCh+1] ' Array to store ADC Values in a row.
Channel VAR BYTE ' Loop variable to go through channels.

Start:

FOR Channel = 0 TO NumOfCh ' Will read 7 analog cannnels and store
ADCIN Channel, AdcValue[Channel] '..ADC values in AdcValue Array.
Next Channel

' do something here.

GOTO Start

Bruce
- 16th October 2007, 15:33
What version of PBP are you using?

I don't have a 685 to test this with, but I ran it through MPSIM using ADCIN, and it appears to work
fine with v2.47. The only difference is instead of setting ADCON1.7 for right justification, you need to
set ADCON0.7. Give this a shot and let me know if it works.


@ DEVICE PIC16F685,MCLR_OFF,WDT_OFF,INTRC_OSC_NOCLKOUT,PROT ECT_OFF

DEFINE ADC_BITS 10
' Set clock source (3=rc)
DEFINE ADC_CLOCK 3
' Set sampling time in microseconds
DEFINE ADC_SAMPLEUS 10

adcVar VAR WORD ' Create variable to store result
Index VAR BYTE ' Loop index for channels

' Set PORTA to all input
TRISA = %00000111 ' AN0 to AN2 inputs
ANSEL = %00000111 ' AN0 to AN2 analog inputs, rest digital
ANSELH = 0 ' AN8 to AN11 digital

' Set up ADCON0 (not ADCON1) like in the manual
ADCON0 = %10000000 ' right justify for 10-bit

Pause 500 ' Wait .5 second
HIGH PORTB.6 ' Turn on status LED

main:
FOR Index = 0 TO 2 ' Display 3 channel results
ADCIN Index,adcVar ' Read AN0 to AN2
GOSUB Display ' Display channel results
NEXT Index
Pause 10 ' Wait.01 second
GoTo main ' Do it forever

Display:
serout portb.7, 6, ["Channel #",#Index,#adcVar]
RETURN

END
You can enable more A/D pins if you need to by setting the ANSEL or ANSELH bits for each port pin
with A/D like shown in the data sheet.