CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Direct bit manipulation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
John Goss
Guest







Direct bit manipulation
PostPosted: Fri Sep 21, 2001 5:03 pm     Reply with quote

I want to assign Direct port pin manipulation using a structured name so that I may do the following:

PORTA = 0x55;
//or
PORTAbits.RA4 = HIGH


It is straightforward to assign the address to the port using the #byte directive. What I need to know is how then do I assign the structure "PORTAbits" to the same address as PORTA?

//PORTA
#byte PORTA = 0x05
extern struct {
unsigned RA0:1;
unsigned RA1:1;
unsigned RA2:1;
unsigned RA3:1;
unsigned RA4:1;
unsigned RA5:1;
unsigned RA6:1;
unsigned RA7:1;
} PORTAbits ;

#byte TRISA = 0x85
#define DDRA TRISA
___________________________
This message was ported from CCS's old forum
Original Post ID: 343
Tomi
Guest







Re: Direct bit manipulation
PostPosted: Sat Sep 22, 2001 11:26 am     Reply with quote

<font face="Courier New" size=-1>Try the following:
extern struct {
unsigned RA0:1;
unsigned RA1:1;
unsigned RA2:1;
unsigned RA3:1;
unsigned RA4:1;
unsigned RA5:1;
unsigned RA6:1;
unsigned RA7:1;
} PORTAbits ;
#byte PORTAbits = 0x05

Usage:
PORTAbits.RA0 = 0;
PORTAbits.RA1= 1;
PORTAbits.RA2 = 0;


:=I want to assign Direct port pin manipulation using a structured name so that I may do the following:
:=
:=PORTA = 0x55;
:=//or
:=PORTAbits.RA4 = HIGH
:=
:=
:=It is straightforward to assign the address to the port using the #byte directive. What I need to know is how then do I assign the structure "PORTAbits" to the same address as PORTA?
:=
:=//PORTA
:=#byte PORTA = 0x05
:=extern struct {
:= unsigned RA0:1;
:= unsigned RA1:1;
:= unsigned RA2:1;
:= unsigned RA3:1;
:= unsigned RA4:1;
:= unsigned RA5:1;
:= unsigned RA6:1;
:= unsigned RA7:1;
:=} PORTAbits ;
:=
:=#byte TRISA = 0x85
:=#define DDRA TRISA</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 347
pha555
Guest







Re: Direct bit manipulation
PostPosted: Sat Sep 22, 2001 2:18 pm     Reply with quote

I use a #include file that defines each register and each bit within a register;

#byte PORTA = 0x05
#byte PORTB = 0x06

#bit rb7 = PORTB.7
#bit rb6 = PORTB.6
#bit rb5 = PORTB.5
#bit rb4 = PORTB.4
#bit rb3 = PORTB.3
#bit rb2 = PORTB.2
#bit rb1 = PORTB.1
#bit rb0 = PORTB.0

This then permits simple manipulation of bits;


if (!rb7) // if at zero
{
}

or

rb0 = 1;


Peter H Anderson, <a href="http://www.phanderson.com/PIC/PICC/index.html" TARGET="_blank">http://www.phanderson.com/PIC/PICC/index.html</a>

:=I want to assign Direct port pin manipulation using a structured name so that I may do the following:
:=
:=PORTA = 0x55;
:=//or
:=PORTAbits.RA4 = HIGH
:=
:=
:=It is straightforward to assign the address to the port using the #byte directive. What I need to know is how then do I assign the structure "PORTAbits" to the same address as PORTA?
:=
:=//PORTA
:=#byte PORTA = 0x05
:=extern struct {
:= unsigned RA0:1;
:= unsigned RA1:1;
:= unsigned RA2:1;
:= unsigned RA3:1;
:= unsigned RA4:1;
:= unsigned RA5:1;
:= unsigned RA6:1;
:= unsigned RA7:1;
:=} PORTAbits ;
:=
:=#byte TRISA = 0x85
:=#define DDRA TRISA
___________________________
This message was ported from CCS's old forum
Original Post ID: 349
Tomi
Guest







Re: Direct bit manipulation
PostPosted: Sun Sep 23, 2001 3:42 am     Reply with quote

Yes, it is simpler and better, but I think John is migrating from MicroChip's MPLAB-C/C17 and he doesn't like to modify his old program (the PortXBits structures are pre-defined in MPLAB-C header files).

:=
:=I use a #include file that defines each register and each bit within a register;
:=
:=#byte PORTA = 0x05
:=#byte PORTB = 0x06
:=
:=#bit rb7 = PORTB.7
:=#bit rb6 = PORTB.6
:=#bit rb5 = PORTB.5
:=#bit rb4 = PORTB.4
:=#bit rb3 = PORTB.3
:=#bit rb2 = PORTB.2
:=#bit rb1 = PORTB.1
:=#bit rb0 = PORTB.0
:=
:=This then permits simple manipulation of bits;
:=
:=
:= if (!rb7) // if at zero
:= {
:= }
:=
:=or
:=
:= rb0 = 1;
:=
:=
:=Peter H Anderson, <a href="http://www.phanderson.com/PIC/PICC/index.html" TARGET="_blank"> <a href="http://www.phanderson.com/PIC/PICC/index.html" TARGET="_blank">http://www.phanderson.com/PIC/PICC/index.html</a></a>
:=
:=:=I want to assign Direct port pin manipulation using a structured name so that I may do the following:
:=:=
:=:=PORTA = 0x55;
:=:=//or
:=:=PORTAbits.RA4 = HIGH
:=:=
:=:=
:=:=It is straightforward to assign the address to the port using the #byte directive. What I need to know is how then do I assign the structure "PORTAbits" to the same address as PORTA?
:=:=
:=:=//PORTA
:=:=#byte PORTA = 0x05
:=:=extern struct {
:=:= unsigned RA0:1;
:=:= unsigned RA1:1;
:=:= unsigned RA2:1;
:=:= unsigned RA3:1;
:=:= unsigned RA4:1;
:=:= unsigned RA5:1;
:=:= unsigned RA6:1;
:=:= unsigned RA7:1;
:=:=} PORTAbits ;
:=:=
:=:=#byte TRISA = 0x85
:=:=#define DDRA TRISA
___________________________
This message was ported from CCS's old forum
Original Post ID: 350
John Goss
Guest







Re: Direct bit manipulation
PostPosted: Sun Sep 23, 2001 5:01 pm     Reply with quote

Yes, this is what I am looking for. I am converting code over from Microchip's C18 compiler actually.

Can two variables be assigned to the same address without problems:

.
.
.
unsigned RA6:1;
unsigned RA7:1;
} PORTAbits ;
#byte PORTAbits = 0x05
#byte PORTA = 0x05

Thanks,
John


:=<font face="Courier New" size=-1>Try the following:
:=extern struct {
:=unsigned RA0:1;
:=unsigned RA1:1;
:=unsigned RA2:1;
:=unsigned RA3:1;
:=unsigned RA4:1;
:=unsigned RA5:1;
:=unsigned RA6:1;
:=unsigned RA7:1;
:=} PORTAbits ;
:=#byte PORTAbits = 0x05
:=
:=Usage:
:=PORTAbits.RA0 = 0;
:=PORTAbits.RA1= 1;
:=PORTAbits.RA2 = 0;
:=
:=
:=:=I want to assign Direct port pin manipulation using a structured name so that I may do the following:
:=:=
:=:=PORTA = 0x55;
:=:=//or
:=:=PORTAbits.RA4 = HIGH
:=:=
:=:=
:=:=It is straightforward to assign the address to the port using the #byte directive. What I need to know is how then do I assign the structure "PORTAbits" to the same address as PORTA?
:=:=
:=:=//PORTA
:=:=#byte PORTA = 0x05
:=:=extern struct {
:=:= unsigned RA0:1;
:=:= unsigned RA1:1;
:=:= unsigned RA2:1;
:=:= unsigned RA3:1;
:=:= unsigned RA4:1;
:=:= unsigned RA5:1;
:=:= unsigned RA6:1;
:=:= unsigned RA7:1;
:=:=} PORTAbits ;
:=:=
:=:=#byte TRISA = 0x85
:=:=#define DDRA TRISA</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 353
Tomi
Guest







Re: Direct bit manipulation
PostPosted: Mon Sep 24, 2001 12:30 pm     Reply with quote

Yes, you can use e.g. the following:
extern struct {
unsigned RA0:1;
unsigned RA1:1;
unsigned RA2:1;
unsigned RA3:1;
unsigned RA4:1;
unsigned RA5:1;
unsigned RA6:1;
unsigned RA7:1;
} PORTAbits ;
#byte PORTAbits = 0x05

extern struct {
unsigned RA03:4;
unsigned RA47:4;
} PORTAnibbles ;
#byte PORTAnibbles = 0x05

#byte PORTA = 0x05

Disassembled samples:
.................... PORTAbits.RA1= 1;
01A3: BSF 05,1
.................... PORTAbits.RA2 = 0;
01A4: BCF 05,2
.................... PORTAnibbles.RA03 = 15;
01A5: MOVLW F0
01A6: ANDWF 05,W
01A7: IORLW 0F
01A8: MOVWF 05
.................... PORTA = 0xAB;
01A9: MOVLW AB
01AA: MOVWF 05
___________________________
This message was ported from CCS's old forum
Original Post ID: 359
John Goss
Guest







Re: Direct bit manipulation - Are two structures possible?
PostPosted: Tue Sep 25, 2001 11:39 am     Reply with quote

Well, I did the following, but when I try direct bit manipulation, it says "element not a member" ... It doesn't seem to like unions.. If I use just one structure, it works.
Can I use two different structures?

Thanks,
John

//PORTD--------------------

extern union {
struct {
unsigned RD0:1;
unsigned RD1:1;
unsigned RD2:1;
unsigned RD3:1;
unsigned RD4:1;
unsigned RD5:1;
unsigned RD6:1;
unsigned RD7:1;
};
struct {
unsigned PSP0:1;
unsigned PSP1:1;
unsigned PSP2:1;
unsigned PSP3:1;
unsigned PSP4:1;
unsigned PSP5:1;
unsigned PSP6:1;
unsigned PSP7:1;
};
} PORTDbits;
#byte PORTDbits = 0x08
#byte PORTD = 0x08
#byte PSP_DATA= 0x08
___________________________
This message was ported from CCS's old forum
Original Post ID: 369
Tomi
Guest







Re: Direct bit manipulation - Are two structures possible?
PostPosted: Tue Sep 25, 2001 12:18 pm     Reply with quote

Your problem is not CCS C specific; it is general C specific.
In your definition the union struct has two anonymous structures. Using a UNION means that you must say what part of the struct you use. In your code the compiler tries to find e.g. "PORTDbits..RD0" what is not an element, indeed (pay attention to double points!).
Use this:
extern union {
struct {
unsigned RD0:1;
unsigned RD1:1;
unsigned RD2:1;
unsigned RD3:1;
unsigned RD4:1;
unsigned RD5:1;
unsigned RD6:1;
unsigned RD7:1;
} IOPort;
struct {
unsigned PSP0:1;
unsigned PSP1:1;
unsigned PSP2:1;
unsigned PSP3:1;
unsigned PSP4:1;
unsigned PSP5:1;
unsigned PSP6:1;
unsigned PSP7:1;
} PSPPort;
} PORTDbits;
In my example you have a PSPPort and an IOPort structures; plus you have a union PORTDbits.
To access an element in a union you can type:
PORTDbits.PSPPort.PSP0 = 1;
PORTDbits.IOPort.RD0 = 0;


:=Well, I did the following, but when I try direct bit manipulation, it says "element not a member" ... It doesn't seem to like unions.. If I use just one structure, it works.
:=Can I use two different structures?
:=
:=Thanks,
:=John
:=
:=//PORTD--------------------
:=
:=extern union {
:= struct {
:= unsigned RD0:1;
:= unsigned RD1:1;
:= unsigned RD2:1;
:= unsigned RD3:1;
:= unsigned RD4:1;
:= unsigned RD5:1;
:= unsigned RD6:1;
:= unsigned RD7:1;
:= };
:= struct {
:= unsigned PSP0:1;
:= unsigned PSP1:1;
:= unsigned PSP2:1;
:= unsigned PSP3:1;
:= unsigned PSP4:1;
:= unsigned PSP5:1;
:= unsigned PSP6:1;
:= unsigned PSP7:1;
:= };
:=} PORTDbits;
:=#byte PORTDbits = 0x08
:=#byte PORTD = 0x08
:=#byte PSP_DATA= 0x08
___________________________
This message was ported from CCS's old forum
Original Post ID: 370
John Goss
Guest







Re: Direct bit manipulation - Are two structures possible?
PostPosted: Tue Sep 25, 2001 1:54 pm     Reply with quote

Thank you!

:=Your problem is not CCS C specific; it is general C specific.
:=In your definition the union struct has two anonymous structures. Using a UNION means that you must say what part of the struct you use. In your code the compiler tries to find e.g. "PORTDbits..RD0" what is not an element, indeed (pay attention to double points!).
:=Use this:
:=extern union {
:=struct {
:=unsigned RD0:1;
:=unsigned RD1:1;
:=unsigned RD2:1;
:=unsigned RD3:1;
:=unsigned RD4:1;
:=unsigned RD5:1;
:=unsigned RD6:1;
:=unsigned RD7:1;
:=} IOPort;
:=struct {
:=unsigned PSP0:1;
:=unsigned PSP1:1;
:=unsigned PSP2:1;
:=unsigned PSP3:1;
:=unsigned PSP4:1;
:=unsigned PSP5:1;
:=unsigned PSP6:1;
:=unsigned PSP7:1;
:=} PSPPort;
:=} PORTDbits;
:=In my example you have a PSPPort and an IOPort structures; plus you have a union PORTDbits.
:=To access an element in a union you can type:
:=PORTDbits.PSPPort.PSP0 = 1;
:=PORTDbits.IOPort.RD0 = 0;
:=
:=
:=:=Well, I did the following, but when I try direct bit manipulation, it says "element not a member" ... It doesn't seem to like unions.. If I use just one structure, it works.
:=:=Can I use two different structures?
:=:=
:=:=Thanks,
:=:=John
:=:=
:=:=//PORTD--------------------
:=:=
:=:=extern union {
:=:= struct {
:=:= unsigned RD0:1;
:=:= unsigned RD1:1;
:=:= unsigned RD2:1;
:=:= unsigned RD3:1;
:=:= unsigned RD4:1;
:=:= unsigned RD5:1;
:=:= unsigned RD6:1;
:=:= unsigned RD7:1;
:=:= };
:=:= struct {
:=:= unsigned PSP0:1;
:=:= unsigned PSP1:1;
:=:= unsigned PSP2:1;
:=:= unsigned PSP3:1;
:=:= unsigned PSP4:1;
:=:= unsigned PSP5:1;
:=:= unsigned PSP6:1;
:=:= unsigned PSP7:1;
:=:= };
:=:=} PORTDbits;
:=:=#byte PORTDbits = 0x08
:=:=#byte PORTD = 0x08
:=:=#byte PSP_DATA= 0x08
___________________________
This message was ported from CCS's old forum
Original Post ID: 376
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group