Beruflich Dokumente
Kultur Dokumente
MICROCONTROLLER
UNIT V : (5 HRS.)
EMBEDDED C PROGRAMMING 8 BIT:
Key words
memory models
memory types
pointers
functions
interrupt functions
reentrant functions.
WHY C
Of higher level languages, C is the closest to assembly
languages.
Most micro controllers have available C compilers.
Writing in C simplifies code development for large
projects.
It is easier and less time consuming to write in C
language than assembly.
C is easier to modify and update.
We can use code available in function libraries.
C code is portable to other micro controllers with little
or no change.
Assembly is the fastest, however, it is difficult to find or
train assembly experts. Then if a new processor is
required, we have to start over!
C is mid-level, lots of good C programmers are
available, C compilers are available. C can be used on 8-
, 16-, 32-, and 64-bits processors.
Details like register allocation and addressing of the
various memory types and data types is managed by
the compiler.
Programs get a formal structure (which is imposed by
the C programming language) and can be divided into
separate functions. This contributes to source code
reusability as well as better overall application
structure.
The ability to combine variable selection with specific
operations improves program readability.
Keywords and operational functions that more nearly
resemble the human thought process may be used.
Programming and program test time is drastically
reduced.
The C run-time library contains many standard
routines such as: formatted output, numeric
conversions, and floating-point arithmetic
EMBEDDED C
Compilers produce hex files that is downloaded to
ROM of microcontroller
---The size of hex file is the main concern
---Microcontrollers have limited on-chip ROM
---Code space for 8051 is limited to 64K bytes
Unsigned char
Signed char
Unsigned int
Signed int
Solution:
#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
{P1=z;}
}
SBIT
sbit name = sfr-name ^ bit-position;
sbit name = sfr-address ^ bit-position;
sbit name = sbit-address;
Where
Name is the name of the SFR bit.
sfr-name is the name of a previously-defined SFR.
bit-position is the position of the bit within the SFR.
sfr-address is the address of an SFR.
sbit-address is the address of the SFR bit. With
typical 8051 applications, it is often necessary to
access individual bits within an SFR.
The sbit type provides access to bit-addressable SFRs
and other bit-addressable objects.
SBIT
Write an 8051 C program to toggle bit D0 of the port P1
(P1.0) 50,000 times.
Solution:
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}}
SFR
The sfr type defines a special function register (SFR). It is used
as follows:
sfr name = address;
Where
Name is the name of the SFR.
addressis the address of the SFR.
For example:
sfr P0 = 0x80; /* Port-0, address 80h */
sfr P1 = 0x90; /* Port-1, address 90h */
sfr P2 = 0xA0; /* Port-2, address 0A0h */
sfr P3 = 0xB0; /* Port-3, address 0B0h */
P0, P1, P2, and P3 are the SFR name declarations. Names for
sfr variables are defined just like other C variable declarations.
Any symbolic name may be used in an sfr declaration.
The address specification after the equal sign ('=') must be a
numeric constant. Expressions with operators are not allowed.
Classic 8051 devices support the SFR address range 0x80-
0xFF. The NXP 80C51MX provides an additional extended
SFR space with the address range 0x180-0x1FF.
EXAMPLES:
sfr PSW = 0xD0;
sfr IE = 0xA8;
sbit OV = PSW^2;
sbit CY = PSW^7;
sbit EA = IE^7;
THE BIT DATA TYPE ALLOWS ACCESS TO SINGLE
BITS OF BIT-ADDRESSABLE MEMORY SPACES 20 H–
2FH
#include <regx51.h>
sbit inbit=P1^0;
sbit outbit=P2^7;
bit membit; //use bit to declare bit- addressable memory
void main(void)
{
while (1)
{
membit=inbit; //get a bit from P1.0
outbit=membit; //send it to P2.7
}
}
We use bit data type to access data in a bit-addressable
section of the data RAM space 20 – 2FH
WRITE AN 8051 C PROGRAM TO TOGGLE ALL THE BITS OF
P0, P1, AND P2 CONTINUOUSLY WITH A 250 MS DELAY. USE
THE SFR KEYWORD TO DECLARE THE PORT ADDRESSES.
Solution:
//Accessing Ports as SFRs using sfr data type
sfr port0=0x80;
sfr port1=0x90;
sfr port2=0xA0;
void MSDelay(unsigned int);
void main(void)
{
while (1)
{ MSDelay(250);
port0=0x55; port0=0xAA;
port1=0x55; port1=0xAA;
port2=0xAA;
port2=0x55;
MSDelay(250);}}
OPERATORS
Logical operators
---AND (&&), OR (||), and NOT (!)
Bit-wise operators
#include <reg51.h>
void main(void)
{
P0=0x35 & 0x0F; //ANDing
P1=0x04 | 0x68; //ORing
P2=0x54 ^ 0x78; //XORing
P0=~0x55; //inversing
P1=0x9A >> 3; //shifting right 3
P2=0x77 >> 4; //shifting right 4
P0=0x6 << 4; //shifting left 4
}
WRITE AN 8051 C PROGRAM TO CONVERT
11111101 (FD HEX) TO DECIMAL AND DISPLAY
THE DIGITS ON P0, P1 AND P2.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char x,binbyte,d1,d2,d3;
binbyte=0xFD;
x=binbyte/10;
d1=binbyte%10;
d2=x%10;
d3=x/10;
P0=d1;
P1=d2;
P2=d3; }
SOME PROBLEMS:
Read data from port P2 and P3. Add data and
display result on port P0. Glow LED connected
at port pinP1.1 if carry flag set after addition
Read data from port P2 and P3. Multiply data
and display result on port P0 and P1
Write program to read switch connected at port
pin P1.0, toggle it and send it to port pin P1.1
Write C language program to perform OR
operation between port pin P1.0 and P1.1.
Display result on port pin P1.2
CONT…
Write a program to toggle the bits of P1 ports continuously with
500 ms delay.
Write a 8051 C program to get a byte of data from P1, wait for ½
seconds and then send it to P2.
Write a 8051 C program to Display Even numbers on seven
segment display unit.
Write a Program to display the numbers in descending order.
Write program to display 0 to 99 numbers on two seven segment
display units in both ascending and descending order.
Write an 8051 C program to get a byte of data form P0. If it is less
than 100, send it to P1; otherwise, send it to P2.
CONT…
Write a 8051 C program to Display “ GOOD MORNING” on 1st
line of 16x2 LCD display and “ ELECTRONICS Dept” on 2nd line
of LCD Display.
Write a C program to display your name on LCD display after
getting an Interrupt. After displaying Blink the Port 0 LED’s
Write a C program to Blink the Port 0 LED first and after
blinking the LED display your name on the LCD screen.
A door sensor is connected to the P1.1 pin, and a buzzer is
connected to P1.7. Write an 8051 C program to monitor the door
sensor, and when it opens, sound the buzzer. You can sound the
buzzer by sending a square wave of a few hundred Hz.
Write program to transmit string “WELCOME TO DKTE” using
serial port. Use interrupt method and baud rate 9600.
CONT…
Write C language program to continuously toggle
pin P3.0 without disturbing other port pins.
Write C language program to read port P1,
Compare content of port P1 with data 80h. If data at
port P1 is greater than 80h, make port P0=0x00 and
if data at port P1 is less than or equal to 80h, make
port P0=0xFF.
Write a program to sense lift door switch connected
at port pin P2.0.Switch on the alarm connected at
port pin P2.1 if lift door is open. Write program in
assembly and C language.
Write a program to generate square wave of 50%
duty cycle having frequency 5 KHz at port pin P1.0
using timer of 70% duty cycle using timer on the
same pin. 1 in mode 2. Modify program to generate
pulse waveform
Write a 8051 C program to read the P1.0 and
P1.1 bits and issue an ASCII character to P0
according to the following table.
P1.1 P1.0
0 0 send ‘0’ to P0
0 1 send ‘1’ to P0
1 0 send ‘2’ to P0
1 1 send ‘3’ to P0
unsignbed char z;
z=P1;
z=z&0x3;
switch (z)
{
case(0):
{P0=‘0’;
break;}
case(1):
{P0=‘1’;
Break;}
case(2):
{P0=‘2’;
Break;}
case(3):
{P0=‘3’;
Break;}}}
Writea program to generate square wave
of 50% duty cycle at port pin P1.0 using
timer 0 in mode 1.
TIMER
The 8051 has two 16-bit timers.
The high byte for timer 1 (TH1) is at address
8DH while the low byte (TL1) is at 8BH.
The high byte for timer 0 (TH0) is at 8CH while
the low byte (TL0) is at 8AH.
Both timers can be used in a number of different
modes.
The programmer sets the timers to a specific
mode by loading the appropriate 8-bit number
into the Timer Mode Register (TMOD) which is
at address 89H.
void delay(unsigned int j) // delay routine for 'j' msec
{
unsignedinti;
TMOD=0x01; // initialize timer0 in mode 1
TH0=0xfc;
TL0=0x66;
TR0=1; // start timer0
TF0=0; //Clear overflow flag
while(TF0==0); //loop until timer 0 overflow flag set
TR0=0; //stop timer 0;
TF0=0;
}
LCD
01H : Clear Display Screen
02H : Return Home
04H: Decrement Cursor
06H : Increment Cursor
05H : Shift Display Right
07H : Shift Display Left
08H : Display off, Cursor off
AH : Display off, Cursor on
CH : Display on, Cursor off
EH : Display on, Cursor Blinking
FH : Display on, Cursor Blinking
10H: Shift Cursor position to left
14H: Shift Cursor position to right
18H: Shift the entire display to left
1CH: Shift the entire display to right
80H: Force cursor to begin from the Ist line
C0H:Force cursor to begin from the 2nd line
38H: 2 lines and 5 x 7 matrix
KEYWORDS