1 / 34

LHO 22

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.

annick
Download Presentation

LHO 22

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LHO 22 C and the 8051

  2. ·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. oThe extensions support various features of the 8051. oSee Chapter 3 of the C51 compiler manual. You can find this using the Help menu.

  3. C Code Assembly Code Compiler Assembler Object Code Object Code Linker Machine Code Code Generation Flow for assembler and C.

  4. 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 */

  5. 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);

  6. 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(); }

  7. //------------------------------------------------------------------//------------------------------------------------------------------ // 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); }

  8. //------------------------------------------------------------------//------------------------------------------------------------------ // wr_leds - Outsput to led's // Returns : None // Parameters : leds //------------------------------------------------------------------ void wr_leds(unsigned char leds) { P5 = (leds << 4) | 0x0f; }

  9. //------------------------------------------------------------------ // 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; ; }

  10. //-----------------------------------------------------------------//----------------------------------------------------------------- // Init_crosbar - Configure crossbar switch // Returns : None // Parameters : None //----------------------------------------------------------------- void init_crossbar (void) { XBR0 = 0x0; XBR1 = 0x0; XBR2 = 0x48; // Enable cross bar }

  11. //------------------------------------------------------------------//------------------------------------------------------------------ // 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 }

  12. //-----------------------------------------------------------------//----------------------------------------------------------------- // delay - delay for visiable blink // Returns : None // Parameters : None //----------------------------------------------------------------- void delay(void) { unsigned int x; unsigned char y; while(x++) { y = 10; while(y--); }

  13. ¨External data memory, up to 64 kB, can be read from and written to and is physically located externally from the CPU ¨Access to external data in XDATA space is very slow when compared to access to internal data ØThis 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 memory ¨There are two different data types in Cx51 used to access external data: xdata and pdata ØThe 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).

  14. 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 operand • This is especially useful to specify a count that will be counted up rather than counted down • Example: unsigned int count = 0x0F; // TMR2RL gets 0xFFFF-0x0F+1 = 0xFFF1 TMR2RL = -count;

  15. 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

  16. Logical operators operate on Boolean data (True and False values) and the outcome is also Boolean

  17. The C language also has several bitwise operators • Bitwise operators affect a variable on a bit-by-bit basis • Example: • Result = Value1 & Value2; • If Value1 = 00100100b and Value2 = 10100000b, the result of Value1 & Value2 is: • 00100100b & 10100000b = 00100000b

  18. Bit wise Operators • Turning Bits On • Turn on a particular bit by ORing with a 1 • Turning Bits Off • Turn off a particular bit by ANDing with a 0 • Toggling Bits • Turning a bit from off to on or on to off by EXCLUSIVELY ORing with a 1

More Related