Anda di halaman 1dari 33

Embedded C

07/09/11

Microcontroller 8051

The development process


Plan tasks and interaction

Setup overall project file

Project.PRJ

Edit

revise

Task1.A51
07/09/11

1
Microcontroller 8051

Task2.C
2

The development process


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

Task2.C

Various.lib

Link/Locate

Project.M51

07/09/11

Microcontroller 8051

The development process


Various.lib

Link/Locate

Project.M51

Project.hex

Burn in EPROM or download

revise
07/09/11 Microcontroller 8051 4

Intel HEX format


Example: :10400000755AFF111675907F11167590BF111675B0 :10401000903F111680EA780379007A00DAFED9FA27 :03402000D8F622AD :00000001FF

07/09/11

Microcontroller 8051

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

07/09/11

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
07/09/11 Microcontroller 8051 7

Development Tools
InIn-circuit emulator combines monitor and simulator functions emulator plugs into the socket where the final processor would reside Costly

07/09/11

Microcontroller 8051

What Language To Use for Embedded Applications


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

07/09/11

Microcontroller 8051

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
07/09/11 Microcontroller 8051 10

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

07/09/11

Microcontroller 8051

11

Memory Areas
Code memory (program memory). External data memory. Internal data memory.

07/09/11

Microcontroller 8051

12

Memory Areas
Internal data memory: >read from or written to. >faster than any other memory. >memory specifiers data idata bdata

07/09/11

Microcontroller 8051

13

Memory Types

07/09/11

Microcontroller 8051

14

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

07/09/11

Microcontroller 8051

15

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

07/09/11

Microcontroller 8051

16

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

07/09/11

Microcontroller 8051

17

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
07/09/11 Microcontroller 8051 18

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
07/09/11 Microcontroller 8051 19

Key differences between C and Keil C

07/09/11

Microcontroller 8051

20

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. 0x80. This is the address of PORT 0 on the 8051.
07/09/11 Microcontroller 8051 21

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
07/09/11 Microcontroller 8051 22

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 the most recent reading on 8 LEDS 07/09/11 Microcontroller 23

8051

Operators
== <= >= != << >> ++ -&& ||
07/09/11

equal to comparison less than or equal to greater than or equal to not equal to shift left shift right increment decrement Boolean and Boolean or
Microcontroller 8051 24

Operators
+= -= *= /= |= &= ^= <<= >>= %= ->
07/09/11

add value to subtract value to multiply value to divide value to or value to and value to exclusive or value to shift value left shift value right modulo divide value to pointer to a structure
Microcontroller 8051 25

Bitwise operators
Logical operation
Assembly

C ~ & | ^

NOT AND OR XOR

CPL A ANL A,# ORL A,# XRL A,#

07/09/11

Microcontroller 8051

26

Assignment operators
Unique to C is the shorthand representation for modifying a variable and assigning it back portA = portA & 0xf7; portA &= oxf7;

07/09/11

Microcontroller 8051

27

Delay by software looping


Void msec (unsigned int x) { unsigned char j; while (x-- > 0) (x-{ for (j=0; j<125; j++) { ; } } }
07/09/11 Microcontroller 8051 28

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

07/09/11

Microcontroller 8051

29

Pointers
C51 compiler supports two different types of pointers: Generic pointers Memory specific pointers

07/09/11

Microcontroller 8051

30

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, highand the third for the low-order byte of the lowoffset Generic pointers may be used to access any variable regardless of its location in 8051 memory space.
07/09/11 Microcontroller 8051 31

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 pointers)

07/09/11

Microcontroller 8051

32

I request Electronics and communication ENGINEERING students to visit my blog for more abhishek1ek.blogspot.com awhengineering.blogspot.com THANK YOU
07/09/11 Microcontroller 8051 33

Anda mungkin juga menyukai