PDA

View Full Version : 16F688 port puzzle



manxman
- 1st October 2007, 21:53
I am using a 16F688, with internal clock enabled and only 2 port connections: to LED1 and LED2 on PortC.

This trivial program shows a problem I have found with the Port addressing:


CMCON0=%0000111 'Comparators off
OSCCON.3=1 'Use internal clock
TRISC=0 'PortC is all output

LED1 Var PortC.3
LED2 Var PortC.5
Timer Var WORD

Timer=0

Loop: Pause 20 'Delay makes LED flash at about 1Hz
Timer=Timer+1
IF Timer.4=1 then
LED1=1
LED2=1
ENDIF
IF Timer.4=0 then
LED1=0
LED2=0
ENDIF
GOTO Loop

END

Using this program only LED2 will flash. However, if the statements LED2=1 and LED2=0 are removed, then LED1 will flash. It is not possible to make both flash together!

Any ideas? I am completely mystified.

mister_e
- 1st October 2007, 22:04
Seems to be an analog port related stuff...

let's open the 16F688 datasheet... mmm i see...

what about if you add...


ANSEL = 0

at the top of your code?

manxman
- 1st October 2007, 22:29
Yup, that solved the problem!

Now I realy must try to understand what ANSEL actually does ....

A BIG thanks again to Mister_E!

mister_e
- 1st October 2007, 22:34
In that datasheet, ANSEL is not really well documented like most other PIC registers. You need to read between the line. Open the PDF and search for ANSEL, you'll find few hits.

Basically each bit of ANSEL is assigned to a ADC channel. Bit 0 to AN0, Bit 1 to AN1 and so on. POR value is 255, %11111111. If a specific bit is set, the corresponding channel will be set as analog, if it's clear, it's digital. ANSEL=0 will obviously set every channel to digital.

EDIT: erm... ok it's documented... Register 4-3, PDF page 36. It's just not in the ADC section... as it 'should be'. Why??? Because :D

manxman
- 1st October 2007, 22:48
Yes, I also did a search for ANSEL in the datsheet and found nothing of much use.

Although it works in this example, I am still unclear why ANSEL has any effect on the behaviour of LEDs on PortC.3 and PortC.5 which are not configurable for analogue.

mister_e
- 1st October 2007, 22:52
Miip! C.3 is also AN7...

In theory ANSEL.7=0 should also work.

PS: Read again post 4, i changed it while you replied.

Using a analog port as digital output will do weird things...

nomad
- 2nd October 2007, 04:30
now why isn't this stuff in the faq section? ansel cmcon etc etc

manxman
- 2nd October 2007, 08:54
A big thanks again Mister_e. Indeed PortC.3 is analogue too. Sorry I missed that! The datasheet could be much clearer on issues like this or, as Nomad says, such pearls of port configuration wisdom could be distilled into a HOW TO section of FAQs.

paul borgmeier
- 2nd October 2007, 14:52
now why isn't this stuff in the faq section? ansel cmcon etc etc

http://www.picbasic.co.uk/forum/showthread.php?t=561

nomad
- 5th October 2007, 01:03
http://www.picbasic.co.uk/forum/showthread.php?t=561

yeah yeah lol, phone browser etc I found them now.