EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / i am new to mc and i want to

i am new to mc and i want to

|

Microcontroller › 8051 › interfacing › i am new to mc and i want to

August 21, 2011 at 1:44 am #6616
Ashish Angural
Participant

i am new to mc and i want to run lcd, led, seven segments and dc motor together and for that i have writen a code just chech that wether this code is correct or not

#include

#include

#define data P1

sbit S1=P3^0;
sbit S2=P3^1;
sbit S3=P3^2;
sbit motor=P3^3;
sbit S4=P3^4;

sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;

sbit A1=P0^0;
sbit B1=P0^1;
sbit C1=P0^2;
sbit D1=P0^3;

void ms_delay(unsigned int a)
{
unsigned int i,j;
for(i=0;i {
for(j=0;j<70;j++)
{
_nop_();
}
}
}

void sec_delay(unsigned int a)
{
unsigned int i,j;
for(i=0;i {
for(j=0;j<1000;j++)
{
ms_delay(1);
}
}
}

void ibit_left()
{
unsigned char i,a;
while(1);
{
if(S1==0)
{
a=0xfe;
for(i=0;i<8;i++)
{
P1=a;
a=a<<1;
a=a+0x01;
ms_delay(500);
}
}
}
}

void right_shift()
{
unsigned char i,a;
while(1);
{
if(S2==0)
{
a=0xfe;
for(i=0;i<8;i++)
{
P1=a;
a=a>>1;
ms_delay(500);
}
}
}
}

void ibit_right()
{
unsigned char i,a;
while(1);
{
if(S3==0)
{
a=0xfe;
for(i=0;i<8;i++)
{
P1=a;
a=a>>1;
a=a+0x80;
ms_delay(500);
}
}
}
}

void even_segments()
{
unsigned char i,a[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98};
while(1);
{
if(S1==0)
{
for(i=0;i<=9;i++)
{
if(i%2==0)
P2=a;
ms_delay(500);
}
}
}
}

void odd_segments()
{
unsigned char i,a[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98};
while(1);
{
if(S2==0)
{
for(i=0;i<=9;i++)
{
if(i%2==1)
P2=a
;
ms_delay(500);
}
}
}
}

void numbers()
{
unsigned char i,a[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98};
while(1);
{
if(S3==0)
{
for(i=0;i<=9;i++)
{
P2=a
;
ms_delay(500);
}
}
}
}

void lcd_cmd()
{
data=ACC;
RS=0;
RW=0;
E=1;
ms_delay(10);
E=0;
}

void lcd_data()
{
data=ACC;
RS=1;
RW=0;
E=1;
ms_delay(10);
E=0;
}

void lcd_initialize()
{
ACC=0x38;
lcd_cmd();
ACC=0x38;
lcd_cmd();
ACC=0x38;
lcd_cmd();
ACC=0x06;
lcd_cmd();
ACC=0x0C;
lcd_cmd();
}

void lcd_puts(unsigned char *a)
{
while(*a!=’’)
{
ACC=*a;
lcd_data();
a++;
}
}

void PWM(unsigned char Ton)
{
motor=0;
ms_delay(Ton);
motor=1;
ms_delay(100-Ton);
}

void clkwise()
{
unsigned char i;
for(i=0;i<=5;i++)
{
A1=0;
B1=C1=D1=1;
ms_delay(250);

B1=0;
A1=C1=D1=1;
ms_delay(250);

C1=0;
A1=B1=D1=1;
ms_delay(250);

D1=0;
A1=B1=C1=1;
ms_delay(250);
}
}

void anticlkwise()
{
unsigned char i;
for(i=0;i<=5;i++)
{
D1=0;
A1=B1=C1=1;
ms_delay(250);

C1=0;
A1=B1=D1=1;
ms_delay(250);

B1=0;
A1=C1=D1=1;
ms_delay(250);

A1=0;
B1=C1=D1=1;
ms_delay(250);
}
}

void main()
{
lcd_initialize();
while(1);
{
if(S1==0)
{
ACC=0x80;
lcd_cmd();
lcd_puts(“1BIT LEFTSHIFT”);
ibit_left();
}
else if(S2==0)
{
ACC=0x01;
lcd_cmd();
ACC=0x80;
lcd_cmd();
lcd_puts(“RIGHT SHIFT”);
right_shift();
}
else if(S3==0)
{
ACC=0x01;
lcd_cmd();
ACC=0x80;
lcd_cmd();
lcd_puts(“1BIT RIGHTSHIFT”);
ibit_right();
}
while(1);
{
if(S1==0)
{
ACC=0x80;
lcd_cmd();
lcd_puts(“EVEN NUMBERS”);
even_segments();
}
else if(S2==0)
{
ACC=0x01;
lcd_cmd();
ACC=0x80;
lcd_cmd();
lcd_puts(“ODD NUMBERS”);
odd_segments();
}
else if(S3==0)
{
ACC=0x01;
lcd_cmd();
ACC=0x80;
lcd_cmd();
lcd_puts(“DECIMALS”);
numbers();
}
}
while(1);
{
motor=0;
sec_delay(10);
motor=1;
sec_delay(5);
PWM(30);
}
while(1)
{
if(S1==0)
{
ACC=0x80;
lcd_cmd();
lcd_puts(“MOTOR CLOCKWISE”);
clkwise();
}
else if(S2==0)
{
ACC=0x01;
lcd_cmd();
ACC=0x80;
lcd_cmd();
lcd_puts(“MOTOR ANTICLOCKWISE”);
anticlkwise();
}
}
}
}

Pls check this code

RSS Recent Posts

  • stud mount Schottky diodes May 14, 2025
  • LED circuit for 1/6 scale diorama May 14, 2025
  • Can I use this charger in every country? May 14, 2025
  • Electronic board faulty?!? May 13, 2025
  • using a RTC in SF basic May 13, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise