0% found this document useful (0 votes)
89 views

Lab Manual For Embedded System Lab - I Course: M.Tech (Est) Course Code - Em511 Preface

This lab manual contains experiments to be conducted over one semester using an embedded system lab. Students will learn to use software timers, task switching, and array manipulation in 8051 microcontrollers using Keil software. The software timer experiment involves creating timers using the hardware timer to generate timing signals. The task switching experiment demonstrates building independent tasks and running them concurrently. The array manipulation experiment shows creating and accessing arrays and structures using pointers in C code compiled for 8051.

Uploaded by

Saurabh Godha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Lab Manual For Embedded System Lab - I Course: M.Tech (Est) Course Code - Em511 Preface

This lab manual contains experiments to be conducted over one semester using an embedded system lab. Students will learn to use software timers, task switching, and array manipulation in 8051 microcontrollers using Keil software. The software timer experiment involves creating timers using the hardware timer to generate timing signals. The task switching experiment demonstrates building independent tasks and running them concurrently. The array manipulation experiment shows creating and accessing arrays and structures using pointers in C code compiled for 8051.

Uploaded by

Saurabh Godha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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 one semester. 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 and C 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 the earlier classes for the students to get familiarize with Keil and Trioz software development environment. The sample programs given in this manual can be given prior to the actual if need be. (depending on the students preparedness). Otherwise a repeat class can be given to complete the experiment in some cases.

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 putting probe 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 here timer1: ds 1 timer2: ds 1 timer3: ds 1 ;---------------------------------------------------------------------------------;stack segment ;---------------------------------------------------------------------------------dseg at 070h stack_start: ds 16 ;stack is of 16 bytes size 70-7f ;---------------------------------------------------------------------------------; ;---------------------------------------------------------------------------------;power on reset segement ;---------------------------------------------------------------------------------cseg at 0 jmp start ;reset vector jump ;---------------------------------------------------------------------------------;interrupt segment ;---------------------------------------------------------------------------------cseg at 0bh ;timer0 interrupt vector location jmp timer0intsvc ;timer0 overflow vector jump ;---------------------------------------------------------------------------------;timer0 service routine ;---------------------------------------------------------------------------------cseg at 30h timer0intsvc: push acc ;save accumulator as it is used in the push psw mov a,timer1 ;service routine cjne a,#0,tm0int3 ;if the value of soft timer is 0 leave it tm0int1: mov a,timer2 ;as such cjne a,#0,tm0int4 tm0int2: mov a,timer3 cjne a,#0,tm0int5 jmp tm0inte ;timer1 serviced check for next tm0int3: dec timer1 jmp tm0int1 tm0int4: dec timer2

jmp tm0int2 timer3 psw ;unsave the flags register pop acc ;unsave the accumulator reti ;---------------------------------------------------------------------------------;initialization code section ;---------------------------------------------------------------------------------cseg at 60h start: init: mov sp,#stack_start-1 ;sp is incremented and used for pushing mov tmod,#timer_mode ;set timer0 to 16bit timer mode mov th0,#high(timer0val) ;load timer0 count value mov tl0,#low(timer0val) setb ea ;enable all interrupts (global enable) setb et0 ;enable timer0 overflow interrupt setb tr0 ;start timer0 to run after initializing the ;timer0 main: mov timer1,#20 mov timer2,#30 mov timer3,#40 ; mov p1,#0ffh ;to make it as output loop: mov a,timer1 cjne a,#0,main1 cpl p1.0 mov timer1,#20 main1: mov a,timer2 cjne a,#0,main2 cpl p1.1 mov timer2,#30 main2: mov a,timer3 cjne a,#0,main3 cpl p1.2 mov timer3,#40 main3: jmp loop end tm0int5: dec tm0inte: pop

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 outputs through 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 12 task1stackSize equ 12 task2stackSize equ 12 selectBank0 selectBank1 selectBank2 equ equ equ 0h 008h 010h ;word written in psw for register bank switching bank0 ;for switching to bank1 (psw bits 6 and 5 are used) ;for switching to bank2

;----------------------------------------------------------------------------------------;data variable declarations ;----------------------------------------------------------------------------------------;stack declaration dseg at 080h-(task0StackSize+task1StackSize+task2StackSize) task0spInit: ds task0stackSize task1spInit: ds task1stackSize task2spInit: ds task2stackSize ;-----------------------------------------------------------------------------------;variable data area for the use of tasks ; dseg at 030h task0data: ds 14 task1data: ds 14 task2data: ds 14 ;----------------------------------------------------------------------------------------;reset vector declaration ;----------------------------------------------------------------------------------------cseg at 0 jmp start ;----------------------------------------------------------------------------------------;interrupt vector declarations ;----------------------------------------------------------------------------------------; no interrupts used now ;----------------------------------------------------------------------------------------;interrupt service routines ;----------------------------------------------------------------------------------------;----------------------------------------------------------------------------------------;main program ;----------------------------------------------------------------------------------------start: init: mov psw,#selectBank1 ;initialize data for task1 in the stack mov sp,#task1spInit mov a,#low(Task1) push acc

mov push inc inc inc mov mov mov mov push mov push inc inc inc mov mov mov jmp

a,#high(Task1) acc sp sp sp r7,sp psw,#selectBank2 sp,#task2spInit a,#low(Task2) acc a,#high(Task2) acc sp sp sp r7,sp psw,#selectBank0 sp,#task0spInit Task0

;store the return addresss to point to the task1 ;acc ;dph ;dpl ;save stack pointer in reg 7 ;initialize data for task2 in the stack ;store the return address to point to task2

;acc ;dph ;dpl ;save stack pointer in reg 7 ;select bank0 to run task0 ;init sp for 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 dptr push dpl mov r7,sp ;save current sp mov psw,#selectBank0 ;switch to bank0 mov sp,r7 ;unsave sp pop dpl ;unsave dptr pop dph pop acc ;unsave acc ret ;switch to task0 goTask1: push push push mov mov mov pop pop pop ret acc dph dpl r7,sp psw,#selectBank1 sp,r7 dpl dph acc ;switch

;switch to bank0

;switch to task1

goTask2:

push acc ;switch push dph push dpl mov r7,sp mov psw,#selectBank2 ;switch to bank0 mov sp,r7 pop dpl pop dph pop acc ret ;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 ;-------------------------------------------------------------------------------------------------------

Task0:

setb mov djnz clr call call jmp setb mov djnz clr call jmp

p1.0 r0,#255 r0,$ p1.0 goTask1 goTask2 Task0 p1.1 r0,#255 r0,$ p1.1 goTask0 Task1 ;schedule task1 ;schedule task2

Task1:

Task2:

setb p1.2 mov r0,#255 djnz r0,$ clr p1.2 call goTask0 jmp Task2 ;------------------------------------------------------------------------------------------------------;end of all task definitions ;------------------------------------------------------------------------------------------------------end

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 of pointers 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 <REG52.h> #include <stdio.h> #include <string.h> //data structure definitions //constant declerations const char arr1[11] = {11,0,1,2,3,4,5,6,7,8,9}; //variables decleration char *pointer1; char *pointer2; //the first byte in the array has the count of data //to define standard register names with their physical addresses

//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 buffers typedef struct{ unsigned int head; unsigned int tail; //head and tail has to be moved forward unsigned 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 offsets messageBuffer.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);

} 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 definition int main(){ int i; char b,a; //initialization portions SCON = 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); }

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 given and observe the occurrence of proper display outputs in the seven segment display. Typical program: PRORGRAM FILE1: C file //stepper control program #include <REG52.h> #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<=4;i++){ if (port_no==1)P1=temp; if (port_no==0)P0=temp; if (direction==1)temp = temp << 1; else temp = temp >> 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<10;a++){ rotateOneStep(1,1); rotateOneStep(0,0); msecDelay(100); } } }

//P1 //P0

PROGRAM FILE2: ASM FILE ;this program in assembly creates dealys in terms of milli seconds ;the subprogram msecDelay returns only after completing the required ;time period public _msecDelay ;for crystal clock of 12 MHz cseg at 50 delay_val equ 50 ;put correct value ;it is assumed the delay value is passed by the "C" program thro' R7 _msecDelay: push 0 delay1: mov 00,#delay_val djnz 00,$ ;2 cycles for this instruction djnz r7,delay1 ;2 cycles for this instruction

pop end

? cycles for this instruction

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 serial I/O in the Keil environment. Typical program:

#include <reg51.h> #include <stdio.h>

/* define 8051 registers */ /* 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 */ TMOD = 0x20; /* TMOD */ TCON = 0x69; /* TCON */ TH1 = 0xf3; /* TH1 */ /* setup serial port control */ /* hardware (2400 BAUD @12MHZ) */

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 <stdio.h> /* 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++);

/* establish sign */ sign = 1; if (*line == '+' || *line == '-') sign = (*line++ == '+'); /* compute decimal value */ for (number=0; *line >= '0' && *line <= '9'; line++) number = (number * 10) + (*line - '0'); return (sign ? number : -number); } unsigned int getnumber (void) { char line [40]; printf ("Input Number ? "); getline (line); return (atoi (line)); } This file is a dummy file just to show how an array is defined: FILE3 #include <stdio.h> /* define I/O functions */ char dummy_buffer [25]; /* only for demostration */ void output (unsigned int number) { printf ("\nresult: %d\n\n", number); }
//end of source code

You might also like