Sei sulla pagina 1di 17

8051 C Programming

Chapter 8 The 8051 Microcontroller by Scott Mc Kenzie

The RS-232 Standard


Most widely used serial I/O interfacing standard Input and output voltage levels are not TTL compatible

1 bit is represented by -3 to -25 V 0 bit is +3 to +25 V -3 to +3 is undefined

To connect RS232 to a microcontroller system must use voltage converters such as MAX232 to convert the TTL logic levels to the RS232 voltage levels, and vice versa

MAX232 IC chips are commonly referred to as line drivers

The RS-232 DB-25 Connector

RS232 Connector DB-25

The RS-232 DB-9 Connector

DB-9 9-Pin Connector

The Line Driver- Max 232

Max-232

Converts from RS232 voltage levels to TTL voltage levels Uses a +5 V power source MAX232 has two sets of line drivers for transferring and receiving data Line drivers used for TxD are called T1 and T2 Line drivers for RxD are designated as R1 and R2 T1 and R1 are used together for TxD and RxD of the 8051 Second set is left unused

Inside Max-232

Why C51?

Offers the benefits of high-level, structured programming languages such as C.


Writing subroutines with ease Sequence of operations is simple to trace, facilitating debugging Finite number of structures with standardized terminology Structures lend themselves easily to building subroutines. The set of structures is complete that is all programs can be written using three structures

Statements

Assignment, call to a subroutine, etc The while/do statement, Repeat/Until statements If-then-else statement, the case statement, the goto statement

Loops

Choice

Structured programming results in increased programming productivity Relieves the programmer of the hardware details Easier to write especially for large and complex programs Produces more readable program source codes.

The downside of C51

Generally generates larger machine codes

May execute more slowly Require relatively large amount of memory

Programmer has less control and less ability to directly interact with the hardware Some problems can be difficult to solve using only the three structures discussed earlier Nested structures can be difficult to follow

C51 Data types


Data Type bit signed char unsigned char signed short unsigned short Bits 1 8 8 16 16 Bytes 1 1 2 2 Value Range 0 to 1 -128 to +127 0 to 255 -32768 to +32767 0 to 65535

signed int
unsigned int signed long unsigned long float sbit sfr and sfr16

16
16 32 32 32 1 8 & 16

2
2 4 4 4 1&2

-32768 to +32767
0 to 65535 -2147483648 to +2147483647 0 to 4294967295 +-1.175494E-38 to +-3.402823E+38 0 to 1 0 to 255 & 0 to 65535

C-51 Data types


Data type bit can be used to declare variables that reside in the 8051s bitaddressable locations bit flag = 0; Declares a bit variable called flag and initializes it to 0. Data type sbit is similar to the bit data type, except that it is normally used to declare 1-bit variables that reside in SFRs, for example: sbit P = 0xD0; Declares the sbit P and specifies that it refers to bit address D0H, which is really the LSB of PSW SFR. In case of sbit declarations the assignment operator indicates what address the sbit resides in, while in bit declarations it is used to specify the initial value of the bit variable. We can use a previously defined sfr variable as base address and assign our sbit variable to refer to a certain bit within that sfr. sfr PSW = 0xD0; sbit P = PSW^0;

REG51.H

REG51.H contains the declarations for:

All the special function registers in 8051 All the individual flags, status and control bits in the bit addressable SFRs

REG51.H is an important include in our C51 programs.

It enables us to refer to the SFRs and all the flags and control bit etc using their symbols i.e. PSW, ACC, B, SP, TR0, TR1 etc.

Memory Types
Memory Type code Description (Size) Code memory (64 Kbytes). Objects declared in this segment must be initialized at compile time. Directly addressable internal data memory (128 bytes) Indirectly addressable internal data memory (256 bytes)

data idata

bdata
xdata pdata

Bit-addressable internal data memory (16 bytes)


External data memory (64 Kbytes) Paged external data memory (256 bytes) MOVX A, @Ri, MOVX @Ri, A

char code errormsg[] = An error occurred; signed int data num1; bit bdata numbit; unsigned int xdata num2;

Accessing External Data and Code Memory Space The higher order address
byte comes from P2, the lower order comes from Ri, so before using this instruction you have to initialize P2

13

Memory Models
Memory Model Description Small Compact Large Variables default to the internal data memory (data) Variables default to the first 256 bytes of external data memory (pdata) Variables default to external data memory (xdata)

If you wish all the variables to be assigned a default memory type without having to specify them one by one then Use memory models by choosing anyone from the above. The default model is Small. Use the #pragma directive to explicitly select any other memory model than the default Small model.
#pragma small #pragma compact #pragma large

C51 Functions
return_type function_name(arguments) [memory] [reentrant] [interrupt] [using] return_type: refers to data type of the return value. function_name: is any name that you wish to call the function as. arguments: is the list of the type and number of input values. memory: refers to an explicit memory model (small, compact or large) reentrant: refers to whether the function is reentrant (recursive) interrupt: indicates that the function is actually an ISR using: explicitly specifies which register bank to use

Parameter Passing

Parameters to functions are passed through registers and memory


Passing through registers is the faster and default way The registers used and their purpose are as follows. Since there are only 8 registers in available, any extra parameters may be passed using fixed memory locations.
Char/1-byte pointer R7 R5 R3 Int/2-byte pointer R6 & R7 R4 & R5 R2 & R3 Long/Float R4-R7 R0-R3 Generic Pointer R1-R3

Number of Arguments 1 2 3

Returning Values

Unlike parameters output values must be returned from functions via registers only.
Register Carry Flag (C) R7 R6 and R7 R4-R7 R4-R7 R1-R3 MSB in R6 and LSB in R7 MSB in R4 and LSB in R7 32-bit IEEE format Memory type in R3, MSB in R2, LSB in R1 Description

Return Type bit char/unsigned char/1byte pointer int/unsigned int/2-byte pointer long/unsigned long float generic pointer

Potrebbero piacerti anche