View previous topic :: View next topic |
Author |
Message |
indiansiva555@gmail.com
Joined: 23 Jun 2012 Posts: 22
|
ACCESSING REGISTER |
Posted: Sat Dec 22, 2012 9:17 am |
|
|
I am a beginner.
Please post some examples for accessing the OPTION register and different ways of accessing the registers in PIC using CCS compiler. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Dec 22, 2012 9:34 am |
|
|
Don't.
99.9% of the time there is no need to directly access registers. Doing so is a 'sure sign' of a programmer thinking in assembler terms, rather than thinking "I'm using a relatively higher level language now".
Function that set the options for you (depend on the chip concerned), but are:
port_b_pullups()
ext_int_edge()
setup_timer_0()
It is like driving an automatic, and insisting on manually changing gear. There are a _few_ occasions, where this may be sensible (manually changing down before overtaking, or on hill descent to give engine braking), but the whole point of paying for the automatic, is to not have to do such things for most of the time....
#byte OPTION=("SFR:OPTION")
Then gives you a variable 'OPTION' that directly accesses the register, but generally, don't do this.
Best Wishes |
|
|
indiansiva555@gmail.com
Joined: 23 Jun 2012 Posts: 22
|
|
Posted: Sat Dec 22, 2012 10:51 am |
|
|
Code: | #include <16F877A.h>
#FUSES HS
#FUSES NOWDT
#FUSES NOLVP
#case
#use delay(clock=20MHz)
#define LED PIN_B4
#define SWITCH1 PIN_B7
void port_b_pullups(char value);
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_comparator(NC_NC_NC_NC);
//Example blinking LED program
while(TRUE)
{
port_b_pullups(1);
if (input(SWITCH1))
{
output_high(LED);
delay_ms(1000);
output_low(LED);
delay_ms(1000);
}
}
}
|
This is my program.
The LED blinks without any response to the pin B7. I have used a pull down resistor also. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sat Dec 22, 2012 11:08 am |
|
|
Hi,
This thread should be locked, as it's essentially identical to your previous
'Logical AND is not working' thread.... Now, all the same questions, and
troubleshooting suggestions have to be repeated over again....
Frankly, I have serious doubts about a 'C' programmer that shows such little
ability to help themselves.....
Good Luck,
John |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Dec 22, 2012 5:29 pm |
|
|
The last code posted was fixed and solved by you in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=49474&start=11
Just add the ! to your program above.
With the internal PortB pullups, the circuit below will work:
Code: |
___ Switch
To _|_|_
PIC -----------------o o------
pin |
B7 --- Ground
-
|
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat Dec 22, 2012 5:51 pm |
|
|
Ttelmah wrote: |
#byte OPTION=("SFR:OPTION")
|
I think you meant:
#byte OPTION = getenv("SFR:OPTION")
(i.e. #byte LATB = getenv("SFR:LATB") )
No? _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
indiansiva555@gmail.com
Joined: 23 Jun 2012 Posts: 22
|
|
Posted: Sat Dec 22, 2012 11:29 pm |
|
|
I got it working with the use of ! but I want to know why I don't get proper results without the use of it. I have also disabled the pull up resistors of PORT B and pulled down PIN B7 externally.
I tried with PIN A0 which does not have any internal pull-ups, but I get the same result for that. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sun Dec 23, 2012 1:24 am |
|
|
If it works with direct access to option, but port_b_pullups doesn't work, then it suggests you are using some 'antique' compiler. Possibly an early V4 version.
If the code doesn't work at all with the internal pullups, but does with an external resistor, then it suggests noise. The internal pullups are _weak_ a few uA only. If you have a reasonable length of wire feeding an input pulled up with these, in a noisy environment, then you will see both 1's and 0's being detected...
Best Wishes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sun Dec 23, 2012 2:42 am |
|
|
bkamen wrote: | Ttelmah wrote: |
#byte OPTION=("SFR:OPTION")
|
I think you meant:
#byte OPTION = getenv("SFR:OPTION")
(i.e. #byte LATB = getenv("SFR:LATB") )
No? |
Absolutely. |
|
|
|