Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] is it possible to use single bit in ports of PIC mcu without this structure??

Status
Not open for further replies.

Deexith Hasan

Advanced Member level 4
Full Member level 1
Joined
Oct 24, 2014
Messages
111
Helped
4
Reputation
8
Reaction score
3
Trophy points
18
Visit site
Activity points
740
is it possible to use single bit in ports of PIC mcu without this structure??


Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
extern volatile near union{
struct{
unsigned RB0:1;
unsigned RB1:1;
unsigned RB2:1;
unsigned RB3:1;
unsigned RB4:1;
unsigned RB5:1;
unsigned RB6:1;
unsigned RB7:1;
};
}PORTBbits;

 
Last edited by a moderator:

Of course, that is just a way of addressing them by name.
You can write a value directly to the port, use logical operations on the port or #define the pins individualy with names of their own.

Brian.
 
examples:
Code:
#define LED PORTB,0
#define SWITCH PORTB,1
#define SENSOR PORTB,2
#define OUTPUT PORTA,5
#define INPUT PORTC,4

Then you can use instructions like 'LED = 0;' or 'OUTPUT = 1;'

A more universal way is to use the bit shift instructions, for example:
Code:
PORTB |= (1 << LED);  PORTB &= ~(1 << LED);

Brian.
 
Not sure which compiler you are using but both the XC and older style compiler families have an "include" file that defines the names of all of the registers and bits. TYe files are generally named "pxxxx.h" where the xxx is the name of the device (i.e. PIC18F4550 would be "p18f4550.h" of something like that - going by memory here as you typically never need to refer to this include file directly).
I there you will find many union and struct definitions and then looking at the examples in the User Guide (and many other documents) you can see how they are used but, typically, you would refer to the 3rd bit in the LATA register as "LATAbits.LATA3". The names of the various registers that are used to control the device (i.e. other than the TRIS, LAT and PORT registers) often have the same bit names as they are labelled in the data sheet for the device.
AS this is all so easy, why would you not use the bit definitions directly? If nothing else, it makes your code easier for others (or you in a few months time) to understand and debug.
Susan
 
Although the use of instructions that performs bit mapped access to ports is available to PIC core, in order to provide some code portability to other cores, I usually prefer to do that using bit masking, so that could migrate the program with minimal changes.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top