sbit SW = P1^1
sbit LED = P1^2
void main() {
if(SW == 1) {
delay_ms(150);
if(SW == 1) {
LED = ~LED;
}
}
}
This is a sample C code, but you should have delay_ms() function in your library or else you have to write the delay routine.
Code:sbit SW = P1^1 sbit LED = P1^2 void main() { if(SW == 1) { delay_ms(150); if(SW == 1) { LED = ~LED; } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include<reg52.h> #define input P0 #define output P1 void main() { if(input==0x01) { output=0xff; if(input==0x01) { output=0x00; } } }
easy.
1.
Check if key is pressed.
loop until key is pressed.
2.
when the key is pressed, toggle the pin connected to the led.
go to 1
also, you should specify what code you are working with
(i.e. assembly or C)
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include<reg52.h> #define input P0 #define output P1 void main() { if(input==0x01) { output=0xff; if(input==0x01) { output=0x00; } } }
#define LEDPORT P2
sbit SW = P1^0
void main() {
while(1) {
if(SW == 0x01) {
delay_ms(150);
if(SW == 0x01) {
LEDPORT = ~LEDPORT;
}
}
}
}
#include<reg52.h>
void delay_ms(unsigned int time);
sbit SW1 = P1^0
sbit SW2 = P1^1
sbit SW3 = P1^2
sbit SW4 = P1^3
sbit SW5 = P1^4
sbit SW6 = P1^5
sbit SW7 = P1^6
sbit SW8 = P1^7
sbit LED1 = P2^0
sbit LED2 = P2^1
sbit LED3 = P2^2
sbit LED4 = P2^3
sbit LED5 = P2^4
sbit LED6 = P2^5
sbit LED7 = P2^6
sbit LED8 = P2^7
void delay_ms(unsigned int msec) {
unsigned int x,y;
for(x=0; x<=msec;x++) {
for(y=0;y<=110;y++);
}
}
void main() {
while(1) {
if(SW1 == 0x01) {
delay_ms(150);
if(SW1 == 0x01) {
LED1 = ~LED1;
}
}
else if(SW2 == 0x01) {
delay_ms(150);
if(SW2 == 0x01) {
LED2 = ~LED2;
}
}
else if(SW3 == 0x01) {
delay_ms(150);
if(SW3 == 0x01) {
LED3 = ~LED3;
}
}
else if(SW4 == 0x01) {
delay_ms(150);
if(SW4 == 0x01) {
LED4 = ~LED4;
}
}
else if(SW5 == 0x01) {
delay_ms(150);
if(SW5 == 0x01) {
LED5 = ~LED5;
}
}
else if(SW6 == 0x01) {
delay_ms(150);
if(SW6 == 0x01) {
LED6 = ~LED6;
}
}
else if(SW7 == 0x01) {
delay_ms(150);
if(SW7 == 0x01) {
LED7 = ~LED7;
}
}
else if(SW8 == 0x01) {
delay_ms(150);
if(SW8 == 0x01) {
LED8 = ~LED8;
}
}
}
}
#include<reg52.h>
#define input P0 //from Master Uc
#define parallel P1
#define leone P2 //control upto 1-8 latch
#define letwo P3 //control upto 8-16 latch
void main()
{
parallel=0x00;// intial low
leone=0x00; // intial low
letwo=0x00; // intial low
while(1)
{
if(input==0x00) // if key pressed once
{
parallel=0x01;
leone=0x01;
letwo=0x00;
}
if(input=0x00) //again the same input is given(the same key pressed again)
{
parallel=0x00;
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?