SWITCH AND LED INTERFACING with 8051
#include<reg52.h> /* register declarations */
sbit LED_pin = P2^0; //Defining LED PIN
sbit switch_pin = P0^0; //Defining Switch PIN
void Delay (int); //Function prototype declaration
void main (void)
{
switch_pin = 1; // Making Switch PIN input
LED_pin=1; //LED off initially
while (1) //infinite loop
{
if (switch_pin == 0 ) //If switch pressed
{
LED_pin = 0; //LED ON
Delay (2000); //Delay
LED_pin = 1;//LED OFF
}
}
}
void Delay(int n){
int i;
for (i = 0; i<n; i++){}
}
8 bit LCD interfacing with 8051
#include <reg51.h>
#define LCD P3
sbit RS = P2 ^ 5;
sbit RW = P2 ^ 6;
sbit EN = P2 ^ 7;
// Function Declarations
void InitLCD();
void LCDcmd(unsigned char);
void LCDdisp(unsigned char c);
void LCDStrdisp(unsigned char *s);
void delay();
void main()
{
InitLCD();
LCDStrdisp("Welcome To This class today!");
}
void InitLCD()
{
LCDcmd(0x38); // 8-bit data initialization
LCDcmd(0x0E); // Display ON with cursor ON.
LCDcmd(0x01); // Clear the LCD display
LCDcmd(0x06); // Increment cursor to display the next character
LCDcmd(0x80); // Set cursor to the first line
}
void LCDcmd(unsigned char cmd)
{
LCD = cmd;
RS = 0;
RW = 0;
EN = 1;
delay();
EN = 0;
// delay ();
}
void LCDdisp(unsigned char c)
{
LCD = c;
RS = 1;
RW = 0;
EN = 1;
delay();
EN = 0;
// delay ();
}
void LCDStrdisp(unsigned char *s)
{
int count = 0;
while (*s){
count++;
if (count == 16){
LCDcmd(0xC0);
}
LCDdisp(*s++);
}
}
void delay()
{
unsigned int i;
for (i = 0; i < 20000; i++);
}
Motor interfacing with 8051
#include<reg52.h>
#include<stdio.h>
void delay(int);
sbit motor_pin_1 = P2^0;
sbit motor_pin_2 = P2^1;
void main()
{
do
{
motor_pin_1 = 1;
motor_pin_2 = 0; //Rotates Motor Anit ClocK
delay(1000);
motor_pin_1 = 1;
motor_pin_2 = 1; //Stops Motor
delay(1000);
motor_pin_1 = 0;
motor_pin_2 = 1; //Rotates Motor Clockwise
delay(1000);
motor_pin_1 = 0;
motor_pin_2 = 0; //Stops Motor
delay(1000);
}while(1);
}
void delay(int max){
int i,j;
for(i=0; i<max; i++){
for(j=0;j<1000;j++){}
}
}
Relay Interfacing with 8051
#include<reg52.h>
sbit relay_pin = P3^0;
void Delay(int);
void main()
{
do
{
relay_pin = 0; //Relay ON
Delay(10000);
relay_pin = 1; //Relay OFF
Delay(10000);
}while(1);
void Delay(int k){
int i;
for(i=0;i<k;i++)
{
}
}
Arduino LED toggle
const int ledPin = 5; // the number of the LED pin
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
Ultrasonic with Arduino
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 13;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
[Link](9600); // Starts the serial communication
delay(1000);
[Link]("#########################################");
[Link]("########## WELCOME #############");
[Link]("#########################################");
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
[Link]("Distance: ");
[Link](distance);
if(distance > 100){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
delay(1000); // delay 1 second
}