Basic Microcontroller in C Programming.pdf

(224 KB) Pobierz
6
C8051F020 C Programming
6.0
Introduction
6.1
Register Definitions, Initialization and Startup
Code
Basic C program Structure
6.2
Programming Memory Models
Overriding the default memory model, Bit-valued data,
Special Function Registers, Locating Variables at absolute
addresses
6.3
C Language Control Structures
Relational Operators, Logical Operators, Bitwise Logical
Operators, Compound Operators, Making Choices (if..else,
switch .. case), Repetition(for loop, while loop), Waiting for
Events, Early Exits
6.4
Functions
Standard functions - Initializing System Clock, Memory Model
Used for a Function
6.5
Interrupt Functions
Timer 3 Interrupt Service Routine, Disabling Interrupts before
Initialization, Timer 3 Interrupt Initialization, Register Banks
6.6
Reentrant functions
6.7
Pointers
A Generic Pointer in Keil TM C, Memory Specific Pointers
6.8
Summary of Data Types
6.9
Tutorial Questions
2
Chapter 6 C8051F020 C Programming
6.0 Introduction
This chapter introduces the Keil TM C compiler for the Cygnal C8051F020
board. We assume some familiarity with the C programming language to
the level covered by most first courses in the C language.
Experienced C programmers who have little experience with the
C8051F020 architecture should become familiar with the system. The
differences in programming the C8051F020 in C compared to a standard
C program are almost all related to architectural issues. These
explanations will have little meaning to those without an understanding of
the C8051F020 chip.
The Keil TM C compiler provided with the Cygnal C8051F020 board does
not come with a floating point library and so the floating point variables
and functions should not be used. However if you require floating point
variables a full license for the Keil TM C compiler can be purchased.
6.1 Register Definitions, Initialization and Startup
Code
C is a high level programming language that is portable across many
hardware architectures. This means that architecture specific features
such as register definitions, initialization and start up code must be made
available to your program via the use of libraries and include files.
For the 8051 chip you need to include the file reg51.h or using the
Cygnal C8051F020-TB development board include the file c8051f020.h :
#include <reg51.h>
Or
#include < c8051f020.h >
These files contain all the definitions of the C8051F020 registers. The
standard initialization and startup procedures for the C8051F020 are
contained in startup.a51 . This file is included in your project and will be
assembled together with the compiled output of your C program. For
custom applications, this start up file might need modification.
1024674441.003.png
Chapter 6 C8051F020 C Programming
3
Basic C program structure
The following is the basic C program structure; all the programs you will
write will have this basic structure.
//-------------------------------------------------
// Basic blank C program that does nothing
// other than disable the watch dog timer
//-------------------------------------------------
// Includes
//-------------------------------------------------
#include <c8051f020.h> // SFR declarations
void main (void)
{
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
while(1); // Stops program terminating and
// restarting
}
//-------------------------------------------------
Note: All variables must be declared at the start of a code block, you
cannot declare variables among the program statements.
You can test this program in the Cygnal IDE connected to the
C8051F020 development board. You won’t see anything happening on
the board, but you can step through the program using the debugger.
6.2
Programming Memory Models
The C8051F020 processor has 126 Bytes of directly addressable internal
memory and up to 64 Kbytes of externally addressable space. The Keil TM
C compiler has two main C programming memory models, SMALL and
LARGE which are related to these two types of memory. In the SMALL
memory model the default storage location is the 126 Bytes of internal
memory while in the LARGE memory model the default storage location
is the externally addressed memory.
 
4
Chapter 6 C8051F020 C Programming
The default memory model required is selected using the pragma
compiler control directive :
#pragma small
int X;
Any variable declared in this file (such as the variable X above) will be
stored in the internal memory of the C8051F020.
The choice of which memory model to use depends on the program, the
anticipated stack size and the size of data. If the stack and the data
cannot fit in the 128 Bytes of internal memory then the default memory
model should be LARGE, otherwise SMALL should be used.
Yet another memory model is the COMPACT memory model. This
memory model is not discussed in this chapter. More information on the
compact model can be found in the document Cx51 Compiler User’s
Guide for Keil TM Software .
You can test the different memory models with the Cygnal IDE
connected to the C8051F020-TB development board. Look at the symbol
view after downloading your program and see in which memory
addresses the compiler has stored your variables.
Overriding the default memory model
The default memory model can be overridden with the use of Keil TM C
programming language extensions that tell the compiler to place the
variables in another location. The two main available language
extensions are data and xdata :
int data X;
char data Initial;
int xdata Y;
char data SInitial;
The integer variable X and character variable Initial are stored in the
internal memory while the integer variable Y and character variable
SInitial are stored in the external memory overriding any default memory
model.
1024674441.004.png
 
Chapter 6 C8051F020 C Programming
5
Constant variables can be stored in the read-only code section of the
C8051F020 using the code language extension:
const char code CR=0xDE;
In general, access to the internal memory is the fastest, so frequently
used data should be stored here while less frequently used data should
be stored on the external memory.
The memory storage related language extensions, bdata, and
associated data types bit , sbit , sfr and sfr16 will be discussed in the
following sections. Additional memory storage language extensions
including, pdata and idata, are not discussed in this chapter; refer to the
document Cx51 Compiler User’s Guide for Keil TM Software for
information on this.
Bit-valued Data
Bit-valued data and bit-addressable data must be stored in the bit-
addressable memory space on the C8051F020 (0x20 to 0x2F). This
means that bit- valued data and bit-addressable data must be labelled as
such using the bit , sbit and bdata .
Bit-addressable data must be identified with the bdata language
extension:
int bdata X;
The integer variable X declared above is bit-addressable.
Any bit valued data must be given the bit data type, this is not a standard
C data type:
bit flag;
The bit-valued data flag is declared as above.
1024674441.001.png 1024674441.002.png
Zgłoś jeśli naruszono regulamin