embedded 009

Upload: paul-lanka

Post on 08-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Embedded 009

    1/22

    The C programming language is a general-purpose high-level programminglanguage that o ers e cient and compact code

    Many control and monitoring-based applicationscan be solved more e ciently with C

    C was originally available on mainframe computers, mini-computers, and personal computers

    The C programming language is now available on most microcontrollers andmicroprocessors.

    the industry standard C51 optimizing Ccompiler is used throughout. This compiler has been developed by KeilElektronik GmbH.

    C51 is available on both MS-DOS and Windows-basedoperating systems

    There are many other high-level language compilers available for micro-controllers, including PASCAL, BASIC, and other C compilers.

    some companies supply free limited capability compilers, mainly for evaluation purposes

    These compilers can be used for learning the features of a specic product and in somecases small projects can be developed with such compilers.

    The C51 compiler has been developed for the 8051 family of microcontrollers.

    This is one of the most commonly used industry standard C compilers for the8051 family, and can generate machine code for most of the 20-pin and 40-pin8051 devices

    2.1.2 signed char/unsigned char These data types are as in standard C language and are used to declare signed

    and unsigned character variables. Each character variable is 1 byte long(8 bits). Signed character variables range from 128 to 127; unsigned

    character variables range from 0 to 255.

    2.1.1 bitThese data types may be used to declare 1-bit variables.

    Example:bit my_ ag; /* declare my_ ag as a bit variable */ my_ ag 1; /* set my_ ag to 1 */

    2.1.2 signed char/unsigned char These data types are as in standard C language and are used to declare signed

    and unsigned character variables. Each character variable is 1 byte long

    (8 bits). Signed character variables range from 128 to 127; unsignedcharacter variables range from 0 to 255.

  • 8/6/2019 Embedded 009

    2/22

    Example:unsigned char var1,var2; /* declare var1 and var2 as unsigned char */var1 0xA4; /* assign hexadecimal A4 to variable var1 */\var2 var1; /* assign var1 to var2 */

    2.1.3 signed short/unsigned short These data types are as in standard C language and are used to declare signed

    and unsigned short variables. Each short variable is 2 bytes long (16 bits).Signed short variables range from 32 768 to 32 767 and unsigned shortvariables range from 0 to 65 535.

    Example:unsigned short temp; /* declare temp as unsigned short */unsigned short wind; /* declare wind as unsigned short */temp 0x0C200; /* assign hexadecimal C200 to variable temp */wind temp; /* assign variable temp to wind */

    2.1.4 signed int/unsigned int As in the standard C language, these data types are used to declare signed and unsigned

    integer variables. Integer variables are 2 bytes long (16 bits). Signed integers range from32 768 to 32 767 and unsigned integers range from 0 to 65 535.

    Example:unsigned int n1,n2; /* declare n1 and n2 as unsigned integers */n1 10; /* assign 10 to n1 */n2 2*n1; /* multiply n1 by 2 and assign to n2 */

    2.1.5 signed long/unsigned long

    These data types are as in standard C language and are used to declare signedand unsigned long integer variables. Each long integer variable is 4 bytes long(32 bits).

    Example:unsigned long temp; /* declare temp as long integer variable */temp 250 000; /* assign 250 000 to variable temp */

    2.1.6 float This data type is used to declare a oating point variable. Example:

    float t1,t2; /* declare t1 and t2 as oating point variables */

    t1 25.4 /* assign 25.4 to t1 */t2 sqrt(t1); /* assign the square-root of t1 to t2 */

    2.1.7 sbit This data type is provided for the 8051 family and is used to declare an

    individual bit within the SFR of the 8051 family. For example, using this datatype one can access individual bits of an I/O port.

    Example:sbit switch P1^3; /* variable switch is assigned to bit 3 of port 1 */switch 0; /* clear bit 3 of port 1 */

    2.1.8 sfr This data type is similar to sbit but is used to declare 8-bit variables.

  • 8/6/2019 Embedded 009

    3/22

    Example:sfr P1 0x90; /* Port 1 address 0x90 assigned to P1 */sfr P2 0xA0; /* Port 2 address 0xA0 assigned to P2 */unsigned char my_data; /* declare my_data as unsigned character */my_data P1; /* read 8 bit data from port 1 and assign to my_data */P2 my_data++; /* increment my_data and send to port 2 */

    2.1.9 sfr16 This data type is similar to sfr but is used to declare 16-bit variables. When

    using this data type, the low byte should precede the high byte. Example:

    Timer 2 of the 8052 microcontroller uses addresses 0xCC and 0xCD for the lowand high bytes. We can declare variable T2 to access both timer locations.sfr16 T2 0xCC; /* Timer 2, T2L=CC and T2H=CD */T2 0xAE01; /* load Timer 2 with hexadecimal value AE01 */

    2.2 Memory Models 8051 architecture supports both program (or code) and data memory areas.Program memory is read-only and it cannot be written. The program memory can beincreased by connecting additional external memory to the basic microcontroller.There may be up to 64 Kbytes of program memory.

    Depending upon the type of processor used di erent amounts of internal program memory are available.

    Data memory resides within the 8051 CPU and can be read from and writteninto. Up to 256 bytes of data memory are available depending upon the type ofmicrocontroller used.

    The memory model determines what type of program memory is to be used fora given application.

    There are three memory models, known as SMALL, COMPACT, and LARGE, and therequired model is specied using the compiler directives.

    The SMALL memory model is used if all the variables reside in the internal data memory ofthe 8051. This memory model generates the fastest and the most efficient code and shouldbe used whenever possible.

    In the COMPACT memory model, all variables reside in one page of external data memory.

    A maximum of 256 bytes of variables can be used. This memory model is not as e cient as the SMALL model.

    In the LARGE memory model, all variables reside in external data memory. A maximum of64 Kbytes of data can be used.

    The LARGE model generates more code than the other two models and thus it is not verye cient.

    Compiling in the SMALL memory model always generates the fastest and the smallest codepossible since accessing the internal memory is always faster than accessing any externalmemory.

    2.3 Interrupts The C51 compiler allows us to declare interrupt service routines (ISRs) in our C code and

    then the program automatically jumps to this code when an interrupt occurs. The compilerautomatically generates the interrupt vectors and entry and exit code for interrupt routines.

  • 8/6/2019 Embedded 009

    4/22

    An ISR is declared similar to a function declaration but the interrupt number is specied aspart of the function declaration.

    For example, the following is a declaration of the ISR for timer 1 interrupts (interruptnumber 3):

    Void timer1() interrupt 3

    { interrupt service code goes in here } Similarly, the ISR for timer 0 (interrupt number 1) is declared as: void timer0() interrupt 1 { interrupt service code goes in here } Note that we can specify the register bank to be used for the ISR with the using function attribute:

    void timer0() interrupt 1 using 2 { interrupt service code goes in here }

    2.4 Structure of a Microcontroller-based C Program The structure of a C program developed for a microcontroller is basically the

    same as the structure of a standard C program, with a few minor changes. It is always advisable to describe the project at the beginning of a program The project name, lename, date, and the target processor type should also be included in

    this part of the program. The register denition fle should then be included for the type of target processor used.

    /****************************************************************************************************

    Project: Give project name File: Give lename Date: Date program was developed Processor: Give target processor type

    This is the program header. Describe your program here brie y. ****************************************************************************************************/

    #include #dene ...... /* include your dene statements here */ sbit ...... /* include your bit denitions here */ int ...... char ...... /* include your global declarations here */ void func1() /* include you functions here */ {

    } main() /* main code */

  • 8/6/2019 Embedded 009

    5/22

    { /* include comments here */ }

    //

    // Designed by www.MCUExamples.com

    // [email protected]

    //

    #include

    #pragma config OSC = XT // 4MHz Crystal, (XT oscillator)

    #pragma config PBADEN = OFF // PORTB pins are configured as digital I/O on

    Reset)

    void delay_ms(unsigned int duration);

    void main()

    { TRISC=0; while (1) { LATC=0xff; //Set all pins of portC to logic high delay_ms(500); // delay 500mS LATC=0; //Set all pins of portC to logic low delay_ms(500); // delay 500mS }}

    void delay_ms(unsigned int duration) // delay in miliseconds for 4.0MHZ crystal

    { unsigned int i; for(;duration!=0;duration--) { for(i=0;i

  • 8/6/2019 Embedded 009

    6/22

    ajmp looporg 0080h; to 0030h office address to avoid sensitive 00-30loop:mov p1, # 0ffh; turn off all lightsclr p1.0; point lighting p1.0lcall delay; delay period of time

    clr p1.1; point lighting p1.1lcall delayclr p1.2; point lighting p1.2lcall delayclr p1.3; point lighting p1.3lcall delayclr p1.4; point lighting p1.4lcall delayclr p1.5; point lighting p1.5lcall delayclr p1.6; point lighting p1.6

    lcall delayclr p1.7; point lighting p1.7lcall delayAJMP LOOP; to the best start to re-run loop Departmentdelay: mov r5, # 20; delay.d1: mov r6, # 40d2: mov r7, # 248djnz r7, $djnz r6, d2djnz r5, d1retend

  • 8/6/2019 Embedded 009

    7/22

    4 / 14

  • 8/6/2019 Embedded 009

    8/22

  • 8/6/2019 Embedded 009

    9/22

  • 8/6/2019 Embedded 009

    10/22

  • 8/6/2019 Embedded 009

    11/22

  • 8/6/2019 Embedded 009

    12/22

  • 8/6/2019 Embedded 009

    13/22

  • 8/6/2019 Embedded 009

    14/22

  • 8/6/2019 Embedded 009

    15/22

  • 8/6/2019 Embedded 009

    16/22

  • 8/6/2019 Embedded 009

    17/22

  • 8/6/2019 Embedded 009

    18/22

  • 8/6/2019 Embedded 009

    19/22

  • 8/6/2019 Embedded 009

    20/22

  • 8/6/2019 Embedded 009

    21/22

  • 8/6/2019 Embedded 009

    22/22