PDA

View Full Version : Help in trying to set pins functions



MinesCoffee
- 7th December 2012, 15:51
I'm trying to set the pins functions of 16F723. As I'm total green to PICs I'm finding it hard to understand the datasheet, I've been trying to find info on how to read and understand the datasheet but with no luck, can anyone point me to such info.
I would like to set all the Ports to digital, pull-ups on Port B and internal oscillator running at 4MHz.

The settings I'm using are the following:
OSCCON = %01100000 ' Sets the internal oscillator register to 4 MHz
ADCON1=%00000111 ' All Ports Digital
OPTION_REG.7 = 0 ' Enable Port B pull-ups

TRISB = %11111111 ' Port B input
TRISA = %00000000 ' Port A output
TRISC = %11111100 ' Port C inputs C.2 to C.7 inputs Port C.0 and C.1 outputs

Can you let me know if the settings any right or explane what is wrong.

HenrikOlsson
- 7th December 2012, 16:55
Hi,

OSCCON = %01100000 ' Sets the internal oscillator register to 4 MHz
Nope, the two most significant bits are not used so that can't be right. OSCCON = %00010000 would be 4MHz IF the PLL is enabled (which you do with the CONFIG bits). If the PLL is not enabled you'd get 125kHz.


ADCON1=%00000111 ' All Ports Digital
Nope, ADCON1 basically controls the clock to the ADC and from where it gets its references. To set which pins are analog vs digital you use ANSELA, ANSELB, ANSELE


OPTION_REG.7 = 0 ' Enable Port B pull-ups
Yes and no. This "turns on" the pullups but you also need to select WHICH of the 8 pullups to be enabled - you do that thru the WPUB register.



TRISB = %11111111 ' Port B input
TRISA = %00000000 ' Port A output
TRISC = %11111100 ' Port C inputs C.2 to C.7 inputs Port C.0 and C.1 outputs
Yep, that seems right.

Always use the datasheet for the correct device. Just because it's done one way with one device for which you may see some code on the 'net doesn't mean it's the same for another device - that would just be TOO easy ;-)

As for "how to read the datasheet" I don'r have any good advice really. It's just a matter of reading thru the section pertaining to the specific peripheral you're using. Oh, one tip though, at the end of each section there's a list of registers which, in one way or another, relates to the specific peripheral. If something doesn't work the way you expect go thru that list and make sure you haven't missed something.

/Henrik.

MinesCoffee
- 8th December 2012, 17:39
Thanks for the input, I do not understand CONFIG bits and PLL is enabling, where can I find how to do this? I have look at lots of basic code and have not seen any reference to CONFIG bits so don't know where to start. I still can not workout how to set all the ports to digital when I try to use ANSELA, ANSELB, ANSELE I can not get the code to compile just get the following errors C:\PBP\PBPPIC14.LIB 463 : Symbol not previously defined (ANSEL) and Error[113] C:\PBP\PBPPIC14.LIB 449 : Symbol not previously defined (ANSELH)...

HenrikOlsson
- 8th December 2012, 18:03
My bad perhaps, ANSELE is not implemented in the 28pin devices (722, 723, 726), only in the larger variants in the family. ANSELA and ANSELB should work though.

PBP has a default CONFIG for each device which it uses if you don't tell it otherwise. You can either override the CONFIG in your programmer software or put the changes in your code (recommended). If you have PBP3 the open the manual and look at the #CONFIG directive, that's what you use to override the defaults. If you have an older version you need to either edit/change the default config OR comment it out and add your own in your code. For more info on that, take a look at this thread (http://www.picbasic.co.uk/forum/showthread.php?t=543&highlight=CONFIG).

/Henrik.