0% found this document useful (0 votes)
65 views14 pages

Bee403 Vishal

The document provides programming examples for microcontrollers, specifically focusing on the 8051 architecture using Assembly and C. It includes timer programs, displaying counts in ASCII, generating waveforms, and handling time calculations. The content is structured as a guide for students in the Electrical and Electronics Engineering department at R R Institute of Technology.

Uploaded by

vishald.21.deee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views14 pages

Bee403 Vishal

The document provides programming examples for microcontrollers, specifically focusing on the 8051 architecture using Assembly and C. It includes timer programs, displaying counts in ASCII, generating waveforms, and handling time calculations. The content is structured as a guide for students in the Electrical and Electronics Engineering department at R R Institute of Technology.

Uploaded by

vishald.21.deee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PKM Educational Trust ®

R R INSTITUTE OF TECHNOLOGY
AN AUTONOMOUS INSTITUTIO N UNDER VTU APPROVED BY AICTE, NEW DELHI & GOVERNMENT OF KARNATAKA ACCREDITED BY NAAC with A+ & NBA

MICROCONTROLLERS
SUB CODE : BEE403
DEPARTEMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNING

“Programming in Assembly and C”

Presented by UNDER THE GUIDANCE


VISHAL KUMAR (1RI24EE430) PROF. PRADEESHA J
VISHNU R (1RI24EE431)

25-04-2025 R R INSTITUTE OF TECHNOLOGY


Contents
1. Timer Program 1

2. Display Count in ASCII

3. Display Seconds and Minutes

4. Generate 2KHz Wave

25-04-2025 R R INSTITUTE OF TECHNOLOGY 2


8051 Timer
Programming in
Assembly and C
08-05-2025 R R INSTITUTE OF TECHNOLOGY 3
#include <reg51.h>
void main(void)
{
T0=1; /* make T0 an input */
TMOD=0x05; /* set count to 0 */
TL0=0; /* set count to 0 */
TH0=0; /* set count to 0 */
while(1) /* repeat forever */
{
TR0=1; /* start timer */
do
{
P0=TL0; /* place value of TL0 on port 0 */
P1=TH0; /* place value of TH0 on port 1 */
}
while(TF0==0); /* wait here */
TR0=0; /* stop timer */
TF0=0; /* clear flag */
}
}

08-05-2025 R R INSTITUTE OF TECHNOLOGY 4


Example 5.2.10
Assume that a 2 Hz external clock is
being fed into pin T1. Write a C
program for counter 1 in mode 2 (8-bit
auto reload) to display the count in
ASCII. The 8-bit binary count must be
converted to ASCII. Display the ASCII
digits (in binary) on P0 serially. Display
least significant digit first.

08-05-2025 R R INSTITUTE OF TECHNOLOGY 5


To display the TL1 count we must convert 8-bit binary data to ASCII.

#include <reg51.h>
void BinToASCII(unsigned char);
void main()
{
unsigned char value;
T1=1;
TMOD=0x06;
TH0=0;
while(1)
{
do
{
TR0=1;
value=TL0;
BinToASCII(value);
}
while(TF0==0);
TR0=0;
TF0=0;
}
}
08-05-2025 R R INSTITUTE OF TECHNOLOGY 6
void BinToASCII(unsigned char value)
{
unsigned char A byte, i,Q;
unsigned char R[3];
A byte = 0x92;
i=0;
do
{
Q=A byte/10; /* divide by 10 */
R[i]=Abyte%10; /* find remainder and save it */
A byte=Q; /* save quotient as a number */
i = i+1;
}
while(Q!=0);
for(i=0;i>0;i--)
{
P0=R[i-1]+0x30; /* Make binary to ASCII */
}
}

08-05-2025 R R INSTITUTE OF TECHNOLOGY 7


Example 5.2.11
Assume that a 100 Hz external clock is
being fed into pin T0. Write a C program
for counter 0 in mode 2 (8-bit auto-
reload) to display the seconds and
minutes on P0 and P1 respectively.

08-05-2025 R R INSTITUTE OF TECHNOLOGY 8


#include <reg51.h>
void Time(unsigned char);
void main(void)
{
unsigned char val=0;
T0=1;
TMOD=0x06; /* T0, mode 2, counter */
TH0 = 100; /* sec = 100 pulses */
while(1)
{
TR0 = 1; /* start timer */
while(TF0==0);
Time(val);
val++;
TR0 = 0; /* stop timer */
TF0 = 0; /* clear flag */
}
}

void Time(unsigned char value)


{
unsigned char sec,min;
min = value/60;
sec = value % 60;
P0 = sec;
P1 = min;
}
08-05-2025 R R INSTITUTE OF TECHNOLOGY 9
Example 5.2.12
Write an 8051 C program to generate
a rectangular wave of 2 kHz with
60% duty cycle in pin P2.1. Assume
crystal frequency as 11.0592 MHz.
Use Timer 0 in mode-1 operation.
Show delay calculations.

08-05-2025 R R INSTITUTE OF TECHNOLOGY 10


For 2 kHz rectangular wave,
T = 1 / 2kHz = 0.5 ms

For 60 % duty cycle :


TON = 0.5 ms x 60 / 100 = 0.3 ms
TOFF = 0.5 ms x 40 / 100 = 0.2 ms

Time for 1 T-state = 12 / 11.0592*10^6 = 1.085 µs

TON = 0.3 ms / 1.085 µs = 276


TOFF = 0.2 ms / 1.085 µs = 184

Count to be loaded for


TON = 65536 - 276 = 65260 = FEECH
TOFF = 65536 - 184 = 65352 = FF48H

#include <reg51.h>
sbit mybit = P2^1;
void T0M1D(unsigned char);
void main (void)
{
while (1)
{
mybit = 1; /* Make P2.1 High */
T0M1D(1); /* Wait for 0.3 msec */
mybit = 0; /* Make P2.1 Low */
T0M1D(0); /* Wait for 0.2 msec */
}
}

08-05-2025 R R INSTITUTE OF TECHNOLOGY 11


void T0M1D (unsigned char i)
{
TMOD = 0 x 01; /* Timer 0, mode 1 */
if (i == 0)
{
TL0 = ECH; /* Load FEECH */
TH0 = FEH;
}
else
{
TL0 = 48H; /* Load FF48H */
TH0 = FFH;
}
TR0 = 1; /* Turn ON Timer 0 */
While (TF0 == 0); /* Wait for TF0 to roll over */
TR0 = 0; /* Turn OFF Timer 0 */
TF0 = 0; /* Clear TF0 */
}

08-05-2025 R R INSTITUTE OF TECHNOLOGY 12


08-05-2025 R R INSTITUTE OF TECHNOLOGY 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy