PDA

View Full Version : Discussion PIC Problems


AndyOne
Dec 21, 2006, 09:08 AM
Can someone explain to me why I'm always running into problems getting PICs to read simple digital inputs, they are always interpreted as 0 even if it is driven to VCC.

It seems more prevalent on devices with analogue functions than those without.

I have pruned the example down to the simplest I can get to. The code was compiled from Oshonsoft PIC Basic and the resultant assembly is also shown. The code is for a PIC12F675 running at 4MHz powered at 5V.

Basic...
Define CONF_WORD = 0x31b2
TRISIO.0 = 0
TRISIO.1 = 1
start:
If GPIO.1 = 1 Then
GPIO.0 = 1
Else
GPIO.0 = 0
Endif
Goto start
End

Assembly...
-----------------------------------
PIC DISASSEMBLER LISTING
Address Opcode Instruction
-----------------------------------
0000 118A BCF PCLATH,3
0001 120A BCF PCLATH,4
0002 2805 GOTO L1
0003 0000 NOP
0004 0009 RETFIE
0005 1683 L1: BSF STATUS,RP0
0006 1005 BCF 0x05,0
0007 1485 BSF 0x05,1
0008 1283 BCF STATUS,RP0
0009 1C85 L4: BTFSS 0x05,1
000A 280D GOTO L2
000B 1405 BSF 0x05,0
000C 2810 GOTO L3
000D 301F L2: MOVLW 0x1F
000E 0583 ANDWF STATUS,F
000F 1005 BCF 0x05,0
0010 301F L3: MOVLW 0x1F
0011 0583 ANDWF STATUS,F
0012 2809 GOTO L4
0013 2813 L5: GOTO L5
0014 2814 L6: GOTO L6


The configuration bits has everything disabled except MCLR enabled and connected as such on the board and HS oscillator for a ceramic resonator. The scope shows the resonator is working and programs with only outputs also work.

Can anyone throw some light on this as it has me stumped.

Thanks,

Andy.

mem
Dec 21, 2006, 10:54 AM
I am not a pic basic user but try disabling the GPIO analog functions (ansel and cmcon)

Acetronics
Dec 21, 2006, 11:08 AM
Hi,Andy ...

Do you remember the 12F675 has ANALOG inputs ...

just think to declare them as DIGITAL ...

That's all !!!

juste some lines ...

IF ( PROC == D'629' || PROC == D'675' )
LIST
movlw 7
movwf CMCON
NOLIST
ENDIF


IF ( PROC == D'675' || PROC == D'683' )
LIST
clrf ADCON0 ;ADC off "
NOLIST
ENDIF

In Basic ...

CMCON = 7
ADCON0 = 0

Alain

PS: the compiler directives ( IF Proc == ... ) are here, just to select the processor from an EXCEL programming sheet ... Ahhhh, V.B. ... that's great !!!

AndyOne
Dec 21, 2006, 11:18 AM
mem,

Thanks, I just looked it up in the datasheet and this is what I found, I'm not sure how to deal with it in Basic, I may have to use some inline assembler code to make it happen...

From the Microchip PIC12F675 datasheet...
"Note: The ANSEL (9Fh) and CMCON (19h)
registers (9Fh) must be initialized to
configure an analog channel as a digital
input. Pins configured as analog inputs will
read ‘0’. The ANSEL register is defined for
the PIC12F675."

Andy.

mem
Dec 21, 2006, 11:21 AM
I think Alain's code should do it:
CMCON = 7
ADCON0 = 0

AndyOne
Dec 21, 2006, 11:22 AM
Alain,

I'm not sure how to use the code you show but I've got a handle on the answer now and I think I can find a way to sort it out using the Oshonsoft compiler.

Thanks,

Andy.

Acetronics
Dec 21, 2006, 11:22 AM
Hi, Andy

Just look at the 2 " in Basic" lines ....

Respect the correct banks are selected ...by Oshon soft.

Alain

AndyOne
Dec 21, 2006, 11:31 AM
I think Alain's code should do it:
CMCON = 7
ADCON0 = 0

Alain/mem,

Both of these statements compile but they don't seem to cure the problem, any other suggestions,

Thanks,

Andy.

mem
Dec 21, 2006, 11:34 AM
ansel is in bank 1, cmcon is bank 0. Your compiler may not change banks implicitly.

FYI, the assembler to change banks will be something like:
bsf STATUS,RP0 ; to Select Bank 1
bcf STATUS,RP0 ; to Select Bank 0

Acetronics
Dec 21, 2006, 11:37 AM
We,ve forgotten

ANSEL = 0 ...

Alain

mem
Dec 21, 2006, 11:39 AM
yep.
And looking at some sample code for yr compiler it does change banks implicitly. You should be good to go.

AndyOne
Dec 21, 2006, 11:47 AM
Great, thanks guys,

Setting ANSEL=0 was the final thing it needed, I just compiled and loaded it into the PIC and it works!

Now all I need to do is to read up on how to selectively set each pin to analogue or digital so I can mix and match.

Much appreciated,

Andy.

mem
Dec 21, 2006, 11:54 AM
Have fun and good luck.
BTW, what are you building?

AndyOne
Dec 21, 2006, 12:30 PM
mem,

This project is just a servo tester but I have recently been working on a GPS telemetry system so this is simple by comparison, the digital input thing was a bit of a blind-spot for me but now I can see!

Cheers,

Andy.

Malc C
Dec 21, 2006, 12:33 PM
Andy,

Just had a look at the website for the basic you'r using and seen this example, which may be a starting point


Dim an0 As Word

AllDigital 'configure all pins for digital use
ADCON1 = 0x0e 'configure AN0 input for analog use

Acetronics
Dec 21, 2006, 12:55 PM
ADCON1 ???

Always new features with you, Malc ... :D

Alain

Malc C
Dec 21, 2006, 01:57 PM
me thinks you are taking the p*ss out of me :)

Nightz
Dec 21, 2006, 02:19 PM
AndyOne,
You maybe want to consider going 100% assembly. It sounds like your fairly new to pics. I'm not suggesting you dump Basic, just that writing some firmware in 100% assembly will greatly increase your understanding of how they operate and what is required for certain tasks. I was writing code in Basic and had to include some function in assembly. Them more I wrote, the more I had in assembly unit I got to the point where Basic just got in the way. Only offering my humble opinion.

-Rocko

AndyOne
Dec 21, 2006, 03:29 PM
Rocko,

Assembly has it's advantages not the least is the one you pointed out, but it's a little like studying Latin to gain a better understanding of English. I know it would be a good thing to get better into assembly then I wouldn't have slipped into ignorance about something so simple as I/O ports usage. To be fair I started out learning a bit of PIC assembly language but I found it so tedious having to figure how to write a simple loop or decision branch each time I wanted to use one that it just seemed a more modern way was to use a high level language to take the drudgery out of it. My goal here is to create some circuitry rather than become a PIC guru so I chose the easy way out. I guess like most people I just wanted the quick fix.

Andy.

Nightz
Dec 21, 2006, 03:44 PM
I found it so tedious

This is true. I've since migrated to C. In my case, I actually had a goal of learning PIC assembly just as much as making my circuit. This is not the case for most. It's good to hear your not afraid to dabble in assembly when required. As your code gets larger and larger, assembly gets harder to follow, even with good documentation. I'm still greatfull for my time with assembly. I like to know whats going on in the background :)

-Rocko

mem
Dec 21, 2006, 04:19 PM
Andy,
If you can call assembly language routines from basic with your compiler then you can have the best of both worlds. Write the high level structure in Basic and do the bit manipulation in assembler.
I think PIC assembler is like learning Latin to speak Latin. Once you are familiar with the architecture of the PIC and mnemonics of your assembler it starts becoming a natural expression of the PICs capabilities.
Basic is like talking in English through an interpreter that translates into Latin. It gets you communicating quickly but is somehow more awkward and less expressive.
My 2p anyway.

Michael

Acetronics
Dec 22, 2006, 05:14 AM
Hi, Michael

You're totally right ... as most up-to-date Basics permit instant interrupting AND assembler including, everything can be done easily.
Note it's due to modern PIC's offering plenty of memory space ...and improved clock speed.

I do agree using 12C508 needs assembler rather than Basic ... but now, we've the 12F683 that can host 2K's !!!

But does someone still create real-world applications aboard a 6502 or 6800 nowadays ????

Alain

AndyOne
Feb 15, 2007, 08:23 PM
I've got the same problem as I had before but with the PIC16F88. Trying to make the analogue inputs work as digital inputs I can't spot what's different, the solution given to me for the 12F675 worked but I need help again. The Alldigital command in Oshonsoft doesn't seem to work and the direct register setting is somehow different between the 12 series and the 16 series PICs.

Thanks,

Andy.

xorcise
Feb 16, 2007, 08:12 PM
Try ANSEL = 0. That will give you digital IO by turning off the ADC's.

If you ever have a question about Digital IO for a particular PIC look in the datasheet under the section "IO Ports". There is usually a code example for each port that shows how to achieve digital IO.

Malc C
Feb 17, 2007, 04:30 AM
Andy, by default the 12F675 sets the ports to analogue.. As xorcise states using ANSEL=0 will set them to digital.

AndyOne
Feb 17, 2007, 06:03 AM
Thanks guys, but it turned out not to be my problem this time. I was missing a trick but it was something else entirely, it seems that it was those pesky configuration bits as well as not having one of the ports held at the correct level to allow smooth flow of the program.

Andy.

salibaj
Feb 23, 2007, 08:14 AM
AndyOne,

I am new to this forum. As like you, I am not a PIC guru but uses basic to write all of PIC programms. I use PDS (Proton Development Suite) from Crownhill. I had read a lot regarding basic compilers, and it seemed that this one is the best basic compiler. So I decided to buy it. Alhtough I bought for the hobby am I now using it in commercial applications. It costed GBP 150 + post. It write in basic, and then you can see assembley as well. Now back to your problem.

In the forum I use when I have a problem they always tell me to have "a good look at the data sheet". It turns out that sometimes doing ALLDIGITAL not always work with some PIC's. The best thing to do is alter the register one by one. Here is a setup I used for a 16F88, internal oscillator at 8 MHz, and a mix of analog/digital pins. Also not the `arrow' on the pin layout of the datasheet that these are not all input and output.

i.e PORTA.5 and PORTA.7 are inputs only. (Arrow only in one direction)

My setup: -

Config cp_off, debug_off, wrt_protect_off, cpd_off, lvp_off, boden_off, mclr_off, pwrte_on, wdt_off, intrc_io, ccp1_RB0

OSCCON = %01110110 ' Set up internal osc for 8.0 MHz
ADCON1 = %10000000 ' Set VRef+ and VRef- to AVDD and AVSS and right justify result
ANSEL = %00000111 ' Set PORTA.0, PORTA.1 and PORTA.2 as analog (Change 1's to 0's for digital)

TRISA = %10100111 ' Set PORTA bits 0, 1, 2, 5, 7 as inputs, 3, 4, 6 as outputs
TRISB = %00000000 ' Set PORTB as output

I have no problems with this and works good.

Regards

Joseph

AndyOne
Feb 23, 2007, 03:42 PM
Joseph,

Thanks for the very detailed answer but if you see my comment above it turned out not to be my problem this time as it was masked by other problems and I just assumed it was the old one of not successfully setting the inputs to digital.
The Oshonsoft basic compiler whilst it may not be as comprehensive as Proton, is considerably cheaper at 29Euros, it's enough for most hobby operations.

Andy.