0% found this document useful (0 votes)
136 views13 pages

8051 - C Programming

This document discusses data types, I/O programming, logic operations, timers, and interrupts in C programming for microcontrollers. It provides examples of C code to toggle ports with delays, read and write bytes to ports based on conditions, perform bitwise logic operations, use timers to generate delays, and create an interrupt service routine to toggle a pin while reading another. The examples demonstrate common tasks in embedded C programming including data transfer, timing, and interrupt handling.

Uploaded by

Rajesh Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
136 views13 pages

8051 - C Programming

This document discusses data types, I/O programming, logic operations, timers, and interrupts in C programming for microcontrollers. It provides examples of C code to toggle ports with delays, read and write bytes to ports based on conditions, perform bitwise logic operations, use timers to generate delays, and create an interrupt service routine to toggle a pin while reading another. The examples demonstrate common tasks in embedded C programming including data transfer, timing, and interrupt handling.

Uploaded by

Rajesh Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

Chapter – 9

Microcontrollers in
Embedded Systems
Programming in C
DATA TYPES

Data Type Size in Bits Data Range/Usage


unsigned char 8-bit 0 to 255
(signed) char 8-bit -128 to +127
unsigned int 16-bit 0 to 65535
(signed) int 16-bit -32768 to +32767
sbit 1-bit SFR bit-addressable only
bit 1-bit RAM bit-addressable only
sfr 8-bit RAM addresses 80 – FFH only
 The character data type is the most
DATA TYPES
natural choice
Unsigned char  8051 is an 8-bit microcontroller
 Unsigned char is an 8-bit data type in
the range of 0 – 255 (00 – FFH)
 One of the most widely used data types
for the 8051
 Counter value
 ASCII characters
 C compilers use the signed char (
-128 to 127) as the default if we do not
put the keyword unsigned
Write an 8051 C program to send values 00 – FF to port P1.
Solution:
DATA TYPES Header file for 8051
#include <reg51.h>
void main(void)
Unsigned char {
(cont’) unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
DATA TYPES Write an 8051 C program to toggle bit D0 of the port P1 (P1.0)
50,000 times.
Single Bit Solution:
sbit keyword allows access to the
(cont’) #include <reg51.h> single bits of port 1
sbit MYBIT=P1^0;

void main(void) Ranges from 0 to 65535 single


{
bits of port 1
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
Delay();
MYBIT=1;
}
}
I/O Write an 8051 C program to get a byte of data form P0. If it is less
PROGRAMMING than 100, send it to P1; otherwise, send it to P2.
Solution:
Byte Size I/O #include <reg51.h>
(cont’) void main(void)
{
unsigned char mybyte;
P0=0xFF; //make P0 input port
while (1)
{
mybyte=P0; //get a byte from P0
if (mybyte<100)
P1=mybyte; //send it to P1
else
P2=mybyte; //send it to P2
}
}
I/O Write an 8051 C program to monitor bit P1.5. If it is high, send 55H
PROGRAMMING to P0; otherwise, send AAH to P2.
Solution:
Bit-addressable #include <reg51.h>
sbit mybit=P1^5;
I/O
(cont’) void main(void)
{
mybit=1; //make mybit an input
while (1)
{
if (mybit==1)
P0=0x55;
else
P2=0xAA;
}
}
LOGIC Run the following program on your simulator and examine the results.
OPERATIONS Solution:
#include <reg51.h>
Bit-wise
void main(void)
Operators in C {
(cont’) 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
}
TIME DELAY Write an 8051 C program to toggle bits of P1 continuously forever
(cont’) with some delay.
Solution:
//Toggle P1 forever with some delay in between
//“on” and “off”
#include <reg51.h>
void main(void) We must use the oscilloscope to
{ measure the exact duration
unsigned int x;
for (;;) //repeat forever
{
p1=0x55;
for (x=0;x<40000;x++); //delay size
//unknown
p1=0xAA;
for (x=0;x<40000;x++);
}
}
Example 9-20
Write an 8051 C program to toggle all the bits of port P1 continuously
PROGRAMMING with 56ms delay in between. Use Timer 0, 16-bit mode to
generate the delay.
TIMERS IN C
Solution:
#include <reg51.h>
void T0Delay(void);
void main(void){
while (1) {
P1=0x55;
T0Delay();
P1=0xAA;
T0Delay();
}
}
void T0Delay(){
TMOD=0x01;
TL0=0x00; FFFFH – 3500H = CAFFH
TH0=0x35; = 51967 + 1 = 51968
TR0=1;
while (TF0==0); 51968  1.085 s = 56.384 ms is the
TR0=0; approximate delay
TF0=0;
}
Example 11-14
Write a C program that continuously gets a single bit of data from P1.7
and sends it to P1.0, while simultaneously creating a square wave of
PROGRAMMING 20 ms period on pin P2.5. Use Timer 0 to create the square wave.
INTERRUPT Assume that XTAL = 11.0592 MHz.
IN C Solution:
We will use timer 0 mode 1. One half of the period is 10 ms.
10,000/1.085 s = 9217 = 65536 – n, n = 56319 = DBFF H

void timer0(void) interrupt 1 {


TR0 = 0;
WAVE=~WAVE; //toggle pin
TL0=0xFF;
TH0=0xDB;
TR0 = 1; //re-run timer
//note it is not required to
clear the timer flag TF0
}
Example 11-14
Write a C program that continuously gets a single bit of data from P1.7
and sends it to P1.0, while simultaneously creating a square wave of
PROGRAMMING 20 ms period on pin P2.5. Use Timer 0 to create the square wave.
INTERRUPT Assume that XTAL = 11.0592 MHz.
IN C Solution:
We will use timer 0 mode 1. One half of the period is 10 ms.
10,000/1.085 s = 9217 = 65536 – n, n = 56319 = DBFF H
#include <reg51.h>
sbit SW =P1^7;
sbit LED =P1^0;
sbit WAVE =P2^5;

void main() {
SW=1; //make switch input
TMOD=0x01;
TL0=0xFF;
TH0=0xDB;
IE=0x82; //enable interrupt for timer 0
TR0 = 1 //start timer
while (1)
{LED=SW;} //send switch to LED
}
8051 C interrupt numbers
PROGRAMMING The 8051 C compilers have extensive support for the 8051
INTERRUPT interrupts with two major features as follows:
IN C They assign a unique number to each of the 8051 interrupts, as
shown in Table below.

8051 Interrupt Name Number used by


8051 C
External Interrupt 0 (INT0) 0
Timer Interrupt 0 (TF0) 1
External Interrupt 1 (INT1) 2
Timer Interrupt 1 (TF1) 3
Serial (RI + TI) 4
Communication

You might also like