You are on page 1of 14

Easy-to-Use Byte and Bit Mode

Operation Using C Data Structure


V1.3 - Dec 22, 2006
English Version

19, Innovation First Road Science Park Hsin-Chu Taiwan 300 R.O.C.
Tel: 886-3-578-6005

Fax: 886-3-578-4418

http://www.sunplusmcu.com

E-mail: mcu@sunplus.com

http://mcu.sunplus.com

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

Important Notice
SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information
provided by SUNPLUS TECHNOLOGY CO. is believed to be accurate and reliable.

However, SUNPLUS

TECHNOLOGY CO. makes no warranty for any errors which may appear in this document. Contact SUNPLUS
TECHNOLOGY CO. to obtain the latest version of device specifications before placing your order.

No

responsibility is assumed by SUNPLUS TECHNOLOGY CO. for any infringement of patent or other rights of third
parties which may result from its use. In addition, SUNPLUS products are not authorized for use as critical
components in life support systems or aviation systems, where a malfunction or failure of the product may
reasonably be expected to result in significant injury to the user, without the express written approval of Sunplus.

Sunplus Technology Co., Ltd.

PAGE 1

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

Revision History

Revision

Date

Translated By

V1.3

2006/12/22

Zhu Meng

Proofreading

V1.2

2006/03/28

Zhu Meng

Translate Easy-to-Use Byte and Bit Mode


Operation Using C Data Structure V1.2,
Chinese version

Sunplus Technology Co., Ltd.

Remark

PAGE 2

Page
Number(s)

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

Table of Content
PAGE

1 General Description ........................................................................................................................... 4


1.1 Introduction.................................................................................................................................... 4
1.2 Concepts........................................................................................................................................ 4
1.3 Design Tips.................................................................................................................................... 4
1.4 Basic Application ........................................................................................................................... 6
2 Application Example .........................................................................................................................11
3 Reference .......................................................................................................................................... 13

Sunplus Technology Co., Ltd.

PAGE 3

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

1 General Description
1.1 Introduction
Owing to the advantages of readability, modularity and maintainability of structured
programming, it is important for the programmer to catch the thinking and method of
structured programming.
In this application, we take a bitwise operation as an example to demonstrate the general
coding method on SPMC75F2413A, where these bits are defined by structure and union.

1.2 Concepts
A structure (also called records) is a collection of one or more variables grouped together
under a single name. Different from array, the variables may be of different types.
Structure is a new data type that is descendent from other ones.
Same to the structure, union is also a descendent data type. A union is a variable that
may hold (at different times) objects of different types and sizes, but these objects share
the same storage and any one of them may be assigned to the union and then used in
expressions. So the variable will be large enough to hold the largest objects in it. It is
the programmers responsibility to keep track of which type is currently stored in a union.
A bit field is a set of adjacent bits within a single implementation-defined storage unit that
we call a word. Bit fields can pack several objects into a single machine word. These
objects must first be declared as int or unsigned int.

1.3 Design Tips


Here we take some simple examples to demostrate how to use structure, union and
bit-fields.
[Example1-1] Declare, initialize a structure and access its members.
/*-------------------------------------------------------*/
/* create a structure type
/*-------------------------------------------------------*/
typedef struct structdef
{
unsigned char
var1;
//declare var1 as unsigned character
unsigned int
var2;
//declare var2 as unsigned integer
unsigned long
var3;
//declare var3 as unsigned long integer
float
var4;
//declare var4 as single-precision floating point
double
var5;
//declare var5 as double-precision floating point
} STRUCTDEF;
/*-------------------------------------------------------*/
/*define a group variables as the declared struct type,
Sunplus Technology Co., Ltd.

PAGE 4

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

//and initialize these members


/*-------------------------------------------------------*/
STRUCTDEF variable1 = {'C',
//Member var1='c'
0xFF00,
//Member var2=0xFF00
0xFFFFF000, //Member var3=0xFFFFF000
0.67,
//Member var4=0.67
6.346234}; //Member var5=6.346234
STRUCTDEF variable2;
STRUCTDEF *ptr = &variable1;
//declare a pointer to the struct
/*-------------------------------------------------------*/
/* access to the member of union
/*-------------------------------------------------------*/
//be referenced to by the operator . or ->
variable2.var1 = ptr->var1;
variable2.var2 = ptr->var2;
variable2.var3 = ptr->var3;
variable2.var4 = ptr->var4;
variable2.var5 = ptr->var5;

[Example1-2] Declare, initialize a union and access its members.


/*-------------------------------------------------------*/
/*create a union type
/*-------------------------------------------------------*/
typedef union uniondef
{
unsigned int
var1;
//declare var1 as unsigned integer
float
var2;
//declare var2 as single-precision float
}UNIONDEF;
/*------------------------------------------------------Define and initialize variables by union
------------------------------------------------------The union must be initialized by the first members type
UNIONDEF unionvar={10};
------------------------------------------------------Access to the member of union
-------------------------------------------------------*/
unionvar.var1 = 5366;
unionvar.var2 = 3.875;

[Example1-3] Define a bit-field and access its members.


/*-------------------------------------------------------*/
/* create a bit-field type
/*-------------------------------------------------------*/
/*
.Note:
There are four unsigned int bit-fields are declared: bit0, bit1,
bit2 and bit3. These bits are declared by following a : and a
integer constant after the member name. The constant defines the
length (bit number)of this member and must be within the range of
zero to the bit number of integer storaged in current system.
For SPMC75F2413A, a 16-bit processor, it ranges within [0,15].
Sunplus Technology Co., Ltd.

PAGE 5

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

After defined as following, bit0 occupies a 8-bit space, bit1 occupies


an 4-bit space,etc. In general, the space are defined by what size of
data the member requires. Bit0 can store the data within [0,255] and
bit1 can store the data within [0,15],etc.
*/
typedef struct bitfielddef
{
UInt16 bit0
: 8;
//8-Bit
UInt16 bit1
: 4;
//8-Bit
UInt16 bit2
: 3;
//8-Bit
UInt16 bit3
: 1;
//8-Bit
}BITFIELDDEF;
/*-------------------------------------------------------*/
/* access to the member of bit-field
/*-------------------------------------------------------*/
BITFIELDDEF
bitvar;
bitvar. bit0 = 108;
bitvar. Bit1 = 10;
bitvar. Bit2 = 6;
bitvar. Bit3 = 1;

1.4 Basic Application


Here we introduce a structure programming method for your design on PMC75F2413A.
Through creating some new data types, we can define variables and registers clearly and
modularly.
[Example1-4] Bitwise operation in C.
/*-------------------------------------------------------*/
/* create a new type
/*-------------------------------------------------------*/
/* -------------------------------------------------------.Note:
1.Declare a union data type and define the member as bit-field
2.Operate a Word (16-bit)data with word wise or bitwise.
------------------------------------------------------*/
typedef union
{
UInt16 W;
struct
{
UInt16 bit0
: 1;
//LSB
UInt16 bit1
: 1;
UInt16 bit2
: 1;
UInt16 bit3
: 1;
UInt16 bit4
: 1;
UInt16 bit5
: 1;
UInt16 bit6
: 1;
UInt16 bit7
: 1;
UInt16 bit8
: 1;
UInt16 bit9
: 1;
UInt16 bit10
: 1;
UInt16 bit11
: 1;
Sunplus Technology Co., Ltd.

PAGE 6

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

UInt16
UInt16
UInt16
UInt16

bit12
bit13
bit14
bit15

:
:
:
:

1;
1;
1;
1;

//MSB
}B;
}GENBITSDEF;
/*-------------------------------------------------------*/
/* define a variable with the descendent type
/*-------------------------------------------------------*/
GENBITSDEF sBits;
//user-defined GENBITSDEF
/*-------------------------------------------------------*/
/* access to its members and bitwise operation
/*-------------------------------------------------------*/
sBits.W = 0xA5FF;
//wordwise operation
sBits.B.bit0 = 0;
//bitwise operation, clear Bit0
sBits.B.bit15 = 0;
//bitwise operation, clear Bit15
...

[Example1-5] Bytewise operation in C.


/*-------------------------------------------------------*/
/* create a new type
/*-------------------------------------------------------*/
/* -------------------------------------------------------.Note:
Operate a Word (16-bit) data with wordwise or bytewise.
/* ------------------------------------------------------*/
typedef union
{
UInt16 W;
struct
{
UInt16 byte0
: 8;
UInt16 byte1
: 8;
}B;
}GENBYTEDEF;
/*-------------------------------------------------------*/
/* define a variable with the descendent type
/*-------------------------------------------------------*/
GENBYTEDEF sByte;
//user-defined GENBYTEDEF
/*-------------------------------------------------------*/
/*access to its members and bytewise operation
/*-------------------------------------------------------*/
sByte.W = 0xA5FF;
//wordwise operation
sByte.B.byte0 = 0xA5;
//bytewise operation, low byte assignment
sByte.B.byte1 = 0xFF;
//bytewise operation, high byte assignment
...

[Example1-6] Bytewise and bitwise operation in C.


/*-------------------------------------------------------*/
/*create a new type
Sunplus Technology Co., Ltd.

PAGE 7

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

/*-------------------------------------------------------*/
/* -------------------------------------------------------.Note:
Create a new data type for the operations of wordwise, bytewise,
Bitwise.
/* ------------------------------------------------------*/
typedef union
{
UInt16 Word;
struct
{
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
UInt16
}Bits;

bit0
bit1
bit2
bit3
bit4
bit5
bit6
bit7
bit8
bit9
bit10
bit11
bit12
bit13
bit14
bit15

:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:

1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;

//LSB

//MSB

struct
{
UInt16 byte0
: 8;
//Low byte
UInt16 byte1
: 8;
//High byte
}Byte;
}GENTYPEDEF;
/*-------------------------------------------------------*/
/* define a variable with the descendent type
/*-------------------------------------------------------*/
GENTYPEDEF sMixb;
//user-defined GENTYPEDEF
/*-------------------------------------------------------*/
/* ccess to its members, bitwise and bytewise operation
/*-------------------------------------------------------*/
sMixb.Word = 0xA5FF;
//wordwise operation
sMixb.Bits.bit14 = 1;
//bitwise operation
sMixb.Bits.bit15 = 0;
//bitwise operation
sMixb.Byte.byte0 = 0;
//bytewise operation
sMixb.Byte.byte1 = 0;
//bytewise operation
...

[Example1-7] Bitwise operation to some common types in C.


/*-------------------------------------------------------*/
/* explicit Conversion among common types
Sunplus Technology Co., Ltd.

PAGE 8

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

/*-------------------------------------------------------*/
/*
.Note:
Convert a UInt16 type variable into a GENBITSDEF pointer, then
the these members can be accessed by the pointer ptr.
*/
#define
ptr ((volatile GENTYPEDEF *)(&variable))
//Convert common type variable
/*-------------------------------------------------------*/
/*define variable
/*-------------------------------------------------------*/
UInt16 variable = 0;
//define a UInt16 variable
/*-------------------------------------------------------*/
/* access to its members, bitwise and bytewise operation
/*-------------------------------------------------------*/
ptr->Word = 0x90Fe;
//accessing to the converted variable
ptr->Bits.bit0 = 1;
ptr->Bits.bit15 = 0;
ptr->Byte.byte0 = 0xFF;
ptr->Byte.byte1 = 0x00;
...

[Example1-8] Bitwise operation to hardware in C


/*-------------------------------------------------------*/
/* A hardware address variable conversion
/*-------------------------------------------------------*/
/* -------------------------------------------------------.Note:
Convert a register address variable to a GENTYPEDEF pointer.
The same to the operations on memory, the hardware can be
accessed by the operations of bitwise and bytewise.
/* ------------------------------------------------------*/
#define
LEDCHG ((volatile GENTYPEDEF *)(P_IOD_Buffer_ADDR))
/*-------------------------------------------------------*/
/* Bitwise operation to IOD
/*-------------------------------------------------------*/
LEDCHG->Byte.byte0 = 0xA5;
//bytewise operation to IOD
LEDCHG->Byte.byte0 = 0x5A;
LEDCHG->Byte.byte1 = 0xA5;
LEDCHG->Byte.byte1 = 0x5A;
LEDCHG->Word

= 0x00;

LEDCHG->Bits.bit0 = 1;
LEDCHG->Bits.bit0 = 0;

//wordwise operation to IOD


//bitwise operation to IOD

LEDCHG->Bits.bit5 = 1;
LEDCHG->Bits.bit5 = 0;
LEDCHG->Bits.bit10 = 1;
LEDCHG->Bits.bit10 = 0;
Sunplus Technology Co., Ltd.

PAGE 9

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

LEDCHG->Bits.bit15 = 1;
LEDCHG->Bits.bit15 = 0;
...

Sunplus Technology Co., Ltd.

PAGE 10

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

2 Application Example
Create a descendent data type to access and initialize SPMC75F2413A register briefly.
/*-------------------------------------------------------*/
/*create a new type
/*-------------------------------------------------------*/
/*************************************************************/
/* UART Control Register (P_UART_Ctrl)
*/
/* bit 0
: reserved
*/
/* bit 1
: PEN, Parity Enable
*/
/* bit 2
: PSEL, Parity Selection
*/
/* bit 3
: SBSEL, Stop Bit Size Selection
*/
/* bit 4 - 8 : reserved
*/
/* bit 9
: RXCHSEL,Reception data channel selection
*/
/*
0: UART reception from RXD2
*/
/*
1: UART reception from RXD2
*/
/* bit 10
: TXCHSEL, Transmission data channel selection
*/
/*
0: UART transmission to TXD2
*/
/*
1. UART transmission to TXD1
*/
/* bit 11
: Reset, Software reset
*/
/* bit 12
: TXEN, txd pin enable
*/
/* bit 13
: RXEN, rxd pin enable
*/
/* bit 14
: TXIE , Transmit Interrupt Enable
*/
/* bit 15
: RXIE , Receive Interrupt Enable
*/
/*************************************************************/
typedef union
{
UInt16 W;
struct
{
UInt16 reserved1
: 1;
UInt16 PEN
: 1;
UInt16 PSEL
: 1;
UInt16 SBSEL
: 1;
UInt16 reserved2
: 5;
UInt16 RXCHSEL
: 1;
UInt16 TXCHSEL
: 1;
UInt16 Reset
: 1;
UInt16 TXEN
: 1;
UInt16 RXEN
: 1;
UInt16 TXIE
: 1;
UInt16 RXIE
: 1;
} B;
} P_UART_Ctrl_DEF;
/*-------------------------------------------------------*/
/* address variable conversion
/*-------------------------------------------------------*/
#define P_UART_Ctrl_ADDR
0x7102
#define P_UART_Ctrl
((volatile P_UART_Ctrl_DEF *)(P_UART_Ctrl_ADDR))
/*-------------------------------------------------------*/
/* Initialize UART control register
/*-------------------------------------------------------*/
Sunplus Technology Co., Ltd.

PAGE 11

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

P_UART_Ctrl->W = 0x00;
P_UART_Ctrl->B.PEN
P_UART_Ctrl->B.RXCHSEL
P_UART_Ctrl->B.TXCHSEL
P_UART_Ctrl->B.RXEN
P_UART_Ctrl->B.TXEN
P_UART_Ctrl->B.RXIE
...

Sunplus Technology Co., Ltd.

=
=
=
=
=
=

1;
1;
1;
1;
1;
1;

//clear register
//parity Enable
//select IOB12 pin as reception channel
//select IOB13 pin as transmission channel
//UART reception enable
//UART transmission enable
//receive interrupt enable

PAGE 12

V1.3 - Dec 22, 2006

Easy-to-Use Byte and Bit Mode Operation Using C Data Structure

3 Reference
[1] SUNPLUS, SPMC75F2413A Programming Guide V1.0, Oct 12, 2004.
[2]H.M.Deitel, P.J.Deitel, Programming in C, China Machine Press.

Sunplus Technology Co., Ltd.

PAGE 13

V1.3 - Dec 22, 2006

You might also like