est labi2011

Upload: saurabh-godha

Post on 07-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Est Labi2011

    1/12

    LAB MANUAL FOR EMBEDDED SYSTEM LAB I

    COURSE: M.TECH (EST)

    COURSE CODE EM511

    PREFACE:

    This manual consists of experiments to conducted for the above said course, over a period onesemester. Normally each experiment needs one session (3 periods of 50 min each).

    It is understood that the student will have necessary exposure in 8051 family micro processor andC language.

    If necessary additional lab hours can be planned for practicing them.

    Some additional experiments (small trial ones like Hello world) program is to be given in theearlier classes for the students to get familiarize with Keil and Trioz software developmentenvironment.

    The sample programs given in this manual can be given prior to the actual if need be. (depending onthe students preparedness). Otherwise a repeat class can be given to complete the experiment insome cases.

  • 8/4/2019 Est Labi2011

    2/12

    KEIL BASED PROGRAMS (8051 processor)

    Project Name Software timers

    Aim: To learn how to build software timers using the inbuilt hardware timer.

    Required Item: Keil simulation environment with C compiler and assembler for 8051.

    Observables: Student has to make this program work. He should show the working of the software timers by puttingprobe outputs through a port and observe the same using logic analyzer of the Keil environment. Measure and note the

    timings generated.

    Typical program:;----------------------------------------------------------------------------------;This program is to demonstrate the use of hardware timer to create software timers;----------------------------------------------------------------------------------;----------------------------------------------------------------------------------;constant decleration section;----------------------------------------------------------------------------------timer0val equ 0ffffh-5*1000 ;5 msec per interrupt at 12MHz crystal with 1usec clock

    timer_mode equ 001h ;timer0 in 16bit timer and timer1 not used;;----------------------------------------------------------------------------------;Data definitions ; variable declerations;----------------------------------------------------------------------------------;

    dseg at 030h ;soft timers are defined heretimer1: ds 1timer2: ds 1timer3: ds 1;----------------------------------------------------------------------------------;stack segment;----------------------------------------------------------------------------------

    dseg at 070hstack_start: ds 16 ;stack is of 16 bytes size 70-7f;----------------------------------------------------------------------------------;;----------------------------------------------------------------------------------;power on reset segement;----------------------------------------------------------------------------------

    cseg at 0jmp start ;reset vector jump

    ;----------------------------------------------------------------------------------;interrupt segment;----------------------------------------------------------------------------------

    cseg at 0bh ;timer0 interrupt vector location

    jmp timer0intsvc ;timer0 overflow vector jump;----------------------------------------------------------------------------------;timer0 service routine;----------------------------------------------------------------------------------

    cseg at 30htimer0intsvc: push acc ;save accumulator as it is used in the

    push pswmov a,timer1 ;service routinecjne a,#0,tm0int3 ;if the value of soft timer is 0 leave it

    tm0int1: mov a,timer2 ;as suchcjne a,#0,tm0int4

    tm0int2:mov a,timer3cjne a,#0,tm0int5

    jmp tm0inte ;timer1 serviced check for nexttm0int3: dec timer1

    jmp tm0int1tm0int4: dec timer2

  • 8/4/2019 Est Labi2011

    3/12

    jmp tm0int2tm0int5: dec timer3tm0inte: pop psw ;unsave the flags register

    pop acc ;unsave the accumulatorreti

    ;----------------------------------------------------------------------------------;initialization code section

    ;----------------------------------------------------------------------------------cseg at 60hstart:init: mov sp,#stack_start-1 ;sp is incremented and used for pushing

    mov tmod,#timer_mode ;set timer0 to 16bit timer modemov th0,#high(timer0val) ;load timer0 count valuemov tl0,#low(timer0val)setb ea ;enable all interrupts (global enable)setb et0 ;enable timer0 overflow interruptsetb tr0 ;start timer0 to run after initializing the

    ;timer0main: mov timer1,#20

    mov timer2,#30

    mov timer3,#40; mov p1,#0ffh ;to make it as outputloop: mov a,timer1

    cjne a,#0,main1cpl p1.0mov timer1,#20

    main1: mov a,timer2cjne a,#0,main2cpl p1.1mov timer2,#30

    main2: mov a,timer3cjne a,#0,main3cpl p1.2

    mov timer3,#40main3: jmp loop

    end

  • 8/4/2019 Est Labi2011

    4/12

    Project Name Simple Task switching for 8051applications

    Aim: To learn how to build simple tasks using 8051, with assembly language.

    Required Item: Keil simulation environment with C compiler and assembler for 8051.

    Observables: Student has to make this program work. He should show the working of the tasks by putting probe outputsthrough a port and observe the same using logic analyzer of the Keil environment.

    Typical program:

    ;this program is to demonstrate how independent tasks can be built;and run in assembly language program;-----------------------------------------------------------------------------------------; constant declarations;-----------------------------------------------------------------------------------------task0StackSize equ 12task1stackSize equ 12

    task2stackSize equ 12

    selectBank0 equ 0h ;word written in psw for register bank switching bank0selectBank1 equ 008h ;for switching to bank1 (psw bits 6 and 5 are used)selectBank2 equ 010h ;for switching to bank2

    ;-----------------------------------------------------------------------------------------;data variable declarations;-----------------------------------------------------------------------------------------;stack declaration

    dseg at 080h-(task0StackSize+task1StackSize+task2StackSize)task0spInit: ds task0stackSizetask1spInit: ds task1stackSize

    task2spInit: ds task2stackSize;------------------------------------------------------------------------------------;variable data area for the use of tasks;

    dseg at 030htask0data: ds 14task1data: ds 14task2data: ds 14;-----------------------------------------------------------------------------------------;reset vector declaration;-----------------------------------------------------------------------------------------

    cseg at 0jmp start

    ;-----------------------------------------------------------------------------------------;interrupt vector declarations;-----------------------------------------------------------------------------------------; no interrupts used now;-----------------------------------------------------------------------------------------;interrupt service routines;-----------------------------------------------------------------------------------------

    ;-----------------------------------------------------------------------------------------;main program;-----------------------------------------------------------------------------------------start:init:

    mov psw,#selectBank1 ;initialize data for task1 in the stackmov sp,#task1spInitmov a,#low(Task1)push acc

  • 8/4/2019 Est Labi2011

    5/12

    mov a,#high(Task1) ;store the return addresss to point to the task1push accinc sp ;accinc sp ;dphinc sp ;dplmov r7,sp ;save stack pointer in reg 7mov psw,#selectBank2 ;initialize data for task2 in the stack

    mov sp,#task2spInit ;store the return address to point to task2mov a,#low(Task2)push accmov a,#high(Task2)push accinc sp ;accinc sp ;dphinc sp ;dplmov r7,sp ;save stack pointer in reg 7mov psw,#selectBank0 ;select bank0 to run task0mov sp,#task0spInit ;init sp for task0jmp Task0

    ;--------------------------------------------------------------------------------------------------------; sub programs for context switching; note that PSW is not preserved and r7 is used as sp store.; hence r7 is not available for use within the tasks;--------------------------------------------------------------------------------------------------------goTask0: push acc ;save acc

    push dph ;save dptrpush dplmov r7,sp ;save current spmov psw,#selectBank0 ;switch to bank0mov sp,r7 ;unsave sppop dpl ;unsave dptrpop dphpop acc ;unsave acc

    ret ;switch to task0

    goTask1: push acc ;switchpush dphpush dplmov r7,spmov psw,#selectBank1 ;switch to bank0mov sp,r7pop dplpop dphpop accret ;switch to task1

    goTask2: push acc ;switchpush dphpush dplmov r7,spmov psw,#selectBank2 ;switch to bank0mov sp,r7pop dplpop dphpop accret ;switch to task2

    ;-------------------------------------------------------------------------------------------------------; end of context switch subroutines;-------------------------------------------------------------------------------------------------------

    ; task definitions are here; each task can include code with yielding call to task0 in between (jmp goTask0); task0 takes the responsibility of scheduling the processor among the tasks;-------------------------------------------------------------------------------------------------------

  • 8/4/2019 Est Labi2011

    6/12

    Task0: setb p1.0mov r0,#255djnz r0,$clr p1.0call goTask1 ;schedule task1call goTask2 ;schedule task2

    jmp Task0

    Task1: setb p1.1mov r0,#255djnz r0,$clr p1.1call goTask0jmp Task1

    Task2: setb p1.2mov r0,#255djnz r0,$clr p1.2

    call goTask0jmp Task2;-------------------------------------------------------------------------------------------------------;end of all task definitions;-------------------------------------------------------------------------------------------------------

    end

  • 8/4/2019 Est Labi2011

    7/12

    Project Name Array manipulation program

    Aim: To demonstrate how C compiler cross compiles for 805, mainly the arrays and structures; to study the use of

    pointers in creation and use of the arrays and structures.

    Required Item: Keil simulation environment with C compiler and assembler for 8051.

    Observables: Student has to make this program work. He should observe the working of output program and the use ofpointers to access arrays. He should also be able to identify the place where the array is stored, by observing the list

    file generated during the build process.

    Typical program://this program is demonstrate the use of arrays pointers and strings//here we learn constant arrays, dynamic arrays and the use of pointers//in using them

    #include //to define standard register names with their physical addresses#include #include

    //data structure definitions//constant declerations

    const char arr1[11] = {11,0,1,2,3,4,5,6,7,8,9}; //the first byte in the array has the count of data

    //variables declerationchar *pointer1;char *pointer2; //general pointer variables for use

    //this is a genral structure for a buffer#define msgBufLength 8

    //the above defined structure is used to create a queue of bufferstypedef struct{unsigned int head;unsigned int tail; //head and tail has to be moved forwardunsigned int count;unsigned char msgbuf[msgBufLength];}msgbuftype;

    msgbuftype messageBuffer1;//msgbuf messageBuffer[10]; defines and array 10 message buffers

    void InitMsgBuf(msgbuftype messageBuffer){

    messageBuffer.head = 0; //head and tail are used as read and write offsetsmessageBuffer.tail = 0; //within the array.messageBuffer.count = 0; //no data in the buffer

    }//subprogram definitions

    bit ReadNextMsg(unsigned char *msg){if(messageBuffer1.count > 0){

    *msg = messageBuffer1.msgbuf[messageBuffer1.tail];messageBuffer1.tail++;messageBuffer1.count--;if(messageBuffer1.tail >= msgBufLength){

    messageBuffer1.tail = 0;

    }return(1); //what does this return?

    }return(0);

  • 8/4/2019 Est Labi2011

    8/12

    }

    bit WriteNextMsg(char msg){if(messageBuffer1.count < msgBufLength){

    messageBuffer1.msgbuf[messageBuffer1.head] = msg;messageBuffer1.head=messageBuffer1.head+1;messageBuffer1.count++;

    if(messageBuffer1.head >= msgBufLength){messageBuffer1.head = 0;}return(1); //what does this return?

    }return(0);

    }

    //main program definitionint main(){int i;char b,a;

    //initialization portionsSCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload. What is |= operator? */TH1 = 221; /* TH1: reload value for 1200 baud @ 16MHz */TR1 = 1; /* TR1: timer 1 run */TI = 1; /* TI: set TI to send first char of UART */

    InitMsgBuf(messageBuffer1);printf ("\nhead = %u : tail = %u : count = %u", messageBuffer1.head, messageBuffer1.tail,

    messageBuffer1.count);pointer1 = &arr1;pointer2 = &arr1 + 11;printf("\nthe array is ");b=*pointer1++;

    for (i=1;i < 11;i++){a=*pointer1++;while(!WriteNextMsg(a));while(ReadNextMsg(&a))printf(" %c",a + 0x30);

    }printf("\n");for (i=1;i < 11;i++){

    a=*--pointer2;printf(" %c",a + 0x30);

    }while(1);

    }

  • 8/4/2019 Est Labi2011

    9/12

    Project Name Stepper Control program

    Aim: To demonstrate how 8051 can be used to control two stepper motors simultaneously.

    Required Item: Keil simulation environment with C compiler and assembler for 8051.

    Observables: Student has to generate stepper wave forms on the simulated logic analyzer window and measure step

    time and time delay between steps; confirm it with software time line, mark expected outputs as per time delays givenand observe the occurrence of proper display outputs in the seven segment display.

    Typical program:

    PRORGRAM FILE1: C file//stepper control program#include #define stepper_off 00;

    void msecDelay(unsigned char temp); //call an assembly program which creates msec delay

    //Port1 low four bits are used for one stepper motor

    //Port2 low four bits are used for second stepper motor

    void rotateOneStep(bit direction, char port_no){unsigned char i,temp;

    if (direction==1)temp = 0x01; else temp=0x08;for (i=1;i 1;msecDelay(10); //step delay

    }

    if (port_no==1)P1=stepper_off;if (port_no==0)P0=stepper_off;

    }

    int main (){unsigned char a;while(1){

    for (a=1;a

  • 8/4/2019 Est Labi2011

    10/12

    pop 0 ? cycles for this instructionend

  • 8/4/2019 Est Labi2011

    11/12

    Project Name Simple command line calculator.

    Aim: To demonstrate the use of input output routines and arithmetic operations in 8051 using C language.

    Aim: To demonstrate how 8051 can be used to control two stepper motors simultaneously.

    Required Item: Keil simulation environment with C compiler and assembler for 8051.

    Observables: Student has to make the program work and check it with a few sample data given from simulated serialI/O in the Keil environment.

    Typical program:

    #include /* define 8051 registers */#include /* define I/O functions */

    extern unsigned int getnumber (void);extern void output (unsigned int);

    void main (void) { /* main program */unsigned int number1, number2; /* define operation registers */bit operation; /* define operation */

    SCON = 0x52; /* SCON */ /* setup serial port control */TMOD = 0x20; /* TMOD */ /* hardware (2400 BAUD @12MHZ) */TCON = 0x69; /* TCON */TH1 = 0xf3; /* TH1 */

    printf ("\n\nC compiler demonstration program\n\n");

    while (1) { /* repeat forever */number1 = getnumber (); /* read number1 */number2 = getnumber (); /* read number2 */printf ("Input operation: '+' (ADD) or '-' (SUB) ? ");operation = (getchar () == '+'); /* get operation */output (operation ? (number1 + number2) /* perform operation */

    : (number1 - number2) );}

    }

    This is second file which contains subprograms used in the main: FILE2

    #include /* define I/O functions */

    void getline (char *line) {while ((*line++ = getchar()) != '\n');

    }

    int atoi (char *line) {bit sign;int number;

    /* skip white space */for ( ; *line == ' ' || *line == '\n' || *line == '\t'; line++);

  • 8/4/2019 Est Labi2011

    12/12

    /* establish sign */sign = 1;if (*line == '+' || *line == '-') sign = (*line++ == '+');

    /* compute decimal value */

    for (number=0; *line >= '0' && *line