Hi All,
There have been some previous posts regarding the use of a structure to reference registers. This looks very clean and allows explicit definition of bits within registers and allows bitwise operations without having to do LSR/ LSL operations. I like this approach.
The problem I have is that the code equivalents for the bit wise operations are MUCH larger. Below we have the header file and a section of code that contrast the code generation for the comparable access types.
As other people have suggested that other compilers/ development systems allow this method in various ways. I'm interested in looking at why the code generated is _SO_ different.
This is what I have at the moment. There's initialisation of PORT3 and an access to toggle PORT3.5 using old and new methods. The Code generated is inserted in italics.
I hope the community can help here.
Regards, Kirem
hardware.h file:
union uport
{
unsigned char BYTE;
struct
{
unsigned char _BIT7 :1;
unsigned char _BIT6 :1;
unsigned char _BIT5 :1;
unsigned char _BIT4 :1;
unsigned char _BIT3 :1;
unsigned char _BIT2 :1;
unsigned char _BIT1 :1;
unsigned char _BIT0 :1;
}BIT;
};
#define P3_OUT (*(union uport *) &P3OUT)
#define P3_DIR (*(union uport *) &P3DIR)
main.c file
#include "hardware.h"
void main(void)
{
// byte access
// (option 1) byte-wise access method, code is exactly the same.
P3_DIR.BYTE |= (0x01 << 5) | (0x01 << 6);
0x085A4: 1840 D0F2 0060 0224 BISX.B #0x00060,&Port_3_4_P3DIR
// (option 2) direct memory accress
P3DIR |= (0x01 << 5) | (0x01 << 6);
0x085AC: 1840 D0F2 0060 0224 BISX.B #0x00060,&Port_3_4_P3DIR
// toggling bit 5 in P3OUT
// (option 1) BIT wise access method using the union
P3_OUT.BIT._BIT5 ^= (0x1<<5);
0x0764E: 008D 0222 MOVA #0x00222,R13
0x07652: 4D6F MOV.B @R13,R15
0x07654: 0FCE MOVA R15,R14
0x07656: C26E BIC.B #4,R14
0x07658: 075F RRUM.W #2,R15
0x0765A: F35F AND.B #1,R15
0x0765C: E07F 0020 XOR.B #0x0020,R15
0x07660: F35F AND.B #1,R15
0x07662: 1841 5F4F RPT #2 RLAX.B R15
0x07666: DE4F BIS.B R14,R15
0x07668: 4FCD 0000 MOV.B R15,0x0000(R13)
// (option 2) BITwise access method using the old method
P3OUT ^= (0x1<<5);
0x0766C: 1840 E0F2 0020 0222 XORX.B #0x00020,&Port_3_4_P3OUT
}