Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
USB
Outline
Introduction to 8051 Basic C Programming Language Extensions Preprocessor Advanced Programming
Introduction to 8051
An 8-bit Microcontroller optimized for control applications. A Microcontroller derivative family based on the 8051 core. A Microcontroller because you can make a one-chip system with the one chip containing: Program & Data Memory I/O Ports Serial Communication Counters/Timers Interrupt Control logic A-to-D and D-to-A convertors & so on ...
XTAL1
XTAL2
EA
P O R T 0
PSEN
ALE
P O R T 1
SECONDARY FUNCTIONS
P O R T 3
P O R T 2
ADDRESS BUS
Interrupt Control
CPU
OSC
Bus Control
I/O Ports
Serial Port
TXD RXD
P0 P2
(Address/Data)
P1
P3
Addressing Space
- 64K X 8 ROM - Program memory. - 64K x 8 RAM - External data memory. - 256 x 8 RAM - Internal data memory. - 128 x 8 Special function registers (SFRs). - Bit addressing of 16 RAM locations and 16 SFRs.
Program Memory
- 16 bit Program Counter (PC). - 16 bit Data Pointer (DPTR). - 64K byte address space each for Program & Data. - Table lookup using relative addressing: PC + ACC (Move). DPTR + ACC (Move and jump). - EA pin disables internal ROM and activates external program memory and addressing.
BIT ADDRESSABLE
07 R7
R6 R5 R4 R3 R2 R1
00 R0
REGISTER BANK 0
WR RD
F8 F0 E8 E0 D8 D0 C8 C0 B8 B0 A8 A0 98 90 88 80 IP P3 IE P2 SCON P1 TCON P0 TMOD TL0 SP DPH TL1 DPL TH0 TH1 PCON SBUF PSW ACC B
F0
RS1
: Carry Flag.
RS0
OV
----
- AC : Auxiliary Carry Flag. - F0 : Flag 0 (available for user). - RS1 : Register Select 1. - RS0 : Register Select 0. - OV : Arithmetic Overflow Flag. -P : Accumulator Parity Flag.
RS1 0 0 1 1
RSO 0 1 0 1
Register Bank 0 1 2 3
I/O Ports
- Four 8-bit I/O ports. - Most have alternate functions. - Quasi-bidirectional: Soft pull-up when port latch contains a 1. Can be used as inputs (30Kohm average pullup). Strong pull-up for 2 CPU cycles during 0 to 1 transitions.
Port Configuration
CM OS
2 OSC. PERIODS
VC C VC C VC C P1 P2 P3
Q
FROM PORT LATCH
INPUT DATA
PORT PIN
NM OS
2 OSC. PERIODS
PORT PIN
Q
FROM PORT LATCH
Counter / Timers
- Two 16-bit Counter/Timers: Up counters, can interrupt on overflow. - Counts: CPU cycles (crystal/12). External input (max. half CPU rate). - Four Operation Modes.
Timer Modes(1/2)
- Timer Mode 0 : Emulates 8048 counter/timer (13-bits). 8-bit counter (TL0 or TL1). 5-bit prescaler (TH0 or TH1). - Timer Mode 1 : Simple 16-bit counter. - Timer Mode 2 : 8-bit auto-reload. Counter in TL0 or TL1. Reload value in TH0 or TH1. Provides a periodic flag or interrupt.
? 2
Int errupt
TL1 TH1
T1 (Pi n) TR1 G e at
8- bits 8- bits
TF1
Cont rol
INT1 (Pi n)
The Gate input controls whether the Counter runs while gated by the interrupt signal or not.
- GATE : Permits INTx pin to enable/disable counter. - C/T : Set for counter operation, reset for timer operation. - M1, M0 : 00 : Emulate 8048 counter/timer (13-bits). 01 :16-bit counter/timer. 10 : 8-bit auto-reload mode 11 :Timer 0 = two 8-bit timers. Timer 1 Counting disabled. Timing function allowed. Can be used as Baud Rate generator.
- TF1, TF0 : Overflow flags for Timer 1 and Timer 0. - TR1, TR0 : Run control bits for Timer 1 and Timer 0. Set to run, reset to hold. - IE1, IE0 : Edge flag for external interrupts 1 and 0. * Set by interrupt edge, cleared when interrupt is processed. - IT1, IT0 : Type bit for external interrupts. * Set for falling edge interrupts, reset for 0 level interrupts. * = not related to counter/timer operation.
Serial Interface
- Full duplex UART. - Four modes of operation: Synchronous serial I/O expansion. Asynchronous serial I/O with variable baud rate. Nine bit mode with variable baud rate. Nine bit mode with fixed baud rate. - 10 or 11 bit frames. - Interrupt driven or polled operation. - Registers: SCON - Serial port control register. SBUF - Read received data. - Write data to be transmitted. PCON - SMOD bit.
Multi-Drop Communication
Serial Communication Modes 2 and 3 allow one "Master" 8051 to control several "Slaves": The serial port can be programmed to generate an interrupt if the 9th data bit = 1. The TXD outputs of the slaves are tied together and to the RXD input of the master. The RXD inputs of the slaves are tied together and to the TXD ouput of the master. Each slave is assigned an address. Address bytes transmitted by the master have the 9th bit = 1. When the master transmits an address byte, all the slaves are interrupted. The slaves then check to see if they are being addressed or not. The Addressed slave can then carry out the master's commands.
Interrupt System
- 5 Interrupt Sources (in order of priority): External Interrupt 0. Timer 0. External Interrupt 1. Timer 1. Serial Port. - Each interrupt type has a separate vector address. - Each interrupt type can be programmed to one of two priority levels. - External interrupts can be programmed for edge or level sensitivity.
- PS
: Serial interface.
- PT1 : Timer 1. - PX1 : External interrupt 1. - PT0 : Timer 0. - PX0 : External interrupt 0. - 0 = Low priority. - 1 = High priority.
Extended I/O
80C51
Memory 2 to 32 K
Introduction to Embedded C
The Cx51 Optimizing C Compiler is a complete implementation of the American National Standards Institute (ANSI) standard for the C language The Cx51 Compiler is not a universal C compiler adapted for the 8051 target. It is a ground-up implementation, dedicated to generating extremely fast and compact code for the 8051 microprocessor
Delay
void delay (int time) { int i,j; for (i=0; i<time; i++) for (j=0; j<400; j++); }
Variable Declare
xdata char PORTACFG _at_ 0x7F93; xdata char OUTA _at_ 0x7F96; xdata char OEA _at_ 0x7F9C; #define DISPLAYTIME 800 void delay (int time);
(For Loop)
main() { int i; PORTACFG = 0x00; OEA = 0xFF; while(1) { for(i = 1; i < 256; i<<=1) { OUTA = i; delay(DISPLAYTIME); } } }
:(For Loop)
for(i = 1; i < 256; i<<=1); // { OUTA = i; delay(DISPLAYTIME); } for(i = 1; i < 256; i<<=1) OUTA = i; delay(DISPLAYTIME); // OUTA=iDelay
While Loop
main() { int i; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { i = 1; while( i < 256) { OUTA = i; delay(DISPLAYTIME); i<<=1; } } }
dowhile
main() { int i; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { i = 1; do { OUTA = i; delay(DISPLAYTIME); i<<=1; }while( i < 256); } }
break
main() { int i = 1; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { i = 1; do { OUTA = i; delay(DISPLAYTIME); i<<=1; if(i==256) break; }while(1); } }
continue
main() { int i = 1; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { i = 1; do { if(i == 16){ i<<=1; continue; } OUTA = i; delay(DISPLAYTIME); i<<=1; if(i==256) break; }while(1); } }
if
main() { int i = 1; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { OUTA = i; delay(DISPLAYTIME); i<<=1; if( i == 256) i= 1; } }
If .. else
main() { int i = 1; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { OUTA = i; delay(DISPLAYTIME); if( i == 128) i= 1; else i<<=1; } }
switch
main() { int i = 1; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { OUTA = i; delay(DISPLAYTIME); switch(i) { case 1: i=2; break; case 2: i=4; break; case 4: i=8; break; default: i=1; break; } } }
Array
main() { int i; code char led[8]={1,2,4,8,16,32,64,128}; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { i = 0; do { OUTA = led[i]; delay(DISPLAYTIME); i++; }while(i<8); } }
Pointer
main() { int i=0; code char led[8]={1,2,4,8,16,32,64,128}; data char *pt; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { i=0; pt=led; while(i<8) { OUTA = *pt; delay(DISPLAYTIME); pt++; i++; } } }
%
main() { int i=0; code char led[8]={1,2,4,8,16,32,64,128}; PORTACFG = 0x00; // PA OEA = 0xFF; // while(1) { OUTA = led[i]; delay(DISPLAYTIME); i= (i+1) % 8; } }
Memory Areas
Program Memory Internal Data Memory External Data Memory Far Memory SFR Memory
Program Memory
Read only Up to 64K Bytes Using the code memory type specifier in the Cx51 Compiler.
Far Memory
refers to the extended address space of many new 8051 variants. uses generic 3-byte pointers to access extended memory spaces. Two Cx51 memory types
far extended RAM space const far - constants in extended ROM space
SFR Memory
128 bytes bit, byte, or word-sized registers used to control timers, counters, serial I/O, port I/O, and peripherals Three data types
sfr sfr16 sbit
sfr
SFRs are declared in the same fashion as other C variables. The only difference is that the data type specified is sfr rather than char or int. For example:
sfr P0 = 0x80; /* Port-0, address 80h */ sfr P1 = 0x90; /* Port-1, address 90h */ sfr P2 = 0xA0; /* Port-2, address 0A0h */ sfr P3 = 0xB0; /* Port-3, address 0B0h */
sfr16
Many 8051 derivatives use two SFRs with consecutive addresses to specify 16-bit values. For example:
sfr16 T2 = 0xCC; /* Timer 2: T2L 0CCh, T2H 0CDh */ sfr16 RCAP2 = 0xCA; /* RCAP2L 0CAh, RCAP2H 0CBh */
sbit
With typical 8051 applications, it is often necessary to access individual bits within an SFR. The Cx51 Compiler makes this possible with the sbit data type which provides access to bitaddressable SFRs and other bit-addressable objects. For example: sbit EA = 0xAF;
Bitwise operator
Operator
& | ^ << >> ~
Description
Bitwiae AND Bitwise OR (inclusive OR) Bitwise XOR(exclusive OR) Left shift Right shift One`s complement
A 0 0 1 1
B 0 1 0 1
A AND B 0 0 0 1
A OR B 0 1 1 1
A XOR B 0 1 1 0
Variant 3: int_constant
This variant uses an absolute bit address for the sbit. For example:
sbit OV = 0xD2; sbit CY = 0xD7; sbit EA = 0xAF;
Memory Models
The C51 Compiler provides three memory models:
Small Model Compact Model Large Model
Small Model
In this model, all variables, by default, reside in the internal data memory of the 8051 system as if they were declared explicitly using the data memory type specifier.
Compact Model
Using the compact model, by default, all variables reside in a single page of external data memory of the 8051 systemas if they were explicitly declared using the pdata memory type specifier.
Large Model
In the large model, all variables, by default, reside in external data memory (which may be up to 64K Bytes). This is the same as if they were explicitly declared using the xdata memory type specifier.
Memory Types
Explicit Memory Types Implicit Memory Types
Data Types
Bit Types
The Cx51 Compiler provides a bit data type that may be used for variable declarations, argument lists, and function-return values. For example:
static bit done_flag = 0; /* bit variable */ bit testfunc ( /* bit function */ bit flag1, /* bit arguments */ bit flag2) { . . . return (0); /* bit return value */ }
Pointers
Generic Pointers Memory-Specific Pointers
Generic Pointers
Generic pointers are declared like standard C pointers. For example:
char *s; /* string ptr */ int *numptr; /* int ptr */ long *state; /* Texas */
Memory-Specific Pointers
Memory-specific pointers always include a memory type specification in the pointer declaration and always refer to a specific memory area. For example:
char data *str; /* ptr to string in data */ int xdata *numtab; /* ptr to int(s) in xdata */ long code *powtab; /* ptr to long(s) in code */
Function Declarations
The C51 Compiler provides a number of extensions for standard C function declarations. return_type funcname ( args ) {small | compact | large} reentrant interrupt x using y
interrupt number
Return Values
Interrupt Functions
For example
Reentrant Functions
A reentrant function may be shared by several processes at the same time. When a reentrant function is executing, another process can interrupt the execution and then begin to execute that same reentrant function.
Preprocessor
Directives Stringize Token Pasting Predefined Macro Constants
Directives
Preprocessor directives must be the first nonwhitespace text specified on a line. All directives are prefixed with the pound or numbersign character ('#'). For example:
#pragma #include <stdio.h> #define DEBUG 1
Stringize
The stringize or number-sign operator ('#'), when used within a macro definition, converts a macro parameter into a string constant. For example:
#define stringer(x) printf (#x "\n")
stringer (text)
Token Pasting
The token-pasting operator (##) within a macro definition combines two arguments. It permits two separate tokens in the macro definition to be joined into a single token. For example:
#define paster(n) printf ("token" #n " = %d", token##n) paster (9);
ASM
EXTERN DATA(jim) MOV A,jim
Examples
8051 /INT0 External Interrupt Example Program(2/2) void main (void) { IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 (P3.2) EX0 = 1; // Enable EX0 Interrupt EA = 1; // Enable Global Interrupt Flag while (1) { } }
#include <reg52.h> #include <stdio.h> void main (void) {/*Set serial port for 9600 baud at 11.0592 MHz. Note that we use Timer 1 for the baud rate generator.*/ SCON = 0x50; TMOD |= 0x20; TH1 = 0xFA; TR1 = 1; TI = 1; PCON |= 0x80;
printf ("\r\nCounter 0 Example Program\r\n"); printf ("\r\nStart toggling P3.4 (T0 input)\r\n\r\n"); /*Set Timer0 for 16-bit counter mode. Set the Timer0 Run control bit.*/ TMOD = (TMOD & 0xF0) | 0x05; TR0 = 1; /*Output the value of the counter. As you toggle P3.4, the timer/counter 0 value will increase. */ while (1) { printf ("\rCounter 0 = %2.2bX%2.2bXh", TH0, TL0); }}
unsigned char idata idata_buffer [16] _at_ 0xB0; unsigned char data data_buffer [16] _at_ 0x60; unsigned char pdata pdata_buffer [16] _at_ 0xFF60; unsigned char xdata xdata_buffer [16] _at_ 0x0200;