Documente Academic
Documente Profesional
Documente Cultură
Write an Embedded C Program to read the switches and output the same on LEDs, using
logic controller interface.
#include <reg51.h>
int main(void)
{
P1 = 0xFF ; P0 = 0x00; //Configure P1 & P0 as input and outputs
while(1)
{
P0 = P1;
}
}
Write an Embedded C program to implement Decimal UP / Decimal Down / Ring Counter
using Logic Controller Interface module.
Embedded C Program:
#include <reg51.h>
typedef unsigned char tByte; //8 bits
typedef unsigned int tWord; //16 bits
void delayMs(tWord); //Delay function
sbit key1 = P3^2;
sbit key2 = P3^3;
sbit key3 = P3^4;
int main(void)
{
tByte count=0,i,temp;
tWord delay = 1000; //Delay in milliseconds used for counting
P0 = 0x00; //Clear all LEDs
while(1)
{
/*Program while loop, code in this loop will run continuously. When one of the
keys is pressed, run the appropriate counter and exit back to this loop to check for
key press again. When a key is pressed, the input value to the microcontroller will
be 0.*/
if(key1 == 0)
{
/* Decimal Up counter loop, use a for loop to increment the counting variable
from 0 to 99, and output the number to P0, the output port used for the interface
module.To display as decimal in the leds, first take the first digit of the number,
next take the second digit and set it as the upper nibble by first left shifting it 4
times and then ORing it. */
if(key3 == 0)
{
while(1)
{
for(i = 0; i < 8; i++)
{
P0 = 0x01<<i; // 0x01 means 0000 0001 bit pattern
delayMs(delay);
}
}
}
}
}
void delayMs(tWord x)
{
tWord i;
while(x--)
for(i=0;i<120;i++);
}
Microcontroller and Embedded Systems Lab [16CS53]
Write an Embedded C program to read the status of 8 inputs bits from 8bit switch and display FF’
if it is even parity otherwise display 00. Also display number of 1’s in the input data on the LED
outputs, using Logic Controller interface module.
Embedded C Program:
#include<reg51.h>
int main(void)
{
tByte temp,count;
while(1)
{
temp = readInput(); // read the 8 bit data from logic controller
count = countOnes(temp); // count the number of 1's in the 8 bit data
delayMs(1000);
P0 = count; // now display count of 1's for next 1 second
delayMs(1000);
}
}
tByte countOnes(tByte x)
{
tByte i,count = 0;
for(i = 0 ;i < 8 ;i++) // loop to check 8 different bits of a number
{
if(x & (0x01<<i))
count++; // keep incrementing whenever any bit is found as 1
}
return count; //return count of 1's in a given number
}
Microcontroller and Embedded Systems Lab [16CS53]
tByte readInput(void)
{
return P1; //assume P1 is connected to 8 switches
void delayMs(tWord x)
{
tByte i;
while(x--)
for(i=0;i<120;i++);
}
Microcontroller and Embedded Systems Lab [16CS53]
Write an Embedded C program to read the status of two 8-bit inputs (X and Y) and display the
result X*Y using the interface module.
Embedded C Program:
#include<reg51.h>
int main(void)
{
tWord a = 0,b = 0,c = 0; //equation is c = a * b
P0 = 0x00; //Clear all LEDs
while(1)
{
if(!key1)
{
/* When key1 is pressed, the variable a is updated with the current value of the
input port. The updated value of a is displayed for half a second. */
a = readInput();
P0 = a;
delayMs(500);
P0 = 0x00;
delayMs(500);
}
if(!key2)
{
/* When key1 is pressed, the variable b is updated with the current value of the
input port. The updated value of b is displayed for half a second. */
b = readInput();
P0 = b;
delayMs(500);
P0 = 0x00;
delayMs(500);
}
Microcontroller and Embedded Systems Lab [16CS53]
if(!key3)
{
/* When key3 is pressed, the result of multiplication of the current values of a and
b is assigned to c. First the LSB of the result is displayed by ANDing the result
with 0xFF .Next the MSB is displayed by right shifting the result 8 times and
writing it to P0. */
c = a*b;
P0 = c & 0xFF;
delayMs(1000);
P0 = c >> 8;
delayMs(1000);
P0 = 0x00;
}
}
}
tByte readInput(void)
{
void delayMs(tWord x)
{
tByte i;
while(x--)
for(i=0;i<120;i++);
}
Microcontroller and Embedded Systems Lab [16CS53]
Program to implement the single digit decimal counter 0-9, using look up table
segment_codes[ ] = {0xC0,0xFA……}; //Store the segment code of 0 to 9 in the look up table
int i;
while (1)
{
for(i=0 ; i<=9 ; i++)
{
P0 = segment_codes[i]; // send the next decimal digit
delay(1000); // delay of 1 second
}
}
Microcontroller and Embedded Systems Lab [16CS53]
When number of seven segment digits is more, using this method requires many ports. Following
circuit solves this problem using set of shift registers. Serial In Parallel Out mode of Shift
Register (74164) is used to send 8 bits of data to seven segment display. Seven segment display
used is of common anode type i.e. we have to send 0 to make corresponding segment ON and 1
to make it OFF.
High
Low
Microcontroller and Embedded Systems Lab [16CS53]
This is B0 in hexadecimal. To send B0H we have to start sending the bits from MSB onwards i.e
D7 first, D6 next and so on with D0 being the last.
Clock pulses are required to clock in the data, 8 clock pulses for one byte of data. As shift
registers are cascaded, 8*4=32 clocks are required to clock in 4 bytes of data. To send “1234”, first we
have to send ‘1’, then ‘2’,‘3’ and lastly ‘4’. All the shift registers are cascaded, the data is fed to the shift
register using serial in parallel out method. The Data and Clock pins are connected to D0 and D1 of the
output port respectively.
Microcontroller and Embedded Systems Lab [16CS53]
Write an Embedded C program to display messages “FIRE” & “HELP” on 4 digit seven segment
display alternately with a suitable delay.
Embedded C Program:
#include <reg52.h>
int main(void)
{
int i = 0;
tByte help[4] = {0x89,0x86,0xC7,0x8C};
tByte fire[4] = {0x8E,0xCF,0xAF,0x86};
P0 = 0x00;
while(1)
{
for(i=0;i<4;i++)
writeSeg(help[i]);
delayMs(1000);
for(i=0;i<4;i++)
writeSeg(fire[i]);
delayMs(1000);
}
}
void writeSeg(tByte x)
{
tByte i;
for(i = 0; i < 8; i++)
{
if(x & (0x80>>i)) // extracting and sending the bits one by one
DAT = 1; // from MSB to LSB
else
DAT = 0;
CLK = 0; //generate one clock pulse to push the data to the shift register
CLK = 1;
}
}
Microcontroller and Embedded Systems Lab [16CS53]
Stepper motors move in steps unlike dc motors, under the digital control. The term stepper motor resolution
refers the angular movement, in degree per step. Also stepper motors direction of movement can be digitally
controlled.Stepper motors finds application – in disk drives, printers, plotters, CNC machines, robotics etc.
Stepper motors – has two parts a) rotor made up permanent magnet or soft iron b) stator – surrounding the
rotor. Stator is constructed with the number of coils / windings, which on energising (supplying current) acts
like a electromagnet.
As shown in the above diagram rotor (magnet) aligns to a particular position based on the energised coil
(acting as electromagnet) as S & N poles gets attracted. When we change energisation sequence, rotor aligns
to new position, that is how the stepper motors movement is realised. In actual stepper motors – the stator
and rotor poles are teethed to achieve higher resolution (more steps per revolution)
Stepping Modes: Stepper motors movement is achieved by employing one of the following coil energising sequences.
a) Full drive (2 coils are energised at a time, more torque) – 4 step sequence
b) Wave drive (one coil only energised at a time, less torque) – 4 step
c) Half drive (here step angle reduces by half, if 1.8” is step angle, here it becomes 0.9”. it is a combination of the
above two types, hence 8 step sequence and torque is in between above types)
Total number of steps for one revolution = 200 steps (200 teeth shaft)
Step angle = 360°/200 = 1.8° ;
𝑎𝑛𝑔𝑙𝑒
𝑠𝑡𝑒𝑝𝑠 =
𝑠𝑡𝑒𝑝 𝑎𝑛𝑔𝑙𝑒
Example1: to rotate stepper motor by 90 degree, requires 90/1.8 = 50 steps.
Example2: rotating 20 steps will result in 25 X 1.8 = 45 degree rotation)
Speeds: Use appropriate delay in between consequent steps, to achieve different speeds
𝑟𝑝𝑚 𝑥 𝑠𝑡𝑒𝑝𝑠 𝑝𝑒𝑟 𝑟𝑒𝑣
𝑠𝑡𝑒𝑝𝑠 𝑝𝑒𝑟 𝑠𝑒𝑐𝑜𝑛𝑑 =
60
rpm – revolutions per minute
Driver Circuits: 2Phase, 4winding stepper motor is used and 12v power is required to
drive the stepper motor. Digital input generated by the microcontroller, is used to
drive and control the direction and rotation of stepper motors. As microcontroller ports
do not drive the current required for motor windings, transistors (like SL100) are used to
derive the required current. If it is required to drive bigger/higher torque stepper
motors only change is- use MOSFETS or stepper driver ICs to drive motors instead of
normal transistors.
Microcontroller and Embedded Systems Lab [16CS53]
Write an Embedded C program to rotate stepper motor in clock wise and in anti-clock
wise direction for “N” steps (Number of steps or angle to rotate to be specified)
Embedded C Program:
#include <reg52.h>
//name the windings, assume P0 bits 7,6,5,4 are connected to stepper windings
sbit W3 = P0^7;
sbit W2 = P0^6;
sbit W1 = P0^5;
sbit W0 = P0^4;
main()
{
while(1)
{ W3=1; W2=0; W1=0; W0=0; delayMs(5); if(--no_of_steps_clk==0) break;
W3=0; W2=1; W1=0; W0=0; delayMs(5); if(--no_of_steps_clk==0) break;
W3=0; W2=0; W1=1; W0=0; delayMs(5); if(--no_of_steps_clk==0) break;
W3=0; W2=0; W1=0; W0=1; delayMs(5); if(--no_of_steps_clk==0) break;
}
while(1)
{ W3=0; W2=0; W1=0; W0=1; delayMs(5); if(--no_of_steps_anticlk==0) break;
W3=0; W2=0; W1=1; W0=0; delayMs(5); if(--no_of_steps_anticlk==0) break;
W3=0; W2=1; W1=0; W0=0; delayMs(5); if(--no_of_steps_anticlk==0) break;
W3=1; W2=0; W1=0; W0=0; delayMs(5); if(--no_of_steps_anticlk==0) break;
}
while(1); //end of program, stay here
}
DAC INTERFACE
DAC refers to Digital to Analog Converter, used to convert digital values to corresponding analog values.
DAC 0800 – an 8 bit DAC from National semiconductors, with the settling time around 100 n sec. P0 of
8051 is connected to DAC to generated 8 bit digital data,00-FF (refer fig below). Since DAC 08
generates corresponding current output, operation amplifier 741 is used to convert current to analog
voltage, 0-5Volts.
Digital I/P : 00 to FF , corresponding Analog O/P : 0V to 5V
Resolution = (5/256) ≈ 20mV ,An 8 bit DAC has 28 = 256 possible output voltage / current levels, so its resolution is 1
part in 256. IF digital input is 00 to FF is mapped to 0 to 5V o/p, then 20mv is the resolution. (more the number of
bits, better is the resolution)
{ Note:
- Supply voltage +5V at Vcc, V- is connected to – 12 V
- Iout(+) and Iout(–) gives current output corresponding to digital i/p, so op-amp is used to convert the
current o/p to voltage o/p – Vout
- For 8 bit DAC, the o/p current I0 is a function of the binary nos at the data i/p pins D0 – D7
𝐷0 𝐷1 𝐷7
𝐼0 = 𝐼𝑛𝑓 × ( + + − − − ∓ )
256 258 2
Inf – is the ref i/p current, depends on Vref + and vref – Voltages
}
𝟐𝟓𝟔
Calculate digital i/p corresponding to 3V Anlalog o/p – × 𝟑 = 𝟏𝟓𝟑 = (𝟗𝟐)𝑯
𝟓
=1001 0010B
Microcontroller and Embedded Systems Lab [16CS53]
Programs to generate different waveforms using DAC-08:
Two techniques are generally used, one without the look up table and other calculate at run time in the
program. Both the types are discussed below. (c program is used for look up table and assembly for other
type)
for(i=0;i<256;i=i++)
P0 = 255- i ; // falling of trinagle
}
(Note : by calling delay in between outputing datas to DAC, the frequency / time period of wave forms can be altered)
Microcontroller and Embedded Systems Lab [16CS53]
In look up table technique, is used when lot of computation is involved in getting the next value to be
outputted to DAC. In the previous programs, it was just addition or subtraction. In case of sine wave form,
sin(angle) is to computed for different angles. Sine function computation by the microcontroller (unlike
normal PCs) may take more and varying times. Hence, what we do is, compute digital values corresponding to
different sine angles and store in the table. At runtime, just take the values from the table and throw these
values to DAC to get accurate waveforms.
Output the above values in the reverse order to get other portion of the top half cycle,
(add 128 for top half cycle, and subtract from 128 for the lower half cycle, refer the table
declaration)
Microcontroller and Embedded Systems Lab [16CS53]
Write an Embedded C program to generate sine waveform/ half rectified sine waveform/
full rectified sine waveform using DAC module.
Program:
#include <reg52.h>
// total 30 values
// total 60 values
tByte i=0,j=0,k=0;
key1=key2=key3=1; //configure as inputs
while(1)
{
//full rectified sine waveform
if(key1==0)
while(1) //continuously output the data in the table to the DAC connected to P0
{
P0 = dac_datas_sine_fullrectified[i++];
if(i==30) i=0; // total of 30 digital values are stored in the table to
// produce full rectified sine wave
if(key1==0 || key2==0 || key3==0)break; //check for the key press
};
if(key2==0)
while(1)
{
P0 = dac_datas_sine_full[j++];
if(j==60) j=0; // total of 30 digital values are stored in the table to
// produce full sine wave
if(key1==0 || key2==0 || key3==0)break;
}
if(key3==0)
while(1)
{ P0 = dac_datas_sine_halfrectified[k++];
if(k==60) k=0; // total of 30 digital values are stored in the table to
// produce half rectified sine wave
if(key1==0 || key2==0 || key3==0)break;
};
}
}
Microcontroller and Embedded Systems Lab [16CS53]
MATRIX KEYBOARD INTERFACE
- One port line is required to interface one key
- [when key is not pressed, port line carries logic 1, ∵it is pulled upto 5V, when
key is pressed, port line gets grounded, hence logic ∅ is applied] . When number of keys increases,
requirement of no. of port. Lines increases, hence concept of matrix arrangement of keys (matrix
keyboard) are used.
Interfacing….
Let us interface 4x4 matrix keyboard to 8051, using P0 and P1 ports, as shown below. Here keys are
place at the intersection of rows and columns, when key is pressed – corresponding row and column
gets shorted and the voltage (logic 0 or 1) of row is transferred to corresponding column.
Debouncing ( ≃ 20 ms delay) overcomes two things – false / wrong key press identification and single
key press treated as multiple key presses.
Write an Embedded C program to interface 4 X 4 matrix keyboard using lookup table and
display the key pressed on the Monitor.
Program:
#include <reg52.h>
main()
{
tByte row_pos, col_pos;
//configure the serial port & the timer1 used for 9600 baud generation
SCON = 0x50; TMOD = 0X20; TH1 = -3; TR1 = 1; TI = 1;
Microcontroller and Embedded Systems Lab [16CS53]
while(1)
{ while(1)
{ //select the first row & check for key press in row0
row0=0; row1=1; row2=1; row3=1; row_pos=0;
if(col0==0){col_pos=0;break;}
if(col1==0){col_pos=1;break;}
if(col2==0){col_pos=2;break;}
if(col3==0){col_pos=3;break;}
//select the second row & check for key press in row1
row0=1;row1=0;row2=1;row3=1;
row_pos=1; if(col0==0){col_pos=0;break;}
if(col1==0){col_pos=1;break;}
if(col2==0){col_pos=2;break;}
if(col3==0){col_pos=3;break;}
//select the third row & check for key press in row2
row0=1;row1=1;row2=0;row3=1; row_pos=2;
if(col0==0){col_pos=0;break;}
if(col1==0){col_pos=1;break;}
if(col2==0){col_pos=2;break;}
if(col3==0){col_pos=3;break;}
//select the fourth row & check for key press in row3
row0=1;row1=1;row2=1;row3=0; row_pos=3;
if(col0==0){col_pos=0;break;}
if(col1==0){col_pos=1;break;}
if(col2==0){col_pos=2;break;}
if(col3==0){col_pos=3;break;}
}
delayMs(20); //debounce
/* use the following lines if you want to output the key code on P0 for a second
P0 = keys[row_pos][col_pos]; // output the keycode on P0 for 1sec
delayMs(1000); */
LCD INTERFACE
LCD’s are preferred to seven segment displays because of their versatility and capability
to house more information. 2 line (16x2) is the most popular, low cost character oriented LCD,
suitable for understanding the working and programming of LCD. You have seen LCD modules
used in many of the electronics devices like coin phone, billing machine and weighing machines.
It is a powerful display options for stand-alone systems. Because of low power dissipation, high
readability, flexibility for programmers, LCD modules are becoming popular.
LCD consists of DDRAM, CGROM, Shift registers, bit/pixel drivers, refreshing logics
and lcd controller. The data to be displayed on lcd, is to be written on to the DDRAM-display
data Ram using the ascii format. CGROM-Character generator rom, contains dot/pixel patterns
for every character to be displayed (pre programmed). Shift registers are used to convert
CGROM parallel data to serial data(serializing), drivers are required to drive (ON/OFF) the bits,
refreshing logics are required to hold the display data, as the dots are displayed row by row basis
continuously, like in CRT.
Microcontroller and Embedded Systems Lab [16CS53]
Instruction D7 D6 D5 D4 D3 D2 D1 D0 Description
Programming LCD
1. Configure the LCD (i.e initialization) for different parameters/settings, by writing series of
commands (command bytes) like (refer above table for preparing the commands)
Function set command(0x38)
Display On command(0x0C)
Clear display (0x01)
2. Writing actual string data to LCD, character by character, (by default characters are displayed
from line1 first column position, we can issue DDRAM address command - 0x80 + char pos, for
first line, 0xc0 + char pos, for second line).
Microcontroller and Embedded Systems Lab [16CS53]
Steps to write command to LCD
* Place command byte on data bus
* make RS = 0
* make En = 1 (450 ns) (min) then make En=0 (high to low pulse)
Extra Information :
Note: The data to be displayed on LCD, is to be written to the display data RAM using ASCII format,
i.e. if ‘A’ is to be displayed, ASCII code of A. i.e. 65 to be written.
The first 40 bytes (00 – 39 decimal) of RAM allocated for 1st Line,
another 40 bytes (40H – 40Hto 79 decimal) of RAM to 2nd line.
Also first 16 locations are allocated to corresponding 16 display digits of 1st line.
If we want to write to the 4th digit, we have to write to the 4th location in DDRAM.
Even though display shows 16 digits at a time, memory holds 40 characters for each line,
so using shift command we can rotate the display to make all the 40 characters visible.
Microcontroller and Embedded Systems Lab [16CS53]
#define LCDData P0
// function prototypes
void LCD_DispStr(tByte line_no,char* str);
void LCD_Init(void);
void LCD_Command(tByte command);
void LCD_Data(tByte databyte);
void enpulse(void);
void delay(tByte val);
main()
{
tByte str1[] = "Hello..RVCE..";
tByte str2[] = ".....CSE. ... ";
//configure the pins
RS=RW=E = 0; // as output
LCDData = 0; // as output
LCD_Init();
LCD_DispStr(1,str1);
LCD_DispStr(2,str2);
while(1); // stay here indefinitely
}
void LCD_DispStr(tByte line_no,char* str)
{
tByte i=0;
if(line_no==1)
LCD_Command(0x80); // command to set the memory ptr to first line
else
LCD_Command(0xc0); // cmd to set the mem ptr to first char of second line
while(str[i]!='\0')
{
LCD_Data(str[i]);i++;
if(i==16)break; // as max of 16 chars per line
}
}
Microcontroller and Embedded Systems Lab [16CS53]
void LCD_Init(void)
{
LCD_Command(0x38); //function set- 2 line display,byte
mode LCD_Command(0x0c); //display on
LCD_Command(0x01); //clear the display
}
void LCD_Command(tByte command) //to send command to the lcd
{
RS=0;
RW=0;
LCDData = command;
enpulse(); // generate enable pulse
delay(50);
}
void LCD_Data(tByte databyte) //to send data to the lcd
{
RS=1; //data is written
RW=0;
LCDData = databyte;
enpulse();
delay(50);
}
void enpulse(void) // to generate enable pulse 1 to 0, on Enable pin
{
E=1;
delay(2);
E=0;
delay(2);
}
void delay(tByte val)
{
tByte i;
for(i=0 ; i<val ; i++)
{ _nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
Microcontroller and Embedded Systems Lab [16CS53]
Microcontroller and Embedded Systems Lab [16CS53]
ADC’s find applications in weight measurement, temp measurement, speed measurement etc.
In general different physical quantities like temp, humidity are converted by sensors into
electrical domain i.e. voltage or current, which in turn fed to ADC for conversion to digital
domain, for doing different types of processing by microcontrollers/microprocessors. ADC’s
are built using different techniques like dual shape conversion, successive approximation type
etc. (since 8051 don’t have internal ADC, external ADC chips are used).
A typical ADC has analog input/s, digital outputs and two handshaking signals.
SOC (Start of conversion): used to start the conversion. (o/p from microcontroller).
EOC (End of Conversion), ADCs take some time to convert from analog to digital. EOC
signal generated by ADC has to polled by microcontrollers to check for completion of
conversion.(i/p to microcontroller).
Interfacing to 8051
Here, ADC0809, a popular unipolar 8 bit successive approximation type ADC, is used to
interface to 8051. This ADC provides 8 analog inputs/channels. One of the 8 inputs, is
selected and used for conversion using channel selection inputs ABC and ALE signal of
ADC.
P0 is connected to digital outputs of ADC, P1.0 & P1.1 connected to SOC and EOC. They
are active high signals. P2.0-P2.2 are connected to channel selection inputs, P1.2 is connected
to ALE.
Microcontroller and Embedded Systems Lab [16CS53]
Resolution of 8 bit ADC: 5V / 28 = 20mv (min of 20mv is required to make one bit change)
For a given analog input, digital value = (256 / 5) * input
Example: calculate the digital output, if input is 2.5V: 256/5 * 2.5 = 128
Microcontroller and Embedded Systems Lab [16CS53]
DC Motor Control
Write a program to control the direction, if control switch is OFF motor rotates in
clockwise, else rotates in anticlockwise.
DC Motor speed control: Achieved using PWM (pulse width modulation) by changing
width of the pulse, we can increase / decrease the power to the motor for a given load.
𝑇𝑂𝑁
Effective dc voltage applied to DC motor = × 𝑠𝑢𝑝𝑙𝑙𝑦 𝑣𝑜𝑙𝑡𝑎𝑔𝑒 (𝑠𝑎𝑦 12𝑉)
𝑇
𝑇 1
(if 𝑇𝑂𝑁 = 𝑇, 𝐹𝑢𝑙𝑙 𝑝𝑜𝑤𝑒𝑟 𝑖. 𝑒. 12𝑉, 𝐼𝑓 𝑇𝑂𝑁 = 2 , 50% 𝐷𝐶 (2 𝑝𝑜𝑤𝑒𝑟)
By controlling 𝑇𝑂𝑁 time, effective DC voltage applied to motor is changed, hence the speed.
Microcontroller and Embedded Systems Lab [16CS53]
In the below circuit, 8051 drives the 12V DC motor at different speeds, in one direction.
[Note: Diode across motor (refer above figure)acts as freewheeling diode to achieve faster
switching. Capacitor is used to reduce EMI- electro-magnetic interference. Opto-isolator is
used to isolate EMI produced by motor on controller, so power supply to motor and controller
has to be different.]
Write embedded c program to drive the motor to run in different speeds using PWM.
#include <reg52.h>
#include <intrins.h>
sbit motor = P0^0; // 1- motor on, 0 – motor - off
void main( )
{
unsigned int count1=100,count2=50; // 50 % duty cycle
while(1)
{
motor = 1;
delay(count1);
motor = 0;
delay(count2);
}
}
void delay(tByte val)
{
tByte i;
for(i=0 ; i<val ; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
Note:
count1 = 33, count2 = 67- 33% duty cycle
count1= 70, count2 =30 - 70% duty cycle
Microcontroller and Embedded Systems Lab [16CS53]
8051 supports full duplex serial asynchronous communication. Serial port i.e UART
(universal asynchronous receiver and transmitter) is built in to the 8051 chip.
Working of Serial Port – Serial port/UART has two sections, transmitter section and
receiver section. UART transmitter section has parallel in serial out shift register, which
converts parallel 8 bit data to serial data, add start and stop bits (optional parity bit) and send
them out on T X D line at specified baud rate ( programmable) as shown in the below figure.
Adding of start and stop bits are referred as framing of data. Baud rate refers to the rate at
which serial data is transmitted.
UART receiver section has serial in parallel out shift register, which collects serial data
through R X D line, de-frames it by removing START and STOP bits and gives parallel 8 bit
data.
8051 serial port supports many modes of serial communication, but the above shown 10bit is
most popular mode. Generally, data represents ASCII code of the character to be transmitted.
Provision is there to add parity bit (odd/even) for error checking in transmission.
the device/peripherals manufacturers provide RS 232 serial ports. So, to connect 8051 TTL
compatible serial port to RS 232 compatible serial ports, one needs translation device. 8051
can be connected to Rs. 232 ports like PC’s COM port by using RS-232 converters /
translators (TTL to RS-232, vice versa). MAX232 IC is a popular RS-232 converter used in
the industry and their connections are shown below.
Step1: Configure the mode and other configurations of serial port using SCON Register.
Mode1 (10 bit mode- 1start,8 data, 1stop, with programmable baud rate), is very commonly
used to communicate with the PC. To configure in Mode1 and enable receiver section, write
MOV SCON, # 50H (in Assembly)
Or
SCON = 0x50; (in C)
Microcontroller and Embedded Systems Lab [16CS53]
Step 2: Baud Rate - Generate Rx & Tx clock using timer1 (mode 2 – 8 bit auto reload)
Let us say, to produce 9600 baud ( bits per second), 28800 has to be divided by 3
28800 / 9600 = 3, hence the timer 1 should over flow after every 3 clock pulses.
Hence, TH1 is to be loaded with,
TH1 = 0x FD ; // FF – 3 = FC + 1 = FD ( 255 – 3 = 252+1 = 253 i.e 0xFD)
Or
TH1 = -3; (in C, -3 is automatically converted and stored as FD, 2’s compliment)
SBUF is used for both receiving and transmitting. (It is designed such that, when we write to
SBUF, it goes to Tx section, when we read from SBUF, it comes from Rx section.
TI indicates, Tx is ready for transmitting next character (in polling we have to make it zero
and then write next character, in interrupt method it is automatically cleared by RETI
Microcontroller and Embedded Systems Lab [16CS53]
instruction ). RI indicates, Rx is ready with the data, i.e. it has received an byte of (in polling
we have to make it zero and then wait for receiving the next character, in interrupt method it
is automatically cleared by RETI instruction).
Write a program to send a string to PC using serial port
#include <reg52.h>
void main( )
{
char str[20] = “rvce cse”;
unsigned char i=0;
SCON = 0X50; // 10 BIT MODE – 1START,8DATA,1STOP
TMOD = 0X20;TH1= -3;TR1=1; - SET TIMER1 FOR 9600 BAUD
TI=1;
while( str[i] != ’\0’ )
{
while(TI==0); // wait for transmitter to complete the previous operation
SBUF = str[i++]; // transmit the next character
TI = 0 ; // CLEAR MANUALLY TI TO ‘0’
}
}
#include <reg52.h>
void main( )
{
char str[20]; // space to store the string, received from serial port
unsigned char i=0;
SCON = 0X50; // 10 BIT MODE – 1START,8DATA,1STOP
TMOD = 0X20;TH1= -3;TR1=1; - SET TIMER1 FOR 9600 BAUD
do
{
while(RI==0); // wait for receiver to receive the character
str[i] = SBUF; // read the received character
RI = 0 ; // CLEAR MANUALLY TI TO ‘0’
}
while(str[i++]!=’\0’); // terminate the loop if NULL character is received
while(1);
}
Microcontroller and Embedded Systems Lab [16CS53]
Microcontroller port pin cannot be directly used to switch ON/OFF (control) high power
ac or dc devices like bulb, fan, dc motor, solenoids etc. as the port pins don’t drive the
required high current or voltages required by high power devices.
Relays are used to interface high power devices to microcontrollers. There principle of
working is shown in the below figure. The microcontroller port pins drives the Relay
input section, where as its output section is connected to high power device. Hence,
relays provide isolation between microcontroller and high power source.
It provides complete isolation between diode and transistor. Hence, input circuit which
drives the diode do not get affected by the voltage/ current spikes produced in the o/p
circuit. Also opto-isolators can be sued to interface devices (like sensors) working at
different voltage levels to Microcontroller.
When metal object comes to the proximity of the sensor, it detects and generates logic 1
output. For the sensor to work, it needs dc supply (+5 to +30V).
Write a program to read sensor connected to P0.1 and make ac device (bulb) ON/OFF
co connected to P0.0 through relay.
#include <reg52.h>
Sbit SENSOR = P0^1;
Sbit RELAY = P0^0;
Microcontroller and Embedded Systems Lab [16CS53]
void main( )
{
while(1)
{
if(SENSOR == 1)
RELAY = 1;
else
RELAY = 0;
}
}