LAB 7 Introduction To C Programming
LAB 7 Introduction To C Programming
PROGRAMMING IN C
WHY PROGRAM THE 8051 IN C?
It is easier and less time consuming to write in C
than Assembly
C is easier to modify and update
You can use code available in function libraries
C code is portable to other microcontrollers with
little or no modification
BASIC C PROGRAM STRUCTURE
#include <reg51.h>
void main (void)
{
// your code will appear here
}
DATA TYPES
Unsigned Char
Signed Char
Unsigned int
Signed int
Sbit (single bit)
Bit and sfr
UNSIGNED CHAR
#include <reg51.h>
void main (void)
{
unsigned char z;
for (z=0; z<255; z++)
P1=z; Note: SFR names should
} be in capital letters
SIGNED CHAR
The signed char is an 8-bit data type that uses the
most significant bit (D7 of D7 D0) to represent
the or + value. As a result it takes a value in
the range of -128 to +127.
#include <reg51.h>
void main (void)
{
char arr[] = {+1, -1, +2, -2, +3, -3, +4, -4};
unsigned char z;
for (z=0; z<8; z++)
P1=arr[z];
}
UNSIGNED INT
#include <reg51.h>
sbit MYBIT = P1^0; Should be define
before main
void main (void)
{
unsigned int z;
for (z=0; z<50000; z++)
{
MYBIT = 0;
MYBIT = 1;
}
}
EXERCISES
Write an 8051 C program to toggle bit D3 & D4 of
the port P1 2000 times
#include <reg51.h>
sbit BIT3 = P1^3;
sbit BIT4 = P1^4;
void main (void)
{
unsigned int z;
for (z=0; z<2000; z++)
{
BIT3 = 0;
BIT4 = 0;
BIT3 = 1;
BIT4 = 1;
}
}
BIT AND SFR
bit <name>
#include <reg51.h>
void Delay(unsigned int);
void main (void) void Delay(unsigned int y)
{ {
while (1) unsigned int i, j;
{ for (i=0; i<y; i++)
P1=0x55; for (j=0; j<1275; j++);
Delay(250); }
P1=0xAA;
Delay(250);
}
}
EXERCISE
A door sensor is connected to 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 few hundred Hz.
I/O PROGRAMMING IN 8051 C
#include <reg51.h>
#define LED P2 // notice how we can define P2
sfr Port0 = 0x80;
void main (void)
{
P1=0; // clear P1
LED =0; // clear P2
Port0 =0; // clear P0
while (1) // repeat forever
{
Port0++; // increment P0
P1++; // increment P1
LED++; // increment P2
}
}
BIT ADDRESSABLE I/O PROGRAMMING
Bit0 = 0;
Bit1 = 0;
EXAMPLES
The following program will monitor P1.5. if it is high, send
55H to P0; otherwise, send AAH to P2.
#include <reg51.h>
sbit MYBIT = P1^5; // port 1 bit 5
#include <reg51.h>
void Delay(unsigned int);
sfr port0 = 0x80;
sbit MYBIT = 0x85;
void main (void) void Delay(unsigned int y)
{ {
while (1) unsigned int i, j;
{ for (i=0; i<y; i++)
port0=0x55; for (j=0; j<113; j++);
MYBIT = 0; }
Delay(250);
port0=0xAA;
MYBIT = 1;
Delay(250);
}
}
EXERCISE
The data pins of LCD are connected to P1 and its
enable pin is connected to P2.0. The information
is latched into the LCD whenever its enable pin
goes from high to low. Write an 8051 C program
to send I love Pakistan to this LCD.
LOGIC OPERATIONS IN 8051 C
Bit-wise Logic operators in C
One of the most important and powerful feature of
the C language is its ability to perform bit
manipulation. Some examples using the C logical
operators are:
#include <reg51.h>
void Delay(unsigned int); void Delay(unsigned int y)
{
void main (void) unsigned int i, j;
for (i=0; i<y; i++)
{ for (j=0; j<113; j++);
P0=0x55; }
while (1)
{
P0=P0^0xFF;
Delay(250);
}
}
EXERCISES