Professional Documents
Culture Documents
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
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.
PAGE 1
Revision History
Revision
Date
Translated By
V1.3
2006/12/22
Zhu Meng
Proofreading
V1.2
2006/03/28
Zhu Meng
Remark
PAGE 2
Page
Number(s)
Table of Content
PAGE
PAGE 3
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.
PAGE 4
PAGE 5
PAGE 6
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
...
PAGE 7
/*-------------------------------------------------------*/
/* -------------------------------------------------------.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
...
PAGE 8
/*-------------------------------------------------------*/
/*
.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;
...
= 0x00;
LEDCHG->Bits.bit0 = 1;
LEDCHG->Bits.bit0 = 0;
LEDCHG->Bits.bit5 = 1;
LEDCHG->Bits.bit5 = 0;
LEDCHG->Bits.bit10 = 1;
LEDCHG->Bits.bit10 = 0;
Sunplus Technology Co., Ltd.
PAGE 9
LEDCHG->Bits.bit15 = 1;
LEDCHG->Bits.bit15 = 0;
...
PAGE 10
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
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
...
=
=
=
=
=
=
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
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.
PAGE 13