Sei sulla pagina 1di 48

Embedded C

9/7/2013

Microcontroller 8051

The development process


Plan tasks and interaction

Setup overall project file

Project.PRJ

Edit

revise

Task1.A51
9/7/2013

1
Microcontroller 8051

Task2.C
2

The development process


Task1.A51
Task2.C
Task1.lst Task2.lst Compiled and assembled

Assembled

Various.lib

Link/Locate
Microcontroller 8051

Project.M51

9/7/2013

The development process


Various.lib

Link/Locate

Project.M51

Project.hex

Burn in EPROM or download

revise
9/7/2013 Microcontroller 8051 4

Example for EDE

9/7/2013

Microcontroller 8051

Intel HEX format


Example: :10400000755AFF111675907F11167590BF111675B0 :10401000903F111680EA780379007A00DAFED9FA27 :03402000D8F622AD

:00000001FF

9/7/2013

Microcontroller 8051

An Example
#include<stdio.h> #include <reg51.h> void main(void) {

9/7/2013

Microcontroller 8051

Development Tools
Debugging Simulator becomes less useful when there is a lot of time critical hardware Download to a target with a monitor Disadvantages: serial port is tied up for downloading need for a software
9/7/2013 Microcontroller 8051 8

Development Tools
In-circuit emulator
combines monitor and simulator functions emulator plugs into the socket where the final processor would reside Costly
9/7/2013 Microcontroller 8051 9

What Language To Use for Embedded Applications


Assembly - Most efficient but difficult to read and maintain C C++ Java

9/7/2013

Microcontroller 8051

10

Why C
Can control many machine level functions without resorting to assembly language Application can be written in C more easily than assembly language because the development software manages the details because of modularity,reusable codes can be

developed and maintained


9/7/2013 Microcontroller 8051 11

Why C
The programmer need not be very thorough with the architecture of the processor Code developed in C is more portable

9/7/2013

Microcontroller 8051

12

Memory Models
SMALL :
all variables default to the internal data memory of the 8051 same as if they were declared explicitly using the data memory type specifier variable access is very efficient However, all data objects, as well as the stack must fit into the internal RAM

9/7/2013

Microcontroller 8051

13

Memory Models
Compact:
all variables default to one page of external data memory same as if they were explicitly declared using the pdata memory type specifier can accommodate a maximum of 256 bytes of variables as it is accessed indirectly through R0 and R1 variable access is not as fast as small model
9/7/2013 Microcontroller 8051 14

Memory Models
Large:
all variables default to external data memory same as if they were explicitly declared using the xdata memory type specifier Memory access through this data pointer is inefficient, especially for variables with a length of two or more bytes This type of data access generates more code than the small or compact models
9/7/2013 Microcontroller 8051 15

Key differences between C and Keil C

9/7/2013

Microcontroller 8051

16

Key differences between C and Keil C


The sbit, sfr, and sfr16 data types are included to allow access to the special function registers that are available on the 8051
E.g: the declaration: sfr P0 = 0x80
declares the variable P0 and assigns it the special function register address of 0x80. This is the address of PORT 0 on the 8051.
9/7/2013 Microcontroller 8051 17

Memory Types

9/7/2013

Microcontroller 8051

18

Memory Types
Frequently used variables are put in

the internal memory in preference to


the external RAM

By including a memory type specifier


in the variable declaration, you can

specify where variables are stored

9/7/2013

Microcontroller 8051

19

Example
char data var1; char code text[] = "ENTER PARAMETER:"; unsigned long xdata array[100]; float idata x,y,z; unsigned int pdata dimension; unsigned char xdata vector[10][4][4]; char bdata flags;

9/7/2013

Microcontroller 8051

20

Default memory type


If the memory type specifier is omitted in a variable declaration, the default or implicit memory type is automatically selected Function arguments and automatic variables which cannot be located in registers are also stored in the default memory area The default memory type is determined by the SMALL, COMPACT and LARGE compiler control directives
9/7/2013 Microcontroller 8051 21

An Example
8 switches are connected to P0 8 LEDs are connected to P2 #include <reg51.h> void msec (unsigned int); void main () { unsigned char array[10]; unsigned char I; while (1) { for (i=0; i<=9; i++) { array[i] = p2=p0; msec(100); } } } This example reads from P0,stores the readings in an array and shows 9/7/2013 8051 22 the most recent reading Microcontroller on 8 LEDS

Bitwise operators
Logical operation
Assembly

C ~ & |

NOT AND OR

CPL A ANL A,# ORL A,#

XOR

XRL A,#

9/7/2013

Microcontroller 8051

23

Assignment operators
Unique to C is the shorthand representation for modifying a variable and assigning it back

portA = portA & 0xf7; portA &= oxf7;

9/7/2013

Microcontroller 8051

24

Delay by software looping


Void msec (unsigned int x) { unsigned char j; while (x-- > 0) { for (j=0; j<125; j++) { ; } } } 9/7/2013 Microcontroller 8051

25

Arrays and Pointers

9/7/2013

Microcontroller 8051

26

Accessing an array element


Unsigned int ary[20]; unsigned int x; ary[9] =x;

While defining arrays, it has to be made sure that large amounts of memory is not tied up

9/7/2013

Microcontroller 8051

27

Structure
As structure is made of contiguous memory locations, it has to be looked into A structure could represent the information about the solenoids of a sequencer ie a 1 for a bit means that a solenoid is on
9/7/2013 Microcontroller 8051 28

Pointers
C51 compiler supports two different

types of pointers:
memory specific pointers

and generic pointers

9/7/2013

Microcontroller 8051

29

Generic Pointers
char *s; int *numptr; long *state; /* string ptr */ /* int ptr */ /* long ptr */

use three bytes,the first for the memory type, the second for the high-order byte of the offset, and the third for the low-order byte of the offset Generic pointers may be used to access any variable regardless of its location in 8051 memory space.
9/7/2013 Microcontroller 8051 30

Memory Specific Pointers


char data *str; int xdata *numtab; long code *powtab; /* ptr to string in data */ /* ptr to int(s) in xdata */ /* ptr to long(s) in code */

can be stored using only one byte (idata, data, bdata, and pdata pointers) or two bytes (code and xdata pointers)
9/7/2013 Microcontroller 8051 31

Comparison

9/7/2013

Microcontroller 8051

32

Unions
A combination of different data types applies different names and types for the same space

9/7/2013

Microcontroller 8051

33

Passing Parameters
Functions that go between software and hardware are called drivers As you call a function from a program ,it is needed to pass the parameters The transfer is specific to a compiler
9/7/2013 Microcontroller 8051 34

Reentrancy
Normally,C51 functions are not reentrant

The reentrant function attribute allows you to


declare functions that may be reentrant and, therefore, may be called recursively

9/7/2013

Microcontroller 8051

35

Example
int calc (char i, int b) reentrant { int x; x = table [i]; return (x * b); } For each reentrant function, a reentrant stack area is simulated in internal or external memory depending on the memory model
9/7/2013 Microcontroller 8051 36

Interrupts
The C51 compiler provides a method of calling a C function when an interrupt occurs This support allows you to create interrupt service routines in C

The compiler automatically generates the


interrupt vector and entry and exit code for the interrupt routine

9/7/2013

Microcontroller 8051

37

Interrupts
The interrupt function attribute, when included in a declaration, specifies that the associated function is an interrupt function The interrupt number and the register bank are specified Additionally, you can specify the register bank

used for that interrupt with the using


function attribute
9/7/2013 Microcontroller 8051 38

Example
unsigned int interruptcnt; unsigned char second; void timer0 (void) interrupt 1 using 2 { if (++interruptcnt == 4000) {

/* count to 4000 */
second++; interruptcnt = 0; }
9/7/2013 Microcontroller 8051 39

/* second counter */ /* clear int counter */

Parameter passing
The C51 compiler passes up to three function arguments in CPU registers Argument passing can be controlled with the REGPARMS and NOREGPARMS control directives The following table lists the registers used for different arguments and data types

9/7/2013

Microcontroller 8051

40

Parameter passing

9/7/2013

Microcontroller 8051

41

Parameter passing

9/7/2013

Microcontroller 8051

42

Interfacing to assembly
Can access assembly routines from C and vice versa Function parameters are passed via CPU registers or, if the NOREGPARMS control is used, via fixed memory locations Values returned from functions are always passed in CPU registers
9/7/2013 Microcontroller 8051 43

Interfacing to assembly
can use the SRC directive to direct the C51 compiler to generate a file ready to assemble with the A51 assembler instead of an object file. For example,the following C source file:
unsigned int asmfunc1 (unsigned int arg) { return (1 + arg); }
9/7/2013 Microcontroller 8051 44

Interfacing to assembly
?PR?_asmfunc1?ASM1
PUBLIC RSEG ?PR? USING 0 _asmfunc1: ;---- Variable 'arg?00' assigned to Register 'R6/R7' ---MOV A,R7 ; load LSB of the int ADD A,#01H ; add 1 MOV R7,A ; put it back into R7 CLR A ADDC A,R6 ; add carry & R6 MOV R6,A ?C0001: RET ; return result in R6/R7
9/7/2013 Microcontroller 8051 45

SEGMENT CODE _asmfunc1 _asmfunc1?ASM1

Interfacing to assembly
Can use the #pragma asm and

#pragma endasm preprocessor directives


to insert assembly instructions into your C source code

9/7/2013

Microcontroller 8051

46

Intrinsic Library Routines


The libraries included with the compiler include a number of routines that are implemented as intrinsic functions Non-intrinsic functions generate ACALL or LCALL instructions to perform the library routine Intrinsic functions generate in-line code (which is faster and more efficient) to perform the library routine
9/7/2013 Microcontroller 8051 47

Intrinsic Function Description


_crol_
_cror_ _irol_ _iror_ _lrol_ _lror_ _nop_ _testbit_
9/7/2013

Rotate character left Rotate character right Rotate integer left Rotate integer right Rotate long integer left Rotate long integer right No operation (8051 NOP instruction) Test and clear bit (8051 JBC instruction)
Microcontroller 8051 48

Potrebbero piacerti anche