Innovative Projects with the 8051 Microcontroller
Time Delay Generation with 8051 Microcontroller
#include
sbit FREQ = P2^3;
void timerDelay() {
TH1 = 0xDC; // Load the timer value
TL1 = 0x00;
TR1 = 1; // Turn ON Timer zero
while (TF1 == 0); // Wait for Timer Overflow
TF1 = 0; // Clear the timer overflow flag
TR1 = 0;
}
void main() {
TMOD = 0x10; // Timer0 mode 1
while (1) {
FREQ = 1;
timerDelay();
FREQ = 0;
timerDelay();
}
}
Relay Interfacing with 8051 Microcontroller
#include
sbit relay_pin = P2^0;
sbit SW = P1^0;
void Delay(int k) {
int j;
int i;
for (i = 0; i < k; i++) {
for (j = 0; j < 100; j++);
}
}
void main() {
SW = 1;
while (1) {
if (SW == 0) {
relay_pin = 0; // Relay OFF
Delay(1000); // 1 sec delay
}
if (SW == 1) {
relay_pin = 1; // Relay ON
Delay(1000); // 1 sec delay
}
}
}
LED Interfacing with 8051 Microcontroller
#include // Special function register declarations
// for the intended 8051 derivative
sbit LED = P1^0; // Defining LED pin
void Delay(void); // Function prototype declaration
void main(void) {
while (1) { // Infinite loop
LED = 0; // LED ON
Delay();
LED = 1; // LED OFF
Delay();
}
}
void Delay(void) {
int j;
int i;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10000; j++);
}
}
Seven Segment Display Interfacing with 8051 Microcontroller
#include
void DELAY_ms(unsigned int ms_Count);
void main() {
char Display[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
int i;
while (1) {
for (i = 0; i <= 9; i++) { // Loop to display 0-9
P2 = Display[i];
DELAY_ms(1000);
}
}
}
void DELAY_ms(unsigned int ms_Count) {
unsigned int i, j;
for (i = 0; i < ms_Count; i++) {
for (j = 0; j < 100; j++);
}
}
LCD Interfacing with 8051 Microcontroller
#include
sbit rs = P2^0; // RS pin connected to pin 2 of port 3
sbit rw = P2^1; // RW pin connected to pin 3 of port 3
sbit e = P2^2; // E pin connected to pin 4 of port 3
void msdelay(unsigned int time) { // Function for creating delay in milliseconds.
unsigned i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 1275; j++);
}
void lcd_cmd(unsigned char command) { // Function to send command instruction to LCD
P1 = command;
rs = 0;
rw = 0;
e = 1;
msdelay(1);
}
void lcd_data(unsigned char disp_data) { // Function to display data on LCD
P1 = disp_data;
rs = 1;
rw = 0;
e = 1;
msdelay(1);
}
ADC Interface
#include
#define input P1 // Input port (read values of ADC)
#define output P3 // Output port (connected to LEDs)
void delay(unsigned int msec);
void adc();
sbit rd = P2^5; // Write pin.
sbit wr = P2^6; // Read pin.
sbit intr = P2^7; // Interrupt pin.
void main() {
input = 0xFF; // Declare port 1 as input port.
output = 0x00; // Declare port 0 as output port.
while (1) {
adc();
}
}
void delay(unsigned int msec) { // Delay function
int i, j;
for (i = 0; i < msec; i++)
for (j = 0; j < 1275; j++);
}
void adc() { // Reading values from ADC and display on the LEDs
rd = 1;
wr = 0;
delay(10);
wr = 1;
while (intr == 1);
rd = 0;
output = input;
delay(10);
intr = 1;
}
Stepper Motor Interfacing with 8051 Microcontroller
#include
void delay(void);
void main() {
while (1) {
P1 = 0x06;
delay();
P1 = 0x03;
delay();
P1 = 0x09;
delay();
P1 = 0x0C;
delay();
}
}
void delay(void) {
unsigned char cnt, cnt1;
for (cnt = 0; cnt <= 254; cnt++)
for (cnt1 = 0; cnt1 < 254; cnt1++);
}
DC Motor Interfacing with 8051 Microcontroller
#include
sbit in1 = P2^0;
sbit in2 = P2^1;
sbit en1 = P2^2;
sbit en2 = P2^3;
sbit in3 = P2^4;
sbit in4 = P2^5;
void forward();
void reverse();
void main() {
en1 = 1;
en2 = 1;
while (1) {
forward();
reverse();
}
}
void forward() {
in1 = 1;
in2 = 0;
}
void reverse() {
in4 = 1;
in3 = 0;
}