lho 22 c and the 8051. the silicon labs ise uses the keil c51 compiler. the code size is limiter...
DESCRIPTION
Assembly Code Object Code Assembler C Code Object Code Linker Machine Code Compiler Code Generation Flow for assembler and C.TRANSCRIPT
LHO 22
C and the 8051
The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original Intel high level language (HLL) for imbedded systems). Versions of basic and a few other HLL such as FOUTH are available for the 8051. The Keil C51 is a complete implementation of ANSI C with extensions.o The extensions support various features of the 8051.o See Chapter 3 of the C51 compiler manual. You can find this using the Help menu.
Assembly Code
Object Code
Assembler
C Code
Object Code
Linker
Machine Code
Compiler
Code Generation Flow for assembler and C.
Include c8051F020.h in each C file. c8051F020.h defines the special function registers (SFR) of the 8051F020./* BYTE Registers */sfr P0 = 0x80; /* PORT 0 */sfr SP = 0x81; /* STACK POINTER */sfr DPL = 0x82; /* DATA POINTER - LOW BYTE */sfr DPH = 0x83; /* DATA POINTER - HIGH BYTE */sfr P4 = 0x84; /* PORT 4 */…/* BIT Registers *//* TCON 0x88 */sbit TF1 = TCON ^ 7; /* TIMER 1 OVERFLOW FLAG */sbit TR1 = TCON ^ 6; /* TIMER 1 ON/OFF CONTROL */
uni.c;Version 1.1 11/29/07#include <c8051f020.h> // SFR declarations#include <stdio.h>//----------------------------------------------------------------// Init - Configure UDC// Returns : None// Parameters : None//----------------------------------------------------------------void init_crossbar(void);void init_ports(void);void init_osc(void);
void Init (void){// Disable interrupts EA = 0;// Disable watchdog timer WDTCN = 0xde; WDTCN = 0xad; init_osc();// Enable crossbar switch init_crossbar();// Set up ports init_ports();}
//------------------------------------------------------------------// rd_buttons - Configure IO ports// Returns : Value of buttons connected to P5.3 - P5.0. // Pressing a button// sets bit to a '1'.// Parameters : None//------------------------------------------------------------------unsigned char rd_buttons(void){ unsigned char btns; btns = (~P5) & 0x0f; return(btns);}
//------------------------------------------------------------------// wr_leds - Outsput to led's// Returns : None// Parameters : leds//------------------------------------------------------------------void wr_leds(unsigned char leds){ P5 = (leds << 4) | 0x0f;}
//------------------------------------------------------------------ // init_ports - Configure IO ports // Returns : None // Parameters : None //------------------------------------------------------------------ void init_ports(void) { P0MDOUT = 0x00; P1MDOUT = 0x00; P2MDOUT = 0x00; P3MDOUT = 0x00; P5 = 0x0f; //TURN ON LEDS P74OUT = 0x08; //SET PORT 5 OUT AS PUSH PULL P4 = 0xff; ; }
//-----------------------------------------------------------------// Init_crosbar - Configure crossbar switch// Returns : None// Parameters : None//-----------------------------------------------------------------void init_crossbar (void){ XBR0 = 0x0; XBR1 = 0x0; XBR2 = 0x48; // Enable cross bar}
//------------------------------------------------------------------// init_osc - Use external 22.1184 XTAL as clock// Returns : None// Parameters : None//------------------------------------------------------------------void init_osc (void){ OSCXCN = 0x67; //enable 22.1184 MHz XTAL OSC while ((OSCXCN & 0x80) == 0); // Wait till XTLVLD // pin is set See Fig 14.3 OSCICN = 0x88; //config internal oscillator }
//-----------------------------------------------------------------// delay - delay for visiable blink// Returns : None// Parameters : None//-----------------------------------------------------------------void delay(void){ unsigned int x; unsigned char y; while(x++) { y = 10; while(y--); }
External data memory, up to 64 kB, can be read from and written to and is physically located externally from the CPUAccess to external data in XDATA space is very slow when compared to access to internal dataThis is because external data memory is accessed indirectly through the data pointer register (DPTR) which must be loaded with a 16-bit address before accessing the external memoryThere are two different data types in Cx51 used to access external data: xdata and pdataThe xdata memory specifier refers to any location in the 64 kB address space of external data memory (default for LARGE memory model)The pdata memory type specifier refers to only 1 page or 256 bytes of external data memory (default for COMPACT memory model)The pdata area is accessed using registers R0 and R1 indirectly (@R0 or @R1) instead of the DPTR (@DPTR), so accessing pdata is slightly faster than xdata. This is also what limits pdata to 256 bytes (R0 and R1 are 8 bits).
Arithmetic operators perform basic arithmetic operations
All arithmetic operators except the negation (–) operator have two operands.
The negation (unary minus) operator returns the 2’s complement value of the operandThis is especially useful to specify a count that will be counted up rather than counted downExample: unsigned int count = 0x0F;
// TMR2RL gets 0xFFFF-0x0F+1 = 0xFFF1TMR2RL = -count;
Operator Description
+ Add
– Subtract
* Multiply
/ Divide
% Modulo (remainder of division)
– Negation (unary minus)
Relational operators compare data and the outcome is either True or False
if statements, for loops and while loops often make use of relational operators
Operator Description
== Equal to
!= Not Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Logical operators operate on Boolean data (True and False values) and the outcome is also Boolean
Operator Description
&& Logical AND
|| Logical OR
! Logical NOT
The C language also has several bitwise operators
Bitwise operators affect a variable on a bit-by-bit basisExample: Result = Value1 & Value2;
If Value1 = 00100100b and Value2 = 10100000b, the result of Value1 & Value2 is:00100100b & 10100000b = 00100000b
Operator Description
& Bitwise AND
| Bitwise OR
~ Bitwise NOT (1’s Compliment)
^ Bitwise Exclusive OR
<< Shift Left
>> Shift Right
Turning Bits OnTurn on a particular bit by ORing with a 1
Turning Bits OffTurn off a particular bit by ANDing with a 0
Toggling BitsTurning a bit from off to on or on to off by EXCLUSIVELY ORing with a 1
Bit wise Operators