Saturday, November 15, 2008

Simple Programs for 8051

MICROCONTROLLER PROGRAMS

To construct the given circuit and write an assembly level program to
• Poll a button and to test the circuit.
THEORY :
In most microcontrollers, passing a single bit from one position to another or to another position in another byte is quite difficult. However this is not the case in 8051.This program simply moves the button bit value into the carry flag and moves the carry flag into the LED bit.
PROGRAM :
#include
org 0x0000
Loop1: JB p3.0,Loop1
MOV c,p1.1
MOV p1.0,c
MOV c,p0.1
MOV p0.0,c
SJMP Loop1
Loop2: JNB p3.0,Loop1
SETB c
ANL c,p1.1
MOV p1.0,c
SETB c
ANL c,p0.1
MOV p0.0,c
SJMP Loop2
END
RESULT:
Thus the ALP was written, executed and programmed into the microcontroller.The circuit was then tested and verified.

SQUARE WAVE GENERATION
AIM :
To write an ALP to generate a square wave of 0.8 seconds using timer.
THEORY :
This program is used to generate a square wave of 0.8 seconds. There are two timers present in 8051. Either one of them can be used to generate the square wave. The timer is used in mode 1 (i.e) 16 bit timer. For one cycle, a 16 bit timer can measure a maximum of 65.536 milli-seconds. In order to generate the required square wave, a number of cycles of the timer is required. Once the required value is reached, a pin is toggled. Thus the square wave is obtained in this pin.
PROGRAM:
#include
org 0x0000
sjmp s
org 0x0050
s: mov 20h,#98
mov tmod,#00000001b
mov th0,#0xf0
mov tl0,#0x00
mov ie,#10000010b
setb tr0
sjmp $
org 0x0000b
clr tf0
mov th0,#0xf0
djnz 20h,x
mov 20h,#98
mov c,p1.0
cpl c
mov p1.0,c
x: reti
end
SAMPLE OUTPUT :
T ON = 0.401431 SECONDS.
T OFF = 0.802839 SECONDS.
T ON = 1.204247 SECONDS.
T OFF = 1.605655 SECONDS.
RESULT:
Thus the square waveform of 0.8 seconds was generated and the output was verified.
PULSE WIDTH MEASUREMENT (PWM)
AIM:
To write a assembly level program to measure the pulse width of the square wave.
THEORY:
The program is used to measure the width of a pulse. In this the timer is controlled by the external interrupt. The pulse to be measured is given as input to the external interrupt pin of that particular timer. Hence the timer is started when the pin is HIGH, and stops when the pin goes LOW. Since the maximum value to be measured is only 256 micro-seconds, only the TL register is used. Thus the width of the pulse is directly obtained from the TL register.
PROGRAM:
#include
org 0x0000
sjmp n
org 0x0050
n: mov tmod,#00001001b
setb it0
jnb p3.2,$
jb p3.2,$
mov ie,#10000001b
sjmp $
org 0x0003
clr ie0
setb tr0
jnb p3.2,$
jb p3.2,$
clr tr0
p2=tl0
reti
end
DEBUG SCRIPT :
Signal void sert()
{
while(1)
{
port3^=0xff;
swatch(0.000150);
}
}
sert()
SAMPLE OUTPUT :

150 micro-seconds.
Port2: 0x96=150
RESULT :
Thus the program to measure the width of the pulse was written and the output was verified.
BIT BANGING ASYNCHRONOUS
SERIAL INTERFACE
AIM:
To transmit data using the serial I/O interface and receive data using the serial I/O interface.
APPARATUS REQUIRED:
Kiel software
P.C.
THEORY:
The 8051 microcomputers can send and receive data via software very efficiently using the Boolean instruction set. Since any I/O pin can be a serial input or output, several serial links can be made at once. The program consists of a start bit, 8 bits and a stop bit. The program is made in such a manner that the bit is read eight times by making the data valid by using the same algorithm.
The Data is received by testing an input pin setting the carry to same state, shifting the carry into a data buffer and saving the partial frame in internal RAM. Data is transmitted by shifting an output buffer through the carry and generating each bit on an output pin.
The timing of the program is synchronized with that of the input data stream baud rate else the data would be out of bounds. The timing is done using the delay that would be needed for each bit time interval.
PROGRAM:
BIT TRANSMISSION:
#include
org 0x0000
ljmp st
org 0x0050
st: mov a,#10101011b
sd: jnb p2.1,$
jb p2.1,$
mov r0,#10
clr c
loop: mov p3.1,c
mov r1,#48
loop1: djnz r1,loop1
nop
nop
setb c
rrc a
djnz r0,loop
end
BIT RECEPTION:
#include
org 00000
start: mov a,#00
str: jnb p1.0,$
jb p1.0,$
mov r1,#08
rx: mov r0,#55
x: djnz r0,x
mov c,p1.0
rrc a
djnz r1,rx
mov c,p1.0
jc str
clr a
ljmp start
end
SAMPLE OUTPUT:
BIT TRANSMISSION:
DATA
BIT INTERVAL
0
2.23761800
1
2.23772300
1
2.23782800
1
2.23793300
0
2.23803800
1
2.23814300
0
2.23824800
1
2.23835300
1
2.23845800

Difference : 1.05 x 10-4 sec
Upper Limit : 9.895 x 10-4 sec
Lower limit : 8.854 x 10-4 sec
Baud rate : 9253 bps
Value : 9.45 x 10-4 sec
BIT RECEPTION:
Data Received : 01010000
RESULT:
Thus a data was transmitted and received using serial I/O interface.
RS-232 INTERFACE
AIM:
To write a program to manipulate the received byte and transmit it to the
Computer using the RS-232 Cable and Atmel 89C51 Microcontroller.
APPARATUS REQUIRED:
1. Max 232
2. Security key
3. P.C
4. RS-232 Cable
5. Atmel 89C51
6. Zif Socket
7. Power Supply
8. Capacitors
9. Breadboard
10. Wires
THEORY:
RS-232 was developed originally in 1962 with RS standing for Resource Standard. It is a full duplex model which means that it can transmit and receive at the same time. The RS-232 usually consists of 2 connectors at either side of the wire which may be the male connector or the female connector. The term male connector refers to a plug with each wire in the cable connecting a pin. The term female connector refers to a receptacle with each wire connecting to a pin.The transmission of the data is done through the 2nd pin of the connector and the receiving of the data is done through the 3rd pin of the connector which shows that the both parties can transmit data and receive the data.
PROGRAM:
#include
unsigned char var;
void serial (void) interrupt 4
{ if(RI= =1)
{ var = SBUF;
SBUF=var+2; }
RI=0; }
void main( )
{ PCON= 0x80;
SCON= 0x50;
P3= 0xff;
TMOD= 0x20;
TH1= 48;
TL1= 47;
TR1=1;
while{1}
{ TI= 0;} }
SAMPLE OUTPUT:
INPUT: t j l m OUTPUT: v l n o
RESULT:
Thus the program to manipulate the received byte and transmit it to computer using RS-232 cable to microcontroller was written and executed.
INTERFACING OF LED
AIM:
To write a program to display the following lines sequentially one by one once in 10 seconds on a 16X1 LCD.
APPARATUS REQUIRED:
1. Micro controller AT89C51 --- 1
2. Trainer ESA 51 --- 1
3. LCD module
4. Connecting chord
5. Power supply

THEORY:
LCD Interface:
LCD modules are widely used in areas where information is conveyed visually. They can be seen in calculators, mobile and cellular phones, pagers and other electronic products. The interface is built around 20x4 LCD module with 15 pins. This can be directly coupled to the 8255 PPI on any of the trainers. The present interface module allows the user to become familiar with the techniques of using the LCD module.
Pin Description:
PIN SYMBOL FUNCTION
1 Vss Ground
2 Vdd +5V supply
3 Vo LCD contrast control
4 RS RS=0 Instruction
RS=1 Data
5 R/W* Read/Write
1 = Read
0 = Write
6 E Enable
7 TO 14 DB0 to DB7 Bi-directional data bus
15 L+ Back light power of
+5V
Instruction Set :
1. Clear Display
2. Return Home Cursor to 00
3. Entry Mode Set
4. Display ON/OFF Control
5. Cursor on Display Shift
6. Function Set
7. Read Busy Flag Address

The Interface module consists of 26-pin FRC (Flat Ribbon Cable) connector at the extreme right, which is used to interface the module to the Micro controller trainer. PORT A of 8255 is used as Bi directional data bus for LCD Module. Lower three bits of PORT B of 8255 supports 3 control signals of LCD Module.
Program:
#include
#include
unsigned char xdata pa _at _ 0xe800;
unsigned char xdata pb _at_ 0xe801;
unsigned char xdata pc _at_ 0xe802;
unsigned char xdata cp _at_ 0xe803;
unsigned char u;
void delay()
{
long i;
for(i=0;i<5000;i++)
{
}
}
void dwrite()
{
pb=0xfa;
pb=0xf9;
pb=0xfd;
pb=0xf9;
delay();
}
void write()
{
pb=0xfb;
pb=0xf8;
pb=0xfc;
pb=0xf8;
delay();
}
void main()
{
cp=0x80;
pb=0x07;
pa=0x30;
write();
delay();
pa=0x30;
write();
delay();
pa=0x30;
write();
delay();
pa=0x38;
write();
pa=0x0f;
write();
pa=0x06;
write();
pa=0x01;
write();
pa=0x49;
dwrite();
pa=0x20;
dwrite();
pa=0x4c;
dwrite();
pa=0x49;
dwrite();
pa=0x4e;
dwrite();
pa=0x45;
dwrite();
for(u=0;u<8;u++)
{delay();}
pa=0x01;
write();
pa=0x49;
dwrite();
pa=0x49;
dwrite();
pa=0x20;
dwrite();
pa=0x4c;
dwrite();
pa=0x49;
dwrite();
pa=0x4e;
dwrite();
pa=0x45;
dwrite();
for(u=0;u<8;u++)
{delay();}
pa=0x01;
write();
pa=0x49;
dwrite();
pa=0x49;
dwrite();
pa=0x49;
dwrite();
pa=0x20;
dwrite();
pa=0x4c;
dwrite();
pa=0x49;
dwrite();
pa=0x4e;
dwrite();
pa=0x45;
dwrite();
for(u=0;u<8;u++)
{delay();}
pa=0x01;
write();
pa=0x49;
dwrite();
pa=0x56;
dwrite();
pa=0x20;
dwrite();
pa=0x4c;
dwrite();
pa=0x49;
dwrite();
pa=0x4e;
dwrite();
pa=0x45;
dwrite();
for(u=0;u<8;u++)
{delay();}
pa=0x01;
write();
}
Sample Output:
I LINE
II LINE
III LINE
IV LINE
Result:
Thus the program to display the given lines sequentially on a 16X1 LCD display had been written, executed and the output verified.
ROUND ROBIN METHOD
AIM:
To write a program using rtx51 that uses round robin methodology.
APPARATUS REQUIRED:
1. Keil software
2. Security key
3. P.C
THEORY:
Many microcontroller applications require simultaneous execution of multiple jobs or tasks. For such applications, a real-time operating system (RTOS) allows flexible scheduling of system resources (CPU, memory, etc.) to several tasks. RTX51 implements a powerful RTOS which is easy to use. RTX51 works with all 8051 derivatives.
RTX51 also performs round-robin multitasking which allows quasi-parallel execution of several endless loops or tasks. Tasks are executed in a time-sliced manner. The available CPU time is divided into time slices and RTX51 assigns a time slice to every task. Each task is allowed to execute for a predetermined amount of time. Then, RTX51 switches to another task that is ready to run and allows that task to execute for a while. The time slices are very short, usually only a few milliseconds. For this reason, it appears as though the tasks are executing simultaneously.
The following program shows an RTX51 application that uses only round-robin task scheduling. The two tasks in this program are simple counter loops. RTX51 starts executing task 0 which is the function names job0. This function adds another task called job1. After job0 executes for a while, RTX51 switches to job1. After job1 executes for a while, RTX51 switches back to job0. This process is repeated indefinitely.
PROGRAM:
#include int counter0; int counter1; void job0 (void) _task_ 0 { os_create (1); /* mark task 1 as ready */ while (1) { /* loop forever */ counter0++; /* update the counter */ }
} void job1 (void) _task_ 1 { while (1) { /* loop forever */ counter1++; /* update the counter */ } }
OBSERVATIONS:
TIMER VALUE
(SEC)
COUNTER0
VALUE
COUNTER1
VALUE
0.00026550
0X0000
0X0000
0.02531450
0X305C
0X0000
0.05039150
0X305C
0X3042

RESULT:
Thus the program on round robin tasking was written using the rtx51 application.
INTERRUPT FUNCTIONS IN “C51”
AIM:
1. To send all the contents of port 1,2,3 as consecutive bytes when a data 75H arrives in serial port and an acknowledgement 70H is got from the other side.
2. To toggle the status of port 1.1 upon receiving external interrupt 0.
3. To send 4 bit gray code sequentially on the lower nibble of port 2 for every 30 ms.

APPARATUS REQUIRED:
1.Microcontroller
2.Keil
THEORY:
The Cx51 compiler supports interrupt functions for 32 interrupts. The interrupt function attribute when included in a declaration, specifies that the associated function is an interrupt function. The interrupt attribute takes as an argument an integer constant in the 0 to 31 value range. The interrupt attribute affects the object code by causing the contents of the SFR, ACC, B, DPH, DPL, PSW to be saved on the stack at function invocation time. Also, the working registers and special registers that were saved on the stack are restored before exiting the function. The function is terminated by RETI instruction. The compiler generates the interrupt vector automatically. The interrupt should not be declared with any arguments. They must be declared as void. Interrupt functions should not be called indirectly through a function pointer. The 'using' function attribute is used to select a register bank different from that of the non-interrupt program code. The standard interrupts found on an 8051 are: external int 0, timer/counter 0, external int 1, timer/counter1, serial port.
PROGRAM :
#include
sbit u=P2^0;
sbit v=P2^1;
sbit w=P2^2;
sbit x=P2^3;
sbit t=P1^1;
int ct=120;
int a=0;
void timer0(void) interrupt 1
{
if(ct--==1)
{
P2=a;
ct=120;
u=u^v;
v=w^v;
w=w^x;
a=a+1;
if(a==16)
{a=0;}
}
}
void ext0(void) interrupt 0
{t=!t;}
void serial(void) interrupt 4
{static int flag=0;
if(RI==1)
{
if(SBUF==5)
{SBUF=P1;
flag=3;
}
else if(SBUF==04&&flag==3)
{SBUF=P2;
flag=5;
}
else if(SBUF==4&&flag==5)
{SBUF=P3;}}
TI=0;
RI=0;
}
void main()
{
PCON=0x80;
SCON=0x50;
IE=0x9b;
TMOD=0x22;
TH1=253;
TL1=253;
TH0=0x06;
TL0=0x06;
TR0=1;
while(1);
}
SAMPLE OUTPUT:
Gray code time interval : 0.60744 s ---> 2nd time period
0.30743 s ---> 1st time period
--------
Time delay : 0.30001 s
--------
Gray code
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
RESULT :
Thus the program was written, executed and the output was verified.
TRAFFIC LIGHT CONTROL SYSTEM
AIM:
To implement traffic light control system using RTX-51with control over serial port.
APPARATUS REQUIRED:
Keil security key
THEORY:
The traffic light is timed and lets cars pass during a
specific time period. There is a pedestrial crossing
button that lets pedestrians cross. The lights are
connected to Port 1. We can see this in action using
dScope.
There are several things of interest here:
* This program uses RTX51 Tiny. Program initialization
and operation code are in TRAFFIC.C. Look for the
init task to see where everything starts.
* Serial I/O is interrupt and signal driven. Look at
SERIAL.C for the complete, encapsulated module (you
may want to use this in your programs).
The TRAFFIC program is available in one target:
Simulator: Small Model: TRAFFIC example in SMALL model
PROGRAM:
/***********************************************************************************/
/* TRAFFIC.C: Traffic Light Controller using the C-51 COMPILER */
/************************************************************************************
char code menu[] =
"\n"
"+**TRAFFIC LIGHT CONTROLLER using C51 and RTX-51 tiny ** +\n"
" This program is a simple Traffic Light Controller. Between \n"
" start time and end time the system controls a traffic light \n"
" with pedestrian self-service. Outside of this time range \n"
" the yellow caution lamp is blinking. \n"
"+ command -+ syntax ----- + function --------------------------------------+\n"
" Display D display times \n"
" Time T hh:mm:ss set clock time \n"
" Start S hh:mm:ss set start time \n"
" End E hh:mm:ss set end time \n"
"+------------- +---------------- +----------------------------------------- ------- +n";
#include /* special function registers 8052 */
#include /* RTX-51 tiny functions & defines */
#include /* standard I/O .h-file */
#include /* character functions */
#include /* string and memory functions */
extern void getline (char idata *, unsigned char);/* ext. func: input line */
extern void serial_init (void); /* external function: init serial UART */
#define INIT 0 /*task number of task: init */
#define COMMAND 1 /* task number of task: command */ #define CLOCK 2 /* task number of task: clock */
#define BLINKING 3 /* task number of task: blinking */
#define LIGHTS 4 /* task number of task: signal */
#define KEYREAD 5 /* task number of task: keyread */
#define GET_ESC 6 /* task number of task: get_escape */
struct time { /* structure of the time record */
unsigned char hour; /* hour */
unsigned char min; /* minute */
unsigned char sec; /* second */
};
struct time ctime = { 12, 0, 0 }; /* storage for clock time values */
struct time start = { 7, 30, 0 }; /* storage for start time values */
struct time end = { 18, 30, 0 }; /* storage for end time values */
sbit red = P1^2; /* I/O Pin: red lamp output */
sbit yellow = P1^1; /* I/O Pin: yellow lamp output */
sbit green = P1^0; /* I/O Pin: green lamp output */
sbit stop = P1^3; /* I/O Pin: stop lamp output */
sbit walk = P1^4; /* I/O Pin: walk lamp output */
sbit key = P1^5; /* I/O Pin: self-service key input */
char idata inline[16]; /* storage for command input line */
/**********************************************************************/
/* Task 0 'init': Initialize */
/**********************************************************************/
void init (void) _task_ INIT { /* program execution starts here */
serial_init (); /* initialize the serial interface */
os_create_task (CLOCK); /* start clock task */
os_create_task (COMMAND); /* start command task */
os_create_task (LIGHTS); /* start lights task */
os_create_task (KEYREAD); /* start keyread task */
os_delete_task (INIT); /* stop init task (no longer needed) */
}

bit display_time = 0; /* flag: signal cmd state display_time */
/**********************************************************************/
/* Task 2 'clock' */
/**********************************************************************/
void clock (void) _task_ CLOCK {
while (1) { /* clock is an endless loop */
if (++ctime.sec == 60) { /* calculate the second */
ctime.sec = 0;
if (++ctime.min == 60) { /* calculate the minute */
ctime.min = 0;
if (++ctime.hour == 24) { /* calculate the hour */
ct ime.hour = 0;
}
}
}
if (display_time) { /* if command_status == display_time */
os_send_signal (COMMAND); /* signal to task command: timechanged */
}
os_wait (K_IVL, 100, 0); /* wait interval: 1 second */
}
}
struct time rtime; /* temporary storage for entry time */
/***************************************************************************/
/* readtime: convert line input to time values & store in rtime */
/***************************************************************************/
bit readtime (char idata *buffer) {
unsigned char args; /* number of arguments */
rtime.sec = 0; /* preset second */
args = sscanf (buffer, "%bd:%bd:%bd", /* scan input line for */
&rtime.hour, /* hour, minute and second */
&rtime.min,
&rtime.sec);
if (rtime.hour > 23 rtime.min > 59 /* check for valid inputs */
rtime.sec > 59 args < 2 args == EOF) {
printf ("\n*** ERROR: INVALID TIME FORMAT\n");
return (0);
}
return (1);
}
#define ESC 0x1B /* ESCAPE character code */
bit escape; /* flag: mark ESCAPE character entered */
/***********************************************************************************/
/* Task 6 'get_escape': check if ESC (escape character) was entered */ *********************************************************************************/
void get_escape (void) _task_ GET_ESC {
while (1) { /* endless loop */
if (_getkey () == ESC) escape = 1; /* set flag if ESC entered */
if (escape) { /* if escape flag send signal */
os_send_signal (COMMAND); /* to task 'command' */
}
}
}
/**********************************************************************/
/* Task 1 'command': command processor */
/**********************************************************************/
void command (void) _task_ COMMAND {
unsigned char i;
printf (menu); /* display command menu */
while (1) { /* endless loop */
printf ("\nCommand: "); /* display prompt */
getline (&inline, sizeof (inline)); /* get command line input */
for (i = 0; inline[i] != 0; i++) { /* convert to uppercase */
inline[i] = toupper(inline[i]);
}
for (i = 0; inline[i] == ' '; i++); /* skip blanks */
switch (inline[i]) { /* proceed to command function */
case 'D': /* Display Time Command */
printf ("Start Time: %02bd:%02bd:%02bd "
"End Time: %02bd:%02bd:%02bd\n",
start.hour, start.min, start.sec,
end.hour, end.min, end.sec);
printf (" type ESC to abort\r");
os_create_task (GET_ESC); /* ESC check in display loop */
escape = 0; /* clear escape flag */
display_time = 1; /* set display time flag */
os_clear_signal (COMMAND); /* clear pending signals */
while (!escape) { /* while no ESC entered */
printf ("Clock Time: %02bd:%02bd:%02bd\r", /* display time */
ctime.hour, ctime.min, ctime.sec);
os_wait (K_SIG, 0, 0); /* wait for time change or ESC */
}
os_delete_task (GET_ESC); /* ESC check not longer needed */
display_time = 0; /* clear display time flag */
printf ("\n\n");
break;
case 'T': /* Set Time Command */
if (readtime (&inline[i+1])) { /* read time input and */
ctime.hour = rtime.hour; /* store in 'ctime' */
ctime.min = rtime.min;
ctime.sec = rtime.sec;
}
break;
case 'E': /* Set End Time Command */
if (readtime (&inline[i+1])) { /* read time input and */
end.hour = rtime.hour; /* store in 'end' */
end.min = rtime.min;
end.sec = rtime.sec;
}
break;
case 'S': /* Set Start Time Command */
if (readtime (&inline[i+1])) { /* read time input and */
start.hour = rtime.hour; /* store in 'start' */
start.min = rtime.min;
start.sec = rtime.sec;
}
break;
default: /* Error Handling */
printf (menu); /* display command menu */
break;
}
}
}
/**********************************************************************/
/* signalon: check if clock time is between start and end */
/**********************************************************************/
bit signalon (void) {
if (memcmp (&start, &end, sizeof (struct time)) < 0) {
if (memcmp (&start, &ctime, sizeof (struct time)) < 0 &&
memcmp (&ctime, &end, sizeof (struct time)) < 0) return (1);
}
else {
if (memcmp (&end, &ctime, sizeof (start)) > 0 &&
memcmp (&ctime, &start, sizeof (start)) > 0) return (1);
}
return (0); /* signal off, blinking on */
}
/****************************************************************************/
/* Task 3 'blinking': runs if current time is outside start & end time */
/****************************************************************************/
void blinking (void) _task_ BLINKING { /* blink yellow light */
red = 0; /* all lights off */
yellow = 0;
green = 0;
stop = 0;
walk = 0;
while (1) { /* endless loop */
yellow = 1; /* yellow light on */
os_wait (K_TMO, 80, 0); /* wait for timeout: 80 ticks */
yellow = 0; /* yellow light off */
os_wait (K_TMO, 80, 0); /* wait for timeout: 80 ticks */
if (signalon ()) { /* if blinking time over */
os_create_task (LIGHTS); /* start lights */
os_delete_task (BLINKING); /* and stop blinking */
}
}
}
/**********************************************************************************/
/* Task 4 'lights': executes if current time is between start & end time */
/**********************************************************************************/
void lights (void) _task_ LIGHTS { /* traffic light operation */
red = 1; /* red & stop lights on */
yellow = 0;
green = 0;
stop = 1;
walk = 0;
while (1) { /* endless loop */
os_wait (K_TMO, 80, 0); /* wait for timeout: 80 ticks */
if (!signalon ()) { /* if traffic signal time over */
os_create_task (BLINKING); /* start blinking */
os_delete_task (LIGHTS); /* stop lights */
}
yellow = 1;
os_wait (K_TMO, 80, 0); /* wait for timeout: 80 ticks */
red = 0; /* green light for cars */
yellow = 0;
green = 1;
os_clear_signal (LIGHTS);
os_wait (K_TMO, 100, 0); /* wait for timeout: 100 ticks */
os_wait (K_TMO + K_SIG, 250, 0); /* wait for timeout & signal */
yellow = 1;
green = 0;
os_wait (K_TMO, 80, 0); /* wait for timeout: 80 ticks */
red = 1; /* red light for cars */
yellow = 0;
os_wait (K_TMO, 80, 0); /* wait for timeout: 80 ticks */
stop = 0; /* green light for walkers */
walk = 1;
os_wait (K_TMO, 250, 0); /* wait for timeout: 250 ticks */
stop = 1; /* red light for walkers */
walk = 0;
}
}
/**********************************************************************************/
/* Task 5 'keyread': process key stroke from pedestrian push button*/
/**********************************************************************************/
void keyread (void) _task_ KEYREAD {
while (1) { /* endless loop */
if (key) { /* if key pressed */
os_send_signal (LIGHTS); /* send signal to task lights */
}
os_wait (K_TMO, 2, 0); /* wait for timeout: 2 ticks */
}
}
RESULT:
Thus the traffic light control system was implemented using RTX-51 with control over serial port.
LOOK UP TABLE ACCESSING
AIM :
To write a program to obtain value from look up table and test it by giving a particular value.
THEORY :
A look up table is a table containing predefined values . The consequtive values are based upon some function . The look up table is maintained in internal memory. The input and output values are 16 bit wide and are obtained from external memory . Access to external data memory use a 16 bit address ( MOVX @DPTR ) or 8 bit address ( MOVX @Ri ). DPTR is used to hold the address of the first element in the table and the input value is added with the starting address . The resulting address contains the output . Thus it is used as a reference table. The answer is then stored in the desired location of external memory.
In this program, look up table is used to add a value of 100 with the input given. Hence in the table the first 16 bit value is 64 ( hexadecimal form of 100 ) for input 0 and incremented values.
PROGRAM :-
#include
org 0x0000
start : jnb p1.0,start
mov dptr,#0x0a00
movx a,@dptr
mov r1,a
inc dptr
movx a,@dptr
mov dptr,#0x0400
clr c
rlc a
mov r0,a
mov a,dpl
add a,r0
mov dpl,a
jnc l1
inc dph
l1 : clr c
mov a,r1
rlc a
mov r0,a
mov a,dph
add a,r0
mov dph,a
mov a,#00
movc a,@a+dptr
mov r0,a
mov a,#01
movc a,@a+dptr
mov r1,a
mov dptr,#0x0a02
mov a,r0
movx @dptr,a
inc dptr
mov a,r1
movx @dptr,a
sjmp start
end
FUNCTION EDITOR PROGRAM :-
Func void dataf()
{ int i;
long vts;
vts=c :0x0400 ;
for(i=0;i<1024;i++)_wword(vts+(i*2), i+100)
printf(“ done \n ”);
}
SAMPLE INPUT & OUTPUT :-
I / P O / P
1) 0A00������02 0A02������02
0A01������03 0A03������67

2) 0A00������01 0A02������01
0A01������06 0A03������6A
RESULT :



READING THE RESISTOR VALUE
WITHOUT MAKING USE OF A/D CONVERTER
AIM:
To find the resistor value without making use of A/D converter.
APPARATUS REQUIRED:
AT89C51 ---- 1
2SC2518 ---- 1
POTENTIOMETER (10Kilo ohms) ---1
CAPACITOR (10Micro farad) ---- 1
THEORY:
In some applications it is necessary to generate an output which corresponds to the instantaneous resistance value . The circuit which uses a High speed switching Transistor .The Timer is used to generate a 1second time period square pulse square wave. This is used to charge and discharge a capacitor through the resistance, whose value is to be found. The resultant waveform obtained across the capacitor is made use of to calculate the duty cycle. The range of Time periods for the output waveform is from 2milliseconds to 24.2 microseconds. This is converted into a scale of 0-100 and the obtained Duty cycle is used to generate a pulse of time period 24.2 microseconds. This is the control signal proportional to the resistance value. Any change in the value of the Potentiometer results in a change of duty cycle and hence the generated signal. This is used in the real time applications like JOYSTICK controls.
PROGRAM:
#include
ctr data 0x20
; p1.0- i/p p1.1- o/p(tran base)
;p1.2-pwm o/p
;ctr-reg of specifying pwm
org 0x0000
ljmp ar
org 0x000b
jb p1.1,st
jnb p1.0,st
mov ctr,a
setb p1.1
st: cjne a,ctr,ed
clr p1.2
ed: inc a
cjne a,#100,edr
inc r0
clr a
setb p1.2
cjne r0,#41,edr
mov r0,#0
clr p1.1
edr: reti
org 0x0100
ar: mov ctr,#50
clr a
mov tmod,#00000010b
mov th0,#15
mov tl0,#15
mov ie,#10000010b
setb p1.2
setb tr0
sjmp $
end
RESULT:
Thus the ALP to find the resistor value of an unknown resistor was written, executed, programmed and the hardware circuit was tested.



STEPPER MOTOR CONTROL
AIM:
To simulate a stepper motor control according to the input given at port2 (shown below) of the Micro controller – 8051.The Sequence for the Half step and Full step (Left) is given below. For Right rotations reverse the sequence.
P2.0 P3.0 D
LEFT
P3.1 C
P2.1
RIGHT
P3.2 B
P2.2
FULL / HALF P3.3 A
GND

Half Step
Hex
A
B
C
D
09
1
0
0
1
08
1
0
0
0
0A
1
0
1
0
02
0
0
1
0
06
0
1
1
0
04
0
1
0
0
05
0
1
0
1
01
0
0
0
1
Full Step
Hex
A
B
C
D
09
1
0
0
1
0A
1
0
1
0
06
0
1
1
0
05
0
1
0
1

PROGRAM:
#include
org 0x0000
ljmp start
org 0x050
start :
jnb p2.0,left
jnb p2.1,right
jmp start
left :
jnb p2.1,start
jnb p2.2,l_full
mov r1,#1000b
mov dptr,#0x0100
l_half: mov a,#0x00
movc a,@a+dptr
mov p3,a
call delay
inc dptr
djnz r1,l_half
sjmp start
l_full:
mov dptr,#0x0100
mov r1,#0100b
l1 : mov a,#0x00
movc a,@a+dptr
mov p3,a
call delay
inc dptr
inc dptr
djnz r1,l1
sjmp start
right :
jnb p2.2,r_full
mov r1,#1000b
mov dptr,#0x0100
mov r0,#0000000b
l2 : mov 2fh,r0
cpl 0x2f.0
cpl 0x2f.1
cpl 0x2f.2
mov a,0x2f
movc a,@a+dptr
mov p3,a
call delay
djnz r1,l3
sjmp start
l3 : inc r0
jmp l2
r_full:
mov r1,#0100b
mov dptr,#0x0100
mov r0,#00000001b
l4 : mov 2fh,r0
cpl 0x2f.0
cpl 0x2f.1
cpl 0x2f.2
mov a,0x2f
movc a,@a+dptr
mov p3,a
call delay
djnz r1,l5
sjmp start
l5 : inc r0
inc r0
jmp l4
delay :
mov 20h,#61
mov tmod,#00000001h
mov th0,#0xf0
mov tl0,#00h
mov ie,#10000010b
setb tr0
ret
org 0x000b
clr tf0
mov th0,#0xf0
djnz 20h,loop1
mov 20h,#61
mov c,p1.0
cpl c
mov p1.0,c
loop1: reti
org 0x0100
db 1001b
db 1000b
db 1010b
db 0010b
db 0110b
db 0100b
db 0101b
db 0001b
end
Result:
Thus the stepper motor control was simulated using Keil-51 for both clockwise (Left) and Anticlockwise (Right) rotations in Full and Half step modes

No comments: