Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC 12F683 ADC problem

Status
Not open for further replies.
Back again in trouble with this basic stuff. Hope some kind guy can explain why this code doesn't work.
I thought ANSEL = 1 caused the selected input to be enabled as analog in an AllDigital environment'
However, the simulator displays the input as digital . So where am I going wrong?

TRISIO = %00000010
ANSEL = 1
AllDigital
GPIO = %00000000
Dim x As Word
main:
Adcin 1, x
GP0 = 1
GP2 = 1
WaitMs x
GP4 = 1
GP5 = 1
WaitMs x
GP0 = 0
GP2 = 0
WaitMs x
GP4 = 0
GP5 = 0
WaitMs x
Goto main
End
 
I haven't used PIC's for a fair while so excuse me if I am wrong.

TRISIO = %00000010 You are setting pin 1 as an input.
ANSEL = 1 You are setting pin 0 as analog.

Perhaps you need to make ANSEL = %00000010 and set pin 1 as analog?

PIC ANSEL.png
 
Last edited:
hi steve,
Run this Sim.
Code:
'12F683
Define SIMULATION_WAITMS_VALUE = 1  'SIMULATION ONLY
Define CONF_WORD = 0x33c4

Dim cnt1 As Byte
Dim aval1 As Byte

ADCON0 = %00000001
ANSEL = %00000001
CMCON0 = %00000111

OSCCON = %01100000  'Internal  osc

OPTION_REG = %10000111

TRISIO.0 = 1
TRISIO.1 = 1
TRISIO.2 = 0
TRISIO.3 = 1
TRISIO.4 = 0
TRISIO.5 = 0
INTCON = 0

start:
Toggle GPIO.2
Adcin 0, aval1
Goto start

End
 
hi eric
As a beginner I won't pretend to understand most of the opening statements in your code example, but would I be wrong in assuming that there would be no error in changing Adcin 0, aval1 to Adcin 1, aval1, leaving everything else the same? I notice that your Tris reg is set up to accommodate this and it's the pin I used in my code. But in doing this, I find your example, like mine, doesn't work either. In fact my code worked with GPIO.0 as the adc input as did yours, and didn't work for any of the other pins substituted. Any suggestions as to why this is happening?
 
hi steve,


Code:
Define SIMULATION_WAITMS_VALUE = 1  'SIMULATION ONLY
Define CONF_WORD = 0x33c4

Dim cnt1 As Byte
Dim aval1 As Byte

ADCON0 = %00000101
ANSEL = %00000010
CMCON0 = %00000111

OSCCON = %01100000  'Internal  osc

OPTION_REG = %10000111

TRISIO.0 = 1
TRISIO.1 = 1
TRISIO.2 = 0
TRISIO.3 = 1
TRISIO.4 = 0
TRISIO.5 = 0
INTCON = 0

start:
Toggle GPIO.2
Adcin 1, aval1
Goto start

End
E
 
Last edited:
Dawning of a bright light. Thanks to you, Eric, I now realise that I was misunderstanding how to use ANSEL. I imagined that setting ANSEL to 1 enabled all analog inputs, and that all one needed to do was specify a particular one in the Adcin statement. I now also realise what Gobbledok meant (he was absolutely right), and I need to thank him too.
I haven't used PIC's for a fair while so excuse me if I am wrong.

TRISIO = %00000010 You are setting pin 1 as an input.
ANSEL = 1 You are setting pin 0 as analog.

Perhaps you need to make ANSEL = %00000010 and set pin 1 as analog?

View attachment 88133
 
Thanks, Gobbledok, I apologise for not understanding your explanation right away. Of course you were right. Ericgibbs helped me see the light.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top