Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 14th June 2006, 11:16 AM   #1
Default I/O output

How do I change a single bit value of a port? What is the syntax?
Manie is offline  
Old 14th June 2006, 11:36 AM   #2
Default

Quote:
Originally Posted by Manie
How do I change a single bit value of a port? What is the syntax?
Depends what processor you're talking about, and what laguage you're using, your question is far too vague to answer!.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th June 2006, 11:37 AM   #3
Default

I am using a PIC16f737 and programming in C
Manie is offline  
Old 14th June 2006, 11:39 AM   #4
Default

Quote:
Originally Posted by Manie
I am using a PIC16f737 and programming in C
Can't help you with C, but in assembler it's simply:

bsf Port, pin

to set a pin high

bcf Port, pin

to clear a pin.

As it's such a basic thing to do, you should have suitable examples with your C compiler?.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th June 2006, 01:50 PM   #5
Default

Quote:
Originally Posted by Manie
I am using a PIC16f737 and programming in C
port_value = port_value | (1<< bitposition)

or

port_value |= (1<< bitposition)
__________________
Gods own Country
Incredible !ndia

www.flickr.com/photos/_akg/

"Give a man a fish, and he will eat for a day. Teach that man to fish, and he will eat for a lifetime."
akg is offline  
Old 14th June 2006, 02:37 PM   #6
Default

example:

setting PORTB bit 0
PORTB|=(1<<0);

setting PORTB bit 2
PORTB|=(1<<2);


clearing PORTB bit 2
PORTB&=~(1<<2);
__________________
Learning is a life-long process...
GraveYard_Killer is offline  
Old 14th June 2006, 02:49 PM   #7
Default

Check your C-compiler. Many of them have a declaration syntax for bits. If they do then the statement is just something like

Code:
bit  GIE  INTCON.7

GIE = 0 ; /* Disable Interrupts */
...
GIE = 1 ; /* Enable Interrupts */
The declaration syntax is not part of the C language standard, but several compilers for processors that support bit addressing have invented such extensions. You do have to look carefully in the compiler documentation though.

Look for #pragma keyword=extended or something similar to see if there is a switch that enables or disables the extended language features.

If you think you've found something then write a short test program and look at the compiler output to see if it matches both your understanding and Nigel's example.

On real ports with a mixture of inputs and outputs you need to be careful with bsf and bcf instructions because in hardware they read all 8 bits of a port, modify the single bit, and write back all 8 bits. This can be problematic and there are warnings in the PIC data sheets.

Let us know what you find.

Last edited by Papabravo; 14th June 2006 at 02:52 PM.
Papabravo is offline  
Reply

Tags
i or o, output

Thread Tools
Display Modes




All times are GMT. The time now is 02:16 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker